uta005

4
Note: Submit by 4-5-2015 UTA005 - Assignment 2 1 Consider the following two classes: public class ClassA { public void methodOne(int i) { } public void methodTwo(int i) { } public static void methodThree(int i) { } public static void methodFour(int i) { } } public class ClassB extends ClassA { public static void methodOne(int i) { } public void methodTwo(int i) { } public void methodThree(int i) { } public static void methodFour(int i) { } } a. Which method overrides a method in the superclass? b. Which method hides a method in the superclass? c. What do the other methods do? 2 Create a class named Square that contains data fields for height, weight, and surfaceArea, and a method named computeSurfaceArea(). Create a child class named Cube . Cube contains an additional data field named depth, and a computeSurfaceArea() method that overrides the parent method. Write a program called DemoSquare that instantiates a Square object and a Cube object and displays the surface areas of the objects. 3 Write a program called CarRental that computes the cost of renting a car for a day based on the size of the car: economy, medium, or full-size. Include a constructor that requires the car size. Add a subclass called CarPhone to add the option of a car phone. Write a program called UseCarRentalAndPhone to use these classes. 4 Consider the following program: class Horse { public void Print() { System.out.println("Horse"); } } class RaceHorse extends Horse { public void Print() { System.out.println("RaceHorse"); } }

Upload: daman-toor

Post on 17-Jul-2015

522 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Uta005

Note: Submit by 4-5-2015

UTA005 - Assignment 2

1 Consider the following two classes:

public class ClassA {

public void methodOne(int i) {

}

public void methodTwo(int i) {

}

public static void methodThree(int i) {

}

public static void methodFour(int i) {

}

}

public class ClassB extends ClassA {

public static void methodOne(int i) {

}

public void methodTwo(int i) {

}

public void methodThree(int i) {

}

public static void methodFour(int i) {

}

}

a. Which method overrides a method in the superclass? b. Which method hides a method in the superclass?

c. What do the other methods do?

2 Create a class named Square that contains data fields for height, weight, and surfaceArea, and a method named computeSurfaceArea(). Create a child class named Cube. Cube contains an additional data field named depth, and a computeSurfaceArea() method that

overrides the parent method. Write a program called DemoSquare that instantiates a Square object and a Cube object and displays the surface areas of the objects.

3 Write a program called CarRental that computes the cost of renting a car for a day based

on the size of the car: economy, medium, or full-size. Include a constructor that requires the car size. Add a subclass called CarPhone to add the option of a car phone. Write a program called UseCarRentalAndPhone to use these classes.

4 Consider the following program:

class Horse {

public void Print() {

System.out.println("Horse");

}

}

class RaceHorse extends Horse {

public void Print() {

System.out.println("RaceHorse");

}

}

Page 2: Uta005

class Test {

public static void DoPrint( Horse h ) {

h.Print();

}

public static void main(String[] args) {

Horse x = new Horse();

Horse y = new RaceHorse();

Racehorse z = new RaceHorse()

DoPrint(x);

DoPrint(y);

DoPrint(z);

}

}

What is printed when this program is executed? Consider the following program:

interface Employee {

public void RaiseSalary();

}

interface Musician {

public Play();

}

class Test implements Employee, Musician {

public void RaiseSalary() {

System.out.println("raising");

}

public void Play() {

System.out.println("playing");

}

public static void main(String[] args) {

Test x = new Test();

x.RaiseSalary();

x.Play();

}

}

True or false:

1. An attempt to compile this program fails because class Test tries to implement two

interfaces at once ______. 2. An attempt to compile this program fails because class Test only implements

interfaces, it does not extend any class ______. 3. An attempt to compile this program fails because the definitions of the two

interfaces and the class are all in the same file ______.

4. It is optional for class Test to implement the methods RaiseSalary and Play ______.

Page 3: Uta005

5 . Consider the following classes B and D:

class B {

public B() { System.out.println("B()"); }

public B(int n) { System.out.println("B(" + n + ")"); }

}

class D extends B {

private B b;

public D() { System.out.println("D()"); }

public D(int n) {

super(n); // this gets executed first [2] and note

// that the corresponding constructor from the superclass

// will be called and B(3) is printed since n == 3

b = new B(-n); // this comes next [3]

// when a new object is created the same constructor will

// be invoked, albeit for a different object, and B(-3)

// will be printed since n == 3 and -n == -3

System.out.println("D(" + n + ")"); // [3] and

// finally this line prints D(3) and the original

// invocation is finished and our simple program ends

}

}

What does the following program print and why? public static void main(String[] args) {

D d = new D(3); // it starts here [1]

}

Trace the program by hand to explain the mechanism and its output.

6 A super class Record has been defined to store the names and ranks of 50 students. Define

a sub class Rank to find the highest rank along with the name. The details of both classes are given below:

Class name : Record

Data Members / instance variables:

name[ ] : to store the names of students rnk[ ] : to store the ranks of students

Member functions:

Record() : constructor to initialize data members void readvalues() : to store names and ranks void display() : displays the names and the corresponding ranks

Class name : Rank

Data Members / instance variables:

index : integer to store the index of the topmost rank

Member functions

Page 4: Uta005

Rank() : constructor to invoke the base class constructor and to initialize index to 0. void highest() : finds the index location of the topmost rank and stores it in index without sorting the array 6

void display() : displays the name and ranks along with the name having the topmost rank.

Specify the class Record giving details of the constructor(), void readvalues(), void

display(). Using the concept of inheritance, specify the class Rank giving details of

constructor(), void highest() and void display().

[Note: Write the main() function also in this program so as to familiarize the students on how to run programs based on the concept of inheritance.]