Jordan University Faculty of Engineering and Technology Department of Computer Engineering Object-Oriented Problem Solving: CPE 342 Lab-8 Eng. Asma Abdel Karim In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram. GeometricObject - color: String - filled: boolean + GeometricObject() + GeometricObject(color:String, filled:boolean) + getcolor(): String + setcolor(color:string): void + isfilled(): boolean + setfilled(filled:boolean): void - radius: double Circle + Circle(color:String, filled:boolean, radius: double) + getradius(): double + setradius(radius:double): void + getarea(): void Part I: Polymorphism and objects casting. Rectangle - width: double - length: double + Rectangle( color:string, filled:boolean, width:double, length:double) + getwidth(): double + setwidth(width:double): void + getlength(): double + setlength(length:double): void +getarea():void Remember that polymorphism means that a variable of a supertype can refer to a subtype object. Create a class that contains your main method (i.e. MainClass), then write a code that performs the following: 1. In your main method: (1 point) a. Declare an array of three objects of type GeometricObject named myarray. b. Create objects of the array such that: - The first object s actual type is GeometricObject whose color is red and is not filled. - The second object s actual type is Circle whose color is blue, is filled and its radius is 5. - The third object s actual type is Rectangle whose color is red, is not filled, with width=3 and height=4. c. Write a for loop that prints the details of each object in the array by invoking its tostring method. Observe the output of the print statements and justify how signature matching and dynamic binding work in this case. 1
2. Define a new method named printarea with the following header: (2 points) public static void printarea(geometricobject g) a. Try to print the area of the passed object by invoking the getarea method (g.getarea()), and justify the resulting syntax error. b. Now remove the print statement you wrote in the previous step, and use the instanceof operator to check whether the actual type of the passed object is a Circle or a Rectangle. Then, cast the object into its actual type in order to invoke the getarea method. The following code checks whether the passed object is a circle and invokes the getarea method after down-casting the passed object: if (g instanceof Circle){ System.out.println("Circle area = "+((Circle)g).getArea()); } c. In your main method, add a for loop that invokes the printarea method by passing elements of the array you created, one element in each iteration. Observe and justify the output. Part II: Overriding the Object s equals method. The following shows an example of an overridden implementation of the Object s equals method in the GeometricObject class which checks whether two objects are equal based on their color. @Override public boolean equals (Object O){ if (O instanceof GeometricObject) return color.equals(((geometricobject)o).color); else return false; } 1. Add the previous implementation of the equals method to the GeometricObject class. 2. Add an implementation that overrides the equals method in the Circle class by checking whether two circles are equal based on their radii.(1 point) 3. Add an implementation that overrides the equals method in the Rectangle class by checking whether two rectangles are equal based on their width and length. (1 point) 4. In your main method (1 points): a. Create a new Circle object name mycircle whose color is red, is filled and its radius is 5. b. Print the output of invoking the equals method by passing the Circle object you created as a parameter (i.e. myarray[i].equals(mycircle)). c. Print the output of invoking the equals method by passing elements of the array myarray as a parameter (i.e. mycircle.equals(myarray[i])). d. Observe and justify the difference in the output in the last two steps. 2
Part III: Protected class members. Protected class members can be accessed only from inside the class and inside its subclasses. The following is the UML class diagram of the GeometricObject class with its color and filled data fields defined protected. GeometricObject # color: String # filled: boolean + GeometricObject() + GeometricObject(color:String, filled:boolean) + getcolor(): String + setcolor(color:string): void + isfilled(): boolean + setfilled(filled:boolean): void 1. Change the visibility of the data fields color and filled in the GeometricObject class to protected. (1 point) 2. Try to access these data fields from any method in the subclasses Circle and Triangle and observe the change in its visibility. Part IV: Displaying objects details in a dialog box. In your main method, replace the statement in the for loop that prints details of objects in the array to the console with a statement that displays the details of each object in a dialog box instead. The dialog box should also show the order of the element with respect to other elements in the array as shown in the following figure: (1 point) Part V: Declaring constructors to get initial values of data fields from input dialog boxes. 1. Add a no-arg constructor to each of the three classes that prompts the user to enter the value of each data field and initializes it with the entered value. (2 points) Note that code written in the GeometricObject class to initialize the data fields color and filled should not be re-written in the subclasses Circle and Rectangle. Constructors of the subclasses should invoke the super-class constructor to initialize these data fields. 2. Modify the creation of the objects in the array myarray to use the no-arg constructors. Rerun your code and test it. 3
Appendix A-1: Displaying Text in a Message Dialog Box. In order to display text in a message dialog box, you need to use the showmessagedialog method in the JOptionPane class. JOptionPane is defined in the javax.swing package, so you need to import it in your code. Note that the method showmessagedialog is static, and hence can be invoked using the class name (JOptionPane). There are several ways to use the showmessagedialog method. Two of these ways are: 1) JOptionPane.showMessageDialog(null, x); The first argument can always be null and x is a string for the text to be displayed. 2) JOptionPane.showMessageDialog(null, x, y, JOptionPane.INFORMATION_MESSAGE); As in the first way, the first argument is null, x is a string for the text to be displayed, y is a string for the title of the message box, the fourth argument can be JOptionPane. INFORMATION_MESSAGE which causes the information icon to be displayed in the message box. Appendix A-2: Getting Input from Input Dialogs. An input dialog box prompts the user to enter an input graphically. You can obtain input from an input dialog box by invoking the JOptionPane.showInputDialog methods. The input is returned from the method as a string. There are several ways for using the showinputdialog method, two of these ways are: 1) String S = JOptionPane.showInputDialog(x); where x is a string for the prompting message. 2) JOptionPane.showInputDialog(null, x, y, JOptionPane.QUESTION_MESSAGE); where x is a string for the prompting message, and y is a string for the title of the input dialog. 4
The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns 123. You have to convert a string into a number to obtain the input as a number. To convert a string into an int value, use the Integer.parseInt method as follows: int intvalue = Integer.parseInt(intString); //intstring is a numeric string such as 123 To convert a string into a double value, use the Double.parseDouble method as follows: double doublevalue = Double.parseDouble(doubleString); //doublestring is a numeric string such as // 12.3 The Integer and Double classes are both included in the java.lang package, and thus are automatically imported. 5