0% found this document useful (0 votes)
6 views7 pages

Exercise 01

The document contains a series of programming questions related to Java, covering topics such as input handling, control structures, loops, error correction, method overloading, object-oriented programming principles, and class design. It includes tasks to write algorithms, rewrite code, identify errors, and explain concepts like inheritance and method overriding. Additionally, it requires the creation of classes and methods to perform specific functions, such as calculating areas and averages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Exercise 01

The document contains a series of programming questions related to Java, covering topics such as input handling, control structures, loops, error correction, method overloading, object-oriented programming principles, and class design. It includes tasks to write algorithms, rewrite code, identify errors, and explain concepts like inheritance and method overriding. Additionally, it requires the creation of classes and methods to perform specific functions, such as calculating areas and averages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

QUESTION 1

a) Consider the following Java program in Figure 1.

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

System.out.printf ("Your Ticket Price is RM


%.2f",ticketPrice);
}
}
Figure 1

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.

b) The following Java program (Figure 2) use a switch-case statement to


input country code and prints its names based on Table 1. Rewrite the (6 marks)
program using if-else-if statement.

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.

public class Main


{
public static void main(String[] args) {
ucap("Rose");
}

public static void ucap(String userName){


System.out.println(" Hello "+ userName);
return 0;
}
}
Figure 4

b) Consider the following program in Figure 5.

public class Main {


public static void main (String [] args){
System.out.println(methodMisteri(2.0,5.0));
}

public static double methodMisteri(double N1,


double N2){
return N1*N2;
}

public static int methodMisteri(int N1, int N2){


return N1+N2;
}
}
Figure 5

i) Write the output for the program. (2 marks)

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”);

c) Write a method that returns an average of a three given integers. (2 marks)


Use the following method header:
public static double avgThree(int N1, int N2, int N3)

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:

Please enter three integers:


2<enter>
3<enter>
10<enter>

The average for three integers is 5.0

QUESTION 3

a) List the four pillars of object-oriented programming. (4 marks)

b) Procedural programming is called ‘Spaghetti code” for the interdependencies (3 marks)


among the separated variable and function. Explain how object oriented
programming concept overcomes the problem.

c) Consider the following Java Program in Figure 6.

class bulatan{
private double jejari;

4
public bulatan(){
jejari =1.5;
}

public void setJejari(double newJejari){


jejari= newJejari;
}

public double getJejari(){


return jejari;
}
}
public class Main
{
public static void main(String[] args) {
bulatan bulat1 = new bulatan();
bulatan bulat2 = new bulatan();
System.out.println("Jejari bulatan pertama :"+
bulat1.getJejari());
bulat2.setJejari(7.0);
System.out.println("Jejari bulatan kedua
:"+bulat2.getJejari());
}
}
Figure 6

i) Write the output for the program.


(3 marks)
ii) Draw the UML Diagram for the Class and Object.
(6 marks)
iii) Explain the error if setJejari visibility method is changed from (3 marks)
public to private as follows (Figure 7).

Public private void setJejari(double newJejari){


jejari= newJejari;
}
Figure 7

5
QUESTION 4

a) Design a class named rectangle to represent a rectangle. The class contains:

▪ 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));}
}

class cylinder extends circle{


int height;
double getArea(){
return ((2*3.14*radius*height)+2*(super.getArea()));
}
}
public class Main
{
public static void main(String[] args) {
cylinder cil1= new cylinder();
cil1.radius=2;
cil1.height=3;
System.out.println(cil1.getArea());

}
}
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)

c) Explain superclass and derived class. (6 marks)

d) Explain method overriding. (5 marks)

e) Explain super keyword. (5 marks)

You might also like