[go: up one dir, main page]

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

Java Programming

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 7

TEACHERS COLLEGE EXAMINATION MAY 2015

COMMON PAPER
COMPUTER STUDIES
JAVA PROGRAMMING
[CS 305]
Year Three Secondary Time: 3 hours

INSTRUCTIONS:

Answer all questions.

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;

public class JavaApplication7


{
static Scanner console = new Scanner(System.in);
public static void main(String []args)

{
Scanner scan = new Scanner(System.in);
int numberOfMinutes = 0;
double amountDue;

double CONNECTION_FEE = 1.99;


double FIRST_THREE_MINUTES = 2.00;
double EACH_ADDITIONAL_MINUTE = 0.45;

System.out.print("Enter the number of minutes the call lasted: ");


numberOfMinutes = scan.nextInt();

if (numberOfMinutes > 3)
amountDue = (numberOfMinutes - 3) * EACH_ADDITIONAL_MINUTE
+ CONNECTION_FEE + FIRST_THREE_MINUTES;

else
amountDue = CONNECTION_FEE + FIRST_THREE_MINUTES;

System.out.printf("Amount due = $%.2f %n", amountDue);


}
}
Question 2

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;

public Employee (String setEmployee_name)


{
employee_name = setEmployee_name;
}

public String getEmployee_name()


{
return employee_name;
}

public void display (int result)


{
if (result > 20000)
{
System.out.printf("The name of the employee is %s\n",
getEmployee_name());
System.out.printf("The employee salary is %d", result);}
}

public static void main(String[] args)


{
String employee_name;
int employee_salary;
int result;

Scanner input = new Scanner (System.in);

System.out.printf("Enter the name of the employee");


employee_name = input.nextLine ();

System.out.printf("Enter the employee salary");


employee_salary =input.nextInt ();

Salary mysal = new Salary (employee_name, employee_salary);


result = mysal.getMonthly_salary();

mysal.display(result);

}
}
public class Salary extends Employee
{
private int monthly_salary;

public Salary (String employee_name, int setMonthly_salary)


{
super (employee_name);
monthly_salary = setMonthly_salary;

public int getMonthly_salary ()


{
return 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;
}

Rectangle(double x,double y,double height,double width)


{
this.x=x;
this.y=y;
this.height=height;
this.width=width;
}

double getArea()
{
return height*width;
}

double getPerimeter()
{
return 2*(height+width);
}

boolean point(double x,double y)


{
double pointX = x;
double pointY = y;
if (pointX < (this.x + (this.width)) && pointX > (this.x - (this.width)) &&
pointY < (this.y + (this.height)) && pointY > (this.y - (this.height)))
return true;
else
return false;
}

public static void main(String [] args)


{
Rectangle obj=new Rectangle();
Rectangle obj1=new Rectangle(5,2,10,50);
System.out.println("For default constructor");
System.out.println("Area = "+obj.getArea());
System.out.println("perimeter = "+obj.getPerimeter());
System.out.println("point inside rectangle ="+obj.point(0,0));
System.out.println("For default constructor");
System.out.println("Area = "+obj1.getArea());
System.out.println("perimeter = "+obj1.getPerimeter());
System.out.println("point inside rectangle ="+obj1.point(5,2));

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

public Book (String setAuthor_name)


{
author_name = setAuthor_name;
}

public String getAuthor_name()


{
return author_name;
}

public void display (String result)


{
{
System.out.printf("The name of the author is %s\n", getAuthor_name());
System.out.printf("The title of the book is %s", result);}
}
}

public class Book_Publication extends Book


{

private String title;

public Book_Publication (String author_name, String setTitle)


{
super (author_name);
title = setTitle;

public String getTitle ()


{
return title;
}
}

import java.util.Scanner;

public class BookPolymorphism


{
public static void main(String[] args)
{
String author_name;
String title;
String result;

Scanner input = new Scanner (System.in);

System.out.printf("Enter the name of the author");


author_name = input.nextLine ();

System.out.printf("Enter the title of the book");


title =input.nextLine ();

Book_Publication my1 = new Book_Publication (author_name, title);


result = my1.getTitle();
my1.display(result);

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

You might also like