0% found this document useful (0 votes)
60 views6 pages

Lab 2

This document provides an overview of Lab Session 2 which focuses on default and parameterized constructors, getter and setter methods in Java. The objectives are to work with zero-parameter and parameterized constructors, and to set and get field values. The activities will use classes Book and TestBook to demonstrate default constructors, adding parameterized constructors, and adding getter and setter methods. Finally, students will define a Phone class with fields and appropriate constructors and methods, and write a TestPhone class to create instances and test the Phone class.

Uploaded by

AHMAD NABOT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views6 pages

Lab 2

This document provides an overview of Lab Session 2 which focuses on default and parameterized constructors, getter and setter methods in Java. The objectives are to work with zero-parameter and parameterized constructors, and to set and get field values. The activities will use classes Book and TestBook to demonstrate default constructors, adding parameterized constructors, and adding getter and setter methods. Finally, students will define a Phone class with fields and appropriate constructors and methods, and write a TestPhone class to create instances and test the Phone class.

Uploaded by

AHMAD NABOT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignments Cover Sheet

Name:……………………………………………… Date: ………………………….

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. public class Book{


2. public String title;
3. public String author;
4. public String isbn;
5. public toString(){return “Book: ” + title + “ By: “ + author + “ ISBN=”+ isbn;}
6. }
7. public class TestBook{
8. public static void main(String[] args) {
9. Book first = new Book();
10. System.out.println(first); }
11. }

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.

2) Add the following zero-parameter constructor to Book Class:


public Book(){
title = "No title";
author = "Unknown";
isbn = "0-0"; }

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)

public Book(String newTitle, String newAuthor, String isbn){


title = newTitle;
author = newAuthor;
this.isbn = isbn;
}

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(){}

8) Add a new field to the Book class: noOfPages


9) Add a new constructor that will accept 4 parameters (the old 3 parameters plus the new
one)
10) Create a third book using the 4-parameter constructor
11) Change the toString method to print noOfPages.
12) What is the result of System.out.println(second) Explain the result
Lesson 2: getter Method

1) Add the following method to Book class: public String getTitle() { return title; }

2) Call this method from TestBook class: System.out.println(Second.getTitle();)

3) What is the main function for getTitle()?

4) Add getter method for each field in Book class.

5) Write down the general syntax of getter method


Lesson 3: setter Method

1) Add the following method to Book class:


public void setTitle(String newTitle ) { title=newTitle; }

2) Call this method from TestBook class: Second.setTitle(“Python”);


3) Display the content of second class

3) What is the main function for setTitle()?

4) Add setter method for each field in Book class.

5) Write down the general syntax of setter method


PostLab Activities

Using the same arguments in the above example,

 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

You might also like