0% found this document useful (0 votes)
159 views12 pages

PARALLAG - LABEXP6 (Declaring Classes)

This document provides instructions for an experiment on declaring classes in object oriented programming. It has three parts: objectives, background information, and experimental procedure. The objectives are to understand how to create classes with attributes and methods, and construct programs using classes and objects. The background information defines a class and its components. The experimental procedure provides three coding exercises: 1) create a Room class with attributes and methods, 2) create an AddressBookEntry class, and 3) create an AddressBook class to store AddressBookEntry objects. The document also includes sample code for the exercises and questions about setters, getters, and constructors.

Uploaded by

Nicole Parallag
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
159 views12 pages

PARALLAG - LABEXP6 (Declaring Classes)

This document provides instructions for an experiment on declaring classes in object oriented programming. It has three parts: objectives, background information, and experimental procedure. The objectives are to understand how to create classes with attributes and methods, and construct programs using classes and objects. The background information defines a class and its components. The experimental procedure provides three coding exercises: 1) create a Room class with attributes and methods, 2) create an AddressBookEntry class, and 3) create an AddressBook class to store AddressBookEntry objects. The document also includes sample code for the exercises and questions about setters, getters, and constructors.

Uploaded by

Nicole Parallag
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 12

COLLEGE OF COMPUTER STUDEIS

INFORMATION TECHNOLOGY DEPARTMENT

CCS0023L
(Object Oriented Programming)

EXERCISE

6
Declaring classes

<Nicole Parallag>
<BSITSMBA B21>
<September 24, 2019>
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:

1. 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.

2. Address Book Entry. 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

3. AddressBook. 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 constructor
resembles an instance 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.

Topic Encapsulation and Inheritance


Lab Activity No 6a
Lab Activity Room Record
CLO 1,3
Program execution (20)
Correct output (20)
Design of output (10)
Design of logic (20)
Standards (20)
Delivery (10)
TOTAL

Topic Encapsulation and Inheritance


Lab Activity No 6b
Lab Activity Address Book Entry
CLO 1,3
Program execution (20)
Correct output (20)
Design of output (10)
Design of logic (20)
Standards (20)
Delivery (10)
TOTAL

Topic Encapsulation and Inheritance


Lab Activity No 6c
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