Uploaded by YongKang YK

Solution for JAN 2019(1)

advertisement
SAMPLE SOLUTION
Question 1
Solution:
(a)
(i) True
(ii) False
(iii) False
(iv) True
(v) True
(vi) True
(vii) True
(viii) False
(ix) True
(x) False
(10 x 1 mark = 10 marks)
(b)
public static int square(int num)
{
return (num * num);
}
(i)
public static void main(String args[]) {
int list[] = {2,4,6,8,10};
int total = 0;
int i;
for(i=0;i<5;++i)
total = total + list[i];
System.out.println(total);
}
iii)
import javax.swing.JOptionPane;
public class SecondConverter {
public static void main(String args[])
{
String second = JOptionPane.showInputDialog(null,"Enter seconds","Second
Converter",JOptionPane.PLAIN_MESSAGE);
int sec = Integer.parseInt(second);
JOptionPane.showMessageDialog(null,"You Have Entered : "+sec +
"seconds","Confirmation",JOptionPane.PLAIN_MESSAGE); <3 marks>
Question 2
(i)
Marking Scheme
Solution:
class Car extends Vehicle {
private int passenger;
public Car() {
super(0,’ ‘);
setPassenger(0);
}
public Car(int nw, char ft, int p) {
super(nw,ft);
setPassenger(p);
}
private void setPassenger(int p) {
passenger = p;
}
public int getPassenger() {
return passenger;
}
public String toString() {
return “Car : \t\t”+super.toString() +"\t"+passenger;
}
}
(ii)
Marking Scheme
class UseVehicle {
public static void main (String args[]) {
Car c1 = new Car(4,’P’,5);
Car c2= new Car(4,’D’,7);
System.out.println("Vehicle\t#Wheels\tFuel\t#Passenger ");
System.out.println(c1());
System.out.println(c2());
}
}
Question 3
a)
i)
Total=213
ii) Temp = 11 Value :7
ii)
i= 5 j= 7
i= 7 j= 7
i= 9 j= 10
i= 11 j= 11
Res :18
b) Marking Scheme
import java.util.Scanner;
public class TestingArea {
public static void main(String[] args) {
String []names = new String[10];
int []scores = new int[10];
int i, temp =0;
int maxscore =0;
Scanner in = new Scanner(System.in);
for( i = 0; i < 10; ++i){
System.out.print("Enter Team Name:");
names[i] = in.next();
System.out.print("Enter Score:");
scores[i] = in.nextInt();
if(scores[i]> maxscore)
{
maxscore = scores[i];
temp = i;
}
}
System.out.println("Team : " + names[temp]+ "Score"+
scores[temp]);
}
}
Question 4
Sample solution
(a) Correct Book class (6 marks)
public abstract class Book {
String title;
double price;
public Book(String t) {
title = t;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
public abstract void setPrice();
}
(6 marks)
(b) Solution
Answer:
Overriding method is when a subclass needs to over write the implementation of a method
inherited from its parent class. To overwrite the method, the subclass must have the same
method name, the same return data type, and/or the same number of arguments in its
parameter list. For example:
class objectA {
…
public void display {
System.out.println(“Hello World”);
}
}
class object extends objectA {
…
public void display {
System.out.println(“Hello Jupiter”);
}
})
Answer:
Overloading method: In a class, if you include a method definition that has the same name
and the same number of parameters or arguments but return a different data type of the same
method name and different number of parameters with the same returning data type, you are
implementing the concept of polymorphism.
Example:
public int add (int x, int y) { return x+y; }
public int add (int x, int y, int z) { return x+y+z; }
public double add (double x, double y) { return x+y; }
Note : Any correct explanation and example is acceptable.
(8 marks)
(c )
(i)
Sample Solution
private void checkBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String value = textField1.getText();
int val = Integer.parseInt(value);
if ( val % 2 == 0) // 1 m
labelMsg.setText(val + " is an even number");
else
labelMsg.setText(val + " is an odd number");
}
(ii) Sample Solution
private void clearBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
textField1.setText("");
labelMsg.setText("");
}
Question 5
(a)
Sample solution
 Correct try block (3 marks)
 Correct catch block (4 marks)
try{
input = JOptionPane.showInputDialog("Enter a floating point number >> ");
num = Double.parseDouble(input);
}
catch(NumberFormatException error) {
num = 0;
JOptionPane.showMessageDialog(null, "Value entred cannot be converted to a floatingpoint number");
(9 marks)
(a) Sample solution
import java.util.Scanner;
class Insurance {
public static void main (String args[]) {
int currYear;
int birthYear;
int premium;
Scanner input = new Scanner(System.in);
System.out.print("Enter the current year >> ");
currYear = input.nextInt();
System.out.print("Enter the birth year >> ");
birthYear = input.nextInt();
premium = calculatePremium(currYear, birthYear);
System.out.println("The premium is $" + premium);
}
public static int calculatePremium(int curr, int birth) {
final int ADDITION_FACTOR = 15;
final int MULTIPLICATION_FACTOR = 20;
int age = curr - birth;
int decade = age / 10;
int premium = (ADDITION_FACTOR + decade) * MULTIPLICATION_FACTOR;
return premium;
}
}.
(16 marks)
Download