Java Lab File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

School of Engineering & Technology

Department of Computer Science & Technology

Object Oriented Programming using Java


CSH201 B-T

Practical Lab File


Submitted by : Submitted to:
DIMPLE GAUR Dr.MEENA CHAUDHARY
2K22CSUN01060

INDEX
S.NO LAB SIGN REMARKS
NO.
1 LAB 01
2 LAB 02
3 LAB 03
4 LAB 04
5 LAB 05
6 LAB 06
7 LAB 07
8 LAB 08

--

LAB – 1
Program 1 :

1. Declare an integer variable called n.


2. Set the n to 5;
3. Declare an integer array called a
4. Write a loop that initializes a to the squares of the first five integers.
5. Write a loop that calculates the average of the values of a.
6. Write a loop that will move the items in array a into another array called b so that the order of
bis the reverse of the order of a.
7. Write a loop that will calculate the first 10 Fibonacci numbers.

Output :
LAB – 2
Program 1 :

Write a java program to design a point class with all the member functions as discussed in the
class. Explain the significance of this keyword

Output :
Program 2 :

Write a Java Program to Extend the Point Class to create Circle class with necessary members
functions.

Output :
LAB - 3
Program 1 :
Write a Java program to calculate the average value of array elements.

Output :

Program 2 :
Write a Java program to find the maximum and minimum value of an array.
Output :

Program 3 :
Write a Java program to find the duplicate values of an array of string values.

Output :

Program 4 :
Write a Java program to add two matrices of the same size.
Output :
LAB – 4
Program 1 :
Write a Java program that accepts two doubles as its command line arguments, multiply
these together and display the product.

Output :

Program 2 :
Write a Java program that accepts one command line argument; display the line of
reporting if number is even or odd.
Output :

Program 3 :
Write a Java program that accepts radius of a circle as its command line argument
display the area.

Output :
Program 4 :
Write a Java program that uses length property for displaying any number of command
line arguments.

Output :

Program 5 :
Write a Java program that describes a class person. It should have instance variables to
record name, age and salary. Create a person object. Set and display its instance
variables.
Output :

Program 6 :
Write a Java program computing the area of rectangle illustrating constructors.
Output :
LAB – 6
Program 1 :

Write a Java program to define a class MyNumber having one private int data
member. Write default constructor to initialize it to 0 and another constructor to
initialize it to a value (Use this for initialization). Write methods isNegative(),
isPositive(), isZero(), isOdd(), isEven(). Create an object in main(). Use Scanner
class to pass a value to the object.
Output :

Program 2 :

Write a Java program Define a Student class (roll number, name, percentage).
Define a default and parameterized constructor. Override the toString method.
Keep a count of objects created.
Create objects using parameterized constructor and display the object count after
each object is created. (Use static member and method). Also display the contents of
each object.
Output :

Program 3 :

Write a Java program to -


Implement the following
Box
-Width: double
-Height: double
-Depth: double
+Box(double,double,double)
+Volume():double
Write a class TestBox that will contain main method. Create two objects of Box
namely box1 and box2 having values 10,20,30 and 5,8,9 respectively for width
height and depth. Calculate the volume and print on console.
Output :
LAB – 7
Program 1 :

Write a Java program


A class called circle is designed as shown in the following class diagram. It contains:
 Two private instance variables: radius (of the type double) and color (of the
type String), with default value of 1.0 and "red", respectively.
 Three overloaded constructors - a default constructor with no argument, a
constructor which takes a double argument for radius and a constructor which
takes a double argument for radius and String argument for Color.
 In this exercise, a subclass called Cylinder is derived from the superclass
Circle as shown in the class diagram
 use super() and super(radius) and inherit the variables and methods from the
superclass Circle

Write a test program (say TestCylinder) to test the Cylinder class created. Create three
objects of Cylinder class namely c1,c2 and c3 as below and display height, radius, area and
volume of all the object

Cylinder c1 = new Cylinder();


Cylinder c2 = new Cylinder(10.0);

Cylinder c3 = new Cylinder(2.0, 10.0);


Output :

Program 2 :
Write a Java Program to implement the following class diagram.
Output :

LAB 8
Question 1:

Write a program to implement the following class hierarchy.


public class TestShape { public static
void main(String[] args) {
Shape s1 = new Rectangle("red", 4, 5);
System.out.println(s1);
System.out.println("Area is " + s1.getArea());

Shape s2 = new Triangle("blue", 4, 5);


System.out.println(s2);
System.out.println("Area is " + s2.getArea());

Shape s3 = new Shape("green");


}
}

List out the errors in above mentioned code if any. Correct them to run the program successfully.
Question 2:

Re-write the abstract superclass Shape into an interface, containing only abstract methods, as
follows:

Create a class TestShape as code of class is given below and predict the output.
public class TestShape { public static
void main(String[] args) {
Shape s1 = new Rectangle("red", 4, 5);
System.out.println(s1);
System.out.println("Area is " + s1.getArea());

Shape s2 = new Triangle("blue", 4, 5);


System.out.println(s2);
System.out.println("Area is " + s2.getArea());

Shape s3 = new Shape("green");


}
}

List out the errors in above mentioned code if any. Correct them to run the program successfully.

You might also like