Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution 1

Here is a Java program that meets your requirements:

public class Main {
    public static void main(String[] args) {
        double radius = 1.1;
        double length = 2.2;
        double breadth = 3.3;

        ShapeUtility circle = new ShapeUtility(radius);
        ShapeUtility rectangle = new ShapeUtility(length, breadth);

        System.out.printf("%.2f\n", circle.calculateArea());
        System.out.printf("%.2f\n", circle.calculatePerimeter());
        System.out.printf("%.2f\n", rectangle.calculateArea());
        System.out.printf("%.2f\n", rectangle.calculatePerimeter());
        System.out.println(ShapeUtility.getTotalShapes());
    }
}

class ShapeUtility {
    private static int totalShapes = 0;
    private double radius;
    private double length;
    private double breadth;

    public ShapeUtility(double radius) {
        this.radius = radius;
        totalShapes++;
    }

    public ShapeUtility(double length, double breadth) {
        this.length = length;
        this.breadth = breadth;
        totalShapes++;
    }

    public double calculateArea() {
        if (radius > 0) {
            return Math.PI * Math.pow(radius, 2);
        } else {
            return length * breadth;
        }
    }

    public double calculatePerimeter() {
        if (radius > 0) {
            return 2 * Math.PI * radius;
        } else {
            return 2 * (length + breadth);
        }
    }

    public static int getTotalShapes() {
        return totalShapes;
    }
}

This program creates a ShapeUtility class that can represent a circle or a rectangle. The ShapeUtility class has methods to calculate the area and perimeter of the shape it represents. The totalShapes static variable is incremented each time a new ShapeUtility object is created, keeping track of the total number of shapes processed. The main method creates a circle and a rectangle, calculates and prints their areas and perimeters, and then prints the total number of shapes processed.

This problem has been solved

Solution 2

Here is a Java program that meets your requirements:

public class Main {
    public static void main(String[] args) {
        double radius = 1.1;
        double length = 2.2;
        double breadth = 3.3;

        ShapeUtility circle = new ShapeUtility(radius);
        ShapeUtility rectangle = new ShapeUtility(length, breadth);

        System.out.printf("%.2f\n", circle.calculateArea());
        System.out.printf("%.2f\n", circle.calculatePerimeter());
        System.out.printf("%.2f\n", rectangle.calculateArea());
        System.out.printf("%.2f\n", rectangle.calculatePerimeter());
        System.out.println(ShapeUtility.getTotalShapes());
    }
}

class ShapeUtility {
    private static int totalShapes = 0;
    private double radius;
    private double length;
    private double breadth;

    public ShapeUtility(double radius) {
        this.radius = radius;
        totalShapes++;
    }

    public ShapeUtility(double length, double breadth) {
        this.length = length;
        this.breadth = breadth;
        totalShapes++;
    }

    public double calculateArea() {
        if (radius > 0) {
            return Math.PI * Math.pow(radius, 2);
        } else {
            return length * breadth;
        }
    }

    public double calculatePerimeter() {
        if (radius > 0) {
            return 2 * Math.PI * radius;
        } else {
            return 2 * (length + breadth);
        }
    }

    public static int getTotalShapes() {
        return totalShapes;
    }
}

This program creates a ShapeUtility class that can represent a circle or a rectangle. The ShapeUtility class has methods to calculate the area and perimeter of the shape it represents. The totalShapes static variable is incremented each time a new ShapeUtility object is created, keeping track of the total number of shapes processed. The Main class creates a circle and a rectangle, calculates and prints their areas and perimeters, and then prints the total number of shapes processed.

This problem has been solved

Similar Questions

Implement the abstract class Shape with the abstract method calculateArea.Create the derived classes Circle and Rectangle that extend Shape and implement the calculateArea method according to the formulas for each shapeWrite a main program that reads inputs for creating a circle and a rectangle, calculates their areas, and prints the results.Input2 //number of objectsC 10 //Circle with radousR 10 10 //Rectangle with width and height....Output2 lines display the respective area and circumference314.0 62.8100 40Input3C 100R 5 5R 10 10oUTPUT31400.0 62825 20100 40uSE iNHERITANCE WITH ABSTRACT CLASS SHAPE// Find area and Find circumference must be abstract methods Circle and Rectangle should be sub classes

Create an abstract class named shape that contains two integers and an empty method named printarea().Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape. Each of the inherited class from shape class should provide the implementation for the method printarea(). Get the input and calculate the area of rectangle, circle and triangle.In the Main class, create the objects for the three inherited classes and invoke the methods and display the  area values of the different shapes.2(length of rectangle)3(breadth)5(triangle breadth)6(Triangle height)4(circle radius)

Create a 2 separate Classes CIRCLE  with private data members - Radius and Area, and RECTANGLE with private data members - Length, Breadth and Area. In each of these classes declare and define the member methods putData() [ To display Area ] and computeArea()[ To Compute Area ] are the private and getData() as public member method to read Radius or Length and Breadth based on the choice('C' or 'R'). As a programmer, Write a java program and subsequent pseudocode, to find the area of circle/rectangle using class CIRCLE/RECTANGLE which have following details:getData() to read either radius or lenght and breadth based on the choice 'C' or 'R'Calculate the area using computeArea()Display the Area using putData().Note :Radius is always +Ve, otherwise print "Invalid Radius"Length and Breadth are +Ve, otherwise print "Invalid Length(if Length is -VE) or Invalid Breadth(if Breadth is -VE) or "Invalid Length and Breadth"Input : First line Read the type ('C'  or 'R')Second line onwards read Radius(if Type = 'C') or Length and Breadth (if Type = 'R')Output :  print Area

Consider the base class "Shape" with methods draw() [prints message "Draw"]. Create three subclasses: Circle, Square, and Triangle. Override the draw() method in each subclass to draw the respective shape, and override the calculateArea() method to calculate and return the area of each shape. Implement the above scenario with suitable pseudocode and appropiate java application.User Choice may be "C/S/T". a.)  C (Circle), then must read "Radius"b.)  S (Square), then must read "Side"c.)  T (Triange), then must read both "Height and Base".Identify the suitable data members for the above derived classes and ensure then all are private.

Define a base class called Shape with two pure virtual functionscalculateArea() and calculatePerimeter(). Then, create two derived classes Rectangleand Circle. Implement these classes such that they inherit from the Shape class andoverride the pure virtual functions to calculate the area and perimeter of rectangles andcircles respectively.

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.