0% found this document useful (0 votes)
39 views

Tutorial 5

The document provides instructions for 4 programming exercises: 1. Implement classes from a class diagram with overridden methods annotated. 2. Implement another set of classes from a class diagram with overridden methods annotated. 3. Implement superclass Shape and subclasses Circle, Rectangle, Square with get/set methods, constructors, and overridden toString methods. 4. (Optional) Find integer sequences defined recursively based on input N.

Uploaded by

Huy Trần Quang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Tutorial 5

The document provides instructions for 4 programming exercises: 1. Implement classes from a class diagram with overridden methods annotated. 2. Implement another set of classes from a class diagram with overridden methods annotated. 3. Implement superclass Shape and subclasses Circle, Rectangle, Square with get/set methods, constructors, and overridden toString methods. 4. (Optional) Find integer sequences defined recursively based on input N.

Uploaded by

Huy Trần Quang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming 2

Tutorial 5

Exercise 1: (Required)
Write the classes as shown in the following class diagram. Mark all the overridden
methods with annotation @Override.

Exercise 2: (Required)
Write the classes as shown in the following class diagram. Mark all the overridden
methods with annotation @Override.
Hints
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.
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).
Exercise 3: (Required)
Superclass Shape and its subclasses Circle, Rectangle and Square

Write a superclass called Shape (as shown in the class diagram), which contains:
• Two instance variables color (String) and filled (boolean).
• 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 a test program to test all the methods defined in Shape.
Write two subclasses of Shape called Circle and Rectangle, as shown in the
class diagram.
The Circle class contains:
• An instance variable radius (double).
• Three constructors as shown. The no-arg constructor initializes the radius
to 1.0.
• Getter and setter for the instance variable radius.
• Methods getArea() and getPerimeter().
• 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.
The Rectangle class contains:
• Two instance variables width (double) and length (double).
• Three constructors as shown. The no-arg constructor initializes the width
and length to 1.0.
• Getter and setter for all the instance variables.
• Methods getArea() and getPerimeter().
• 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:
public Square(double side) {
super(side, side); // Call superclass Rectangle(double,
double
)
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.

Exercise 4: (Optional)
Find the N integer sequences A0,…,AN−1 defined as follows.
• For each i (0≤i≤N−1), the length of Ai is i+1.
• For each i and j (0≤i≤N−1,0≤j≤i), the (j+1)-th term of Ai, denoted by ai,j,
is defined as follows.
o ai,j=1, if j=0 or j=i.
o ai,j=ai−1,j−1 + ai−1,j , otherwise.
Constraints
• 1≤N≤30
• N is an integer.

Input: N

Example:
Input: 3
Output:

1
1 1
1 2 1
Input: 1
Output:

Input: 2
Output:

1
1 1

Input: 4
Output:

1
1 1
1 2 1
1 3 3 1

Submission
Submit a zip file containing all Java programs to this tutorial’s submission box in the course
website on FIT Portal.

You might also like