Tsa2 Macasaet
Tsa2 Macasaet
CCS0023L
(Object Oriented Programming)
EXERCISE 6
Declaring classes
I. Objectives:
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
}
– 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.
a. Provide the necessary accessor and mutator methods for all the
attributes.
b. Constructors
CODE:
1) package
room;
OUTPUT:
2/3)
CODE:
package addressbook; import
java.io.BufferedReader; import
java.io.InputStreamReader;
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: