(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;
(ii)Explain two (2) programming situations that will require the use of overloaded methods [5 marks]
public class Main
{
public static void main(String[] args) {
//initialize variables
int product = 5;
int c = 4;
while (c <= 5) {
product *= c;
++c;
}
System.out.println(product);
//first initialize variables before him using
int gender = 0;
if (gender == 1) {
System.out.println("Woman");
} else {
System.out.println("Man");
}
//initialize variables z and sum
int z = 0;
int sum = 1;
while (z <= 0) {
sum += z;
//Should be a break condition for stopping while loop othervice it will infinite.Other solution change the condition:: inside the while loop.
}
}
}
//(ii)Overloading in Java is the ability tocreate multiple methods of the same name, but with different parameters.
//The main advantage of this is cleanlinessof code.
Comments
Leave a comment