Answered step by step
Verified Expert Solution
Question
1 Approved Answer
String[] arr = new String[3]; public static void main(String args[]){ classname n = new classname(); arr[0] = bob; arr[1] = Lisa; arr[2] = Rob; n.applesEaten(bob,5)
String[] arr = new String[3]; public static void main(String args[]){ classname n = new classname(); arr[0] = "bob"; arr[1] = "Lisa"; arr[2] = "Rob"; n.applesEaten(bob,5) n.applesEaten(bob,9) n.applesEaten(bob,5) n.applesEaten(Lisa,3) n.applesEaten(Lisa,5) n.applesEaten(Rob,7) } public int applesEaten(String name, int apples){ //because this method was called 3 times for bob and bob ate 5+9+5 apples, //this method should return 19. and 8 for Lisa, 7 for Rob. }
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started