You are on page 1of 5

Tutorial 4 Classes and Objects

NOTE: Put all the source code and answers into a word file, and upload the word file (just
ONE Word file! name it with “your ID_name_date”, e.g. 20191963****_***_20201028.doc).

Attempt ALL questions. Each question is worth 2 marks, with 10 marks in total.

1. Java API has the GregorianCalendar class in the java.util package, which
you can use to obtain the year, month, and day of a date. The no-arg
constructor constructs an instance for the current date, and the methods
get(GregorianCalendar.YEAR), get(GregorianCalendar.MONTH), and
get(GregorianCalendar.DAY_OF_MONTH) return the year, month, and day.
Write a program to perform two tasks:
■ Display the current year, month, and day.
■ The GregorianCalendar class has the setTimeInMillis(long), which can be
used to set a specified elapsed time since January 1, 1970. Set the value to
1234567898765L and display the year, month, and day.

 SOURCE CODE (TEXT)

 EXECUTION RESULTS (TEXT OR SNAPSHOTS)

 DISCUSSION AND CONCLUSION

2. Design a class according to the UML class diagram shown below:

Rectangle

-length: double

-width: double

+Rectangle()

+Rectangle(len:double, wid:double)

+getArea(): double

+getPerimeter(): double
+drawRect(): void

Where:

- the default constructor (without any parameters) set the length and width

of the rectangle both to be 10.0

- the constructor with parameters will set the length and width of that

rectangle with the given value (if the given value are larger than 0,

otherwise set both to be 10.0)

- getArea() returns the area of that rectangle if the range of the length and

width are between 10 to 50 (inclusive), otherwise return value -1

- getPerimeter() returns the perimeter of the rectangle

- drawRect() draws a shape of that rectangle using asterisk (*) with the

length and width change to integer if the length and width are between

10~50 (inclusive), otherwise draw a shape of 10*10

Write a program (another class) called TestRectangle with the main() to test all

methods within class Rectangle.

 SOURCE CODE (TEXT)

 EXECUTION RESULTS (TEXT OR SNAPSHOTS)

 DISCUSSION AND CONCLUSION

3. Write a program. In which,

Define a class called Vehicle, with


Properties:

- speed, size, brand, and so on (whatever you think is needed);

Methods:

- Constructor method – initiate the variables with given value (from

parameters)

- move() – print a string with current speed, eg. “The vehicle is running

with speed xxx”,

- setSpeed(int speed) – reset the speed to specific value,

- speedup() – increase the speed with 5,

- speedDown() – decrease the speed with 5,

- getSpeed() – return the speed,

- The main method – by instantiating an object of a vehicle, use the

Constructor methods to initiate the value of speed, size and brand

(and the others if necessary). And print the speed of the vehicle object.

Use the methods to reset and change the speed, and then print the

information.

 SOURCE CODE (TEXT)

 EXECUTION RESULTS (TEXT OR SNAPSHOTS)

 DISCUSSION AND CONCLUSION


4. On some phone keypads, the alphabets are mapped to digits as follows:

ABC(2), DEF(3), GHI(4), JKL(5), MNO(6), PQRS(7), TUV(8), WXYZ(9). Write a

program called PhoneKeyPad, which prompts user to input a string (case

insensitive), and converts to a sequence of digits (numbers).

Hints: You can use input.next().toLowerCase() to read a string and

convert all characters to lowercase (where input is an object of the Scanner

class) to reduce your cases (in switch-case statements); charAt(index) is a

method from String class that you can invoke with an string object to get the

character from certain index, e.g.


String str = “hello”; //index starts from 0, ends at
str.length()
char ch = str.charAt(1); //character ‘e’ stores in variable ch

 SOURCE CODE (TEXT)

 EXECUTION RESULTS (TEXT OR SNAPSHOTS)

 DISCUSSION AND CONCLUSION

5. (Algebra: quadratic equations) Design a class named QuadraticEquation for


a quadratic equation ax2 + bx + c = 0. The class contains:
■ Private data fields a, b, and c that represent three coefficients.
■ A constructor for the arguments for a, b, and c.
■ Three getter methods for a, b, and c.
■ A method named getDiscriminant() that returns the discriminant, which is
b2 - 4ac.
■ The methods named getRoot1() and getRoot2() for returning two roots of
the equation

These methods are useful only if the discriminant is nonnegative. Let these
methods return 0 if the discriminant is negative.
Draw the UML diagram for the class and then implement the class. Write a
test
program that prompts the user to enter values for a, b, and c and displays the
result based on the discriminant. If the discriminant is positive, display the two
roots. If the discriminant is 0, display the one root. Otherwise, display “The
equation has no roots.”

 SOURCE CODE (TEXT)

 EXECUTION RESULTS (TEXT OR SNAPSHOTS)

 DISCUSSION AND CONCLUSION

You might also like