CSCE145 Test 2-Review 03/29/2015 Hongkai Yu 1. What results are printed when the main method in TestBase is executed? public class Base private int value; public Base(int x) value = x; System.out.println( Constructed Base: + value); public class TestBase extends Base private Base val; public TestBase(int x, int y) super(x); val = new Base(y); public static void main(string[] args) TestBase d = new TestBase(11, 28); Constructed Base: 11 Constructed Base: 28 2. Given the following classes named Rectangle and Point, write an equals method for the Rectangle class that returns true if two rectangles have the same area, and returns false otherwise: public class Point public double x; // Because these are public, you can access them public double y; // directly without getters and setters public class Rectangle private Point ll; // the lower left corner of the rectangle private Point ur; // the upper right corner of the rectangle public boolean equals(rectangle r1) double arear1 = (r1.ur.x - r1.ll.x)*(r1.ll.y - r1.ur.y); double areathis = (this.ur.x - this.ll.x)*(this.ll.y - this.ur.y); if (arear1 == areathis) return true; else return false
3. The Building class is defined in terms of the following private instance variables: a String owner and an int numberofrooms. Assume there are accessor methods for the instance variables, called getnumberofrooms() and getowner(). Write a method equals for the Building class that determines whether an instance of the Building class has the same values for its instance variables as another instance of the Building class. public boolean equals(building b) if (this.getnumberofrooms() == b.getnumberofrooms() && this.getowner.equals(b.getowner)) return true; else return false; 4. Given an array of Building objects, where Building is defined as in problem #1, write a Java method that will accept the array of Building objects as a parameter and will display, i.e., print, the owner and numberofrrooms of each Building. public void print(building[] b) for(int i=0; i< b.length; i++) System.out.println( Owner is + b[i].getowner() + and # of room = + b[i].getnumberofrooms()); 5. Given the array from problem #3, write a Java method that will accept the array of Building objects as a parameter and will return the total number of rooms in all of the buildings in the array. public int gettotalrooms(building[] b) int total = 0; for (int i=0; i< b.length; i++) total += b[i].getnumberofrooms(); 6. The class House is derived from the base class Building (from problem #1) because it has an additional private instance variable, a boolean hasgarage. Write a constructor for House that takes three
parameters for owner, numberofrooms, and hasgarage, and that makes use of the following constructor for Building: public Building(String own, double n) owner = own; numberofrooms = n; public House(String owner, int numberofrooms, boolean hasgarage) super(owner, numberofrooms); this.hasgarage = hasgarage; // Problem 7 to 9 are together. 7. We have a class called 2DPoint like this: public class 2DPoint private double x; private double y; Please write default constructor and parameterized constructor for this class. 8. Write a new class called 3DPoint derived from the class 2DPoint. 3DPoint class has an additional private instance variable, a double type z. You do not need to write accessor methods(get and set). Default constructor and parameterized constructor have to be defined in your 3DPoint class. 9. Create an object/instance for the base class 2DPoint and another object/instance for the sub class 3DPoint. Use their parameterized constructors to initialize the object of 2DPoint to be (10,11) and the object of 3DPoint to be (100,110,120)
10. Suppose we have two overloading methods: public double ADD(double a, double b) return a+b; public double ADD(double a, String b) return -9999; If we call ADD(3.0, 4.0) in the main method, what will be the result? If we call ADD(3.0, 4.0 ) in the main method, what will be the result? 11. Please explain the difference of overloading method and overriding method in JAVA? 12. Polymorphism question: We have the following three classes: public class Person public void Test () System.out.println ("Base Class Person"); public class Doctor extends Person public void Test () System.out.println ("Sub Class Doctor"); public class Professor extends Person
public void Test () System.out.println ("Sub Class Professor"); Given the above classes, what will be the printout of the following code? public static void main(string[] args) Person[] people = new Person[4]; people [0] = new Doctor(); people [1] = new Professor(); people [2] = new Doctor(); people [3] = new Person(); for (Person p : people) p.test(); System.out.println(); 13. Question for interface: What is wrong with the following interface? And fix it. public interface SomethingIsWrong void amethod(int avalue) System.out.println("Hi Mom"); Answer: It has a method implementation in it. Fix the interface: public interface SomethingIsWrong void amethod(int avalue); 14. Please explain the idea of interface in JAVA.
15. Suppose we have an interface called Measurable, which defines the rule to measure the 2D shapes in Perimeter and Area. public interface Measurable /** Returns the perimeter. */ public double getperimeter (); /** Returns the area. */ public double getarea (); Write a class called Rectangle which implements the interface Measurable. Some parts of the class Rectangle are missed, please make up for it. public class Rectangle //do not need to worry about accessor methods(get and set) and constructors here private double Width; private double Height; public Rectangle (double w, double h) Width = w; Height = h; 16. Please explain abstract class and abstract methods. You need to understand them.