21 An introduction to OOP composition by example the Author and Book classes

In this course, you’ll explore inheritance and composition in Python. Inheritance and composition are two important concepts in object oriented programming that model the relationship between two classes. They are the building blocks of object oriented design, and they help programmers to write reusable code.

By the end of this course, you’ll know how to:

  • Use inheritance in Python
  • Model class hierarchies using inheritance
  • Use multiple inheritance in Python and understand its drawbacks
  • Use composition to create complex objects
  • Reuse existing code by applying composition
  • Change application behavior at run-time through composition

Download

Sample Code (.zip)

24.2 KB

00:00 Hi! My name is Austin Cepalia with realpython.com, and I welcome you to the next stepping stone on your path to learning object-oriented programming in Python 3. This course covers inheritance and composition, two major ideas that will allow you to write better object-oriented Python code.

00:21 By the end of this course, you will know how to create classes that inherit from one another, design basic class hierarchies with UML diagrams, use interfaces and abstract classes, use multiple inheritance and understand its drawbacks, build complex classes with composition, and finally, change run-time behavior using composition.

00:47 So sit back and relax, because this is a big course. Before you continue, it’s important that you understand the basic ideas behind object-oriented programming in Python.

01:00 That means that you should have already gone through my first Real Python course, Intro to Object-Oriented Programming in Python. In that course, I introduce you to the fundamental ideas behind OOP, and I demystify buzzwords like class, object, instance, attribute, and method. If those words sound foreign to you, I’d recommend taking another pass through that course before continuing.

01:29 Optionally, you can also read the article on the right, which teaches you about the Python super() function.

01:37 And finally, just a little bit of advice. This is a long course, and unless you already understand these concepts and you’re watching as a refresher, then you really shouldn’t rush through this in one sitting.

01:50 Believe it or not, your brain works hard to understand a concept even when you’ve stepped away from the computer after a learning session. That’s why it’s so important to take breaks.

02:02 Give yourself time to process a few ideas before you try to learn a bunch more. The best way to learn something that seems big and daunting is to take it bit by bit—no pun intended.

02:14 That’s one reason why every video course and article on realpython.com is organized into sections. Watch the videos, code along with me, and solidify your understanding of the material by seeing how small modifications to the code you write changes the output of the program.

02:34 I can tell you as an undergraduate student studying computer science that this is how you effectively learn to program. Let’s get started.

patientwriter on April 30, 2020

Can you tell me why Django, at least in the official docs, spends so much space talking about inheritance and almost none talking about composition?

Class // Construct an author instance Author codeLean = new Author("Code Lean", "codeleanvn@gmail.com", 'f'); System.out.println(codeLean); // Author's toString() Book dummyBook = new Book("Java for dummy", codeLean, 19.95, 99); // Test Book's Constructor System.out.println(dummyBook); // Test Book's toString() // Test Getters and Setters dummyBook.setPrice(29.95); dummyBook.setQty(28); System.out.println("name is: " + dummyBook.getName()); System.out.println("price is: " + dummyBook.getPrice()); System.out.println("qty is: " + dummyBook.getQty()); System.out.println("Author is: " + dummyBook.getAuthor()); // Author's toString() System.out.println("Author's name is: " + dummyBook.getAuthor().getName()); System.out.println("Author's email is: " + dummyBook.getAuthor().getEmail()); // Use an anonymous instance of Author to construct a Book instance Book anotherBook = new Book("more Java", new Author("Dang Kim Thi", "dangkimthi@gmail.com", 'f'), 29.95); System.out.println(anotherBook); // toString()9 được thiết kế (như hiển thị trên class diagram) để mô hình hoá một cuốn sách được viết bởi một tác giả one author. Nó chứa:

This 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 class does not have a 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20164 method. Hence, it cannot be run directly. This 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 class is a “building block” and is meant to be used in another program.

Let us write a test program called 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20166 (in another source file called 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20167) which uses the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 class, as follows:

Now, run the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20166 and study the results.

More Basic OOP Concepts
  1. Constructor: Modify the class 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 to include a third constructor for constructing a 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 instance with two arguments - a Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2 for Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1 and a Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]4 for Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3. Modify the test program 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20166 to construct an instance of 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 using this constructor.
  2. Getter: Add a getter for variable Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3 for retrieving the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3 of this instance. Modify the test program to test this method.
  3. public vs. private: In 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20166, can you access the instance variable Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1 directly (e.g., Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]2); or assign a new value to Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1 (e.g., Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]4)? Try it out and explain the error messages.
  4. Setter: Is there a need to change the values of Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1 and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3 of a 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 instance after it is constructed? If so, add two Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods called setters for changing the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1 and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3 of a 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 instance as follows: Modify the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20166 to test these methods, e.g.,
  5. Keyword "this": Instead of using variable names such as public Ball(float x, float y, int radius, int speed, int directionInDegree)3 (for Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1) and public Ball(float x, float y, int radius, int speed, int directionInDegree)5 (for Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3) in the methods' arguments, it is better to use variable names Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1 (for Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1) and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3 (for Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3) and use the special keyword "public Author (String name, String email, char gender) {......}1" to resolve the conflict between instance variables and methods' arguments. For example, Modify ALL the constructors and setters in the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 class to use the keyword "public Author (String name, String email, char gender) {......}1".
  6. Method toString(): Every well-designed Java class should contain a Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 method called public Author (String name, String email, char gender) {......}5 that returns a description of the instance (in the return type of Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]4). The public Author (String name, String email, char gender) {......}5 method can be called explicitly (via public Author (String name, String email, char gender) {......}8) just like any other method; or implicitly through public Author (String name, String email, char gender) {......}9. If an instance is passed to the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8000 method, the public Author (String name, String email, char gender) {......}5 method of that instance will be invoked implicitly. For example, include the following public Author (String name, String email, char gender) {......}5 methods to the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 class: Try calling public Author (String name, String email, char gender) {......}5 method explicitly, just like any other method: public Author (String name, String email, char gender) {......}5 is called implicitly when an instance is passed to public Author (String name, String email, char gender) {......}9 method, for example,

The final class diagram for the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 class is as follows:

Ex: Yet Another Circle Class

A class called 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162, which models a circle with a radius, is designed as shown in the following class diagram. Write the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 class.

