I. Identify and correct the errors in each of the following sets of code: [6 marks] i.
while ( c <= 5 ) {
product *= c; ++c;
ii. if ( gender == 1 )
System.out.println( "Woman" );
else;
System.out.println( "Man" );
iii. What is wrong with the following while statement?
while ( z >= 0 )
sum += z;
D. Explain two (2) programming situations that will require the use of overloaded methods [5 marks]
1 There is no closing curly brace.
while (c <= 5) {
product *= c;
++c;
}
2 Semicolon after else.
if (gender == 1)
System.out.println("Woman");
else
System.out.println("Man");
3 If z is greater than or equal to 0, then the loop will become infinite.
while (z >= 0) {
sum += z;
z--;
}
D If you want the child method to have different behavior, when you need to implement an abstract method or an interface method.
Comments
Leave a comment