0% found this document useful (0 votes)
111 views14 pages

Tsa2 Macasaet

This document provides instructions for an experiment on creating classes in Java. It begins by outlining the objectives of understanding how to create classes, attributes, methods, and constructing a program using classes and objects. It then provides background on defining a class with modifiers, name, attributes, constructors, and methods. The experimental procedure instructs students to create a Room class with attributes like room number, type, area, and AC status, and methods to set and display data. It also asks students to create an AddressBook class that can hold 100 entries, and provide methods to add, delete, view, and update entries. The code section provides an implementation of the Room class and AddressBook class with these required methods

Uploaded by

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

Tsa2 Macasaet

This document provides instructions for an experiment on creating classes in Java. It begins by outlining the objectives of understanding how to create classes, attributes, methods, and constructing a program using classes and objects. It then provides background on defining a class with modifiers, name, attributes, constructors, and methods. The experimental procedure instructs students to create a Room class with attributes like room number, type, area, and AC status, and methods to set and display data. It also asks students to create an AddressBook class that can hold 100 entries, and provide methods to add, delete, view, and update entries. The code section provides an implementation of the Room class and AddressBook class with these required methods

Uploaded by

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

COLLEGE OF COMPUTER STUDEIS

INFORMATION TECHNOLOGY DEPARTMENT

CCS0023L
(Object Oriented Programming)

EXERCISE 6
Declaring classes
I. Objectives:

At the end of the experiment, students must be able to:

Cognitive
a) understand how to create classes
b) understand attributes and methods

Psychomotor:
a) construct a program using classes and objects
b) compile and debug the error of the program

Affective
a) appreciate the concept behind this experiment

II. BACKGROUND INFORMATION

To define a class, we write:

<modifier> class <name> {


<attributeDeclaration>*
<constructorDeclaration>*
<methodDeclaration>*
}
– where
 <modifier> is an access modifier, which may be combined with other types
of modifier.

public class StudentRecord {

//we'll add more code here later

}
– where,
• public - means that our class is accessible to other classes outside the
package
• class - this is the keyword used to create a class in Java
• StudentRecord - a unique identifier that describes our class
III. EXPERIMENTAL PROCEDURE:
Write a program to create a room class, the attributes of this class is roomno,
roomtype, roomarea and ACmachine. In this class the member functions are
setdata and displaydata.

Your task is to create a class that contains an


address book entry. The following table describes the information that an
adressbook entry has.

a. Provide the necessary accessor and mutator methods for all the
attributes.
b. Constructors

Create a class address book that can contain 100 entries of


AddressBookEntry objects (use the class you created in the first exercise). You
should provide the following methods for the address book.
a. Add entry
b. Delete entry
c. View all entries
d. Update an entry

CODE:
1) package
room;

public class Room {


int roomNo; String
roomType; float
roomArea; boolean
acMachine;

void setData(int rno, String rt, float area, boolean ac)


{ roomNo = rno;
roomType = rt;
roomArea = area;
acMachine = ac;
} void
displayData()
{
System.out.println("The room #. Is: " + roomNo);
System.out.println ("The room Type is: " + roomType);
System.out.println ("The room area is: " + roomArea);
String s = (acMachine) ? "yes " : "no ";
System.out.println ("The A/c Machine needed: " + s);
}
public static void main(String[] args) {
{
Room room1 = new Room ( ); room1.
setData (101, "Deluxe", 240.0f, true);
room1.displayData ( );
}
}

OUTPUT:

2/3)
CODE:
package addressbook; import
java.io.BufferedReader; import
java.io.InputStreamReader;

public class Addressbook {


public static int displayMenu()throws Exception{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));

System.out.print("\nADDRESS BOOK MAIN MENU" +


"\n[1] Add Entry" +
"\n[2] Delete Entry" +
"\n[3] View Entry" +
"\n[4] Update Entry" +
"\n[5] Exit" +
"\n Enter an option --> ");
int option=Integer.parseInt(br.readLine());
return option;
}

