You are on page 1of 3

CS 212

Object-Oriented Programming

Fall 2013

Lab Assignment 1
During this lab assignment you will explore the BlueJ environment, work with a
student object, modify the Student class and implement, based on the Blueprint
of the Student class, a Friend class.
This lab assignment consists of two parts. The first part is handed to the
lecturer for correction while in the second you create your first class.
Please get the BlueJ project Lab Assignment 1 (our TA will give you the
instructions) and open it in the BlueJ environment.
You can then create an object by right clicking the mouse over the class symbol
and invoking the new Student() method (= the constructor):

Class

You then will be asked to input the actual parameter values. Please note, that
strings must be specified using the , otherwise you are referring to a variable
name:

On the so called Object bench you will now see the new student object:

Object

You can create several students with different values. By right clicking on the
students object you can inspect the object, remove it and call the students
methods.
Tip: Please ask us if you have any problems in creating the object or accessing
the methods!

CS 212

Object-Oriented Programming

Fall 2013

Important note:
Whenever the class symbol is
changed), the class must be recompile button:

striped (i.e. the source code has


compiled using the

First Part:
Please answer the following questions and return the solutions on paper to the
instructor.
1. When you inspect an object, you see the attributes. List the names and data
types of the attributes of a student object.
2. Right-clicking on the students objects allows you to call the students
methods. Which methods are offered in the class student? Please indicate the
names, the return types as well as the formal parameter list.
3. How many methods does a student object inherit from class Object? Write
down the signatures of these methods.
4. Create a new student and call before calling any other method the method
getAge(). How old is the student after he was created?
5. Call the method birthday(). How old is the student now?
6. Now call the method setAge()with an actual parameter 22. What is the return
value of the method?
7. Did the age value of the student change? (callgetAge() again.)
8. By right-clicking on the student class you can open the editor. Search the
method setAge() and read it. Explain your answers to questions 6 and 7 with
reference to the code of the setAge() method.
9. Change the method setAge() such that the age can only be set if age equals
0 and the new age to be set is at least 16 (we dont have children as
students). Provide the complete code of the method on your answers paper.
10.There is no method to change the name of a student (mutator). Add such a
method to the class. Provide the complete code of the method on your
answers paper.
11.Actually student objects are created with age 0. This is not very useful.
Change the method, that creates a new student object (public Student(),
constructor) in the following way:
a. a new formal parameter namedsAge of type int is defined
b. ifsAge is at least 16, the students age is set to the specified value.
Otherwise, the sAge value is ignored and age is set to 18.
Provide the complete code of the method (constructor) on your answers
paper.
12.After creating student objects with this new constructor, does calling the
setAge() method have any effect? Explain.

Page 2 of 3

CS 212

Object-Oriented Programming

Fall 2013

Second Part:
By left-clicking on New class

you can create a new class. Call it Friend and make it of type class:

Open the editor of the class and delete anything but the following code lines:

Implement analogous to the class Student (open the editor to watch the code of
the class Student) the class Friend with the following characteristics:
1. the fields are
Data type
String
String
String
int

Name
name
phonenumber
address
sinceYear

2. the constructor (public Friend() )has the following characteristics:

parameters: String fName, String fPhone, int since, String


fAddress

all fields are initialized within the constructor

3. implement methods to read (get) the name, the phonenumber, the address
and the sinceYear values.
4. implement methods to change (set) the address and the phonenumber.

5. If you want to further practice, you can add other attributes or implement
anif statement in the constructor that ensures a plausible value for
sinceYear.

Page 3 of 3

You might also like