Exercise 01
Exercise 01
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double ticketPrice;
System.out.print("Please enter age: ");
int age= input.nextInt(); // input age
if (age >= 13) // determine age category
ticketPrice=10.00; //set ticket price to 10
else
ticketPrice=7.00; // set ticket price to 7
i) Write the output for the program when input is 12. (2 marks)
ii) Write an algorithm (either pseudocode or flowchart) for the program in (4 marks)
Figure 1.
Table 1
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int country_code;
System.out.println("Please enter country code ");
country_code = input.nextInt(); //get input
switch(country_code) {
case 1: System.out.println("Canada");
break;
case 60: System.out.println("Malaysia");
break;
case 62: System.out.println("Indonesia");
break;
case 66: System.out.println("Thailand");
break;
} //switch
}
}
Figure 2
c) Loops can be controlled using several ways such as counter controlled, (4 marks)
sentinel controlled and flag controlled. Differentiate counter controlled and
sentinel controlled.
d) Write a Java code segment to print odd numbers from 1 to 15 (use loops). Here
is output sample (Figure 3): (4 marks)
Figure 3
2
QUESTION 2
a) Identify and correct ONE (1) error in the following Java program in Figure (3 marks)
4.
3
ii) Write the output for the program if the statement (2 marks)
System.out.println(methodMisteri(2.0,5.0)); is replaced
with System.out.println(methodMisteri(2,5));
iii) Identify whether the following method call is valid. Give a reason. (3 marks)
System.out.println(methodMisteri (2.0,5.0,3.0);
iv) Identify whether the following method call is valid. Give a reason. (3 marks)
System.out.println(methodMisteri(“2”,0,”5”);
d) Write a test program (main method) that prompts the user to enter three (5 marks)
numbers and invokes the method in c) and print the average.
Here is a sample run:
QUESTION 3
class bulatan{
private double jejari;
4
public bulatan(){
jejari =1.5;
}
5
QUESTION 4
▪ Two integer data fields named width and height that specify the width
and height of a rectangle. The default values are 10 for both width and
height.
▪ A no-arg constructor that creates a default rectangle.
▪ A constructor that creates a rectangle with the specified width and
height.
▪ A method named getArea that returns the area of this rectangle
▪ Area= width x height
▪ A method named getPerimeter that returns the perimeter of this
rectangle.
▪ Perimeter= 2(width + height)
(10 marks)
The UML class diagram for rectangle is given as follows (Figure 8).
Rectangle
width:int
height:int
Rectangle()
Rectangle(newWidth:int, newHeight:int)
getArea():int
getPerimeter():int
Figure 8
b) Write a test program that creates two Rectangle objects- one with width 20 (6 marks)
and height 30 and the other one with width 100 and height 200. Display the
area and perimeter of each rectangle.
6
QUESTION 5
Use the following java program in figure 9 to support your answer in the
following question a), b), c), d) and e).
class circle {
int radius;
double getArea(){
return (3.14*(radius*radius));}
}
}
}
Figure 9
a) State the number of classes and the number of objects defined in the (3 marks)
program.
b) Explain inheritance and how inheritance promotes code reusability. (6 marks)