Below is a Test Driver to test your 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 class.

The expected output is:

Circle[radius=1.1] Circle[radius=1.0] Circle[radius=2.2] radius is: 2.2 area is: 15.21 circumference is: 13.82

Ex: The Rectangle Class

A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8011, which models a rectangle with a length and a width (in Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8012), is designed as shown in the following class diagram. Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8011 class.

Below is a test driver to test the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8011 class:

The expected output is:

Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.80

Ex: The Employee Class

A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8015, which models an employee with an ID, name and salary, is designed as shown in the following class diagram. The method Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8016 increases the salary by the given percentage. Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8015 class.

Below is a test driver to test the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8015 class:

The expected out is:

Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]

Ex: The InvoiceItem Class

A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8019, which models an item of an invoice, with ID, description, quantity and unit price, is designed as shown in the following class diagram. Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8019 class.

Below is a test driver to test the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8019 class:

The expected output is:

InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01

Ex: The Account Class

A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8022, which models a bank account of a customer, is designed as shown in the following class diagram. The methods Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8023 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8024 add or subtract the given Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8025 to the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8026. The method Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8027 transfers the given Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8025 from this Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8022 to the given Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8030. Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8022 class.

Below is a test driver to test the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8022 class:

The expected output is:

Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]

Ex: The Date Class

A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8033, which models a calendar date, is designed as shown in the following class diagram. Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8033 class.

Below is a test driver to test the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8033 class:

The expected output is:

01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/2016

Ex: The Time Class

A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8036, which models a time instance, is designed as shown in the following class diagram. The methods Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8037 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8038 shall advance or rewind this instance by one second, and return this instance, so as to support chaining operation such as Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8039. Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8036 class.

Below is a test driver for testing the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8036 class:

The expected output is:

01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:58

Ex: The Ball Class

A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8042, which models a bouncing ball, is designed as shown in the following class diagram. It contains its radius, x and y position. Each move-step advances the x and y by delta-x and delta-y, respectively. delta-x and delta-y could be positive or negative. The Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8043 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8044 methods could be used to bounce the ball off the walls. Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8042 class. Study the test driver on how the ball bounces.

Below is a test driver:

The expected output is:

Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]

Try: Modify the constructor to take in speed and direction (in polar coordinates) instead of delta-x and delta-y (in cartesian coordinates), which is more convenient for the users.

public Ball(float x, float y, int radius, int speed, int directionInDegree)

Exercises on Composition

This first exercise shall lead you through all the concepts involved in OOP Composition.

A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 (as shown in the class diagram) is designed to model a book's author. It contains:

  • Three Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]0 instance variables: Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8048 (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]4), Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8050 (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]4), and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8052 (Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8053 of either Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8054 or Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8055);
  • One constructor to initialize the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8048, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8050 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8052 with the given values;public Author (String name, String email, char gender) {......}(There is no default constructor for Author, as there are no defaults for name, email and gender.)
  • Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 getters/setters: Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8060, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8061, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8062, and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8063;
    (There are no setters for Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8048 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8052, as these attributes cannot be changed.)
  • A public Author (String name, String email, char gender) {......}5 method that returns "Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8067", e.g., "Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8068".

Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 class. Also write a test driver called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8070 to test all the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods, e.g.,


A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 is designed (as shown in the class diagram) to model a book written by one author. It contains:

  • Four Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]0 instance variables: Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8048 (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]4), Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8076 (of the class Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 you have just created, assume that a book has one and only one author), Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8078 (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2), and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8080 (Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8081);
  • Two constructors:Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.800
  • public methods Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8060, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8083, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8084, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8085, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8086, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8087.
  • A public Author (String name, String email, char gender) {......}5 that returns "Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8089".  You should reuse Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046’s public Author (String name, String email, char gender) {......}5.

Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 class (which uses the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 class written earlier). Also write a test driver called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8094 to test all the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods in the class Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072. Take Note that you have to construct an instance of Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 before you can construct an instance of Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072. E.g.,

Take note that both Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 classes have a variable called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8048. However, it can be differentiated via the referencing instance. For a Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 instance says Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]03, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]04 refers to the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8048 of the book; whereas for an Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046's instance say Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]07, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]08 refers to the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8048 of the author. There is no need (and not recommended) to call the variables Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]10 and Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]11.

TRY:

  1. Printing the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8048 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8050 of the author from a Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 instance. (Hint: Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]15, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]16).
  2. Introduce new methods called Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]17, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]18, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]19 in the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 class to return the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8048, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8050 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8052 of the author of the book. For example,

(Advanced) The Author and Book Classes Again - An Array of Objects as an Instance Variable

In the earlier exercise, a book is written by one and only one author. In reality, a book can be written by one or more author. Modify the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 class to support one or more authors by changing the instance variable Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]25 to an Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 array.

Notes:

  • The constructors take an array of Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 (i.e., Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]28), instead of an Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 instance. In this design, once a Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 instance is constructor, you cannot add or remove author.
  • The public Author (String name, String email, char gender) {......}5 method shall return "Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]32".

You are required to:

  1. Write the code for the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 class. You shall re-use the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 class written earlier.
  2. Write a test driver (called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8094) to test the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 class.

Hints:

Ex: The Author and Book Classes - Your Turn

A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046, which models an author of a book, is designed as shown in the class diagram. A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072, which models a book written by ONE author and composes an instance of Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 as its instance variable, is also shown. Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8046 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8072 classes.

Below is a test driver:

The expected output is:

Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.801

Ex: The Customer and Invoice classes

A class called Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]42, which models a customer in a transaction, is designed as shown in the class diagram. A class called Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]43, which models an invoice for a particular customer and composes an instance of Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]42 as its instance variable, is also shown. Write the Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]42 and Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]43 classes.

Below is a test driver:

The expected output is:

Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.802

Ex: The Customer and Account classes

The Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]42 class models a customer is design as shown in the class diagram. Write the codes for the Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]42 class and a test driver to test all the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods.

The Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8022 class models a bank account, design as shown in the class diagram, composes a Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]42 instance (written earlier) as its member. Write the codes for the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8022 class and a test driver to test all the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods.

Ex: The MyPoint Class

A class called Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54, which models a 2D point with x and y coordinates, is designed as shown in the class diagram.

