Java Programming
Java Programming
Java Programming
COMMON PAPER
COMPUTER STUDIES
JAVA PROGRAMMING
[CS 305]
Year Three Secondary Time: 3 hours
INSTRUCTIONS:
Total marks 70
Question 1
The cost of an international call from New York to New Delhi is calculated as follows:
connection fee< $1.99; $2.00 for the first three minutes; and $0.45 for each additional minute.
Write a Java program that prompts the user to enter the number of minutes the call lasted and
outputs the amount due. . [10 marks]
import java.util.Scanner;
import java.text.DecimalFormat;
{
Scanner scan = new Scanner(System.in);
int numberOfMinutes = 0;
double amountDue;
if (numberOfMinutes > 3)
amountDue = (numberOfMinutes - 3) * EACH_ADDITIONAL_MINUTE
+ CONNECTION_FEE + FIRST_THREE_MINUTES;
else
amountDue = CONNECTION_FEE + FIRST_THREE_MINUTES;
Declare a class called employee having <employee id> and <employee name> as its member.
Extend class employee to have a subclass called salary having <monthly salary> as its member.
You are required to define constructors and method main for receiving the name and salary of an
employee. In addition, a method to calculate and display all details of an employee who earns
more than one hundred thousand dollars. . [10 marks]
public class Employee
{
private String employee_name;
mysal.display(result);
}
}
public class Salary extends Employee
{
private int monthly_salary;
Question 3
Define the Rectangle class that contains: Two double fields x and y that specify the center of the
rectangle, the data field width and height , A no-arg constructor that creates the default rectangle
with (0,0) for (x,y) and 1 for both width and height. A parameterized constructor creates a
rectangle with the specified x,y,height and width.
Required:
i. A method getArea() that returns the area of the rectangle.
ii. A method getPerimeter() that returns the perimeter of the rectangle.
iii. A method contains(double x, double y) that returns true if the specified point
(x,y) is inside this rectangle.
iv. Write a test program that creates two rectangle objects. One with default values
and other with user specified values. Test all the methods of the class for both
the objects. . [20 marks]
class Rectangle
{
double x,y,height,width;
Rectangle()
{
x=0;
y=0;
height=1;
width=1;
}
double getArea()
{
return height*width;
}
double getPerimeter()
{
return 2*(height+width);
}
}
}
Question 4
Declare a class called book having <author_name> as private data member. Extend book
class to have a sub class called <book_publication> with private member called title.
Write a complete program to show usage of polymorphism to display <book_publication> of
given author. . [10 marks]
public class Book
{
private String author_name;
import java.util.Scanner;
input.close();
}
Question 5
Write a program that prompts the user to enter the length in feet and inches and outputs the
equivalent length in centimeters. If the user enters a negative number or a nondigit number,
throw and handle an appropriate exception and prompt the user to enter another set of numbers.
. [10
marks]
Question 6
The area of a rectangle is calculated using the formula: a = l * w. Write a GUI application that
has two text fields for accepting the length and width of a rectangle. A text field must be used for
displaying the calculation and one action button, <Calculate Area>. The user must be prompted
to enter the length and width rectangle in the text fields. Furthermore, after the user clicks on the
button <Calculate Area> the area of the rectangle should be calculated and displayed in a text
field. . [10 marks]
END OF EXAMINATION