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
student’s object you can inspect the object, remove it and call the student’s
methods.
Tip: Please ask us if you have any problems in creating the object or accessing
the methods!

1. 9.There is no method to change the name of a student (mutator). Add such a method to the class.CS 212 Object-Oriented Programming Fall 2013 Important note: Whenever the class symbol is changed). Create a new student and call before calling any other method the method getAge(). Now call the method setAge()with an actual parameter 22. the return types as well as the formal parameter list. Provide the complete code of the method on your answers’ paper. Explain your answers to questions 6 and 7 with reference to the code of the setAge() method. “constructor”) in the following way: a. the class must be recompile button: striped (i.e.Actually student objects are created with age 0. 11.After creating student objects with this new constructor. 12. This is not very useful. the source code has compiled using the First Part: Please answer the following questions and return the solutions on paper to the instructor. List the names and data types of the attributes of a student object. Provide the complete code of the method (constructor) on your answers’ paper. How old is the student now? 6. 10. the student’s age is set to the specified value. you see the attributes. does calling the setAge() method have any effect? Explain. Provide the complete code of the method on your answers’ paper. What is the return value of the method? 7. Page 2 of 3 . 3. 4. Right-clicking on the student’s objects allows you to call the student’s methods. How many methods does a student object inherit from class Object? Write down the signatures of these methods. Did the age value of the student change? (callgetAge() again. a new formal parameter namedsAge of type int is defined b. ifsAge is at least 16. Otherwise. Which methods are offered in the class student? Please indicate the names. By right-clicking on the student class you can open the editor. How old is the student after he was created? 5. 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 don’t have children as students). Change the method.) 8. the sAge value is ignored and age is set to 18. When you inspect an object. 2. that creates a new student object (public Student(). Search the method setAge() and read it. Call the method birthday().

the fields are Data type String String String int Name name phonenumber address sinceYear 2. the address and the sinceYear values. 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. 5.CS 212 Object-Oriented Programming Fall 2013 Second Part: By left-clicking on “New class” you can create a new class. 4. If you want to further practice. the phonenumber. String fPhone. Page 3 of 3 . int since. String fAddress  all fields are initialized within the constructor 3. implement methods to change (set) the address and the phonenumber. implement methods to read (get) the name. the constructor (public Friend() )has the following characteristics:  parameters: String fName. you can add other attributes or implement anif statement in the constructor that ensures a plausible value for sinceYear.