0% found this document useful (0 votes)
48 views8 pages

B22ai013 Oopj T9 Solutions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views8 pages

B22ai013 Oopj T9 Solutions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

B22AI013

Tutorial-9 Solutions
1.Develop a java program to emulate Student class with properties such as Name, Roll number, age
etc. Create two objects of the Student class and assess whether those two objects are equal or not
by overriding equals method present in Object class.

Ans:

Code:

class Student {

private String name;

private int rollNumber;

private int age;

public Student(String name, int rollNumber, int age) {

this.name = name;

this.rollNumber = rollNumber;

this.age = age;

public boolean equals(Object obj) {

if (this == obj) {

return true;

if (obj == null || getClass() != obj.getClass()) {

return false;

Student student = (Student) obj;

return rollNumber == student.rollNumber && age == student.age &&


name.equals(student.name);

public class Main {

public static void main(String[] args) {

Student student1 = new Student("Alice", 101, 20);


Student student2 = new Student("Bob", 102, 21);

if (student1.equals(student2)) {

System.out.println("student1 and student2 are equal");

} else {

System.out.println("student1 and student2 are not equal");

Output:

student1 and student2 are not equal.

2. Develop a java program to create a Shape abstract class with area and perimeter methods as
abstract methods. Create a new class called Triangle which extends Shape class. Triangle class will
have three sides as its properties. Now override area and perimeter methods of Shape class to give
concrete definition in Triangle class. Display the area and perimeter of the Triangle objects.

Ans:

Code:

abstract class Shape {

public abstract double area();

public abstract double perimeter();

class Triangle extends Shape {

private double side1;

private double side2;

private double side3;

public Triangle(double side1, double side2, double side3) {

this.side1 = side1;

this.side2 = side2;

this.side3 = side3;

public double area() {

double s = (side1 + side2 + side3) / 2.0;

return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));


}

public double perimeter() {

return side1 + side2 + side3;

public class Main {

public static void main(String[] args) {

Triangle triangle1 = new Triangle(3.0, 4.0, 5.0);

Triangle triangle2 = new Triangle(5.0, 5.0, 5.0);

System.out.println("Triangle 1:");

System.out.println("Area: " + triangle1.area());

System.out.println("Perimeter: " + triangle1.perimeter());

System.out.println("\nTriangle 2:");

System.out.println("Area: " + triangle2.area());

System.out.println("Perimeter: " + triangle2.perimeter());

Output:

Triangle 1:

Area: 6.0

Perimeter: 12.0

Triangle 2:

Area: 10.825317547305483

Perimeter: 15.0

3. Consider Q. No. 2 and ensure there is a constructor in Shape class and develop a java program to
assess whether constructor of abstract class will get called when the object or derived class is
created.

Ans:

Code:

abstract class Shape {

public abstract double area();

public abstract double perimeter();


public Shape() {

System.out.println("Constructor of the Shape class is called.");

class Triangle extends Shape {

private double side1;

private double side2;

private double side3;

public Triangle(double side1, double side2, double side3) {

this.side1 = side1;

this.side2 = side2;

this.side3 = side3;

public double area() {

double s = (side1 + side2 + side3) / 2.0;

return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

public double perimeter() {

return side1 + side2 + side3;

public class Main {

public static void main(String[] args) {

Triangle triangle1 = new Triangle(3.0, 4.0, 5.0);

Triangle triangle2 = new Triangle(5.0, 5.0, 5.0);

System.out.println("Triangle 1:");

System.out.println("Area: " + triangle1.area());

System.out.println("Perimeter: " + triangle1.perimeter());

System.out.println("\nTriangle 2:");

System.out.println("Area: " + triangle2.area());

System.out.println("Perimeter: " + triangle2.perimeter());


}

Output:

Constructor of the Shape class is called.

Constructor of the Shape class is called.

Triangle 1:

Area: 6.0

Perimeter: 12.0

Triangle 2:

Area: 10.825317547305483

Perimeter: 15.0

4. Consider Q. No. 1 and override toString method of Object class in Student class to return all the
members of the Student class as a String out of toString method.

Ans:

Code:

class Student {

private String name;

private int rollNumber;

private int age;

public Student(String name, int rollNumber, int age) {

this.name = name;

this.rollNumber = rollNumber;

this.age = age;

public String toString() {

return "Name: " + name + "\nRoll Number: " + rollNumber + "\nAge: " + age;

public class Main {

public static void main(String[] args) {

Student student1 = new Student("Alice", 101, 20);


Student student2 = new Student("Bob", 102, 21);

System.out.println("Student 1 Info:\n" + student1.toString());

System.out.println("\nStudent 2 Info:\n" + student2.toString());

Output:

Student 1 Info:

Name: Alice

Roll Number: 101

Age: 20

Student 2 Info:

Name: Bob

Roll Number: 102

Age: 21

5. Develop a java program to emulate the usage of final keyword with data members, member
methods and class as well. Assume your own class name, data members and member methods to
show the usage of final keyword.

Ans:

Code:

class FinalDataDemo {

final int constantValue = 42;

public void printConstantValue() {

System.out.println("Constant Value: " + constantValue);

class ParentClass {

final void finalMethod() {

System.out.println("This is a final method.");

class ChildClass extends ParentClass {

}
final class FinalClass {

public class Main {

public static void main(String[] args) {

FinalDataDemo dataDemo = new FinalDataDemo();

dataDemo.printConstantValue();

ParentClass parent = new ParentClass();

parent.finalMethod();

FinalClass finalObj = new FinalClass();

System.out.println("Final class object created.");

Output:

Constant Value: 42

This is a final method.

Final class object created.

6.Develop a java program to show that the interface contains public, static and final data members
and static methods, default methods and abstract methods. Assume your own interface name, data
members and various member methods.

Ans:

Code:

interface MyInterface {

public static final int MAX_VALUE = 100;

String INTERFACE_NAME = "MyInterface";

void abstractMethod();

static void staticMethod() {

System.out.println("This is a static method in the interface.");

default void defaultMethod() {

System.out.println("This is a default method in the interface.");

}
class MyClass implements MyInterface {

public void abstractMethod() {

System.out.println("Abstract method implementation in MyClass.");

public class Main {

public static void main(String[] args) {

System.out.println("MAX_VALUE: " + MyInterface.MAX_VALUE);

System.out.println("INTERFACE_NAME: " + MyInterface.INTERFACE_NAME);

MyInterface.staticMethod();

MyClass myObj = new MyClass();

myObj.abstractMethod();

myObj.defaultMethod();

Output:

This is a static method in the interface.

Abstract method implementation in MyClass.

This is a default method in the interface.

You might also like