Which of the following is a shorter way to code the following gettotal() method?
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. Show
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
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 ClassA 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 ClassA 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 ClassA 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 ClassA 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 ClassA 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 ClassA 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 ClassA 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 ClassA 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 CompositionThis 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:
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:
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:
(Advanced) The Author and Book Classes Again - An Array of Objects as an Instance VariableIn 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:
You are required to:
Hints: Ex: The Author and Book Classes - Your TurnA 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 classesA 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 classesThe 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 ClassA 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:
You are required to:
Notes: Point is such a common entity that JDK certainly provided for in all flavors. Ex: The MyLine and MyPoint ClassesA 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 ClassesA 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:
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.0125) to test all the public methods defined in the class. Hints: Ex: The MyTriangle and MyPoint ClassesA 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.0126, 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.0126 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:
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.0126 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.0150) 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 ClassesDesign 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.0152 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 ClassesEx: The MyComplex classA 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.0154, which models a complex number with real and imaginary parts, is designed as shown in the class diagram. It contains:
You are required to:
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.0154 class is shown below:
Take note that there are a few flaws in the design of this class, which was introduced solely 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.0154. 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 ClassA 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:
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 ClassRecall 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:
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 ClassA 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:
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:
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 ClassA 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:
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):
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):
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:
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 ClassesA 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:
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:
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:
Use the following statements to test your program: Ex: The Ball and Player ClassesThe 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 InheritanceAn Introduction to OOP Inheritance by Example - The Circle and Cylinder ClassesThis 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 subclassesWrite 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 Point3DWrite 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:
Ex: Point and MovablePointWrite 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
Ex: Superclass Shape and its subclasses Circle, Rectangle and SquareWrite 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:
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:
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:
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.
Ex: Superclass Animal and its subclassesWrite the codes for all the classes as shown in the class diagram. Exercises on Composition vs InheritanceThey are two ways to reuse a class in your applications: composition and inheritance. Ex: The Point and Line ClassesLet 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.010 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 CompositionTry 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 InterfacesEx: Abstract Superclass Shape and Its Concrete SubclassesRewrite 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.0142, 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:
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.0142 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 RectangleWrite 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.0142, 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 ClassWrite 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 MovableCircleWrite 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
Ex: Abstract Superclass Animal and its Implementation SubclassesWrite 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 SubclassesExamine 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.013 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.014 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.015 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.016 Explain the outputs (or error) for the following test program. Ex: Interface Movable and its implementation subclasses MovablePoint and MovableCircleSuppose 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 OOPEx: The Discount SystemYou 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 ArrayListA 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.017 Exercises on Data StructuresEx: MyIntStackA 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 function is called first when the program runs?The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.
Which operator returns true if an object is the specified object type?The in operator returns true if the specified property is in the specified object or its prototype chain.
What function is called first when the program runs quizlet?Explanation: A program must define a main() function, which is automatically called first when a program executes. Complete the function beginning to page a parameter named userAge of type int.
|