Java Programming Language and Application to Projects (CSCT4103)

Computer Science - COS

Semester: First Semester

Level: 400

Year: 2015

SCHOOL: H.T.T.T.C DEPARTEMENT: CS LECTURER(S): Mr. DEMANOU YMELE R.
COURSE CODE: CSC315 COURSE TITLE: Java Programming OPTION: FCS 300
DATE: July 2015 HALL: …….. TIME: 2 hrs NATURE: EXAM
Exercise 1 (7 marks)
The purpose of this exercise is to define a class “CN” for representing complex numbers.
Part A
Define CN following the specifications given below:
- Attributes: CN contains two attributes “re” and “im” representing respectively the real and imaginary parts of the
complex number. (0.5× 2)
- Constructors: CN contains a no-argument constructor (it sets re and im to zero), and a 2-argument constructor
having as parameters initial values of real and imaginary parts. (0.5× 2)
- Methods: CN has the following methods:
getRe and getIm: respectively returns the real and imaginary part of a complex number. (0.5× 2)
setRe and setIm: respectively modify the value of real and imaginary parts. (0.5× 2)
add, multiply and subtract: to add, multiply or subtract two complex numbers. (0.5× 3)
getModule: return the module of an instance of CN (0.5)
equals: Tests if two complex numbers are equal (0.5)
clone: returns a copy of an instance of CN (0.5)
Part B
Propose now a class TestCN with the main method to illustrate how to use the class CN defined above. You must call
at least the method add, equals, getModule and clone. (0.5× 4)
Exercise 2 (3 marks)
A, B, and C are three classes. Assume that:
- B extends A;
- B and C implement the interface I;
- B overrides the method meth() of A;
- B and C define a method fonc() of the interface I.
For instructions (1), (2), (3), (4) and (5) given below, which version of method “meth” or “fonc” is called?
A a;
B b;
C c;
I i;
a = new A();
b = new B();
c = new C();
a.meth(); (1)
b.meth(); (2)
a = b;
a.meth(); (3)
i = b; i.fonc(); (4)
i = c; i.fonc(); (5)
REPUBLIC OF CAMEROON
Peace Work Fatherland
***********
THE UNIVERSITY OF BAMENDA
***********
HIGHER TECHNICAL TEACHER TRAINING
COLLEGE (H.T.T.T.C.) BAMBILI
***********
DIRECTORATE OF STUDIES
**********
P.O.BOX 39 BAMBILI
REPUBLIQUE DU CAMEROON
Paix Travail - Patrie
***********
UNIVERSITE DE BAMENDA
***********
ECOLE NORMALE SUPERIEURE
D’ENSEIGNEMENT TECHNIQUE
***********
DIRECTION DES ETUDES
**********
Tél: 33 05 10 69
1/2
www.schoolfaqs.net
Exercise 3 (6 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)
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)
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)
Exercise 4 (2 marks)
Write a program that reads one line of input text and breaks it up into words. The words should be output one per line.
Assume that words are separated by the blank character “ ”. For example, if the user inputs the line “Hello Word!” the
program should print:
Hello
Word!
Exercise 5 (2 marks)
Write a program that simulates rolling a pair of dice. You can simulate rolling one die by choosing one of the
integers 1, 2, 3, 4, 5, or 6 at random. The number you pick represents the number on the die after it is rolled (Use the
method random of java.math.Math. For example, int n = (int)(Math.random()*6)+1 returns a random integer between
1 and 6).
You can assign this value to a variable to represent one of the dice that are being rolled. Do this twice and add the
results together to get the total roll. Your program should report the number showing on each die as well as the total
roll.
For example:
The first die comes up 3
The second die comes up 5
Good Luck!
2/2
www.schoolfaqs.net