It contains:

  • Two instance variables Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55 (Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8081) and Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57 (Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8081).
  • A default (or "no-argument" or "no-arg") constructor that construct a point at the default location of Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]59.
  • A overloaded constructor that constructs a point with the given Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55 and Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57 coordinates.
  • Getter and setter for the instance variables Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55 and Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57.
  • A method Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]64 to set both Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55 and Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57.
  • A method Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]67 which returns the x and y in a 2-element Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8081 array.
  • A public Author (String name, String email, char gender) {......}5 method that returns a string description of the instance in the format "Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]70".
  • A method called Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]71 that returns the distance from this point to another point at the given Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]72 coordinates, e.g.,Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.803
  • An overloaded Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]73 that returns the distance from this point to the given Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54 instance (called Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]75), e.g.,Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.804
  • Another overloaded Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]76 method that returns the distance from public Author (String name, String email, char gender) {......}1 point to the origin Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]78, e.g.,Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.805

You are required to:

  1. Write the code for the class Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54. Also write a test program (called Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]80) to test all the methods defined in the class.
    Hints:
  2. Write a program that allocates Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]81 points in an array of Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54, and initializes to Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]83, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]84, ... Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]85.
    Hints: You need to allocate the array, as well as each of the 10 Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54 instances.  In other words, you need to issue 11 Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]87, 1 for the array and 10 for the Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54 instances.

Notes: Point is such a common entity that JDK certainly provided for in all flavors.

Ex: The MyLine and MyPoint Classes

A class called Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]89, which models a line with a begin point at (x1, y1) and an end point at (x2, y2), is designed as shown in the class diagram. The Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]89 class uses two Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54 instances (written in the earlier exercise) as its begin and end points. Write the Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]89 class. Also write a test driver to test all the public methods in the Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]89 class.

Ex: The MyCircle and MyPoint Classes

A class called Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]94, which models a circle with a center and a radius, is designed as shown in the class diagram. The Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]94 class uses a Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54 instance (written in the earlier exercise) as its center.

The class contains:

  • Two Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]0 instance variables: Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]98 (an instance of Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54) and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1 (Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8081).
  • A constructor that constructs a circle with the given center's (Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57) and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1.
  • An overloaded constructor that constructs a Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]94 given a Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54 instance as Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]98, and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1.
  • A default constructor that construct a circle with center at Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]78 and radius of InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10.
  • Various getters and setters.
  • A public Author (String name, String email, char gender) {......}5 method that returns a string description of this instance in the format "InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 12". You shall reuse the public Author (String name, String email, char gender) {......}5 of Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54.
  • Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 16 methods that return the area and circumference of public Author (String name, String email, char gender) {......}1 circle in Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2.
  • A InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 19 method that returns the distance of the centers from public Author (String name, String email, char gender) {......}1 instance and the given Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]94 instance.  You should use Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54’s Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]76 method to compute this distance.

Write the Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]94 class. Also write a test driver (called InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 25) to test all the public methods defined in the class.

Hints:

Ex: The MyTriangle and MyPoint Classes

A class called InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 26, which models a triangle with 3 vertices, is designed as shown in the class diagram. The InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 26 class uses three Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54 instances (created in the earlier exercise) as the three vertices.

It contains:

  • Three Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]0 instance variables InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 30, InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 31, InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 32 (instances of Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54), for the three vertices.
  • A constructor that constructs a InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 26 with three set of coordinates, InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 35, InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 36, InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 37.
  • An overloaded constructor that constructs a InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 26 given three instances of Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54.
  • A public Author (String name, String email, char gender) {......}5 method that returns a string description of the instance in the format "InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 41".
  • A InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 42 method that returns the length of the perimeter in double. You should use the Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]76 method of Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54 to compute the perimeter.
  • A method InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 45, which prints "InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 46" if all the three sides are equal, "InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 47" if any two of the three sides are equal, or "InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 48" if the three sides are different.

Write the InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 26 class. Also write a test driver (called InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 50) to test all the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods defined in the class.

Ex: The MyRectangle and MyPoint Classes

Design a InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 52 class which is composed of two Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]54 instances as its top-left and bottom-right corners. Draw the class diagrams, write the codes, and write the test drivers.

More Exercises on Classes

Ex: The MyComplex class

A class called InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54, which models a complex number with real and imaginary parts, is designed as shown in the class diagram.

It contains:

  • Two instance variable named InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 55 (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2) and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 57 (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2) which stores the real and imaginary parts of the complex number, respectively.
  • A constructor that creates a InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54 instance with the given real and imaginary values.
  • A default constructor that create a MyComplex at InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 60.
  • Getters and setters for instance variables InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 55 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 57.
  • A method InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 63 to set the value of the complex number.
  • A public Author (String name, String email, char gender) {......}5 that returns "InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 65" where Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55 and Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57 are the real and imaginary parts, respectively.
  • Methods InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 68 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 69 that returns InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 70 if this complex number is real or imaginary, respectively.
    Hints:
  • A method InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 71 that returns InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 70 if public Author (String name, String email, char gender) {......}1 complex number is equal to the given complex number (real, imag).
    Hints:Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.806
  • An overloaded InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 74 that returns InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 70 if this complex number is equal to the given InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54 instance Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]75.
    Hints:Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.807
  • A method InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 78 that returns the magnitude of this complex number.Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.808
  • Methods InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 79 that adds and subtract the given InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54 instance (called InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 81) into public Author (String name, String email, char gender) {......}1 instance and returns public Author (String name, String email, char gender) {......}1 instance.Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.809Hints:
  • Methods InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 84 that adds public Author (String name, String email, char gender) {......}1 instance with the given InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54 instance called InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 81, and returns a new InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54 instance containing the result.
    Hint:

You are required to:

  1. Write the InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54 class.
  2. Write a test driver to test all the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods defined in the class.
  3. Write an application called InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 91 that uses the InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54 class. The application shall prompt the user for two complex numbers, print their values, check for real, imaginary and equality, and carry out all the arithmetic operations.Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]0

Try: A (more) complete design of InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54 class is shown below:

  • Methods InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 94 that returns the argument of this complex number in radians (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2).Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]1Note: The InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 96 library has two arc-tangent methods, InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 97 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 98. We commonly use the InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 99 instead of Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]00 to avoid division by zero. Read the documentation of InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 96 class in package Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]02.
  • The method Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]03 is renamed Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]04. Also added Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]05 and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]06.
  • Methods Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]07 and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]08 that multiplies and divides public Author (String name, String email, char gender) {......}1 instance with the given InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54 instance InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 81, and keeps the result in public Author (String name, String email, char gender) {......}1 instance, and returns this instance.Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]2
  • A method Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]13 that operates on public Author (String name, String email, char gender) {......}1 instance and returns public Author (String name, String email, char gender) {......}1 instance containing the complex conjugate.Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]3

