Knowee
Questions
Features
Study Tools

An architect measuring the area and circumference of rooms, found that some are squares and    some are rectangles. Design a java program that can set the dimensions and calculate the area and circumference.

Question

An architect measuring the area and circumference of rooms, found that some are squares and    some are rectangles. Design a java program that can set the dimensions and calculate the area and circumference.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Java program that can be used to calculate the area and circumference of squares and rectangles:

public abstract class Shape {
    public abstract double getArea();
    public abstract double getCircumference();
}

public class Square extends Shape {
    private double side;

    public Square(double side) {
        this.side = side;
    }

    @Override
    public double getArea() {
        return side * side;
    }

    @Override
    public double getCircumference() {
        return 4 * side;
    }
}

public class Rectangle extends Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

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

    @Override
    public double getCircumference() {
        return 2 * (length + width);
    }
}

public class Main {
    public static void main(String[] args) {
        Square square = new Square(5);
        System.out.println("Square Area: " + square.getArea());
        System.out.println("Square Circumference: " + square.getCircumference());

        Rectangle rectangle = new Rectangle(4, 6);
        System.out.println("Rectangle Area: " + rectangle.getArea());
        System.out.println("Rectangle Circumference: " + rectangle.getCircumference());
    }
}

This program defines an abstract class Shape with two abstract methods getArea() and getCircumference(). Then it defines two subclasses Square and Rectangle that extend Shape and implement these methods. The Main class then creates a Square and a Rectangle, and prints their area and circumference.

This problem has been solved

Similar Questions

Write a program to calculate the circumference and area of a rectangle. Read the required values from the keyboard. Your output must be dislpayed in a user-friendly manner.

Area of a RectangleCreate a Java program that calculates the area of a rectangle using variables to store the length and width.Print out the result.Constraints:NAExample:Input:5.04.0Output:20.0

4. Create a class called Rectangle that represents a rectangle with width andheight attributes. Include methods to calculate the area and perimeter of therectangle. Create an instance of the class and demonstrate its functionality

Write a Java program using " static" that utilizes your ShapeUtility class to demonstrate the calculation of area and perimeter for a circle and a rectangle. Display the total number of shapes processed at the end.The ShapeUtility class to calculate the area and perimeter of a circle and a rectangle. The totalShapes variable (static)  in the ShapeUtility class should be used to keep track of the total number of shapes processed, then display the results and the total number of shapes processed at the end ( as per the Output Format).Input Format:The first line contains the radius of a circle (a double variable)The second line contains the length of a rectangle (a double variable)The third line contains the breadth of a rectangle (a double variable)  Output Format:First line contains the area of a circle (a float : restrict to two precisions)Next line contains the perimeter of a circle (a float : restrict to two precisions)Next line contains the area of a rectangle (a float : restrict to two precisions)Next line contains the perimeter of a rectangle (a float : restrict to two precisions)Last line contains the total number of shapesprocesed.  Testcase-1:Input:1.12.23.3Output:3.806.917.2611.004 Testcase-2:Input:1.23.54.2Output:4.527.5414.7015.404

Write a program to print the area of a rectangle by creating a class named 'Area' having two methods. First method named as 'setDim' takes length and breadth of rectangle as parameters and the second method named as 'getArea' returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.