Lab 2
Lab 2
Lab Session 2
Topics
Default and Parameterized Constructors
Getter and setter Methods
Objectives
To be able to deal with zero-parameter and parameterized constructor
To be able to set and get the value for each field.
Activities Grades
Prelab
None
inLab
Lesson 1: Default and parameterized constructor
Lesson 2: getter Method
Lesson 3: setter Method
PostLab
Phone Class
Total
InLab Activities
Lesson 1: Default and parameterized constructor
Use class Book and application TestBook for the following activities.
1) Run the above program. Write down the output. Explain the result
Note 1: class Book doesn’t have zero-parameter constructor and there is no syntax
error!!!! This means that java will add internally a zero parameter constructor to each
class that initializes each filed with the default value.
3) Rerun the program and write down the output. Explain the result
Note 2: Java will not add zero-parameter constructor if you add this constructor.
4) Add the following 3-parameters constructor to Book Class (Now Book class has two
constructors)
5) Create a new object in Testbook class using the 3-par constructor and print its values
Book second = new Book("Java", "Ahmad", "0-7637-3402-0");
System.out.println(second);
6) Rerun the program and write down the output. Explain the result
7) Disable the zero-parameter constructor from Book class and then rerun the program.
Write down the syntax error description that appear after running the program… which
line generate the error? Why?
Note 3: if you start adding constructors to any class java will not add a zero-parameter
constructor ….. you should add it even with its simple shape like this: public book(){}
1) Add the following method to Book class: public String getTitle() { return title; }
Define a class Phone that has the following fields: phone brand name, model,
Memory, color (for example samsung , s3, 3 mega memory and red) .
o Define zero and 4-paprameter constructor
o Add the getter and setter method
o Add toString method
Write TestPhone class that
o Create 3 instances
o Use the set method to change the values
o Use Joption pane to input data
End Lab 2