Take note that there are a few flaws in the design of this class, which was introduced solely for teaching purpose:

  • Comparing Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2s in Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]17 using "Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]18" may produce unexpected outcome. For example, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]19 returns Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]20. It is common to define a small threshold called Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]21 (set to about Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]22) for comparing floating point numbers.
  • The method Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]23, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]06 produce new instances, whereas Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]04, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]05, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]27, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]28 and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]13 modify public Author (String name, String email, char gender) {......}1 instance. There is inconsistency in the design (introduced for teaching purpose).

Also take note that methods such as Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]04 returns an instance of InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 54. Hence, you can place the result inside a Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]33 (which implicitly invoke the public Author (String name, String email, char gender) {......}5). You can also chain the operations, e.g., Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]35 (same as Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]36, or Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]37.

Ex: The MyPolynomial Class

A class called Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]38, which models polynomials of degree-Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]39 (see equation), is designed as shown in the class diagram.

It contains:

  • An instance variable named Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]40, which stores the coefficients of the n-degree polynomial in a Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2 array of size Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]42, where c0 is kept at index 0.
  • A constructor Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]43 that takes a variable number of doubles to initialize the coeffs array, where the first argument corresponds to c0.
    The three dots is known as varargs (variable number of arguments), which is a new feature introduced in JDK 1.5. It accepts an array or a sequence of comma-separated arguments. The compiler automatically packs the comma-separated arguments in an array. The three dots can only be used for the last argument of the method.
    Hints:
  • A method Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]44 that returns the degree of this polynomial.
  • A method public Author (String name, String email, char gender) {......}5 that returns "cnx^n+cn-1x^(n-1)+...+c1x+c0".
  • A method Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]46 that evaluate the polynomial for the given Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55, by substituting the given Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55 into the polynomial expression.
  • Methods Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]04 and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]27 that adds and multiplies this polynomial with the given Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]38 instance Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]75, and returns public Author (String name, String email, char gender) {......}1 instance that contains the result.

Write the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]38 class. Also write a test driver (called Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]55) to test all the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods defined in the class.

Question: Do you need to keep the degree of the polynomial as an instance variable in the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]38 class in Java? How about C/C++? Why?

Ex: Using JDK's BigInteger Class

Recall that primitive integer type Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]58, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]59, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8081 and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]61 represent 8-, 16-, 32-, and 64-bit signed integers, respectively. You cannot use them for integers bigger than 64 bits. Java API provides a class called Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]62 in a package called Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]63. Study the API of the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]62 class (Java API ⇒ From "Packages", choose "java.math" " From "classes", choose "BigInteger" " Study the constructors (choose "CONSTR") on how to construct a Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]62 instance, and the public methods available (choose "METHOD"). Look for methods for adding and multiplying two Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]66.

Write a program called Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]67 that:

  1. adds "11111111111111111111111111111111111111111111111111111111111111" to "22222222222222222222222222222222222222222222222222" and prints the result.
  2. multiplies the above two number and prints the result.

Hints:

Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]4

Ex: The MyTime Class

A class called Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]68, which models a time instance, is designed as shown in the class diagram.

It contains the following Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]0 instance variables:

  • Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]70: between 0 to 23.
  • Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]71: between 0 to 59.
  • Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]72: between 0 to 59.

You are required to perform input validation.

It contains the following Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods:

  • Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]74: It shall check if the given Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]70, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]71 and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]77 are valid before setting the instance variables.
    (Advanced: Otherwise, it shall throw an Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]78 with the message "Invalid hour, minute, or second!".)
  • Setters Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]79, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]80, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]81: It shall check if the parameters are valid, similar to the above.
  • Getters Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]82, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]83, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]84.
  • public Author (String name, String email, char gender) {......}5: returns "Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]86".
  • Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8037: Update this instance to the next second and return this instance. Take note that the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8037 of Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]89 is Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]90.
  • Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]91, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]92, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8038, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]94, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]95: similar to the above.

Write the code for the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]68 class. Also write a test driver (called Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]97) to test all the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods defined in the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]68 class.

Ex: The MyDate Class

A class called 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201600, which models a date instance, is defined as shown in the class diagram.

The 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201600 class contains the following Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]0 instance variables:

  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201603 (Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8081): Between InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10 to 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201606.
  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201607 (Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8081): Between InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10 (Jan) to 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201610 (Dec).
  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201611 (Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8081): Between InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10 to 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201614, where the last day depends on the month and whether it is a leap year for Feb (01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201615).

It also contains the following Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201617 variables (drawn with underlined in the class diagram):

  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201618 (01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201619), 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201620 (01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201619), and DAY_IN_01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201618 (01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201623): 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201624 variables, initialized as shown, which are used in the methods.

The 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201600 class has the following Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201624 methods (drawn with underlined in the class diagram):

  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201628: returns InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 70 if the given 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201603 is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400.
  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201631: returns InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 70 if the given 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201603, 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201607, and 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201611 constitute a valid date. Assume that 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201603 is between InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10 and 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201606, 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201607 is between InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10 (Jan) to 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201610 (Dec) and 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201611 shall be between InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10 and 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201614 depending on the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201607 and whether it is a leap year on Feb.
  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201646: returns the day of the week, where 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201647 for Sun, InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10 for Mon, ..., 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201649 for Sat, for the given date. Assume that the date is valid. Read the earlier exercise on how to determine the day of the week (or Wiki "Determination of the day of the week").

The 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201600 class has one constructor, which takes 3 parameters: 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201603, 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201607 and 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201611. It shall invoke 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201654 method (to be described later) to set the instance variables.

