100% found this document useful (2 votes)
269 views8 pages

Lab Session 4

This document contains code examples for two programming exercises. The first defines an Employee class with attributes for name, father's name, CNIC number, department, salary, and designation. Get/set methods and a printInfo method are included. The second defines a Box class with attributes for width, height, depth, and a calculated volume. Get/set methods are defined and the code tests two Box objects.

Uploaded by

Wajiha Rehman
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
100% found this document useful (2 votes)
269 views8 pages

Lab Session 4

This document contains code examples for two programming exercises. The first defines an Employee class with attributes for name, father's name, CNIC number, department, salary, and designation. Get/set methods and a printInfo method are included. The second defines a Box class with attributes for width, height, depth, and a calculated volume. Get/set methods are defined and the code tests two Box objects.

Uploaded by

Wajiha Rehman
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/ 8

CSS 1042

Object Oriented Programming


LAB SESSION: 4 BS(SE) 2019
Department of CS and IT Name: Rumaisa Dilshad
Enrollment : 2019/Comp/BS(SE)/24340

Topic 1: Accessing Attributes Using Methods


class MyBox
{
private int w;
private int d;
private int h;

int getWidth( )
{
return w;
}

int getDepth( )
{
return d;
}

int getHeight( )
{
return h;
}

void setWidth(int width)


{
w= width;
}

void setHeight(int height)


{
h= height;
}

void setDepth(int depth)


{
d= depth;
}
}

class TestMyBox
{
public static void main( String args[ ] )
{
MyBox red= new MyBox( ) ;
red.setWidth( 10) ;
red.setHeight( 20) ;
red.setDepth( 30) ;

System.out.println( "The width is " + red.getWidth( ) ) ;


System.out.println( "The height is " + red.getHeight( ) ) ;
System.out.println( "The depth is " + red.getDepth( ) ) ;
}
}

1. Type the above given code in a text editor (notepad) and save the file as “TestMyBox.java”.
2. Compile the code using javac TestMyBox.java
3. Execute the code using java TestMyBox

Output:

The width is 10
The height is 20
The depth is 30

Exercise

Class Task:

Source Code:

class Name
{
private String n;

String getName()
{
return n;
}

void setName(String Name)


{
n=Name;
}
}

class MyName
{
public static void main(String args[])

2
{

Name blue=new Name();


blue.setName("Rumaisa Dilshad");
System.out.println("My name is " + blue.getName());

}
}

Output:

My name is Rumaisa Dilshad

Qusetion 1

 Write a class Employee having data members to store the following information: Name,
Father’s Name, CNIC, Department, salary and designation
 Write get/set methods for all attributes.
 Write a method void printInfo() that prints all information of the employee. Also write a
class to test all methods Employee class.

Source Code:

class Employee
{
private String n;
private String f;
private int c;
private String d;
private int s;
private String de;

String getName()
{
return n;
}

String getfatherName()
{
return f;
}

3
int getCnic()
{
return c;
}

String getDepartment()
{
return d;
}

int getSalary()
{
return c;
}

String getDesignation()
{
return de;
}

void setName(String Name)


{
n=Name;
}

void setfatherName(String fatherName)


{
f= fatherName;
}

void setCnic(int Cnic)


{
c= Cnic;
}

void setDepartment(String Department)


{
d= Department;
}

4
void setSalary(int Salary)
{
s= Salary;
}

void setDesignation(String Designation)


{
de= Designation;
}

void printInfo()
{
System.out.println("Employee name = " + getName());
System.out.println("Employee father's name = " + getfatherName());
System.out.println("Employee cnic = " + getCnic());
System.out.println("Employee department = " + getDepartment());
System.out.println("Employee salary = " + getSalary());
System.out.println("Employee designation = " + getDesignation());
}
}

class Employee1
{
public static void main(String args[])
{
Employee e=new Employee();

e.setName("Rumaisa Dilshad");
e.setfatherName("Muhammad Dilshad");
e.setCnic(1234-5678-9);
e.setDepartment("IT");
e.setSalary(40000);
e.setDesignation("Senior IT Manager");

e.printInfo();
}
}

5
Output:

Employee name = Rumaisa Dilshad


Employee father's name = Muhammad Dilshad
Employee cnic = 123456789
Employee department = IT
Employee salary = 40000
Employee designation = Senior IT Manager

Question 2

 Write a class Box having data members to store the following information: Width, height
and depth and method to store the volume of a box using formula (width*height*depth).
 Create two objects myBox1 and myBox2 for the above class.
 Write get/set methods for all the attributes
 Print values of width, depth and height.
 Calculate volume for both objects.

Source Code:

class Box
{
int w;
int d;
int h;
int v;

int getWidth( )
{
return w;
}

int getDepth( )
{
return d;
}

int getHeight( )
{
return h;
}

int getVolume()
{
return v;

6
}

void setWidth(int width)


{
w= width;
}

void setHeight(int height)


{
h= height;
}

void setDepth(int depth)


{
d= depth;
}

void setVolume()
{
v=w*h*d;
}
}

class MyBox
{
public static void main(String args[ ])
{
Box myBox1= new Box( ) ;
myBox1.setWidth(5) ;
myBox1.setHeight(10) ;
myBox1.setDepth(15) ;
myBox1.setVolume();

System.out.println("The width is " + myBox1.getWidth( )) ;


System.out.println("The height is " + myBox1.getHeight( )) ;
System.out.println("The depth is " + myBox1.getDepth( )) ;
System.out.println("The volume is " + myBox1.getVolume()) ;

System.out.println(" ");

Box myBox2= new Box( ) ;


myBox2.setWidth(4) ;
myBox2.setHeight(5) ;
myBox2.setDepth(6) ;
myBox2.setVolume();

7
System.out.println("The width is " + myBox2.getWidth( )) ;
System.out.println("The height is " + myBox2.getHeight( )) ;
System.out.println("The depth is " + myBox2.getDepth( )) ;
System.out.println("The volume is " + myBox2.getVolume()) ;
}
}

Output:

The width is 5
The height is 10
The depth is 15
The volume is 750

The width is 4
The height is 5
The depth is 6
The volume is 120

You might also like