The J. B.
Vachha High School For Parsi Girls
CLASS - IX
Computer Application
Programs For Classes and Objects
Unit Test 2
Program 1
• Write a program by using a class 'Picnic' without any data members
but having only functions as per the specifications given below:
class name : Picnic
void display1( ): To print venue, place and reporting time.
void display2( ): To print number of students, name of the teacher
accompanying and bus number.
Write a main class to create an object of the class 'Picnic' and call the
functions display1() and display2( ).
public class Picnic
{
public void display1()
{
System.out.println("Venue : Kidzania");
System.out.println("Place : Mumbai");
System.out.println("Reporting time : 9am");
}
public void display2()
{
System.out.println("Number of students : 50");
System.out.println("Name of the teacher : ABC");
1
System.out.println("Bus Number : 1233");
}
public static void main(String args[]){
Picnic obj = new Picnic();
obj.display1();
obj.display2();
}
}
Program 2
• Write a program by using a class ‘Employee' without any data
members but having only functions as per the specifications given
below:
class name : Employee
void display1( ): To print Employee id, name and department of staff1.
void display2( ): To print Employee id, name and department of staff2
• Write a main class to create an object of the class ‘Employee' and call
the functions display1() and display2( ).
public class Employee
{
public void display1()
{
System.out.println("Employee id : 101");
System.out.println("Employee Name : ABC");
System.out.println("Employee Department : Finance");
}
public void display2()
{
System.out.println("Employee id : 102");
2
System.out.println("Employee Name : XYZ");
System.out.println("Employee Department : Accounts");
}
public static void main(String args[]){
Employee staff1 = new Employee();
Employee staff2 = new Employee();
staff1.display1();
staff2.display2();
}
}
Program 3
• Write a program by using a class ‘Student’ without any data members
but having only functions as per the specifications given below:
class name : Student
void display1( ): To print RollNo, name and Date Of Birth of student1.
void display2( ): To print RollNo, name and Date Of Birth of student2
• Write a main class to create an object of the class ‘Employee' and call
the functions print1() and print2( ).
public class Student
{
public void print1()
{
System.out.println("Roll No : 1");
System.out.println("Student Name : ABC");
System.out.println("DOB : 1-12-2009");
}
3
public void print2()
{
System.out.println("Roll No: 2");
System.out.println("Student Name : XYZ");
System.out.println("DOB : 2-5-2009");
}
public static void main(String args[]){
Student student1 = new Student();
Student student2 = new Student();
student1.print1();
student2.print2();
}
}
Program 4
Write a java program to create a class Shapes with data members as the
length, breadth of a rectangle and side of a square and functions to display the
area of a rectangle and square. The specifications are given below:
Class name : Shapes
void display1() – To calculate and print the area of a rectangle whose length is
23.5cm and breadth is 14.5cm
void display2() – To calculate and print the area of a square whose side is
21.5cm
public class Shapes
{
double l = 23.5, b = 14.5, s = 21.5;
public void display1()
4
{
System.out.println("Length of the rectangle="+l);
System.out.println("Breadth of the rectangle="+b);
area=l*b;
System.out.println("Area of the rectangle=" +area);
}
public void display2()
{
System.out.println("side of the square="+s);
area=s*s;
System.out.println("Areaof the square="+area);
}
public static void main(String args[]){
Shapes rectangle=new Shapes();
Shapes square=new Shapes();
rectangle.display1();
square.display2();
}
Program 5
Write a java program to create a class Shapes with data members as the
radius of a circle and side of a square and functions to display the area and
circumference of a circle and the area and perimeter of a square. The
specifications are given below:
Class name : Shapes
void display1() – To calculate and print the area and circumference of a circle
whose radius is 14.2cm
5
void display2() – To calculate and print the area and perimeter of a square
whose side is 11.2cm
public class Shapes
{
double r = 14.2, s = 11.2, area, circum,peri;
double pie=3.14;
public void display1()
{
System.out.println("radius ="+r);
area=pie*r*r;
circum=2*pie*r;
System.out.println("Area of the circle="+area);
System.out.println("Circumference of the circle="+circum);
}
public void display2()
{
System.out.println("side of the square="+s);
area=s*s;
peri=4*s;
System.out.println("Area of the square="+area);
System.out.println("Perimeter of the square="+peri);
}
public static void main(String args[]){
Shapes circle=new Shapes();
6
Shapes square=new Shapes();
circle.display1();
square.display2();
}
}
Program 6
Define a class named Area with the following description
Member variables:
double base – to store the base of the triangle as 4.5
double height - to store the height of the triangle as 8.9
double diagonal1 - to store the length of diagonal1 of rhombus as 6.7
double diagonal2 - to store the length of diagonal2 of rhombus as 3.7
Member functions:
compute1( ) – to calculate and display the area of a triangle using the
formula ½ base x height
compute2( ) – to calculate and display the area of a rhombus using the
formula ½ diagonal1 x diagonal2
Define a class with the above-mentioned specifications, create the main
method, create 2 objects triangle and rhombus and invoke the member
method compute1() and compute2().
public class Area
{
double base=4.5, height = 8.9, diagonal1=6.7,diagonal2=3.7;
7
double pie=3.14;
double area;
public void compute1(){
System.out.println("Base ="+base);
System.out.println("Height ="+height);
area=1.0/2*base*height;
System.out.println("Area of the circle="+area);
}
public void compute2()
{
System.out.println("Diagonal1="+diagonal1);
System.out.println("Diagonal2="+diagonal2);
area=1.0/2*diagonal1*diagonal2;
System.out.println("Area of the square="+area);
}
public static void main(String args[]){
Area triangle=new Area();
Area rhombus=new Area();
triangle.compute1();
rhombus.compute2();
}
}
Program 7
Define a class named Volume with the following description
Member variables:
8
double radius – to store the radius of the cylinder as 14.5
double height - to store the height of the cylinder as 8.4
double side - to store the side of the cube as 3.4
Member functions:
calculate1( ) – to calculate and display the volume of the cylinder using the
formula, v = πr2h
calculate2( ) – to calculate and display the volume of a cube using the
formula v = side3
Define a class with the above-mentioned specifications, create the main
method, create 2 objects cylinder and cube and invoke the member method
calculate1() and calculate2().
public class Volume
{
double radius =14.5, height = 8.4, side= 3.4, volume;
public void calculate1(){
System.out.println("Radius ="+radius);
System.out.println("Height ="+height);
volume=3.14*radius*height;
System.out.println("Volume of the Cylinder="+volume);
}
public void calculate2()
{
System.out.println("side="+side);
volume=side*side*side;
9
System.out.println("Volume of the cube="+volume);
}
public static void main(String args[]){
Volume cylinder=new Volume();
Volume cube=new Volume();
cylinder.calculate1();
cube.calculate2();
}
}
**********
10