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

Similar documents
Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

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

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

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Chapter 11 Inheritance and Polymorphism

CS1150 Principles of Computer Science Objects and Classes

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

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

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

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

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

Inheritance and Polymorphism

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

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

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

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

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

Abstract Classes Interfaces

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

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

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

CS171:Introduction to Computer Science II. Li Xiong

Chapter 11 Inheritance and Polymorphism

Java Classes, Objects, Inheritance, Abstract and Interfaces Recap

We are on the GUI fast track path

Generics. CSE260, Computer Science B: Honors Stony Brook University

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

COMP 250. inheritance (cont.) interfaces abstract classes

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

AShape.java Sun Jan 21 22:32: /** * Abstract strategy to compute the area of a geometrical shape. Dung X. Nguyen * * Copyright

Chapter 14 Abstract Classes and Interfaces

Programming 2. Inheritance & Polymorphism

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

Reusing Classes. Hendrik Speleers

Chapter 11 Inheritance and Polymorphism

Reviewing OO Concepts

Chapter 13 Abstract Classes and Interfaces

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

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

Abstract Classes and Interfaces. CSE 114, Computer Science 1 Stony Brook University

ITI Introduction to Computing II

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

ITI Introduction to Computing II

The Nervous Shapes Example

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

CS 1301 Ch 8, Handout 2

Chapter 13 Abstract Classes and Interfaces

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

Inheritance (Deitel chapter 9)

CMP-326 Exam 2 Spring 2018 Total 120 Points Version 1

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Making New instances of Classes

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CSE 143 Lecture 20. Circle

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Object Oriented Programming

The Spring Framework: Overview and Setup

CS2 Assignment A1S The Simple Shapes Package

CIS265 - Spring Exam 2 First Name Last Name CSU#

Interfaces. Relatedness of types. Interface as a contract. The area and perimeter of shapes 7/15/15. Savitch ch. 8.4

Lab Manual. Programming 2 CS 112. Student ID: Student Name:

Reviewing OO Concepts

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

Java Comparable interface

CS-202 Introduction to Object Oriented Programming

S.O.L.I.D: Software Engineering Principles

Advanced Object-Oriented Programming. 11 Features. C# Programming: From Problem Analysis to Program Design. 4th Edition

Object-Oriented Programming

Chapter 9 Abstract Classes and Interfaces

CIS 120 Midterm II March 31, 2017 SOLUTIONS

25. Generic Programming

CSCE145 Test 2-Review 03/29/2015 Hongkai Yu

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Java Primer. CITS2200 Data Structures and Algorithms. Topic 0

Chapter 9 - Object-Oriented Programming: Polymorphism

Programming in the Large II: Objects and Classes (Part 1)

Java Programming. Abstract classes and Interfaces

Rectangle. Lecture 2 week 2 - Objects & Classes Exercise 1. A. Write a driver class code in java depending on the following UML:

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS 113 PRACTICE FINAL

TWO-DIMENSIONAL FIGURES

Interfaces and itera-on. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Interfaces. An interface defines a set of methods. An interface declaration contains signatures, but no implementations.

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Chapter 13 Abstract Classes and Interfaces

UFCE3T-15-M Object-oriented Design and Programming

CS Week 13. Jim Williams, PhD

The Prototype Framework Part III: Better OOP

What is Polymorphism? CS 112 Introduction to Programming

Use the scantron sheet to enter the answer to questions (pages 1-6)

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

What is an interface? Why is an interface useful?

CSIS 10A Assignment 9 Solutions

Introduction to Computer Science Unit 4B. Programs: Classes and Objects

Introductory Programming Inheritance, sections

Transcription:

Chapter 21- Using Generics Case Study: Geometric Bunch In this example a class called GeometricBunch is made to wrap around a list of GeometricObjects. Circle and Rectangle are subclasses of GeometricObject. The subclasses are comparable. The wrapper includes three methods: showlist(), findlocationelementwithlargestarea(), and sortelements() Class: Driver import java.util.arraylist; public class Driver { /** * Author: V. Matos * Date: Spring-2012 * Goal: Various code fragments to demonstrate issues on * Java Generics - Chapter 21. */ public static void main(string[] args) { TestGeometricBunch test = new TestGeometricBunch(); test.main(null); //main

Class: TestGeometricBunch /** * Author: V. Matos * Date: Spring-2012 * Goal: Generics Case STudy - In this example a class called * GeometricBunch is made to wrap around a list of GeometricObjects. * Circle and Rectangle are subclasses of GeometricObject. The * subclasses are comparable. The wrapper includes three methods: * showlist(), findlocationelementwithlargestarea(), and sortelements() */ import java.util.arraylist; public class TestGeometricBunch { public static void main(string[] arg) { GeometricBunch<GeometricObject> bunch = new GeometricBunch<GeometricObject>(); Circle c1 = new Circle(1); Circle c2 = new Circle(2); Circle c3 = new Circle(3); bunch.add(c1); bunch.add(c2); bunch.add(c3); Rectangle r1 = new Rectangle(1, 20); Rectangle r2 = new Rectangle(10, 2); Rectangle r3 = new Rectangle(10, 0.5); bunch.add(r1); bunch.add(r2); bunch.add(r3); bunch.showlist(); System.out.println("\nLargestArea at " + bunch.getlargest()); ArrayList<GeometricObject> list2 = bunch.sortelements(); int i = 0; for (GeometricObject g : list2) { String objtype = g.getclass().getsimplename(); System.out.printf("\n[%d]: %s \t %3.2f ", i++, objtype, g.getarea()); Class: GeometricObject public abstract class GeometricObject implements Comparable<GeometricObject> { @Override public int compareto(geometricobject o2) { double area1 = this.getarea(); double area2 = o2.getarea(); if (area1 < area2) return -1; else if (area1 == area2) return 0; else return 1; private String color = "white"; private boolean filled; private java.util.date datecreated;

// Construct a default geometric object protected GeometricObject() { datecreated = new java.util.date(); // create object with color and filled value protected GeometricObject(String color, boolean filled) { datecreated = new java.util.date(); this.color = color; this.filled = filled; // Return color public String getcolor() { return color; // Set a new color public void setcolor(string color) { this.color = color; // Return TRUE is object is filled public boolean isfilled() { return filled; public void setdatecreated(java.util.date datecreated) { this.datecreated = datecreated; // Set filled to either: true/false public void setfilled(boolean filled) { this.filled = filled; // Get datecreated public java.util.date getdatecreated() { return datecreated; // Return a string representing this object return " created on " + datecreated + "\n color: " + color + " and filled: " + filled; // Abstract method getarea public abstract double getarea(); // Abstract method getperimeter public abstract double getperimeter(); Class: Rectangle public class Rectangle extends GeometricObject { private double width; private double height; public Rectangle() { public Rectangle(double width, double height) { this.width = width; this.height = height; /** Return width */ public double getwidth() { return width;

/** Set a new width */ public void setwidth(double width) { this.width = width; /** Return height */ public double getheight() { return height; /** Set a new height */ public void setheight(double height) { this.height = height; /** Return area */ public double getarea() { return width * height; /** Return perimeter */ public double getperimeter() { return 2 * (width + height); return " >>> Rectangle " + getcolor() + "\n" + super.tostring() + "\n >>> Height: " + getheight() + " Width: " + getwidth() + " Perimeter: " + getperimeter() + " Area: " + getarea(); Class: Circle public class Circle extends GeometricObject { private double radius; public Circle() { public Circle(double radius) { this.radius = radius; /** Return radius */ public double getradius() { return radius; /** Set a new radius */ public void setradius(double radius) { this.radius = radius; /** Return area */ public double getarea() { return radius * radius * Math.PI; /** Return diameter */ public double getdiameter() { return 2 * radius; /** Return perimeter */ public double getperimeter() { return 2 * radius * Math.PI; /* Print the circle info */ public void printcircle() { System.out.println("The circle is created " + getdatecreated()

+ " and the radius is " + radius); return " >>> Circle " + getcolor() + "\n" + super.tostring() + "\n >>> Radius: " + getradius() + " Perimeter: " + getperimeter() + " Area: " + getarea(); CONSOLE [0]: >>> Circle white >>> Radius: 1.0 Perimeter: 6.283185307179586 Area: 3.141592653589793 [1]: >>> Circle white >>> Radius: 2.0 Perimeter: 12.566370614359172 Area: 12.566370614359172 [2]: >>> Circle white >>> Radius: 3.0 Perimeter: 18.84955592153876 Area: 28.274333882308138 [3]: >>> Rectangle white >>> Height: 20.0 Width: 1.0 Perimeter: 42.0 Area: 20.0 [4]: >>> Rectangle white >>> Height: 2.0 Width: 10.0 Perimeter: 24.0 Area: 20.0 [5]: >>> Rectangle white >>> Height: 0.5 Width: 10.0 Perimeter: 21.0 Area: 5.0 LargestArea at 2 [0]: Circle 3.14 [1]: Rectangle 5.00 [2]: Circle 12.57 [3]: Rectangle 20.00 [4]: Rectangle 20.00 [5]: Circle 28.27