0% found this document useful (0 votes)
18 views28 pages

Java Object-Oriented Programming Lab Guide

The document describes a lab file for an Object Oriented Programming course using Java. It contains the index and details of 8 labs covering topics like arrays, classes, inheritance, polymorphism and interfaces. Code snippets and output are provided for problems assigned in each lab.

Uploaded by

Dimple Gaur
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
0% found this document useful (0 votes)
18 views28 pages

Java Object-Oriented Programming Lab Guide

The document describes a lab file for an Object Oriented Programming course using Java. It contains the index and details of 8 labs covering topics like arrays, classes, inheritance, polymorphism and interfaces. Code snippets and output are provided for problems assigned in each lab.

Uploaded by

Dimple Gaur
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

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 [Link] CHAUDHARY
2K22CSUN01060

INDEX
[Link] 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);
[Link](s1);
[Link]("Area is " + [Link]());

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


[Link](s2);
[Link]("Area is " + [Link]());

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);
[Link](s1);
[Link]("Area is " + [Link]());

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


[Link](s2);
[Link]("Area is " + [Link]());

Shape s3 = new Shape("green");


}
}

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

Common questions

Powered by AI

Object-oriented principles suggest using exceptions to handle errors in a Java class hierarchy. By encapsulating error conditions into custom exception classes, you allow for modular and logical error handling, adhering to encapsulation. Overrides of base class methods in subclass can throw appropriate exceptions meaningful to the context, and polymorphism enables handling of these exceptions transparently, promoting robust and maintainable error management .

The 'this' keyword in Java is crucial for distinguishing between instance variables and parameters with the same name within constructors and methods. It is also used to call other constructors within the same class, enabling more organized and efficient object initialization. Using 'this' ensures clarity and correctness in instance member access, particularly during object construction and within methods that modify or utilize instance variables .

To implement the 'MyNumber' class in Java, define a class with a private int data member. Create a default constructor to initialize this member to 0 and another constructor to set it to a specific value using 'this'. Define methods like isNegative(), isPositive(), isZero(), isOdd(), and isEven() by implementing logical conditions (e.g., isEven() returns true if the number modulo 2 is 0).

Converting an abstract class into an interface in Java highlights the shift from concrete inheritance to a contract of behavior. Interfaces define method signatures without any implementation, obligating implementing classes to provide method bodies. This encourages loose coupling and greater flexibility in class design by supporting multiple inheritance, as classes can implement multiple interfaces but only extend one class. This conversion clarifies role-specific interfaces facilitating interaction without imposing a strict class hierarchy .

The steps to initialize an integer array with squares of the first five integers and then reverse it in Java are as follows: First, declare an integer variable n set to 5. Then declare an integer array 'a'. Use a loop to fill 'a' with squares of the first n integers. Calculate values using a[i] = i*i for i from 0 to n-1. To reverse, declare another array 'b' and use a loop to assign values from 'a' to 'b' in reverse order, i.e., b[i] = a[n-i-1].

Constructors in Java can be overloaded by defining multiple constructors with different parameter lists. This allows for the creation of objects with different initial states depending on the information provided. For example, in the Circle class, constructors can be used to create a circle with a default radius, a specified radius, or both a specified radius and color. Overloading enhances flexibility, allowing a class to be instantiated in various ways depending on the needs of different context .

Inheritance allows the Cylinder class to extend the Circle class, inheriting its properties and methods. Polymorphism is utilized when methods from the Circle class are overridden in the Cylinder class to provide specific functionality related to cylinders, such as computing its volume. Constructors such as super() and super(radius) are used to call parent class constructors, facilitating polymorphic behavior and shared functionality .

Designing a parameterized constructor involves ensuring that it initializes all necessary class fields, often taking flexible numbers and types of parameters to offer versatility in instantiations. Considerations include validation of input values, consistency of object state post-construction, and the interface the constructor provides to the class user. These constructors should align with class invariant and maintain encapsulation, providing clear differentiation from default constructors .

Static members in Java are associated with the class rather than any instance, enabling shared states or behaviors across all instances of the class. They are used when a property or method should remain consistent regardless of how many instances exist, such as counters or shared resources like configuration settings. Static methods can access static fields but not instance fields directly, enforcing encapsulation and enabling functionality that's relevant to the class conceptually rather than any specific object .

Overriding the toString() method is important because it provides a human-readable representation of an object's current state, enhancing debugging and logging. It is commonly implemented by returning a string that concisely describes object properties, often by concatenating property values with labels. Effective toString() implementations help in quickly understanding the context and content of objects during program execution .

You might also like