0% found this document useful (0 votes)
43 views2 pages

Exercise Set1

The document describes three exercises related to object-oriented programming concepts in Java: Exercise 1 defines a Point class with x and y coordinates and a move and display method, and asks to define a PointCol class that extends Point and adds a color attribute, overriding the display method. Exercise 2 asks to write a MyPoint class with x and y coordinates, constructors, getter/setter methods, and distance calculation methods, and a MyTriangle class that uses three MyPoint instances as vertices and calculates perimeter and type. Exercise 3 describes a discount system for a beauty salon with Customer, DiscountRate, and Visit classes, where DiscountRate contains static discount rates and a visit computes the total bill based on products
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Exercise Set1

The document describes three exercises related to object-oriented programming concepts in Java: Exercise 1 defines a Point class with x and y coordinates and a move and display method, and asks to define a PointCol class that extends Point and adds a color attribute, overriding the display method. Exercise 2 asks to write a MyPoint class with x and y coordinates, constructors, getter/setter methods, and distance calculation methods, and a MyTriangle class that uses three MyPoint instances as vertices and calculates perimeter and type. Exercise 3 describes a discount system for a beauty salon with Customer, DiscountRate, and Visit classes, where DiscountRate contains static discount rates and a visit computes the total bill based on products
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercise 1

1. Define a class named Point, with two attributes x and y representing its coordinates. The constructor
of class Point takes as parameters, initial values of x and y [Point(int x_init, int y_init)].
Its methods are described below:
- move(int dx, int dy) : this method adds dx to x and dy to y
- display() : Displays the point coordinates (x and y)
2. Suppose that your program must also manipulate colored points represented by a class named PointCol
Describes as follow:
- Class PointCol extends Point and has only one attribute color
- Its constructor signature is PointCol(int x_init, int y_init, byte color).
- PointCol overrides the methods display() of its super class, to show its coordinates and color .
Propose a definition of class PointCol.
3. Consider class TestPoint defined by:
Class TestPoint{
public static void main(String arg[]){
Point p = new Point(3, 2);
p.display(); (a)
PointCol pc = new PointCol(5, 5, (byte)3);
p = pc;
p.display(); (b)
}
}
What output is produced by instructions (a) and (b)?

Exercise 2
A class called MyPoint, which models a 2D point with x and y coordinates has the following attributes and
methods:
• Two instance variables x (int) and y (int).
• A "no-argument" (or "no-arg") constructor that construct a point at (0, 0).
• A constructor that constructs a point with the given x and y coordinates.
• Getter and setter for the instance variables x and y.
• A method setXY() to set both x and y.
• 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.
• An overloaded distance(MyPoint another) 1 that returns the distance from this point to the given
MyPoint instance another.

1. You are required to Write the code for the class MyPoint.
2. Write the code of another class called MyTriangle, which models a triangle with 3 vertices designed
as follows. The MyTriangle class uses three MyPoint instances (created in the earlier exercise) as the
three vertices.
The class contains:
• Three private instance variables v1, v2, v3 (instances of MyPoint), for the three vertices.
• A constructor that constructs a MyTriangle with three points v1=(x1, y1), v2=(x2, y2), v3=(x3,
y3). [Prototype: public MyTriangle(MyPoint v1, MyPoint v2, MyPoint v3) ]
• A toString() method that returns a string description of the instance in the format "Triangle @
(x1, y1), (x2, y2), (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.

Exercise 3
You are asked to write a discount system for a beauty salon, 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: Customer, DiscountRate and Visit, as shown in the class
diagram below. 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 DiscountRate contains only static variables and methods (underlined in the class diagram).

GOOD LUCK!
Examiner: Mr. NYAMBI

You might also like