The 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201600 class has the following Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods:

  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201657: It shall invoke the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201624 method 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201659 to verify that the given 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201603, 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201607 and 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201611 constitute a valid date.
    (Advanced: Otherwise, it shall throw an Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]78 with the message "Invalid year, month, or day!".)
  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201664: It shall verify that the given 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201603 is between InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10 and 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201606.
    (Advanced: Otherwise, it shall throw an Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]78 with the message "Invalid year!".)
  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201669: It shall verify that the given 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201607 is between InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10 and 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201610.
    (Advanced: Otherwise, it shall throw an Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]78 with the message "Invalid month!".)
  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201674: It shall verify that the given 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201611 is between InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 10 and 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201677, where 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201677 depends on the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201607 and whether it is a leap year for Feb.
    (Advanced: Otherwise, it shall throw an Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]78 with the message "Invalid month!".)
  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201681, 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201682, 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201683: return the value for the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201603, 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201607 and 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201611, respectively.
  • public Author (String name, String email, char gender) {......}5: returns a date string in the format "01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201688", e.g., "Tuesday 14 Feb 2012".
  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201689: update public Author (String name, String email, char gender) {......}1 instance to the next day and return public Author (String name, String email, char gender) {......}1 instance. Take note that 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201689 for 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201693 shall be 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201694.
  • 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201695: update public Author (String name, String email, char gender) {......}1 instance to the next month and return public Author (String name, String email, char gender) {......}1 instance. Take note that 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201695 for 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201699 shall be 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5800.
  • 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5801: update public Author (String name, String email, char gender) {......}1 instance to the next year and return public Author (String name, String email, char gender) {......}1 instance. Take note that 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5801 for 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5805 shall be 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5806.
    (Advanced: throw an 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5807 with the message "Year out of range!" if year > 9999.)
  • 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5808, 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5809, 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5810: similar to the above.

Write the code for the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201600 class.

Use the following test statements to test the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201600 class:

Write a test program that tests the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201689 in a loop, by printing the dates from 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5814 to 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5815.

Ex: Bouncing Balls - Ball and Container Classes

A class called Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8042 is designed as shown in the class diagram.

The Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8042 class contains the following Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]0 instance variables:

  • Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57 and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1, which represent the ball's center Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]72 co-ordinates and the radius, respectively.
  • 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5823 (01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5824) and 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5825 (01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5826), which represent the displacement (movement) per step, in the Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55 and Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57 direction respectively.

The Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8042 class contains the following Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods:

  • A constructor which accepts Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57, Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1, 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5834, and 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5835 as arguments. For user friendliness, user specifies 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5834 (in pixels per step) and 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5835 (in degrees in the range of 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5838). For the internal operations, the 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5834 and 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5835 are to be converted to 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5841 in the internal representation. Note that the y-axis of the Java graphics coordinate system is inverted, i.e., the origin Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]59 is located at the top-left corner.
    Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]5
  • Getter and setter for all the instance variables.
  • A method 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5843 which move the ball by one step.Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]6
  • Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8043 which reflects the ball horizontally (i.e., hitting a vertical wall)Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]7
  • Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8044 (the ball hits a horizontal wall).Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]8
  • public Author (String name, String email, char gender) {......}5 which prints the message "01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5847".

Write the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8042 class. Also write a test program to test all the methods defined in the class.


A class called 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5849, which represents the enclosing box for the ball, is designed as shown in the class diagram. It contains:

  • Instance variables 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5850 and 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5851 which denote the top-left and bottom-right corners of the rectangular box.
  • A constructor which accepts Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]72 of the top-left corner, 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5853 and 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5854 as argument, and converts them into the internal representation (i.e., 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5855). 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5856 and 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5854 is used in the argument for safer operation (there is no need to check the validity of 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5858 etc.).
  • A public Author (String name, String email, char gender) {......}5 method that returns "01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5860".
  • A 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5861 method called 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5862, which check if the given Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8042 is outside the bounds of the container box. If so, it invokes the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8042's Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8043 and/or Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8044 to change the movement direction of the ball, and returns InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 70.Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]9

Use the following statements to test your program:

Ex: The Ball and Player Classes

The Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8042 class, which models the ball in a soccer game, is designed as shown in the class diagram. Write the codes for the Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8042 class and a test driver to test all the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods.

The 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5871 class, which models the players in a soccer game, is designed as shown in the class diagram. The Player interacts with the Ball (written earlier). Write the codes for the 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5871 class and a test driver to test all the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 methods. Make your assumption for the 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5874.

Can you write a very simple soccer game with 2 teams of players and a ball, inside a soccer field?

Exercises on Inheritance

An Introduction to OOP Inheritance by Example - The Circle and Cylinder Classes

This exercise shall guide you through the important concepts in inheritance.

In this exercise, a subclass called 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5875 is derived from the superclass 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 as shown in the class diagram (where an an arrow pointing up from the subclass to its superclass). Study how the subclass 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5875 invokes the superclass' constructors (via 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5878 and 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5879) and inherits the variables and methods from the superclass 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162.

You can reuse the 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 class that you have created in the previous exercise. Make sure that you keep "01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5882" in the same directory.

Write a test program (says 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5883) to test the 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5875 class created, as follow:

Method Overriding and "Super": The subclass 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5875 inherits Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 method from its superclass Circle. Try overriding the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 method in the subclass 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5875 to compute the surface area (=2π×radius×height + 2×base-area) of the cylinder instead of base area. That is, if Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 is called by a 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 instance, it returns the area. If Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 is called by a 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5875 instance, it returns the surface area of the cylinder.

If you override the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 in the subclass 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5875, the 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5895 no longer works. This is because the 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5895 uses the overridden Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 method found in the same class. (Java runtime will search the superclass only if it cannot locate the method in this class). Fix the 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5895.

Hints: After overridding the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 in subclass 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5875, you can choose to invoke the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 of the superclass 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 by calling Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]03.

TRY:

Provide a public Author (String name, String email, char gender) {......}5 method to the 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5875 class, which overrides the public Author (String name, String email, char gender) {......}5 inherited from the superclass 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162, e.g.,

Try out the public Author (String name, String email, char gender) {......}5 method in 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5883.

Note: Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]10 is known as annotation (introduced in JDK 1.5), which asks compiler to check whether there is such a method in the superclass to be overridden. This helps greatly if you misspell the name of the public Author (String name, String email, char gender) {......}5. If Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]10 is not used and public Author (String name, String email, char gender) {......}5 is misspelled as Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]14, it will be treated as a new method in the subclass, instead of overriding the superclass. If Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]10 is used, the compiler will signal an error. Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]10 annotation is optional, but certainly nice to have.

Ex: Superclass Person and its subclasses

