Java Test #2 Solutions: Overloading, Hiding, Inheritance, Interfaces & File I/O, Exams of Computer Science

The solutions to test #2 of the cop3330.01 java programming course, which covers topics such as method overloading, constructor invocation, instance variable hiding, interface implementation, and file i/o using java. The solutions include multiple choice questions, java code examples, and explanations.

Typology: Exams

Pre 2010

Uploaded on 11/08/2009

koofers-user-vye
koofers-user-vye 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COP3330.01, Fall 2001
S. Lang Solution Key to Test #2 (11/07/2001)
1. (30 pts.) Multiple Choice questions. Select the answers from the list following this problem,
and place the numerical label next to the answer into the blank space of the questions. Note
that an answer from the answer list may be used multiple times (or zero times).
(a) The situation of methods (or constructors) within a class hierarchy that use the same
name but have different parameters is overloading.
(b) The constructor of an extended class uses the method name super to invoke the
constructor of its base class.
(c) When assigning an object of a supertype to a reference of a subtype, cast is needed to
avoid the compile-time error.
(d) An interface can extends other interface or interfaces.
(e) When splitting a class into an interface and an implementation, instance variable of the
class cannot be kept in the interface.
Answer questions (f) (j) based on the Java code in the code box below:
(f) How is method m1( ) of class B related to method m1( ) of class A overloading
(g) How is instance variable double x of class B related to int x of class A hiding
(h) In class C, how is method m2( ) of class A related to method m2( ) of interface K nothing
(i) How is method m1( ) implemented in class C inherits
(j) How is method m2( ) of class B related to method m2( ) of class A overriding
Answer List (1 –15): Java code for Questions (f) (j):
1. hiding
2. overloading
3. overriding
4. compile-time error
5. super
6. instance variable
7. method
8. static constant
9. this
10. cast
11. collapsing
12. implements
13. extends
14. inherits
15. nothing
interface K {
void m1( );
int m2( );
}
class A {
public void m1( ) { …}
public void m2( ) { …}
protected int x;
}
class B extends A {
public void m1(int x) { …}
public void m2( ) { …}
protected double x;
}
class C extends B implements K {
….
}
pf3
pf4

Partial preview of the text

Download Java Test #2 Solutions: Overloading, Hiding, Inheritance, Interfaces & File I/O and more Exams Computer Science in PDF only on Docsity!

COP3330.01, Fall 2001 S. Lang Solution Key to Test #2 (11/07/2001)

1. (30 pts.) Multiple Choice questions. Select the answers from the list following this problem, and place the numerical label next to the answer into the blank space of the questions. Note that an answer from the answer list may be used multiple times (or zero times). (a) The situation of methods (or constructors) within a class hierarchy that use the same name but have different parameters is overloading. (b) The constructor of an extended class uses the method name super to invoke the constructor of its base class. (c) When assigning an object of a supertype to a reference of a subtype, cast is needed to avoid the compile-time error. (d) An interface can extends other interface or interfaces. (e) When splitting a class into an interface and an implementation, instance variable of the class cannot be kept in the interface. Answer questions (f)  (j) based on the Java code in the code box below: (f) How is method m1( ) of class B related to method m1( ) of class A overloading (g) How is instance variable double x of class B related to int x of class A hiding (h) In class C, how is method m2( ) of class A related to method m2( ) of interface K nothing (i) How is method m1( ) implemented in class C inherits (j) How is method m2( ) of class B related to method m2( ) of class A overriding Answer List (1 –15): Java code for Questions (f)(j): 1. hiding 2. overloading 3. overriding 4. compile-time error 5. super 6. instance variable 7. method 8. static constant 9. this 10. cast 11. collapsing 12. implements 13. extends 14. inherits 15. nothing interface K { void m1( ); int m2( ); } class A { public void m1( ) { …} public void m2( ) { …} protected int x; } class B extends A { public void m1(int x) { …} public void m2( ) { …} protected double x; } class C extends B implements K { …. }

2. (30 pts.) Answer both questions (a) and (b) of this problem using the Employee and Boss classes as the basis. Write your Java code clearly and, in particular, show the difference between your writing of upper-case and lower-case letters very clearly. Your code will be graded based on its correctness and its conciseness. (a) (10 pts.) Define an extended class of Boss named BonusBoss which has one additional instance variable named bonus of type double. The class needs to implement the following methods: (1) a constructor that takes two String parameters for the first and last names of the employee, and two double parameters for the weeklySalary and bonus fields; (2) a earnings( ) method that returns the sum of weeklySalary and bonus; and (3) a toString( ) method that returns a String consisting of “BonusBoss: “ followed by the first and last names, then the earnings( ), separated by one space between adjacent fields. /** Answer to Problem 2(a) on Test #2 / public class BonusBoss extends Boss { private double bonus; public BonusBoss(String first, String last, double salary, double bonus) { super(first, last, salary); this.bonus = bonus; } public double earnings() { return super.earnings() + bonus; } public String toString() { return "BonusBoss: " + getFirstName() + " " + getLastName() + " " + earnings(); } } (b) (20 pts.) Write a class TestBonusBoss that uses a single method main( ) to perform the following: (1) open a text file whose name is passed to main( ) via the first command-line argument; the file consists strings representing the fields of BonusBoss employees with one employee record per line; terminate the program with a message if the argument count is not 1; (2) read the file line by line, tokenizing the strings of each line and creating one BonusBoss object for each line, saving each BonusBoss object into a Vector; (3) when the input is complete, close the file, output (to the screen) the BonusBoss objects in the order of the last in the Vector to the first in the Vector, using the toString( ) method for each BonusBoss object and with one object per line; (4) when Step (3) is done, output (to the screen) the average value of the earnings of all the BonusBoss employees. Be sure that your code checks for the IOException (only this exception is needed). /* Answer to Problem 2(b) on Test #2 / import java.util.; import java.io.*; public class TestBonusBoss { public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: java argument arg1"); System.exit(-1);

public Rectangle(double width, double length) { this.width = width; this.length = length; } public double area() { return widthlength; } } (d) (7 pts.) Define a class named Circle that extends ConcreteShape as follows: the class contains a constructor with one parameter radius of type double; it implements the method area( ) by returning the value of Pi * radius * radius. /* Answer to Problem 3(d) on Test 2 / public class Circle extends ConcreteShape { private double radius; public Circle(double radius) { this.radius = radius; } public double area() { return Piradiusradius; } } (e) (16 pts.) Write a Java class TestShapes which uses the single method main( ) to do the following: (1) create an array of type ConcreteShape[ ], of size 3; (2) store 1 Circle object of radius 2.0 into the array, then store 2 Rectangle objects of width, length equal to, respectively, 2.0, 3.0, and 4.0, 5.0, into the array; (3) use a for loop to go through the array to find out which object has the largest area, then output to the screen the message “a circle” or “a rectangle” depending on which type of that object. Be sure to use the operator instanceof in your code (in a meaningful way). /* Answer to Problem 3(e) on Test 2 */ public class TestShapes { public static void main(String[] args) { ConcreteShape[] arr = new ConcreteShape[3]; arr[0] = new Circle(2.0); arr[1] = new Rectangle(2.0, 3.0); arr[2] = new Rectangle(4.0, 5.0); int k = 0; double max = arr[0].area(); for (int i = 1; i < 3; i++) if (arr[i].area() > max) k = i; if (arr[k] instanceof Circle) System.out.println("Circle"); else System.out.println("Rectangle"); } // end of main() } // end of class TestShapes