Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) Specify all the data types (including classes) used in the program, giving on

ID: 3868145 • Letter: A

Question

a) Specify all the data types (including classes) used in the program, giving one of the values used for each type.

b) List all the loop constructs used in the program and give the keywords and syntax required in each case. Where there is the possibility to use an alternative construct, say what this is.

c) Describe the sequence of operations when calling getSpend with “P3” as the parameter. What will the returned value be?

d)Describe the sequence of operations when calling getSpend with “P1” as the parameter. What will the returned value be?

e)How will an instance of this class appear when printed out, i.e, when supplied as an actual parameter in a call to System.out.println()?

1. Consider the following Java program, which consists of the class Spends. Notice this defines getSpend, isAmount and toString methods. Remember that Integer.parseInt applied to a String representation of an integer returns the integer in question public class Spends I d private String [ ] {"P1", "10", "15", "P2", "12", = public int getSpend (String id) i int spend0; boolean done = false; for (int i-0; i

Explanation / Answer

a)

b)

1. FOR LOOP

for(init;condition;increment){}

Alternatively while loop can be used.

Eg. i=0;

while(i<d.length&&!done){

i++;

}

2. WHILE LOOP

while(condition ){}

Alternatively FOR LOOP can be used;

eg;

for(i=0;i<d.length;i++)

c)

1. i=0

2.done=false

3. Enter i loop

4. Enter j loop when d[i] equals P3 for i=7

5. Execute spend +=d[j] if d[j] is amount and j>=0

6. exit from j loop when d[j] =P3

7.exit i loop

Return value is 73

d)

1. i=0

2.done=false

3. Enter i loop

4. Enter j loop when d[i] equals P1 for i=0

5. Exit j loop when j=-1

6. exit i loop

Return value is 0

e) P2=25 P3=75 P4=3

Spends NA int (spend) 0 String (s) "" boolean (done) false String[] (d) {"P1 ".." P4"}