Académique Documents
Professionnel Documents
Culture Documents
int getWidth( )
{
return w;
}
int getDepth( )
{
return d;
}
int getHeight( )
{
return h;
}
class TestMyBox
{
public static void main( String args[ ] )
{
MyBox red= new MyBox( ) ;
red.setWidth( 10) ;
red.setHeight( 20) ;
red.setDepth( 30) ;
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;
}
class MyName
{
public static void main(String args[])
2
{
}
}
Output:
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;
}
4
void setSalary(int Salary)
{
s= Salary;
}
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:
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 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(" ");
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