Java Programming Language and Application to Projects (CSCT4103)

Computer Science - COS

Semester: Resit

Level: 400

Year: 2017

Page 1 sur 2
RESIT
BTECH
LEVEL : 300
Option : ICT
Examiner: Mr. DEMANOU
Academic year 2016/2017
PAPER : JAVA PROGRAMMING & PROJECT DURATION : 1 h
30min
Exercise 1 (35 marks)
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) (0.25×2+0.5+0.5+0.5 mks)
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. (1+1+0.5×2 mks)
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)? (0.5× 2 mks)
Exercise 2 (35 marks)
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).
REPUBLIC OF CAMEROON
Peace Work Father Land
THE UNIVERSITY OF BAMENDA
HIGHER TECHNICAL TEACHERS’
TRAINING COLLEGE (H.T.T.T.C)
BAMBILI - BAMENDA
P.O. BOX 39 Bamenda Tel: 233 36 36 94
REPUBLIQUE DU CAMEROUN
Paix Travail - Patrie
UNIVERSITE DE BAMENDA
ECOLE NORMALE SUPERIEURE DE
L’ENSEIGNEMENT TECHNIQUE
(ENSET)
DE BAMBILI - BAMENDA
www.schoolfaqs.net
Page 2 sur 2
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) 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. (0.5× 8 mks)
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. (0.5 mk)
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) ] (1 mk)
A toString() method that returns a string description of the instance in the format "Triangle @
(x1, y1), (x2, y2), (x3, y3)". (0.5 mk)
A getPerimeter() method that returns the length of the perimeter in double. You should use the
distance() method of MyPoint to compute the perimeter. (1 mk)
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. (1 mk)
www.schoolfaqs.net