In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

Similar documents
19. GUI Basics. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

More about JOptionPane Dialog Boxes

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Inheritance and Polymorphism

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Course PJL. Arithmetic Operations

We are on the GUI fast track path

CS1150 Principles of Computer Science Objects and Classes

Chapter 2 ELEMENTARY PROGRAMMING

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

CS-202 Introduction to Object Oriented Programming

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Chapter 11 Inheritance and Polymorphism

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

CH. 2 OBJECT-ORIENTED PROGRAMMING

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

ITI Introduction to Computing II

ITI Introduction to Computing II

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JOptionPane Dialogs. javax.swing.joptionpane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs.

Chapter 2 Primitive Data Types and Operations

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

25. Generic Programming

Inheritance (continued) Inheritance

CST141 Thinking in Objects Page 1

Chapter 9 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

CS171:Introduction to Computer Science II. Li Xiong

Introduction Programming Using Python Lecture 8. Dr. Zhang COSC 1437 Fall 2017 Nov 30, 2017

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Chapter 2 Primitive Data Types and Operations

Inheritance Sort in ascending order. Reusability 5 Sort Take The 4 Order 12,10,5,4. Class. Use this class to define a new class

Chapter 6: Inheritance

System.out.print(); Scanner.nextLine(); String.compareTo();

JAVA Programming Concepts

Abstract Classes Interfaces

Topic 7: Algebraic Data Types

Java Object Oriented Design. CSC207 Fall 2014

Introduction to Java. Nihar Ranjan Roy.

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Inheritance and Polymorphism

Object-Oriented Programming in Java

4 WORKING WITH DATA TYPES AND OPERATIONS

Object Oriented Programming. Java-Lecture 11 Polymorphism

Lesson 3: Accepting User Input and Using Different Methods for Output

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Programming 2. Inheritance & Polymorphism

Guessing Game with Objects

1 Shyam sir JAVA Notes

Introduction. Introduction. Introduction

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Chapter 2 Elementary Programming

CLASS DESIGN. Objectives MODULE 4

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CSC 111, Test 1 (Fall 2009)

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Computer Science II (20073) Week 1: Review and Inheritance

Chapter 11 Inheritance and Polymorphism

Practice for Chapter 11

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Guessing Game with Objects

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Advanced Placement Computer Science. Inheritance and Polymorphism

Polymorphism and Interfaces. CGS 3416 Spring 2018

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

OBJECT ORİENTATİON ENCAPSULATİON

In this lab we will practice creating, throwing and handling exceptions.

Chapter 6 Class and Method

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Methods Common to all Classes

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Introduction to Programming Using Java (98-388)

Inheritance, Polymorphism, and Interfaces

CT 229 Fundamentals of Java Syntax

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 9 - Object-Oriented Programming: Polymorphism

Chapter 8 Objects and Classes

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

ITI Introduction to Computing II

Chapter 3 Objects and Classes

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

ITI Introduction to Computing II

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

IS502052: Enterprise Systems Development Concepts Lab 1: Java Review

Transcription:

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