Write the classes as shown in the following class diagram. Mark all the overridden methods with annotation Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]10.

Ex: Point2D and Point3D

Write the classes as shown in the following class diagram. Mark all the overridden methods with annotation Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]10.

Hints:
  1. You cannot assign floating-point literal say Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]19 (which is a Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2) to a Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8012 variable, you need to add a suffix f, e.g. Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]22, Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]23.
  2. The instance variables Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55 and Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57 are Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]0 in Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]27 and cannot be accessed directly in the subclass Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]28. You need to access via the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 getters and setters. For example,
  3. The method Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]67 shall return a Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8012 array:

Ex: Point and MovablePoint

Write the classes as shown in the following class diagram. Mark all the overridden methods with annotation Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]32

Hints
  1. You cannot assign floating-point literal say Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]19 (which is a Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2) to a Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8012 variable, you need to add a suffix f, e.g. Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]22, Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]23.
  2. The instance variables Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55 and Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57 are Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]0 in Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]41 and cannot be accessed directly in the subclass Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]42. You need to access via the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]7 getters and setters. For example, you cannot write Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]44, you need to write Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]45.

Ex: Superclass Shape and its subclasses Circle, Rectangle and Square

Write a superclass called Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]46 (as shown in the class diagram), which contains:

  • Two instance variables Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3 (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]4) and Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]49 (01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5861).
  • Two constructors: a no-arg (no-argument) constructor that initializes the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3 to "green" and Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]49 to InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 70, and a constructor that initializes the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3 and Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]49 to the given values.
  • Getter and setter for all the instance variables. By convention, the getter for a 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5861 variable Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]57 is called Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]58 (instead of Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]59 for all the other types).
  • A public Author (String name, String email, char gender) {......}5 method that returns "Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]61".

Write a test program to test all the methods defined in Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]46.

Write two subclasses of Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]46 called 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8011, as shown in the class diagram.

The 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 class contains:

  • An instance variable Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1 (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2).
  • Three constructors as shown. The no-arg constructor initializes the radius to Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]5.
  • Getter and setter for the instance variable Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1.
  • Methods Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 42.
  • Override the public Author (String name, String email, char gender) {......}5 method inherited, to return "Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]74", where Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]75 is the output of the public Author (String name, String email, char gender) {......}5 method from the superclass.

The Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8011 class contains:

  • Two instance variables 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5853 (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2) and Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]80 (Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]2).
  • Three constructors as shown. The no-arg constructor initializes the 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5853 and Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]80 to Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]5.
  • Getter and setter for all the instance variables.
  • Methods Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 42.
  • Override the public Author (String name, String email, char gender) {......}5 method inherited, to return "Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]88", where Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]75 is the output of the public Author (String name, String email, char gender) {......}5 method from the superclass.

Write a class called Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]91, as a subclass of Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8011. Convince yourself that Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]91 can be modeled as a subclass of Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8011. Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]91 has no instance variable, but inherits the instance variables width and length from its superclass Rectangle.

  • Provide the appropriate constructors (as shown in the class diagram). Hint:
  • Override the public Author (String name, String email, char gender) {......}5 method to return "Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]97", where Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]75 is the output of the public Author (String name, String email, char gender) {......}5 method from the superclass.
  • Do you need to override the Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 42? Try them out.
  • Override the public Ball(float x, float y, int radius, int speed, int directionInDegree)02 and public Ball(float x, float y, int radius, int speed, int directionInDegree)03 to change both the 01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5853 and Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]80, so as to maintain the square geometry.

Ex: Superclass Animal and its subclasses

Write the codes for all the classes as shown in the class diagram.

Exercises on Composition vs Inheritance

They are two ways to reuse a class in your applications: composition and inheritance.

Ex: The Point and Line Classes

Let us begin with composition with the statement "a line composes of two points".

Complete the definition of the following two classes: Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]41 and public Ball(float x, float y, int radius, int speed, int directionInDegree)07. The class public Ball(float x, float y, int radius, int speed, int directionInDegree)07 composes 2 instances of class Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]41, representing the beginning and ending points of the line. Also write test classes for Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]41 and public Ball(float x, float y, int radius, int speed, int directionInDegree)07 (says public Ball(float x, float y, int radius, int speed, int directionInDegree)12 and public Ball(float x, float y, int radius, int speed, int directionInDegree)13).

InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 0

The class diagram for composition is as follows (where a diamond-hollow-head arrow pointing to its constituents):

Instead of composition, we can design a public Ball(float x, float y, int radius, int speed, int directionInDegree)07 class using public Ball(float x, float y, int radius, int speed, int directionInDegree)15. Instead of "a line composes of two points", we can say that "a line is a point extended by another point", as shown in the following class diagram:

Let's re-design the public Ball(float x, float y, int radius, int speed, int directionInDegree)07 class (called public Ball(float x, float y, int radius, int speed, int directionInDegree)17) as a subclass of class Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]41. public Ball(float x, float y, int radius, int speed, int directionInDegree)17 inherits the starting point from its superclass Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]41, and adds an ending point. Complete the class definition. Write a testing class called public Ball(float x, float y, int radius, int speed, int directionInDegree)21 to test public Ball(float x, float y, int radius, int speed, int directionInDegree)17.

Summary: There are two approaches that you can design a line, public Ball(float x, float y, int radius, int speed, int directionInDegree)23 or public Ball(float x, float y, int radius, int speed, int directionInDegree)15. "A line composes two points" or "A line is a point extended with another point"”. Compare the public Ball(float x, float y, int radius, int speed, int directionInDegree)07 and public Ball(float x, float y, int radius, int speed, int directionInDegree)17 designs: public Ball(float x, float y, int radius, int speed, int directionInDegree)07 uses composition and public Ball(float x, float y, int radius, int speed, int directionInDegree)17 uses inheritance. Which design is better?

Ex: The Circle and Cylinder Classes Using Composition

Try rewriting the public Ball(float x, float y, int radius, int speed, int directionInDegree)29 of the previous exercise using composition (as shown in the class diagram) instead of inheritance. That is, "a cylinder is composed of a base circle and a height".

Which design (inheritance or composition) is better?

Exercises on Polymorphism, Abstract Classes and Interfaces

Ex: Abstract Superclass Shape and Its Concrete Subclasses

