You are on page 1of 2

Singapore Polytechnic TOTAL MARKS

School of Electrical & Electronic Engineering LAB1 @ 10%

ET0706 Object Oriented Programming /100

Sample Lab Test 1 Time Allowed: 1.5 hour

Class _DCPE/FT/3_________ Adm. No.______________________

Part 1: Fill in the blanks(10 marks, 5 mark each)


Refer to the following code segment:
int v[]={1, 4, 7, 8};
public static int findProduct(int j, int k){
return j*k;
}
public static double findProduct(double j, double k){
return j*k;
}
To correct way to call the method to calculate the product of array v’s first and last
element is findProduct(v[0], v[v.length-1]); or __ findProduct(v[0], v[3]);
or_ findProduct(v[3], v[0]); or findProduct( v[v.length-1], v[0]); . The method call
result should be 8.

Part 2: Debugging:(10 marks, 5 mark each)


There are 2 errors in the codes below, please correct the errors.
String b = "I must try my best!";
System.out.println("The length of "+b+" is "+b.length);
if (b.charAt( 3)== 'u') System.out.println("The 4th letter in string " + b +" is u.");
else (b.charAt( 3)!= 'u')System.out.println("The 4th letter in string " + b +" is not u.");

Corrected codes: System,out.println("The length of "+b+" is "+b.length());//add() after b,length


else //remove (b.charAt( 3)!= 'u') after else___________________________
Part 3: Coding(80 marks)
/*
SampleLT1.java
* by __________. pXXXXXX, DCPE/3_____
*/
//please try to use while loop, for loop to replace do-while loop below.
import javax.swing.JOptionPane;

public class SampleLT1 {


public static void main(String[] a){
double dAllowance;
int days;
String strDAllowance, strDays;
do{
strDAllowance = JOptionPane.showInputDialog(null,"Please enter Daily Allowance $:(>0)",
"Allowance", JOptionPane.INFORMATION_MESSAGE);
dAllowance = Double.parseDouble(strDAllowance);
if (dAllowance<0) System.out.println("Daily Allowance shoould be >0. Please enter again.");
if (dAllowance==0) {//if enter daily Allowance as 0, exit the program
System.out.println("Thank you for using Allowance Calculating Application.");
System.exit(0);
}
}while (dAllowance<0);//if daily allowance is smaller than 0, enter again

do{
strDays = JOptionPane.showInputDialog(null,"Please enter No. of working days: (>0 and <42)",
"Allowance", JOptionPane.INFORMATION_MESSAGE);
days = Integer.parseInt(strDays);
if(days<0 || days>42) System.out.println("No. of working days should >0 and <42. Please enter
again.");
if (days==0) {//enter 0 to quit the program
System.out.println("Thank you for using Allowance Calculating Application.");
System.exit(0);
}
}while (days<0 || days>42);//if days <0 or >42, enter days again
System.out.println("For an internship student who worked "+days +" days with daily allowance
$"+dAllowance+", his total allowance is $"+calAllowance(dAllowance,days));
System.exit(0);
}

public static double calAllowance(double dailyAllowance, int noOfDays){


return dailyAllowance * noOfDays;//calculate and return total allowance
}
}

You might also like