I've got my client and my class.
My class seems to be okay, at least on the written part i'm not getting any errors.
import java.text.DecimalFormat; public class EncapTest { public static void main(String[]args) { int[]numofTeamHits={22,31,25,21,29,35,33,23,30}; int[]teamAtBats={102,100,99,81,7576,83,71,90}; int numOfPlayers=0; //initiate an object calling the overloaded constructor BaseballClient bCli1 = new BaseballClient(numofTeamHits,teamAtBats,numOfPlayers); //call toString System.out.println("bStat1:\n"+bCli1); //instantiate identical object BaseballClient bCli2 = new BaseballClient(numofTeamHits,teamAtBats, numOfPlayers); //calling accessors System.out.println("bStat2\n"+bCli2); int[]x=bCli1.getAtBats(); for(int i=0;i<x.length;i++) System.out.print(x[i]+""); System.out.println(); //test equals if(bCli1.equals(bCli2)) System.out.println("Equal"); else System.out.println("Not Equal"); //change some data-call mutator System.out.println("\nSetting Stat1 and Stat2 arrays to teamHits"); bCli1.setHits(numofTeamHits); bCli2.setHits(numofTeamHits); System.out.println(bCli1); System.out.println("Comparing State1 and Stat2 for equality"); //test average>100 DecimalFormat average = new DecimalFormat("0.00"); System.out.println("The average score is" + average.format(bCli1.battingAverages())); //sorting System.out.println("\nSorted Score"); x=bCli1.sortedHits(); for(int i=0;i<x.length;i++) System.out.println(x[i]+""); System.out.println(); } }
But on the client i'm getting errors here:
says empty statement for i and can't find symbol.public void setAtBats(int[]newAtBats) { atBats=new int[newAtBats.length]; for(int i = 0; i <atBats.length;i++); atBats[i]= newAtBats[i]; }
Also getting error here:
at this part: for(int i = 0;<atBats.length;i++) says incompatible types.public boolean equals(Object o) { if(!(o instanceof BaseballClient)) return false; else { BaseballClient b=(BaseballClient)o; if(numberOfPlayers!=b.numberOfPlayers) return false; for(int i = 0;<atBats.length;i++) { if(atBats[i]!=b.atBats[i]) return false; } for(int i=0;i<numOfhits.length;i++) { if(numOfhits[i]!=b.numOfhits[i]) return false; } return true; } }
Here is teh intirety of the client:
/* 62. Write a class encapsulating the concept of statistics for a baseball team, which has the following attributes: a number of players, a list of number of hits for each player, a list of number of at-bats for each player. Write the following methods: ❑ A constuctor with two equal-length arrays as parameters, the number of hits * per player, and the number of at-bats per player. ❑ Accessors, mutators, toString, and equals methods. ❑ Generate and return an array of batting averages based on the attributes given. ❑ Calculate and return the total number of hits for the team. ❑ Calculate and return the number of players with a batting average greater than .300. ❑ A method returning an array holding the number of hits,sorted in ascending order. Write a client class to test all the methods in your class. */ package natashaepperson_chapter_08_exercise_62; import java.util.Arrays; /** * * @author natasha */ public class BaseballClient { private int numberOfPlayers; private int [] atBats; private int []numOfhits; //constructor public BaseballClient(int [] initHits, int [] forAtBats, int num) { atBats = forAtBats; numOfhits= initHits; numberOfPlayers=num; } //accessor for numberOfPlayers //@return number of players public int getNumberOfPlayers() { return numberOfPlayers; } //@return array with number of at-bats per player public int[]getAtBats() { int [] temp = new int[numberOfPlayers]; System.arraycopy(atBats, 0, temp, 0, atBats.length); return temp; } //@return array with number of hits per player public int[]getHits() { int[]temp = new int[numberOfPlayers]; System.arraycopy(numOfhits, 0, temp, 0, numOfhits.length); return temp; } //mutator for atBats //newAtBats number of times each player batted public void setAtBats(int[]newAtBats) { atBats=new int[newAtBats.length]; for(int i = 0; i <atBats.length;i++); atBats[i]= newAtBats[i]; } //mutator for atHits //newHits number of times each player batted public void setHits(int[]newHits) { numOfhits=new int[newHits.length]; } //number of Players, at bats, and hits @Override public String toString() { String returnString=""; for(int i=0;i<numOfhits.length;i++) returnString=returnString+(i++)+"\t"+numOfhits[i] +"\t"+atBats[i]+"\n"; return returnString; } //true if the numberOfPlayers, atBats, and hits in this object //are equal to those in parameter object; false otherwise @Override public boolean equals(Object o) { if(!(o instanceof BaseballClient)) return false; else { BaseballClient b=(BaseballClient)o; if(numberOfPlayers!=b.numberOfPlayers) return false; for(int i = 0;<atBats.length;i++) { if(atBats[i]!=b.atBats[i]) return false; } for(int i=0;i<numOfhits.length;i++) { if(numOfhits[i]!=b.numOfhits[i]) return false; } return true; } } @Override public int hashCode() { int hash = 3; hash = 97 * hash + this.numberOfPlayers; hash = 97 * hash + Arrays.hashCode(this.atBats); hash = 97 * hash + Arrays.hashCode(this.numOfhits); return hash; } //return array of batting averages for each player public double[]battingAverages() { double[]temp = new double[numberOfPlayers]; for(int i = 0; i<numberOfPlayers;i++) { if(atBats[i]==0) temp[i]=0; else temp[i]=(double)(numOfhits[i]/atBats[i]); } return temp; } //return total number of hits for the team public int totalHits() { int total=0; for(int i=0;i<numberOfPlayers;i++) { total=total+numOfhits[i]; } return total; } public int goodPlayers() { double average[]=battingAverages(); int count=0; for(int i=0;i<numberOfPlayers;i++) { if(average[i]>0.300) { count++; } } return count; } private static int indexOfLargestElement(int[]array,int size) { int index=0; for(int i=0;i<size;i++) { if(array[i]>array[index]) index=i; } return index; } public int[] sortedHits() { int[]temp = new int[numOfhits.length]; System.arraycopy(numOfhits, 0, temp, 0, numOfhits.length); int max; for(int i=0;i<temp.length;i++) { max=indexOfLargestElement(temp,temp.length-i); int swapTemp=temp[max]; temp[max]=temp[temp.length-i-1]; temp[temp.length-i-1]=swapTemp; } return temp; } }