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)))));
}
}
import java.io.*;
import java.util.Scanner;
class Player2 {
private double data[][] = new double[10][3]; //stores all the inut data of each player
private double everyoneannsal[] = new double[10]; //stores every player's annual salary
private double everyonepergamesal[] = new double[10]; //stores every player's per game salary
private double everyoneexpectval[] = new double[10]; //stores every player's contract's expected value
//takes in a player and the number of games in a season and returns the expected value of the player's contract
public double getExpectedValue(int forwho, int numberofgames){
double chanceofinjury = 0;
if(numberofgames == 18){chanceofinjury = 0.03375;}
else{chanceofinjury = 0.03;}
return (1- data[forwho][0]*chanceofinjury)*data[forwho][1] + data[forwho][0]*chanceofinjury*data[forwho][2];
}
//takes in the number of games in the season and returns an array containing every player's contract's expected value
public double[] getEveryoneExpectVal(int numberofgames){
for(int i=0; i<10; i++){
everyoneexpectval[i] = getExpectedValue(i, numberofgames);
}
return everyoneexpectval;
}
//takes in the number of games in a season and an expected value and returns which player's it is
public double findWhoseExepctVal(int numberofgames, double expectedvalue){
int whose = -1;
for(int i=0; i<10; i++){
if(getEveryoneExpectVal(numberofgames)[i]==expectedvalue){
whose = i;
}
}
return whose+1;
}
//takes in a player and returns their annual salary
public double getAnnualSalary(int forwho){
return data[forwho][1]/data[forwho][0];
}
//returns an array containing every player's annual salary
public double[] getEveryoneAnnSal(){
for(int i=0; i<10; i++){
everyoneannsal[i] = getAnnualSalary(i);
}
return everyoneannsal;
}
//takes in the number of games in a season and a player and returns their per game salary
public double getPerGameSalary(int numberofgames, int forwho){
return getAnnualSalary(forwho)/numberofgames;
}
//takes in the number of games in a season and returns an array containing every player's per game salary
public double[] getEveryonePerGameSal(int numberofgames){
for(int i=0; i<10; i++){
everyonepergamesal[i] = getPerGameSalary(numberofgames, i);
}
return everyonepergamesal;
}
//takes in an array and returns the average value of all the elements in the array
public double getAverage(double[] ofwhat){
double sum = 0;
for(int i=0; i<ofwhat.length; i++){
sum = sum + ofwhat[i];
}
return sum/ofwhat.length;
}
//takes in an array and returns that array but sorted from least to greatest
public double[] sort(double[] what){
double temp;
double sortthis[] = what;
for (int i=1; i<what.length; i++){
for (int j=i; j>0; j--){
if (sortthis[j]<sortthis[j-1]){
temp = sortthis[j];
sortthis[j] = sortthis[j-1];
sortthis[j-1] = temp;
}
}
}
return sortthis;
}
//processes the input data and puts it into an array
Player2() throws IOException{
Scanner TomBrady = new Scanner(new FileReader("data/input.txt"));
for(int i=0; i<10; i++){
String input = TomBrady.nextLine();
String inputs[] = input.split(", ");
data[i][0] = Double.parseDouble(inputs[0]);
data[i][1] = Double.parseDouble(inputs[1])*1000000;
data[i][2] = Double.parseDouble(inputs[2])*1000000;
}
}
public static void main(String[] args) throws IOException{
Player2 Contracts = new Player2();
System.out.println(Math.round(Contracts.sort(Contracts.getEveryonePerGameSal(16))[9] - Contracts.sort(Contracts.getEveryonePerGameSal(16))[0]));
System.out.println(Math.round((Contracts.sort(Contracts.getEveryonePerGameSal(18))[9] + Contracts.sort(Contracts.getEveryonePerGameSal(18))[0])/2));
double highestexpectval = Contracts.sort(Contracts.getEveryoneExpectVal(16))[9];
System.out.println(Math.round(highestexpectval) + " by #" + Math.round(Contracts.findWhoseExepctVal(16, highestexpectval)));
System.out.println(Math.round(Contracts.getAverage(Contracts.getEveryoneExpectVal(18))));
System.out.println(Math.round((Contracts.sort(Contracts.getEveryoneAnnSal())[4] + Contracts.sort(Contracts.getEveryoneAnnSal())[5])/2));
}
}