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

Java Exercises

The document describes exercises to design classes to model real-world concepts. It includes: 1. An Author class with name, email, gender properties and related methods. 2. A Book class with name, author, price, quantity properties that references an Author object. 3. A Point class to represent x,y coordinates and a Line class referencing two Point objects. 4. A Circle class with a center Point and radius, along with related methods like distance between circles. 5. A Person superclass and Student/Teacher subclasses to model common and specific properties. 6. A Shape superclass with a getArea() method for subclasses like rectangles, circles to implement.

Uploaded by

tuan lethanh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Java Exercises

The document describes exercises to design classes to model real-world concepts. It includes: 1. An Author class with name, email, gender properties and related methods. 2. A Book class with name, author, price, quantity properties that references an Author object. 3. A Point class to represent x,y coordinates and a Line class referencing two Point objects. 4. A Circle class with a center Point and radius, along with related methods like distance between circles. 5. A Person superclass and Student/Teacher subclasses to model common and specific properties. 6. A Shape superclass with a getArea() method for subclasses like rectangles, circles to implement.

Uploaded by

tuan lethanh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Exercise 1:

1. Creating class called Author as below:

A class called Author is designed as shown in the class diagram. It contains:

 Three private member variables: name (String), email (String), and gender (char of


either 'm' or 'f' - you might also use a boolean variable called isMale having value
of true or false).
 A constructor to initialize the name, email and gender with the given values.
(There is no default constructor, as there is no default value for name, email and gender.)
 Public getters/setters: getName(), getEmail(), setEmail(), and getGender().
(There are no setters for name and gender, as these properties are not designed to be
changed.)
 A toString() method that returns "name (gender) at email", e.g., "Tan Ah Teck (m)
at [email protected]".
Let's design a Book class. Assume that a book is written by one (and exactly one) author. The Book
class (as shown in the class diagram) contains the following members:
 Four private member variables: name (String), author (an instance of the Author class
we have just created, assuming that each book has exactly one author), price (double),
and qty (int).
 The public getters and
setters: getName(), getAuthor(), getPrice(), setPrice(), getQty(), setQty().
 A toString() that returns "'book-name' by author-name (gender) at email". You
could reuse the Author's toString() method, which returns "author-name (gender)
at email".
Exercise 2:

1. Creating class called Point as below:


2. Creating class call Line as below:

Exercise 3:
1. Creating class called Point as below (can use Point class in exercise 2):

1. Creating class called Circle as below (can use Point class in exercise 2):

It contains:
 Two private member variables: a radius (double) and a center (an instance
of Point class, which we created earlier).
 The constructors, public getters and setters.
 Methods getCenterX(), setCenterX(), getCenterY(), setCenterY(), getCenterXY(), 
setCenterXY(), etc.
 A toString() method that returns a string description of this instance in the format of
"Circle[center=(x,y),radius=r]". You should re-use the Point's toString() to print
"(x,y)".
 A distance(Circle another) method that returns the distance from the center
of this instance to the center of the given Circle instance (called another).

Exercise 4:

1. Creating classes as below


Exercise 5:

1. Creating classes as below

A superclass called Person stores common properties such as name and address, and


subclasses Student and Teacher for their specific properties. For students, we need to maintain the
courses taken and their respective grades; add a course with grade, print all courses taken and the
average grade. Assume that a student takes no more than 30 courses for the entire program. For
teachers, we need to maintain the courses taught currently, and able to add or remove a course
taught. Assume that a teacher teaches not more than 5 courses concurrently.

Exercise 6: designing a superclass called Shape, which defines the public interfaces (or behaviors) of
all the shapes. For example, we would like all the shapes to have a method called getArea(), which
returns the area of that particular shape. The Shape class can be written as follow.

You might also like