0% found this document useful (0 votes)
4 views6 pages

Java Program Assignment18

The document describes two Java programs that implement abstract classes and inheritance. The first program defines a Shape class with subclasses Cube, Cylinder, and Cuboid to calculate volume and surface area. The second program defines a Fruit class with subclasses Apple, Banana, Orange, and Strawberry to display their color and taste.

Uploaded by

Parineeta Deb
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)
4 views6 pages

Java Program Assignment18

The document describes two Java programs that implement abstract classes and inheritance. The first program defines a Shape class with subclasses Cube, Cylinder, and Cuboid to calculate volume and surface area. The second program defines a Fruit class with subclasses Apple, Banana, Orange, and Strawberry to display their color and taste.

Uploaded by

Parineeta Deb
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

Q1.

Create a class Shape having data members length, breadth, height and abstract
methods such as volume and surfaceArea. Inherit this class into cube, cylinder and
cuboid classes. Redefine the required methods to calculate and display the volume and
surface area of each shape.

// Abstract class Shape


abstract class Shape {
protected double length;
protected double breadth;
protected double height;

public Shape(double length, double breadth, double height) {


[Link] = length;
[Link] = breadth;
[Link] = height;
}

// Abstract methods
public abstract double volume();
public abstract double surfaceArea();

// Concrete method to display details


public void displayDetails() {
[Link]("Length: " + length);
[Link]("Breadth: " + breadth);
[Link]("Height: " + height);
[Link]("Volume: " + [Link]("%.2f", volume()));
[Link]("Surface Area: " + [Link]("%.2f", surfaceArea()));
}
}

// Cube class inheriting from Shape


class Cube extends Shape {
public Cube(double side) {
super(side, side, side);
}

@Override
public double volume() {
return length * length * length;
}

@Override
public double surfaceArea() {
return 6 * length * length;
}

@Override
public void displayDetails() {
[Link]("--- Cube ---");
[Link]();
}
}
// Cylinder class inheriting from Shape
class Cylinder extends Shape {
private double radius;

public Cylinder(double radius, double height) {


super(0, 0, height); // Length and breadth are not directly used for cylinder
[Link] = radius;
}

@Override
public double volume() {
return [Link] * radius * radius * height;
}

@Override
public double surfaceArea() {
return 2 * [Link] * radius * (radius + height);
}

@Override
public void displayDetails() {
[Link]("--- Cylinder ---");
[Link]("Radius: " + radius);
[Link]();
}
}

// Cuboid class inheriting from Shape


class Cuboid extends Shape {
public Cuboid(double length, double breadth, double height) {
super(length, breadth, height);
}

@Override
public double volume() {
return length * breadth * height;
}

@Override
public double surfaceArea() {
return 2 * (length * breadth + length * height + breadth * height);
}

@Override
public void displayDetails() {
[Link]("--- Cuboid ---");
[Link]();
}
}

// Main class to demonstrate the shapes


public class ShapeDemo {
public static void main(String[] args) {
Cube cube = new Cube(5);
Cylinder cylinder = new Cylinder(3, 7);
Cuboid cuboid = new Cuboid(4, 6, 8);

[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

Output:-
Q2. Design an abstract class fruit with data members colour, taste and an abstract
method display. Inherit this class to other classes such as Apple, Banana, Orange and
Strawberry. Redefine the display method to show the color and taste of each fruit along
with its name.

// Abstract class Fruit


abstract class Fruit {
protected String colour;
protected String taste;

public Fruit(String colour, String taste) {


[Link] = colour;
[Link] = taste;
}

// Abstract method to display fruit details


public abstract void display();
}

// Apple class inheriting from Fruit


class Apple extends Fruit {
private String name;

public Apple(String colour, String taste) {


super(colour, taste);
[Link] = "Apple";
}

@Override
public void display() {
[Link]("Fruit: " + name);
[Link]("Colour: " + colour);
[Link]("Taste: " + taste);
}
}

// Banana class inheriting from Fruit


class Banana extends Fruit {
private String name;

public Banana(String colour, String taste) {


super(colour, taste);
[Link] = "Banana";
}

@Override
public void display() {
[Link]("Fruit: " + name);
[Link]("Colour: " + colour);
[Link]("Taste: " + taste);
}
}

// Orange class inheriting from Fruit


class Orange extends Fruit {
private String name;

public Orange(String colour, String taste) {


super(colour, taste);
[Link] = "Orange";
}

@Override
public void display() {
[Link]("Fruit: " + name);
[Link]("Colour: " + colour);
[Link]("Taste: " + taste);
}
}

// Strawberry class inheriting from Fruit


class Strawberry extends Fruit {
private String name;

public Strawberry(String colour, String taste) {


super(colour, taste);
[Link] = "Strawberry";
}

@Override
public void display() {
[Link]("Fruit: " + name);
[Link]("Colour: " + colour);
[Link]("Taste: " + taste);
}
}

// Main class to demonstrate the fruits


public class FruitDemo {
public static void main(String[] args) {
Apple apple = new Apple("Red", "Sweet and slightly tart");
Banana banana = new Banana("Yellow", "Sweet");
Orange orange = new Orange("Orange", "Citrusy and sweet-tart");
Strawberry strawberry = new Strawberry("Red", "Sweet and slightly acidic");

[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

Output:-

You might also like