Rewrite the superclass Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]46 and its subclasses 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162, Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8011 and Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]91, as shown in the class diagram.

Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]46 is an public Ball(float x, float y, int radius, int speed, int directionInDegree)35 class containing 2 public Ball(float x, float y, int radius, int speed, int directionInDegree)35 methods: Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 42, where its concrete subclasses must provide its implementation. All instance variables shall have public Ball(float x, float y, int radius, int speed, int directionInDegree)39 access, i.e., accessible by its subclasses and classes in the same package. Mark all the overridden methods with annotation Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]10.

In this exercise, Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]46 shall be defined as an public Ball(float x, float y, int radius, int speed, int directionInDegree)35 class, which contains:

  • Two public Ball(float x, float y, int radius, int speed, int directionInDegree)39 instance variables Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]3(Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]4) and Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]49(01:02:03 04:05:06 Hour: 4 Minute: 5 Second: 6 23:59:58 23:59:59 00:00:01 00:00:00 23:59:5861). The public Ball(float x, float y, int radius, int speed, int directionInDegree)39 variables can be accessed by its subclasses and classes in the same package. They are denoted with a public Ball(float x, float y, int radius, int speed, int directionInDegree)49 sign in the class diagram.
  • Getter and setter for all the instance variables, and public Author (String name, String email, char gender) {......}5.
  • Two public Ball(float x, float y, int radius, int speed, int directionInDegree)35 methods Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 42 (shown in italics in the class diagram).

The subclasses 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162 and Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8011 shall override the public Ball(float x, float y, int radius, int speed, int directionInDegree)35 methods Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 42 and provide the proper implementation. They also override the public Author (String name, String email, char gender) {......}5.

Write a test class to test these statements involving polymorphism and explain the outputs. Some statements may trigger compilation errors. Explain the errors, if any.

What is the usage of the public Ball(float x, float y, int radius, int speed, int directionInDegree)35 method and public Ball(float x, float y, int radius, int speed, int directionInDegree)35 class?

Ex: GeometricObject Interface and its Implementation Classes Circle and Rectangle

Write an interface called public Ball(float x, float y, int radius, int speed, int directionInDegree)62, which contains 2 public Ball(float x, float y, int radius, int speed, int directionInDegree)35 methods: Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9 and InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 42, as shown in the class diagram. Also write an implementation class called 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162. Mark all the overridden methods with annotation Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]10.

Ex: Movable Interface and its Implementation MovablePoint Class

Write an interface called public Ball(float x, float y, int radius, int speed, int directionInDegree)68, which contains 4 public Ball(float x, float y, int radius, int speed, int directionInDegree)35 methods public Ball(float x, float y, int radius, int speed, int directionInDegree)70, public Ball(float x, float y, int radius, int speed, int directionInDegree)71, public Ball(float x, float y, int radius, int speed, int directionInDegree)72 and public Ball(float x, float y, int radius, int speed, int directionInDegree)73, as shown in the class diagram. Also write an implementation class called Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]42. Mark all the overridden methods with annotation Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]10.

Ex: Movable Interface and its Implementation Classes MovablePoint and MovableCircle

Write an interface called public Ball(float x, float y, int radius, int speed, int directionInDegree)68, which contains 4 public Ball(float x, float y, int radius, int speed, int directionInDegree)35 methods public Ball(float x, float y, int radius, int speed, int directionInDegree)70, public Ball(float x, float y, int radius, int speed, int directionInDegree)71, public Ball(float x, float y, int radius, int speed, int directionInDegree)72 and public Ball(float x, float y, int radius, int speed, int directionInDegree)73, as shown in the class diagram. Also write the implementation classes called Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]42 and public Ball(float x, float y, int radius, int speed, int directionInDegree)83. Mark all the overridden methods with annotation Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]10.

Ex: Interfaces Resizable and GeometricObject

  1. Write the public Ball(float x, float y, int radius, int speed, int directionInDegree)85 called public Ball(float x, float y, int radius, int speed, int directionInDegree)62, which declares two public Ball(float x, float y, int radius, int speed, int directionInDegree)35 methods: public Ball(float x, float y, int radius, int speed, int directionInDegree)88 and Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]9, as specified in the class diagram.
    Hints:InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 1
  2. Write the implementation class 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162, with a protected variable Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1, which implements the interface public Ball(float x, float y, int radius, int speed, int directionInDegree)62.
    Hints:
  3. Write a test program called 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20166 to test the methods defined in 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162.
  4. The class public Ball(float x, float y, int radius, int speed, int directionInDegree)95 is defined as a subclass of the class 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/20162, which also implements an interface called public Ball(float x, float y, int radius, int speed, int directionInDegree)97, as shown in class diagram. The interface public Ball(float x, float y, int radius, int speed, int directionInDegree)97 declares an public Ball(float x, float y, int radius, int speed, int directionInDegree)35 method public Author (String name, String email, char gender) {......}00, which modifies the dimension (such as Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1) by the given percentage. Write the interface public Ball(float x, float y, int radius, int speed, int directionInDegree)97 and the class public Ball(float x, float y, int radius, int speed, int directionInDegree)95.
    Hints:InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 2
  5. Write a test program called public Author (String name, String email, char gender) {......}04 to test the methods defined in public Ball(float x, float y, int radius, int speed, int directionInDegree)95.

Ex: Abstract Superclass Animal and its Implementation Subclasses

Write the codes for all the classes shown in the class diagram. Mark all the overridden methods with annotation @Override.

Ex: Another View of Abstract Superclass Animal and its Implementation Subclasses

Examine the following codes and draw the class diagram.

InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 3InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 4InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 5InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 6

Explain the outputs (or error) for the following test program.

Ex: Interface Movable and its implementation subclasses MovablePoint and MovableCircle

Suppose that we have a set of objects with some common behaviors: they could move up, down, left or right. The exact behaviors (such as how to move and how far to move) depend on the objects themselves. One common way to model these common behaviors is to define an interface called public Author (String name, String email, char gender) {......}06, with public Ball(float x, float y, int radius, int speed, int directionInDegree)35 methods public Ball(float x, float y, int radius, int speed, int directionInDegree)70, public Ball(float x, float y, int radius, int speed, int directionInDegree)71, public Ball(float x, float y, int radius, int speed, int directionInDegree)72 and public Ball(float x, float y, int radius, int speed, int directionInDegree)73. The classes that implement the public Author (String name, String email, char gender) {......}06 interface will provide actual implementation to these public Ball(float x, float y, int radius, int speed, int directionInDegree)35 methods.