public static int addEntry(Addressbook[] sr, int i)throws Exception{


BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
char answer;

do{
sr[i]=new Addressbook();
System.out.println("\n\n\tADD ENTRY");
System.out.print("Name --> ");
String name=br.readLine();
System.out.print("Address --> ");
String address=br.readLine();
System.out.print("Email --> ");
String email=br.readLine();
System.out.print("Contact No. --> "); int
telNo=Integer.parseInt(br.readLine());
//set the values
sr[i].setName(name);
sr[i].setAddress(address);
sr[i].setEmail(email);
sr[i].setTelNo(telNo);
System.out.print("\nInput again? [y/n] --> ");
answer=(char)System.in.read();
System.in.read();
//if(answer=='y' || answer=='Y') i++;
i++;
}while(answer=='y' || answer=='Y');
return i;
}
public static void main(String[] args) throws Exception{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
Addressbook[] sr=new Addressbook[5];
String nameToCompare;
int opt,updateOption,i=0;
char answer;
boolean found;

do{
opt=displayMenu();
switch(opt){
case 1: i=addEntry(sr,i);
break;

case 2: System.out.println("\n\n\tDELETE
ENTRY"); System.out.print("Enter name to
delete --> "); nameToCompare=br.readLine();
found=false;
for(int index=0;index<i;index++){
if(nameToCompare.equalsIgnoreCase(sr[index].getName())){
sr[index].deleteEntry();
System.out.println("Student " + nameToCompare + " is deleted!");
found=true;
}
}
if (found==false){
System.out.println("No match found!");
}

break;

case 3: System.out.println("\n\n\tVIEW
ENTRY"); System.out.print("Enter name to
view --> "); nameToCompare=br.readLine();
found=false;
for(int index=0;index<i;index++){

if(nameToCompare.equalsIgnoreCase(sr[index].getName())){
sr[index].viewEntry(); found=true;
}
}
if (found==false){
System.out.println("No match found!");
}
break;
case 4: System.out.println("\n\n\tUPDATE
ENTRY"); System.out.print("Enter name to
update --> "); nameToCompare=br.readLine();
found=false;
for(int index=0;index<i;index++){

if(nameToCompare.equalsIgnoreCase(sr[index].getName())){
sr[index].viewEntry(); found=true; do{
System.out.println("[1.] Name : " + sr[index].getName());
System.out.println("[2.] Address : " + sr[index].getAddress());
System.out.println("[3.] Email : " + sr[index].getEmail());
System.out.println("[4.] Tel No. : " + sr[index].getTelNo());
System.out.println("[5.] Back to Main Menu");
System.out.print("Please enter an option to update --> ");
updateOption=Integer.parseInt(br.readLine());
switch(updateOption){ case 1: System.out.print("Enter new
name --> "); sr[index].setName(br.readLine()); break;
case 2: System.out.print("Enter new address --> ");
sr[index].setAddress(br.readLine()); break; case 3:
System.out.print("Enter new email --> ");
sr[index].setEmail(br.readLine()); break; case 4:
System.out.print("Enter new telephone number --> ");
sr[index].setTelNo(Integer.parseInt(br.readLine())); break;
}
}while(updateOption!=5);
}
}
if (found==false)
System.out.println("No match found!");
break;

case 5: System.out.println("\n\tGoodbye!");
}

}while(opt!=5);
}
OUTPUT:
V. QUESTION AND ANSWER:

1. Differentiate setters and getters.


- Getter and setter methods are used to retrieve and manipulate private variables in a
different class. A "getter" method does as it name suggest, retrieves a the attribute of the same
name. A setter method allows you to set the value of the attribute.

2. What is the importance of constructor?


- Constructor is a block of code that initializes the newly created object. A
constructorresembles an insance method in java but it’s not a method as it doesn’t have a
return type. In short constructor and method are different. People often refer constructor as
special type of method in Java.
Lab Activity AddressBook
CLO 1,3
Program execution (20)
Correct output (20)
Design of output (10)
Design of logic (20)
Standards (20)
Delivery (10)
TOTAL

You might also like