PPT: https://drive.google.com/open?id=1jR4_TEhG5KXkPvW1MB2YUrbcUKPeAR_S
We have learnt a lot about classes and objects. Now it's the time to put those in play!
Challenge:
1. You have turned Scrabbles and Maps into classes and objects. The DraftPick in last post is the next. Apply the encapsulation guidelines in the PPT and design a class Player1 that complete the original requirement first. Can you create a method that takes an object as a parameter?
2. Below for code practice, it's a slight different version of DraftPick of last post. Create a class Player2 to complete new requirement.
3. (Coding Practice): A slight different version of DraftPick
import java.io.*;
import java.util.Scanner;
class Player1 {
private double data[][] = new double[5][2]; //stores all the input data
private double everyoneannualsalary[] = new double[5]; //stores all the annual salaries
private double everyonepergamesalary[] = new double[5]; //stores all the per game salaries
//takes in a player returns their annual salary
public double getAnnualSalary(int forwho){
return data[forwho][1]/data[forwho][0];
}
//returns an array containing everyone's annual salary
public double[] getEveryoneAnnualSalary(){
for(int i=0; i<5; i++){
everyoneannualsalary[i] = getAnnualSalary(i);
}
return everyoneannualsalary;
}
//takes in an amount returns the number of salaries above the amount
public int getSalariesAboveAmount(double amount){
int count = 0;
for(int i=0; i<5; i++){
if(getAnnualSalary(i)>amount){count++;}
}
return count;
}
//takes in a player and the number of games in a season and returns their per game salary
public double getPerGameSalary(double annualsalary, int numberofgames){
return annualsalary/numberofgames;
}
//takes in th number of games in a season and returns an array containing everyone's per game salary
public double[] getEveryonePerGameSalary(int numberofgames){
for(int i=0; i<5; i++){
everyonepergamesalary[i] = getPerGameSalary(getAnnualSalary(i), numberofgames);
}
return everyonepergamesalary;
}
//takes in an array returns the average value of the array
public double getAverage(double[] ofwhat){
double sum = 0;
for(int i=0; i<5; i++){
sum = sum + ofwhat[i];
}
return sum/ofwhat.length;
}
//takes in an array and returns the greatest value in the array
public double getHighest(double[] ofwhat){
double max = ofwhat[0];
for(int i=0; i<4; i++){
if(max<ofwhat[i+1]){
max = ofwhat[i+1];
}
}
return max;
}
//takes in an array and returns the lowest value in the array
public double getLowest(double[] ofwhat){
double min = ofwhat[0];
for(int i=0; i<4; i++){
if(min>ofwhat[i+1]){
min = ofwhat[i+1];
}
}
return min;
}
//processes all the input data and puts it into an array
Player1() throws IOException{
Scanner TomBrady = new Scanner(new FileReader("data/input.java"));
for(int i=0; i<5; i++){
String input = TomBrady.nextLine();
String inputs[] = input.split(", ");
data[i][0] = Double.parseDouble(inputs[0]);
data[i][1] = Double.parseDouble(inputs[1])*1000000;
}
}
public static void main(String[] args) throws IOException{
Player1 Salaries = new Player1();
System.out.println(Math.round(Salaries.getSalariesAboveAmount(10000000)));
System.out.println(Math.round(Salaries.getAverage(Salaries.getEveryoneAnnualSalary())));
System.out.println(Math.round(Salaries.getLowest(Salaries.getEveryonePerGameSalary(16))));
System.out.println(Math.round(Salaries.getHighest(Salaries.getEveryonePerGameSalary(18))));
System.out.println(Math.round((Salaries.getAverage(Salaries.getEveryonePerGameSalary(16)))-(Salaries.getAverage(Salaries.getEveryonePerGameSalary(18)))));
}
}