Let's write two concrete classes - Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]42 and public Ball(float x, float y, int radius, int speed, int directionInDegree)83 - that implement the Movable interface.

The code for the interface public Author (String name, String email, char gender) {......}06 is straight forward.

For the Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]42 class, declare the instance variable Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57, public Author (String name, String email, char gender) {......}20 and public Author (String name, String email, char gender) {......}21 with package access as shown with public Author (String name, String email, char gender) {......}22 in the class diagram (i.e., classes in the same package can access these variables directly). For the public Ball(float x, float y, int radius, int speed, int directionInDegree)83 class, use a Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]42 to represent its center (which contains four variable Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]55, Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]57, public Author (String name, String email, char gender) {......}20 and public Author (String name, String email, char gender) {......}21). In other words, the public Ball(float x, float y, int radius, int speed, int directionInDegree)83 composes a Ball[(1.1,2.2),speed=(3.3,4.4)] Ball[(80.0,35.0),speed=(4.0,6.0)] x is: 80.0 y is: 35.0 radius is: 5 xDelta is: 4.0 yDelta is: 6.0 Ball[(84.0,41.0),speed=(4.0,6.0)] Ball[(88.0,47.0),speed=(4.0,6.0)] Ball[(92.0,41.0),speed=(4.0,-6.0)] Ball[(96.0,35.0),speed=(4.0,-6.0)] Ball[(92.0,29.0),speed=(-4.0,-6.0)] Ball[(88.0,23.0),speed=(-4.0,-6.0)] Ball[(84.0,17.0),speed=(-4.0,-6.0)] Ball[(80.0,11.0),speed=(-4.0,-6.0)] Ball[(76.0,5.0),speed=(-4.0,-6.0)] Ball[(72.0,-1.0),speed=(-4.0,-6.0)] Ball[(68.0,5.0),speed=(-4.0,6.0)] Ball[(64.0,11.0),speed=(-4.0,6.0)] Ball[(60.0,17.0),speed=(-4.0,6.0)] Ball[(56.0,23.0),speed=(-4.0,6.0)] Ball[(52.0,29.0),speed=(-4.0,6.0)]42, and its Account[id=A101,name=Tan Ah Teck,balance=88] Account[id=A102,name=Kumar,balance=0] ID: A101 Name: Tan Ah Teck Balance: 88 Account[id=A101,name=Tan Ah Teck,balance=188] Account[id=A101,name=Tan Ah Teck,balance=138] Amount exceeded balance Account[id=A101,name=Tan Ah Teck,balance=138] Account[id=A101,name=Tan Ah Teck,balance=38] Account[id=A102,name=Kumar,balance=100]1.

Write a test program and try out these statements:

Write a new class called public Author (String name, String email, char gender) {......}32, which composes two public Author (String name, String email, char gender) {......}33 (representing the top-left and bottom-right corners) and implementing the public Author (String name, String email, char gender) {......}06 Interface. Make sure that the two points has the same speed.

What is the difference between an interface and an abstract class?

More Exercises on OOP

Ex: The Discount System

You are asked to write a discount system for a beauty saloon, which provides services and sells beauty products. It offers 3 types of memberships: Premium, Gold and Silver. Premium, gold and silver members receive a discount of 20%, 15%, and 10%, respectively, for all services provided. Customers without membership receive no discount. All members receives a flat 10% discount on products purchased (this might change in future). Your system shall consist of three classes: Employee[id=8,name=Peter Tan,salary=2500] Employee[id=8,name=Peter Tan,salary=999] id is: 8 firstname is: Peter lastname is: Tan salary is: 999 name is: Peter Tan annual salary is: 11988 1098 Employee[id=8,name=Peter Tan,salary=1098]42, public Author (String name, String email, char gender) {......}36 and public Author (String name, String email, char gender) {......}37, as shown in the class diagram. It shall compute the total bill if a customer purchases $x of products and $y of services, for a visit. Also write a test program to exercise all the classes.

The class public Author (String name, String email, char gender) {......}38 contains only 01/02/2014 09/12/2099 Month: 12 Day: 9 Year: 2099 03/04/201624 variables and methods (underlined in the class diagram).

Ex: Polyline of Points with ArrayList

A polyline is a line with segments formed by points. Let's use the public Author (String name, String email, char gender) {......}40 (dynamically allocated array) to keep the points, but upcast to public Author (String name, String email, char gender) {......}41 in the instance variable. (Take note that array is of fixed-length, and you need to set the initial length).

InvoiceItem[id=A101,desc=Pen Red,qty=888,unitPrice=0.08] InvoiceItem[id=A101,desc=Pen Red,qty=999,unitPrice=0.99] id is: A101 desc is: Pen Red qty is: 999 unitPrice is: 0.99 The total is: 989.01 7

Exercises on Data Structures

Ex: MyIntStack

A stack is a first-in-last-out queue. Write a program called public Author (String name, String email, char gender) {......}42, which uses an array to store the contents, restricted to Rectangle[length=1.2,width=3.4] Rectangle[length=1.0,width=1.0] Rectangle[length=5.6,width=7.8] length is: 5.6 width is: 7.8 area is: 43.68 perimeter is: 26.8081.

What is a class in OOP example?

In object-oriented programming , a class is a template definition of the method s and variable s in a particular kind of object . Thus, an object is a specific instance of a class; it contains real values instead of variables. The class is one of the defining ideas of object-oriented programming.

What is OOP and class?

OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.

What is a class and object and explain with real life example?

Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. A Class is like an object constructor, or a "blueprint" for creating objects.

How to practise OOPS in Java?

OOP Exercises.
1.1 An Introduction to Classes and Instances by Example - The Circle Class. ... .
1.2 Ex: Yet Another Circle Class. ... .
1.3 Ex: The Rectangle Class. ... .
1.4 Ex: The Employee Class. ... .
1.5 Ex: The InvoiceItem Class. ... .
1.6 Ex: The Account Class. ... .
1.7 Ex: The Date Class. ... .
1.8 Ex: The Time Class..

Chủ đề