Lab01 ObjectAndClass
Lab01 ObjectAndClass
KEJURUTERAAN KELAUTAN
DAN INFORMATIK
2019/2020
VERSION 1
`
TABLE OF CONTENTS
INSTRUCTIONS.........................................................................................................................1
TASK 1: BASIC KNOWLEDGE ON OBJECT AND CLASS............................................................2
TASK 2: POST-LABORATORY PROBLEMS.............................................................................10
i
`
INSTRUCTIONS
Manual makmal ini adalah untuk kegunaan pelajar-pelajar Fakulti Teknologi Kejuruteraan
Kelautan dan Informatik, Universiti Malaysia Terengganu (UMT) sahaja. Tidak dibenarkan
mencetak dan mengedar manual ini tanpa kebenaran rasmi daripada penulis.
Sila ikuti langkah demi langkah sebagaimana yang dinyatakan di dalam manual.
This laboratory manual is for use by the students of the Faculty of Ocean Engineering
Technology and Informatics, Universiti Malaysia Terengganu (UMT) only. It is not permissible
to print and distribute this manual without the official authorisation of the author.
1
`
OBJECTIVES
ESTIMATED TIME
[120 Minutes]
STEPS:
In this lab, the Rectangle class will be defined.
A rectangle has a height and width, and given this data, the area and perimeter of a rectangle
can be determined. Therefore, the class Rectangle should define data members: height
and width. Since each Rectangle object stores its own values for these variables, height
and width are instance variables. The methods are the method's that determine the area and
perimeter of a Rectangle object and are, therefore, instance methods. The Rectangle
class contains only instance variables and instance methods.
The following diagram is a diagram of a Rectangle object showing the instance variables
width and height and the methods area and perimeter. The state of the object are the values of
its data members. Since the state of this object has been set, also illustrated are the values
returned when the area and perimeter methods are invoked on the object.
2
`
5. Compile the program RectangleTest.java. Execute the program and record the results.
The program will print String “My rectangle has area ” + the area of the rectangle
based on the value of the width and height keyed in by the user
3
`
6. Currently, you can access the values of width and height directly by joining the variable
to the name of the object using the dot operator. Add the following statements to the
end of the main method.
Since the value of 5 and 6 are chosen respectively for the width and height, the
program will print
“Width is 5”
“Height is 6”
7. Compile and execute the program. Record the results. Was your Step 5 prediction
correct? If not, correct your answers.
Result:
“Width is 5”
“Height is 6”
8. Modify the program by adding these statements at an appropriate place in the main
method so that the area of myRect is no longer 0.
4
`
9. Being able to directly access the instance variables of an object (Rectangle) from an
outside class (RectangleTest) is considered to be an inappropriate practice in
object-oriented languages. To prevent this, the instance variables of a class should be
modified by the access modifier private. Modify the Rectangle class by inserting the
private modifier in the data member declaration statement:
10. In the Rectangle class, insert the code for the method getWidth which has no
parameters and returns a double
Compile the code. Then, insert a similar method to give the user, or client, access to the
height of a Rectangle.
11. Modify the client class, RectangleTest, to correctly access the width and height
of the Rectangle object.
5
`
Insert the similar code to access the height of the Rectangle object.
Add the methods setWidth and setHeight to the Rectangle class. Make
changes to RectangleTest to correctly use these methods. Your Rectangle class
should now be:
6
`
7
`
Modify the Rectangle class by adding the constructor, placing it after the declaration
of the instance variables, and before the definitions of the existing methods. This
location is not mandatory, but it makes the code more readable.
to
8
`
13. A constructor must have the same name as the class name. Therefore, a constructor
used to construct a Rectangle object, must be named Rectangle. We say that a
constructor is a special type of method because it does not have a return type and
because it can only be used in conjunction with the new operator. A constructor that
initializes the height and width of a Rectangle object would take the form
9
`
10
`
OBJECTIVE
To test the understanding of basic concepts and terminologies in object and class.
TASK DESCRIPTION
This task is basically to test the understanding of basic concepts and terminologies in object
and class.
ESTIMATED TIME
[60 Minutes]
a. Three constants named PERLAHAN, SEDERHANA, and LAJU with the values 1, 2, and 3
to denote the fan speed.
b. A private int data field named speed that specifies the speed of the fan (the
default is PERLAHAN).
c. A private boolean data field named on that specifies whether the fan is on (the
default is false).
d. A private double data field named radius that specifies the radius of the fan
(the default is 5).
e. A string data field named colour that specifies the colour of the fan (the default is
biru).
f. The accessor and mutator methods for all four data fields.
g. A no-arg constructor that creates a default fan.
h. A method named toString() that returns a string description for the fan. If the fan
is on, the method returns the fan speed, colour, and radius in one combined
11
`
string. If the fan is not on, the method returns the fan color and radius along with the
string “fan is off” in one combined string.
Write a test program that creates two Fan objects. Assign maximum speed, radius 10,
color merah, and turn it on to the first object. Assign medium speed, radius 5, color biru,
and turn it off to the second object. Display the objects by invoking their toString
method.
12