java - Method Not Displaying Properly -
the method "aboveaverage" in following code not displaying correctly , i've tried can. please explain what's going wrong?
my code:
import java.util.*; public class dailycatch { private int fishermanid, fisherid; private string dateofsample, date; private double[] fishcaught = new double[10]; private int currweight = 0; private string summary; private double average; private int aboveavg; public dailycatch() { } public dailycatch (int fishermanid, string dateofsample) { fisherid = fishermanid; date = dateofsample; } public dailycatch (int fishermanid, string dateofsample, string weight) { this(fishermanid, dateofsample); readweights(weight); } public void addfish(double weight) { if (currweight > 10) { // array full } else { fishcaught[currweight] = weight; currweight += 1; // update current index of array } } private void readweights(string weightsasstring) { string[] weightsread = weightsasstring.split("\\s+"); (int = 0; < weightsread.length; i++) { this.addfish(double.parsedouble(weightsread[i])); } } public string tostring() { return "fisherman id: " + fisherid + "\ndate:" + date + "\nfish caught weights: " + arrays.tostring(fishcaught); } public void printweights() { (int = 0; < fishcaught.length; i++) { system.out.println(fishcaught[i]); } } public double averageweight() { double sum = 0; double count = 0; (int = 0; < fishcaught.length; i++) { if (fishcaught[i] != 0) { sum += fishcaught[i]; count += 1; average = sum/count; } } return average; } public string getsummary() { int storytellercount = 0; int keepercount = 0; int throwbackcount = 0; (int = 0; < fishcaught.length; i++) { if (fishcaught[i] > 5) { storytellercount++; } else if (fishcaught[i] >=1 && fishcaught[i] <= 5) { keepercount++; } else if (fishcaught[i] < 1 && fishcaught[i] > 0) { throwbackcount++; } } string summary = ("\nstoryteller - " + storytellercount+ "\nkeeper - " + keepercount + "\nthrowback - " + throwbackcount); return summary; } public int aboveaverage() { int greatavgcount = 0; (int = 0; < fishcaught.length; i++) { if (fishcaught[i] > average) { aboveavg = greatavgcount++; } } return aboveavg; } }
test code:
public class bigbass { public static void main (string[]args) { //part 1 dailycatch monday1 = new dailycatch(32, "4/1/2013", "4.1 5.5 2.3 0.5 4.8 1.5"); system.out.println(monday1); //part 2 dailycatch monday2 = new dailycatch(44, "4/1/2013"); system.out.println(monday2); monday2.addfish(2.1); monday2.addfish(4.2); system.out.println(monday2); //part 3 system.out.println("\n\nsummary of fisherman 32"); system.out.println(monday1.getsummary()); //part 4 double avg = monday1.averageweight(); system.out.printf("\nthere %d fish above average weight of %.1f.", monday1.aboveaverage(), avg); } }
i need part 4 work here. return there have been 2 fish caught above average when know should 3. average 3.1.
a simple mistake.
public int aboveaverage() { int greatavgcount = 0; (int = 0; < fishcaught.length; i++) { if (fishcaught[i] > 3.1) { greatavgcount++; // no 'return' } } return greatavgcount; }
Comments
Post a Comment