0% found this document useful (0 votes)
31 views13 pages

Assignment-OOP(2)

Uploaded by

roadgamer888
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)
31 views13 pages

Assignment-OOP(2)

Uploaded by

roadgamer888
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
You are on page 1/ 13

Assignment

// Circle class

class Circle {

private double radius;

private String color;

// Constructors

public Circle() {

this.radius = 1.0;

this.color = "red";

public Circle(double radius) {

this.radius = radius;

this.color = "red";

public Circle(double radius, String color) {

this.radius = radius;

this.color = color;

// Getters and Setters

public double getRadius() {

return radius;
}

public void setRadius(double radius) {

this.radius = radius;

public String getColor() {

return color;

public void setColor(String color) {

this.color = color;

// Other Methods

public double getArea() {

return Math.PI * radius * radius;

public String toString() {

return "Circle [radius=" + radius + ", color=" + color + "]";

// Cylinder class

class Cylinder extends Circle {

private double height;


// Constructors

public Cylinder() {

super();

this.height = 1.0;

public Cylinder(double radius) {

super(radius);

this.height = 1.0;

public Cylinder(double radius, double height) {

super(radius);

this.height = height;

public Cylinder(double radius, double height, String color) {

super(radius, color);

this.height = height;

// Getter and Setter for height

public double getHeight() {

return height;

}
public void setHeight(double height) {

this.height = height;

// Other Methods

public double getVolume() {

return getArea() * height;

// Main class

public class Main {

public static void main(String[] args) {

Circle circle = new Circle(2.0, "blue");

System.out.println("Circle:");

System.out.println("Radius: " + circle.getRadius());

System.out.println("Color: " + circle.getColor());

System.out.println("Area: " + circle.getArea());

System.out.println("Circle Details: " + circle.toString());

System.out.println();

Cylinder cylinder = new Cylinder(3.0, 4.0, "green");

System.out.println("Cylinder:");

System.out.println("Radius: " + cylinder.getRadius());

System.out.println("Color: " + cylinder.getColor());

System.out.println("Height: " + cylinder.getHeight());


System.out.println("Volume: " + cylinder.getVolume());

System.out.println("Cylinder Details: " + cylinder.toString());

Do the following: • The subclass Cylinder invokes the superclass'


constructors (via super( ) and super(radius)) . • The subclass Cylinder
inherits getArea( ) method from its superclass Circle. Try overriding the
getArea() method in the subclass Cylinder to compute the surface area
(=2π×radius×height + 2×(circle area)) of the cylinder instead of base area.
That is, if getArea() is called by a Circle instance, it returns the area. If
getArea() is called by a Cylinder instance, it returns the surface area of the
cylinder. Hints: After overridding the getArea() in subclass Cylinder, you can
choose to invoke the getArea() of the superclass Circle by calling
super.getArea(). • Provide a toString() method to the Cylinder class, which
overrides the toString() inherited from the superclass Circle . The Cylinder
class toString() method should return the returned value from the super class
toString() method and the value of height instance variable.

// Circle class

class Circle {

private double radius;

private String color;

// Constructors

public Circle() {

this.radius = 1.0;

this.color = "red";

public Circle(double radius) {

this.radius = radius;

this.color = "red";
}

public Circle(double radius, String color) {

this.radius = radius;

this.color = color;

// Getters and Setters

public double getRadius() {

return radius;

public void setRadius(double radius) {

this.radius = radius;

public String getColor() {

return color;

public void setColor(String color) {

this.color = color;

// Other Methods

public double getArea() {

return Math.PI * radius * radius;


}

public String toString() {

return "Circle [radius=" + radius + ", color=" + color + "]";

// Cylinder class

class Cylinder extends Circle {

private double height;

// Constructors

public Cylinder() {

super();

this.height = 1.0;

public Cylinder(double radius) {

super(radius);

this.height = 1.0;

public Cylinder(double radius, double height) {

super(radius);

this.height = height;

}
public Cylinder(double radius, double height, String color) {

super(radius, color);

this.height = height;

// Getter and Setter for height

public double getHeight() {

return height;

public void setHeight(double height) {

this.height = height;

// Other Methods

public double getVolume() {

return getArea() * height;

// Main class

public class Main {

public static void main(String[] args) {

Circle circle = new Circle(2.0, "blue");

System.out.println("Circle:");

System.out.println("Radius: " + circle.getRadius());

System.out.println("Color: " + circle.getColor());


System.out.println("Area: " + circle.getArea());

System.out.println("Circle Details: " + circle.toString());

System.out.println();

Cylinder cylinder = new Cylinder(3.0, 4.0, "green");

System.out.println("Cylinder:");

System.out.println("Radius: " + cylinder.getRadius());

System.out.println("Color: " + cylinder.getColor());

System.out.println("Height: " + cylinder.getHeight());

System.out.println("Volume: " + cylinder.getVolume());

System.out.println("Cylinder Details: " + cylinder.toString());

// Circle class

class Circle {

private double radius;

private String color;

// Constructors

public Circle() {

this.radius = 1.0;

this.color = "red";

public Circle(double radius) {

this.radius = radius;
this.color = "red";

public Circle(double radius, String color) {

this.radius = radius;

this.color = color;

// Getters and Setters

public double getRadius() {

return radius;

public void setRadius(double radius) {

this.radius = radius;

public String getColor() {

return color;

public void setColor(String color) {

this.color = color;

// Other Methods

public double getArea() {


return Math.PI * radius * radius;

public String toString() {

return "Circle [radius=" + radius + ", color=" + color + "]";

// Cylinder class

class Cylinder extends Circle {

private double height;

// Constructors

public Cylinder() {

super();

this.height = 1.0;

public Cylinder(double radius) {

super(radius);

this.height = 1.0;

public Cylinder(double radius, double height) {

super(radius);

this.height = height;

}
public Cylinder(double radius, double height, String color) {

super(radius, color);

this.height = height;

// Getter and Setter for height

public double getHeight() {

return height;

public void setHeight(double height) {

this.height = height;

// Overridden getArea() method

@Override

public double getArea() {

return 2 * Math.PI * getRadius() * height + 2 * super.getArea();

// Overridden toString() method

@Override

public String toString() {

return super.toString() + " [height=" + height + "]";

}
// Main class

public class Main {

public static void main(String[] args) {

Circle circle = new Circle(2.0, "blue");

System.out.println("Circle:");

System.out.println("Radius: " + circle.getRadius());

System.out.println("Color: " + circle.getColor());

System.out.println("Area: " + circle.getArea());

System.out.println("Circle Details: " + circle.toString());

System.out.println();

Cylinder cylinder = new Cylinder(3.0, 4.0, "green");

System.out.println("Cylinder:");

System.out.println("Radius: " + cylinder.getRadius());

System.out.println("Color: " + cylinder.getColor());

System.out.println("Height: " + cylinder.getHeight());

System.out.println("Area: " + cylinder.getArea());

System.out.println("Cylinder Details: " + cylinder.toString());

You might also like