Question: public class ws1 { String[] arr = new String[7]; public static void main(String[] args) { ws1 n = new ws1(); n.arr[0] = bob; n.arr[1] =

public class ws1 { String[] arr = new String[7]; public static void main(String[] args) { ws1 n = new ws1(); n.arr[0] = "bob"; n.arr[1] = "Cara"; n.arr[2] = "Lisa"; n.arr[3] = "Joe"; n.arr[4] = "Rob"; n.arr[5] = "Sara"; n.arr[6] = "Kat"; n.apples("bob", 5); n.apples("bob", 9); n.apples("bob", 5); n.apples("Lisa", 3); n.apples("Lisa", 5); n.apples("Rob", 7); n.totalApplesEaten("Bob") //should return 19 n.totalApplesEaten("Cara") //should return 0 n.totalApplesEaten("Lisa") //should return 8 } public boolean apples(String name, int apples) {
 //because this method was called 3 times for bob and bob ate 5+9+5 apples, //this method adds up the apples and should return true
 } public int totalApplesEaten(String name) { //this method returns the number of apples ate calculated by the method above. } }

I tried using a single for-loop that traverses through arr array, however, the loop adds everyone's apple, how do I make it so that this method can return the different amount of apples for different names? (Only with ARRAY)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!