OOP Exercises - Java Programming Tutorial
OOP Exercises - Java Programming Tutorial
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 1 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
A class called circle is designed as shown in the following class diagram. It contains:
Two private instance variables: radius (of the type double) and color (of the type String), with default value of 1.0
and "red", respectively.
Two overloaded constructors - a default constructor with no argument, and a constructor which takes a double argument for
radius.
Two public methods: getRadius() and getArea(), which return the radius and area of this instance, respectively.
/**
* The Circle class models a circle with a radius and color.
*/
public class Circle { // Save as "Circle.java"
// private instance variable, not accessible from outside this class
private double radius;
private String color;
// Constructors (overloaded)
/** Constructs a Circle instance with default value for radius and color */
public Circle() { // 1st (default) constructor
radius = 1.0;
color = "red";
}
/** Constructs a Circle instance with the given radius and default color */
public Circle(double r) { // 2nd constructor
radius = r;
color = "red";
}
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 2 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
This Circle class does not have a main() method. Hence, it cannot be run directly. This Circle class is a “building block” and is
meant to be used in another program.
Let us write a test program called TestCircle (in another source file called TestCircle.java) which uses the Circle class,
as follows:
/**
* A Test Driver for the Circle class
*/
public class TestCircle { // Save as "TestCircle.java"
public static void main(String[] args) {
// Declare an instance of Circle class called c1.
// Construct the instance c1 by invoking the "default" constructor
// which sets its radius and color to their default value.
Circle c1 = new Circle();
// Invoke public methods on instance c1, via dot operator.
System.out.println("The circle has radius of "
+ c1.getRadius() + " and area of " + c1.getArea());
//The circle has radius of 1.0 and area of 3.141592653589793
// 3rd constructor to construct a new instance of Circle with the given radius and color
public Circle (double r, String c) { ...... }
Modify the test program TestCircle to construct an instance of Circle using this constructor.
2. Getter: Add a getter for variable color for retrieving the color of this instance.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 3 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
two public methods called setters for changing the radius and color of a Circle instance as follows:
// You cannot do the following because setRadius() returns void, which cannot be printed
System.out.println(c4.setRadius(4.4));
5. Keyword "this": Instead of using variable names such as r (for radius) and c (for color) in the methods'
arguments, it is better to use variable names radius (for radius) and color (for color) and use the special keyword
"this" to resolve the conflict between instance variables and methods' arguments. For example,
// Instance variable
private double radius;
/** Constructs a Circle instance with the given radius and default color */
public Circle(double radius) {
this.radius = radius; // "this.radius" refers to the instance variable
// "radius" refers to the method's parameter
color = "red";
}
Modify ALL the constructors and setters in the Circle class to use the keyword "this".
6. Method toString(): Every well-designed Java class should contain a public method called toString() that
returns a description of the instance (in the return type of String). The toString() method can be called explicitly (via
instanceName.toString()) just like any other method; or implicitly through println(). If an instance is passed to the
println(anInstance) method, the toString() method of that instance will be invoked implicitly. For example, include
the following toString() methods to the Circle class:
Try calling toString() method explicitly, just like any other method:
toString() is called implicitly when an instance is passed to println() method, for example,
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 4 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Circle[radius=1.1]
Circle[radius=1.0]
Circle[radius=2.2]
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 5 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
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
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 6 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
// Test raiseSalary()
System.out.println(e1.raiseSalary(10));
System.out.println(e1);
}
}
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]
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 7 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
// Test getTotal()
System.out.println("The total is: " + inv1.getTotal());
}
}
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
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 8 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
// Test Getters
System.out.println("ID: " + a1.getID());
System.out.println("Name: " + a1.getName());
System.out.println("Balance: " + a1.getBalance());
// Test transfer()
a1.transferTo(a2, 100); // toString()
System.out.println(a1);
System.out.println(a2);
}
}
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]
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 9 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
// Test setDate()
d1.setDate(3, 4, 2016);
System.out.println(d1); // toString()
}
}
01/02/2014
09/12/2099
Month: 12
Day: 9
Year: 2099
03/04/2016
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 10 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
// Test setTime()
t1.setTime(23, 59, 58);
System.out.println(t1); // toString()
// Test nextSecond();
System.out.println(t1.nextSecond());
System.out.println(t1.nextSecond().nextSecond());
// Test previousSecond()
System.out.println(t1.previousSecond());
System.out.println(t1.previousSecond().previousSecond());
}
}
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
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 11 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 12 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
ball.move();
System.out.println(ball);
float xNew = ball.getX();
float yNew = ball.getY();
int radius = ball.getRadius();
// Check boundary value to bounce back
if ((xNew + radius) > xMax || (xNew - radius) < xMin) {
ball.reflectHorizontal();
}
if ((yNew + radius) > yMax || (yNew - radius) < yMin) {
ball.reflectVertical();
}
}
}
}
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.
2. Exercises on Composition
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 13 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
A class called Author (as shown in the class diagram) is designed to model a book's author. It contains:
Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f');
One constructor to initialize the name, email and gender with the given values;
(There is no default constructor for Author, as there are no defaults for name, email and gender.)
public getters/setters: getName(), getEmail(), setEmail(), and getGender();
(There are no setters for name and gender, as these attributes cannot be changed.)
Write the Author class. Also write a test driver called TestAuthor to test all the public methods, e.g.,
Author ahTeck = new Author("Tan Ah Teck", "[email protected]", 'm'); // Test the constructor
System.out.println(ahTeck); // Test toString()
ahTeck.setEmail("[email protected]"); // Test setter
System.out.println("name is: " + ahTeck.getName()); // Test getter
System.out.println("eamil is: " + ahTeck.getEmail()); // Test getter
System.out.println("gender is: " + ahTeck.getGender()); // Test gExerciseOOP_MyPolynomial.pngetter
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 14 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
A class called Book is designed (as shown in the class diagram) to model a book written by one author. It contains:
Four private instance variables: name (String), author (of the class Author you have just created, assume that a book
has one and only one author), price (double), and qty (int);
Two constructors:
Write the Book class (which uses the Author class written earlier). Also write a test driver called TestBook to test all the public
methods in the class Book. Take Note that you have to construct an instance of Author before you can construct an instance of
Book. E.g.,
Book dummyBook = new Book("Java for dummy", ahTeck, 19.95, 99); // Test Book's Constructor
System.out.println(dummyBook); // Test Book's toString()
Take note that both Book and Author classes have a variable called name. However, it can be differentiated via the referencing
instance. For a Book instance says aBook, aBook.name refers to the name of the book; whereas for an Author's instance say
auAuthor, anAuthor.name refers to the name of the author. There is no need (and not recommended) to call the variables
bookName and authorName.
TRY:
1. Printing the name and email of the author from a Book instance. (Hint: aBook.getAuthor().getName(),
aBook.getAuthor().getEmail()).
2. Introduce new methods called getAuthorName(), getAuthorEmail(), getAuthorGender() in the Book class to return
the name, email and gender of the author of the book. For example,
2.2 (Advanced) The Author and Book Classes Again - An Array of Objects
as an Instance Variable
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 15 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
In the earlier exercise, a book is written by one and only one author. In reality, a book can be written by one or more author. Modify
the Book class to support one or more authors by changing the instance variable authors to an Author array.
Notes:
The constructors take an array of Author (i.e., Author[]), instead of an Author instance. In this design, once a Book
instance is constructor, you cannot add or remove author.
The toString() method shall return "Book[name=?,authors=
{Author[name=?,email=?,gender=?],......},price=?,qty=?]".
Hints:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 16 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
a1.setEmail("[email protected]");
System.out.println(a1);
System.out.println("name is: " + a1.getName());
System.out.println("email is: " + a1.getEmail());
b1.setPrice(9.9);
b1.setQty(99);
System.out.println(b1);
System.out.println("isbn is: " + b1.getName());
System.out.println("name is: " + b1.getName());
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 17 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Author[name=Tan Ah Teck,[email protected]]
Author[name=Tan Ah Teck,[email protected]]
name is: Tan Ah Teck
email is: [email protected]
Book[isbn=12345,name=Java for dummies,Author[name=Tan Ah Teck,[email protected]],price=8.8,qty=88]
Book[isbn=12345,name=Java for dummies,Author[name=Tan Ah Teck,[email protected]],price=9.9,qty=99]
isbn is: Java for dummies
name is: Java for dummies
price is: 9.9
qty is: 99
author is: Author[name=Tan Ah Teck,[email protected]]
author's name: Tan Ah Teck
author's name: Tan Ah Teck
author's email: [email protected]
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 18 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
c1.setDiscount(8);
System.out.println(c1);
System.out.println("id is: " + c1.getID());
System.out.println("name is: " + c1.getName());
System.out.println("discount is: " + c1.getDiscount());
inv1.setAmount(999.9);
System.out.println(inv1);
System.out.println("id is: " + inv1.getID());
System.out.println("customer is: " + inv1.getCustomer()); // Customer's toString()
System.out.println("amount is: " + inv1.getAmount());
System.out.println("customer's id is: " + inv1.getCustomerID());
System.out.println("customer's name is: " + inv1.getCustomerName());
System.out.println("customer's discount is: " + inv1.getCustomerDiscount());
System.out.printf("amount after discount is: %.2f%n", inv1.getAmountAfterDiscount());
}
}
Tan Ah Teck(88)(10%)
Tan Ah Teck(88)(8%)
id is: 88
name is: Tan Ah Teck
discount is: 8
Invoice[id=101,customer=Tan Ah Teck(88)(8%),amount=888.8]
Invoice[id=101,customer=Tan Ah Teck(88)(8%),amount=999.9]
id is: 101
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 19 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
The Customer class models a customer is design as shown in the class diagram. Write the codes for the Customer class and a
test driver to test all the public methods.
The Account class models a bank account, design as shown in the class diagram, composes a Customer instance (written earlier)
as its member. Write the codes for the Account class and a test driver to test all the public methods.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 20 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
It contains:
Two instance variables x (int) and y (int).
A default (or "no-argument" or "no-arg") constructor that construct a point at the default location of (0, 0).
A overloaded constructor that constructs a point with the given x and y coordinates.
A toString() method that returns a string description of the instance in the format "(x, y)".
A method called distance(int x, int y) that returns the distance from this point to another point at the given (x, y)
coordinates, e.g.,
An overloaded distance(MyPoint another) that returns the distance from this point to the given MyPoint instance
(called another), e.g.,
Another overloaded distance() method that returns the distance from this point to the origin (0,0), e.g.,
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 21 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
2. Write a program that allocates 10 points in an array of MyPoint, and initializes to (1, 1), (2, 2), ... (10, 10).
Hints: You need to allocate the array, as well as each of the 10 MyPoint instances. In other words, you need to issue 11
new, 1 for the array and 10 for the MyPoint instances.
Notes: Point is such a common entity that JDK certainly provided for in all flavors.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 22 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 23 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
A constructor that constructs a circle with the given center's (x, y) and radius.
An overloaded constructor that constructs a MyCircle given a MyPoint instance as center, and radius.
A default constructor that construct a circle with center at (0,0) and radius of 1.
getArea() and getCircumference() methods that return the area and circumference of this circle in double.
A distance(MyCircle another) method that returns the distance of the centers from this instance and the given
MyCircle instance. You should use MyPoint’s distance() method to compute this distance.
Write the MyCircle class. Also write a test driver (called TestMyCircle) to test all the public methods defined in the class.
Hints:
// Constructors
public MyCircle(int x, int y, int radius) {
// Need to construct an instance of MyPoint for the variable center
center = new MyPoint(x, y);
this.radius = radius;
}
public MyCircle(MyPoint center, int radius) {
// An instance of MyPoint already constructed by caller; simply assign.
this.center = center;
......
}
public MyCircle() {
center = new MyPoint(.....); // construct MyPoint instance
this.radius = ......
}
// Returns the distance of the center for this MyCircle and another MyCircle
public double distance(MyCircle another) {
return center.distance(another.center); // use distance() of MyPoint
}
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 24 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
It contains:
Three private instance variables v1, v2, v3 (instances of MyPoint), for the three vertices.
A constructor that constructs a MyTriangle with three set of coordinates, v1=(x1, y1), v2=(x2, y2), v3=(x3, y3).
A toString() method that returns a string description of the instance in the format "MyTriangle[v1=(x1,y1),v2=
(x2,y2),v3=(x3,y3)]".
A getPerimeter() method that returns the length of the perimeter in double. You should use the distance() method of
MyPoint to compute the perimeter.
A method printType(), which prints "equilateral" if all the three sides are equal, "isosceles" if any two of the three
sides are equal, or "scalene" if the three sides are different.
Write the MyTriangle class. Also write a test driver (called TestMyTriangle) to test all the public methods defined in the
class.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 25 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
It contains:
Two instance variable named real (double) and imag (double) which stores the real and imaginary parts of the complex
number, respectively.
A constructor that creates a MyComplex instance with the given real and imaginary values.
A toString() that returns "(x + yi)" where x and y are the real and imaginary parts, respectively.
Methods isReal() and isImaginary() that returns true if this complex number is real or imaginary, respectively.
Hints:
A method equals(double real, double imag) that returns true if this complex number is equal to the given complex
number (real, imag).
Hints:
An overloaded equals(MyComplex another) that returns true if this complex number is equal to the given MyComplex
instance another.
Hints:
Methods addInto(MyComplex right) that adds and subtract the given MyComplex instance (called right) into this
instance and returns this instance.
Hints:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 26 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Methods addNew(MyComplex right) that adds this instance with the given MyComplex instance called right, and
returns a new MyComplex instance containing the result.
Hint:
2. Write a test driver to test all the public methods defined in the class.
3. Write an application called MyComplexApp that uses the MyComplex class. The application shall prompt the user for two
complex numbers, print their values, check for real, imaginary and equality, and carry out all the arithmetic operations.
Enter complex number 1 (real and imaginary part): 1.1 2.2
Enter complex number 2 (real and imaginary part): 3.3 4.4
Methods argument() that returns the argument of this complex number in radians (double).
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 27 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Note: The Math library has two arc-tangent methods, Math.atan(double) and Math.atan2(double, double). We
commonly use the Math.atan2(y, x) instead of Math.atan(y/x) to avoid division by zero. Read the documentation of
Math class in package java.lang.
The method addInto() is renamed add(). Also added subtract() and subtractNew().
Methods multiply(MyComplex right) and divide(MyComplex right) that multiplies and divides this instance with
the given MyComplex instance right, and keeps the result in this instance, and returns this instance.
A method conjugate() that operates on this instance and returns this instance containing the complex conjugate.
conjugate(x+yi) = x - yi
Take note that there are a few flaws in the design of this class, which was introduced solely for teaching purpose:
Comparing doubles in equal() using "==" may produce unexpected outcome. For example, (2.2+4.4)==6.6 returns
false. It is common to define a small threshold called EPSILON (set to about 10^-8) for comparing floating point numbers.
The method addNew(), subtractNew() produce new instances, whereas add(), subtract(), multiply(), divide()
and conjugate() modify this instance. There is inconsistency in the design (introduced for teaching purpose).
Also take note that methods such as add() returns an instance of MyComplex. Hence, you can place the result inside a
System.out.println() (which implicitly invoke the toString()). You can also chain the operations, e.g.,
c1.add(c2).add(c3) (same as (c1.add(c2)).add(c3)), or c1.add(c2).subtract(c3).
A class called MyPolynomial, which models polynomials of degree-n (see equation), is designed as shown in the class diagram.
It contains:
An instance variable named coeffs, which stores the coefficients of the n-degree polynomial in a double array of size n+1,
where c0 is kept at index 0.
A constructor MyPolynomial(coeffs:double...) that takes a variable number of doubles to initialize the coeffs array,
where the first argument corresponds to c0.
The three dots is known as varargs (variable number of arguments), which is a new feature introduced in JDK 1.5. It accepts
an array or a sequence of comma-separated arguments. The compiler automatically packs the comma-separated arguments in
an array. The three dots can only be used for the last argument of the method.
Hints:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 28 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
}
......
}
// Test program
// Can invoke with a variable number of arguments
MyPolynomial p1 = new MyPolynomial(1.1, 2.2, 3.3);
MyPolynomial p1 = new MyPolynomial(1.1, 2.2, 3.3, 4.4, 5.5);
// Can also invoke with an array
Double coeffs = {1.2, 3.4, 5.6, 7.8}
MyPolynomial p2 = new MyPolynomial(coeffs);
A method evaluate(double x) that evaluate the polynomial for the given x, by substituting the given x into the polynomial
expression.
Methods add() and multiply() that adds and multiplies this polynomial with the given MyPolynomial instance another,
and returns this instance that contains the result.
Write the MyPolynomial class. Also write a test driver (called TestMyPolynomial) to test all the public methods defined in
the class.
Question: Do you need to keep the degree of the polynomial as an instance variable in the MyPolynomial class in Java? How
about C/C++? Why?
1. adds "11111111111111111111111111111111111111111111111111111111111111" to
"22222222222222222222222222222222222222222222222222" and prints the result.
2. multiplies the above two number and prints the result.
Hints:
import java.math.BigInteger
public class TestBigInteger {
public static void main(String[] args) {
BigInteger i1 = new BigInteger(...);
BigInteger i2 = new BigInteger(...);
System.out.println(i1.add(i2));
.......
}
}
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 29 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
A class called MyTime, which models a time instance, is designed as shown in the class diagram.
setTime(int hour, int minute, int second): It shall check if the given hour, minute and second are valid before
setting the instance variables.
(Advanced: Otherwise, it shall throw an IllegalArgumentException with the message "Invalid hour, minute, or second!".)
Setters setHour(int hour), setMinute(int minute), setSecond(int second): It shall check if the parameters are
valid, similar to the above.
Getters getHour(), getMinute(), getSecond().
nextSecond(): Update this instance to the next second and return this instance. Take note that the nextSecond() of
23:59:59 is 00:00:00.
Write the code for the MyTime class. Also write a test driver (called TestMyTime) to test all the public methods defined in the
MyTime class.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 30 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
A class called MyDate, which models a date instance, is defined as shown in the class diagram.
day (int): Between 1 to 28|29|30|31, where the last day depends on the month and whether it is a leap year for Feb
(28|29).
It also contains the following public static final variables (drawn with underlined in the class diagram):
MONTHS (String[]), DAYS (String[]), and DAY_IN_MONTHS (int[]): static variables, initialized as shown, which are
used in the methods.
The MyDate class has the following public static methods (drawn with underlined in the class diagram):
isLeapYear(int year): returns true if the given year is a leap year. A year is a leap year if it is divisible by 4 but not by
100, or it is divisible by 400.
isValidDate(int year, int month, int day): returns true if the given year, month, and day constitute a valid
date. Assume that year is between 1 and 9999, month is between 1 (Jan) to 12 (Dec) and day shall be between 1 and
28|29|30|31 depending on the month and whether it is a leap year on Feb.
getDayOfWeek(int year, int month, int day): returns the day of the week, where 0 for Sun, 1 for Mon, ..., 6 for
Sat, for the given date. Assume that the date is valid. Read the earlier exercise on how to determine the day of the week (or
Wiki "Determination of the day of the week").
The MyDate class has one constructor, which takes 3 parameters: year, month and day. It shall invoke setDate() method (to
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 31 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
setDate(int year, int month, int day): It shall invoke the static method isValidDate() to verify that the
given year, month and day constitute a valid date.
(Advanced: Otherwise, it shall throw an IllegalArgumentException with the message "Invalid year, month, or day!".)
setYear(int year): It shall verify that the given year is between 1 and 9999.
(Advanced: Otherwise, it shall throw an IllegalArgumentException with the message "Invalid year!".)
setMonth(int month): It shall verify that the given month is between 1 and 12.
(Advanced: Otherwise, it shall throw an IllegalArgumentException with the message "Invalid month!".)
setDay(int day): It shall verify that the given day is between 1 and dayMax, where dayMax depends on the month and
whether it is a leap year for Feb.
(Advanced: Otherwise, it shall throw an IllegalArgumentException with the message "Invalid month!".)
getYear(), getMonth(), getDay(): return the value for the year, month and day, respectively.
toString(): returns a date string in the format "xxxday d mmm yyyy", e.g., "Tuesday 14 Feb 2012".
nextDay(): update this instance to the next day and return this instance. Take note that nextDay() for 31 Dec 2000
shall be 1 Jan 2001.
nextMonth(): update this instance to the next month and return this instance. Take note that nextMonth() for 31 Oct
2012 shall be 30 Nov 2012.
nextYear(): update this instance to the next year and return this instance. Take note that nextYear() for 29 Feb
2012 shall be 28 Feb 2013.
(Advanced: throw an IllegalStateException with the message "Year out of range!" if year > 9999.)
Write a test program that tests the nextDay() in a loop, by printing the dates from 28 Dec 2011 to 2 Mar 2012.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 32 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
x, y and radius, which represent the ball's center (x, y) co-ordinates and the radius, respectively.
xDelta (Δx) and yDelta (Δy), which represent the displacement (movement) per step, in the x and y direction respectively.
A constructor which accepts x, y, radius, speed, and direction as arguments. For user friendliness, user specifies speed
(in pixels per step) and direction (in degrees in the range of (-180°, 180°]). For the internal operations, the speed and
direction are to be converted to (Δx, Δy) in the internal representation. Note that the y-axis of the Java graphics
coordinate system is inverted, i.e., the origin (0, 0) is located at the top-left corner.
Δx = d × cos(θ)
Δy = -d × sin(θ)
x += Δx
y += Δy
reflectHorizontal() which reflects the ball horizontally (i.e., hitting a vertical wall)
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 33 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Δx = -Δx
Δy no changes
Δx no changes
Δy = -Δy
toString() which prints the message "Ball at (x, y) of velocity (Δx, Δy)".
Write the Ball class. Also write a test program to test all the methods defined in the class.
A class called Container, which represents the enclosing box for the ball, is designed as shown in the class diagram. It contains:
Instance variables (x1, y1) and (x2, y2) which denote the top-left and bottom-right corners of the rectangular box.
A constructor which accepts (x, y) of the top-left corner, width and height as argument, and converts them into the
internal representation (i.e., x2=x1+width-1). Width and height is used in the argument for safer operation (there is no
need to check the validity of x2>x1 etc.).
A boolean method called collidesWith(Ball), which check if the given Ball is outside the bounds of the container box.
If so, it invokes the Ball's reflectHorizontal() and/or reflectVertical() to change the movement direction of the
ball, and returns true.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 34 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
The Ball class, which models the ball in a soccer game, is designed as shown in the class diagram. Write the codes for the Ball
class and a test driver to test all the public methods.
The Player 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 Player class and a test driver to test all the public methods. Make your
assumption for the kick().
Can you write a very simple soccer game with 2 teams of players and a ball, inside a soccer field?
4. Exercises on Inheritance
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 35 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
In this exercise, a subclass called Cylinder is derived from the superclass Circle as shown in the class diagram (where an an
arrow pointing up from the subclass to its superclass). Study how the subclass Cylinder invokes the superclass' constructors (via
super() and super(radius)) and inherits the variables and methods from the superclass Circle.
You can reuse the Circle class that you have created in the previous exercise. Make sure that you keep "Circle.class" in the
same directory.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 36 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
return getArea()*height;
}
}
Write a test program (says TestCylinder) to test the Cylinder class created, as follow:
Method Overriding and "Super": The subclass Cylinder inherits getArea() method from its superclass Circle. Try
overriding the getArea() method in the subclass Cylinder to compute the surface area (=2π×radius×height + 2×base-area) of
the cylinder instead of base area. That is, if getArea() is called by a Circle instance, it returns the area. If getArea() is called
by a Cylinder instance, it returns the surface area of the cylinder.
If you override the getArea() in the subclass Cylinder, the getVolume() no longer works. This is because the getVolume()
uses the overridden getArea() method found in the same class. (Java runtime will search the superclass only if it cannot locate
the method in this class). Fix the getVolume().
Hints: After overridding the getArea() in subclass Cylinder, you can choose to invoke the getArea() of the superclass
Circle by calling super.getArea().
TRY:
Provide a toString() method to the Cylinder class, which overrides the toString() inherited from the superclass Circle,
e.g.,
@Override
public String toString() { // in Cylinder class
return "Cylinder: subclass of " + super.toString() // use Circle's toString()
+ " height=" + height;
}
Note: @Override 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 toString(). If @Override is not used and
toString() is misspelled as ToString(), it will be treated as a new method in the subclass, instead of overriding the
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 37 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
superclass. If @Override is used, the compiler will signal an error. @Override annotation is optional, but certainly nice to have.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 38 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Hints:
1. You cannot assign floating-point literal say 1.1 (which is a double) to a float variable, you need to add a suffix f, e.g.
0.0f, 1.1f.
2. The instance variables x and y are private in Point2D and cannot be accessed directly in the subclass Point3D. You need
to access via the public getters and setters. For example,
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 39 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Hints
1. You cannot assign floating-point literal say 1.1 (which is a double) to a float variable, you need to add a suffix f, e.g.
0.0f, 1.1f.
2. The instance variables x and y are private in Point and cannot be accessed directly in the subclass MovablePoint. You
need to access via the public getters and setters. For example, you cannot write x += xSpeed, you need to write
setX(getX() + xSpeed).
4.5 Ex: Superclass Shape and its subclasses Circle, Rectangle and Square
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 40 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Write a superclass called Shape (as shown in the class diagram), which contains:
Two constructors: a no-arg (no-argument) constructor that initializes the color to "green" and filled to true, and a
constructor that initializes the color and filled to the given values.
Getter and setter for all the instance variables. By convention, the getter for a boolean variable xxx is called isXXX()
(instead of getXxx() for all the other types).
A toString() method that returns "A Shape with color of xxx and filled/Not filled".
Write two subclasses of Shape called Circle and Rectangle, as shown in the class diagram.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 41 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Three constructors as shown. The no-arg constructor initializes the radius to 1.0.
Override the toString() method inherited, to return "A Circle with radius=xxx, which is a subclass of
yyy", where yyy is the output of the toString() method from the superclass.
Three constructors as shown. The no-arg constructor initializes the width and length to 1.0.
Override the toString() method inherited, to return "A Rectangle with width=xxx and length=zzz, which is
a subclass of yyy", where yyy is the output of the toString() method from the superclass.
Write a class called Square, as a subclass of Rectangle. Convince yourself that Square can be modeled as a subclass of
Rectangle. Square has no instance variable, but inherits the instance variables width and length from its superclass Rectangle.
Provide the appropriate constructors (as shown in the class diagram). Hint:
Override the toString() method to return "A Square with side=xxx, which is a subclass of yyy", where yyy
is the output of the toString() method from the superclass.
Do you need to override the getArea() and getPerimeter()? Try them out.
Override the setLength() and setWidth() to change both the width and length, so as to maintain the square geometry.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 42 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Complete the definition of the following two classes: Point and Line. The class Line composes 2 instances of class Point,
representing the beginning and ending points of the line. Also write test classes for Point and Line (says TestPoint and
TestLine).
// Constructor
public Point (int x, int y) {......}
// Public methods
public String toString() {
return "Point: (" + x + "," + y + ")";
}
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 43 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
System.out.println(p1);
// Try setting p1 to (100, 10).
......
}
}
// Constructors
public Line (Point begin, Point end) { // caller to construct the Points
this.begin = begin;
......
}
public Line (int beginX, int beginY, int endX, int endY) {
begin = new Point(beginX, beginY); // construct the Points here
......
}
// Public methods
public String toString() { ...... }
The class diagram for composition is as follows (where a diamond-hollow-head arrow pointing to its constituents):
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 44 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Instead of composition, we can design a Line class using inheritance. 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 Line class (called LineSub) as a subclass of class Point. LineSub inherits the starting point from its
superclass Point, and adds an ending point. Complete the class definition. Write a testing class called TestLineSub to test
LineSub.
// Constructors
public LineSub (int beginX, int beginY, int endX, int endY) {
super(beginX, beginY); // construct the begin Point
this.end = new Point(endX, endY); // construct the end Point
}
public LineSub (Point begin, Point end) { // caller to construct the Points
super(begin.getX(), begin.getY()); // need to reconstruct the begin Point
this.end = end;
}
// Public methods
// Inherits methods getX() and getY() from superclass Point
public String toString() { ... }
Summary: There are two approaches that you can design a line, composition or inheritance. "A line composes two points" or
"A line is a point extended with another point"”. Compare the Line and LineSub designs: Line uses composition and LineSub
uses inheritance. Which design is better?
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 45 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Try rewriting the Circle-Cylinder 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".
Shape is an abstract class containing 2 abstract methods: getArea() and getPerimeter(), where its concrete subclasses
must provide its implementation. All instance variables shall have protected access, i.e., accessible by its subclasses and classes
in the same package. Mark all the overridden methods with annotation @Override.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 46 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Two protected instance variables color(String) and filled(boolean). The protected variables can be accessed by its
subclasses and classes in the same package. They are denoted with a '#' sign in the class diagram.
Getter and setter for all the instance variables, and toString().
Two abstract methods getArea() and getPerimeter() (shown in italics in the class diagram).
The subclasses Circle and Rectangle shall override the abstract methods getArea() and getPerimeter() and provide
the proper implementation. They also override the toString().
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.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 47 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
System.out.println(s1.getRadius());
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 48 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 49 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 50 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
1. Write the interface called GeometricObject, which declares two abstract methods: getParameter() and
getArea(), as specified in the class diagram.
Hints:
2. Write the implementation class Circle, with a protected variable radius, which implements the interface
GeometricObject.
Hints:
// Constructor
......
......
}
3. Write a test program called TestCircle to test the methods defined in Circle.
4. The class ResizableCircle is defined as a subclass of the class Circle, which also implements an interface called
Resizable, as shown in class diagram. The interface Resizable declares an abstract method resize(), which
modifies the dimension (such as radius) by the given percentage. Write the interface Resizable and the class
ResizableCircle.
Hints:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 51 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
// Constructor
public ResizableCircle(double radius) {
super(...);
}
5. Write a test program called TestResizableCircle to test the methods defined in ResizableCircle.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 52 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
@Override
public void greeting(Dog another) {
System.out.println("Woooooowwwww!");
}
}
Explain the outputs (or error) for the following test program.
// Using Polymorphism
Animal animal1 = new Cat();
animal1.greeting();
Animal animal2 = new Dog();
animal2.greeting();
Animal animal3 = new BigDog();
animal3.greeting();
Animal animal4 = new Animal();
// Downcast
Dog dog2 = (Dog)animal2;
BigDog bigDog2 = (BigDog)animal3;
Dog dog3 = (Dog)animal3;
Cat cat2 = (Cat)animal2;
dog2.greeting(dog3);
dog3.greeting(dog2);
dog2.greeting(bigDog2);
bigDog2.greeting(dog2);
bigDog2.greeting(bigDog1);
}
}
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 53 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
(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 Movable, with abstract methods moveUp(), moveDown(), moveLeft() and
moveRight(). The classes that implement the Movable interface will provide actual implementation to these abstract
methods.
Let's write two concrete classes - MovablePoint and MovableCircle - that implement the Movable interface.
For the MovablePoint class, declare the instance variable x, y, xSpeed and ySpeed with package access as shown with '~' in
the class diagram (i.e., classes in the same package can access these variables directly). For the MovableCircle class, use a
MovablePoint to represent its center (which contains four variable x, y, xSpeed and ySpeed). In other words, the
MovableCircle composes a MovablePoint, and its radius.
// Constructor
public MovablePoint(int x, int y, int xSpeed, int ySpeed) {
this.x = x;
......
}
......
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 54 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
// Constructor
public MovableCircle(int x, int y, int xSpeed, int ySpeed, int radius) {
// Call the MovablePoint's constructor to allocate the center instance.
center = new MovablePoint(x, y, xSpeed, ySpeed);
......
}
......
Write a new class called MovableRectangle, which composes two MovablePoints (representing the top-left and bottom-right
corners) and implementing the Movable Interface. Make sure that the two points has the same speed.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 55 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
The class DiscountRate contains only static variables and methods (underlined in the class diagram).
A polyline is a line with segments formed by points. Let's use the ArrayList (dynamically allocated array) to keep the points, but
upcast to List in the instance variable. (Take note that array is of fixed-length, and you need to set the initial length).
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 56 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
private int y;
public Point(int x, int y) { ...... }
public int getX() { ...... }
public int getY() { ...... }
public void setX(int x) { ...... }
public void setY(int y) { ...... }
public int[] getXY() { ...... }
public void setXY(int x, int y) { ...... }
public String toString() { ...... }
public double distance(Point another) { ...... }
}
import java.util.*;
public class PolyLine {
private List<Point> points; // List of Point instances
// Constructors
public PolyLine() { // default constructor
points = new ArrayList<Point>(); // implement with ArrayList
}
public PolyLine(List<Point> points) {
this.points = points;
}
// Return {(x1,y1)(x2,y2)(x3,y3)....}
public String toString() {
// Use a StringBuilder to efficiently build the return String
StringBuilder sb = new StringBuilder("{");
for (Point aPoint : points) {
sb.append(aPoint.toString());
}
sb.append("}");
return sb.toString();
}
/*
* A Test Driver for the PolyLine class.
*/
import java.util.*;
public class TestPolyLine {
public static void main(String[] args) {
// Test default constructor and toString()
PolyLine l1 = new PolyLine();
System.out.println(l1); // {}
// Test appendPoint()
l1.appendPoint(new Point(1, 2));
l1.appendPoint(3, 4);
l1.appendPoint(5, 6);
System.out.println(l1); // {(1,2)(3,4)(5,6)}
// Test constructor 2
List<Point> points = new ArrayList<Point>();
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 57 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Try:
1. Modify the push() method to throw an IllegalStateException if the stack is full.
2. Modify the push() to return true if the operation is successful, or false otherwise.
3. Modify the push() to increase the capacity by reallocating another array, if the stack is full.
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 58 of 59
OOP Exercises - Java Programming Tutorial 09/11/2021, 2:09 PM
Exercise (Maps):
[TODO]
Representation of map data.
Specialized algorithms, such as shortest path.
Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan ([email protected]) | HOME
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html Page 59 of 59