Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

Here is the Java code for the problem:

import java.util.Scanner;

class CIRCLE {
    private double radius;
    private double area;

    public void getData() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the radius:");
        radius = sc.nextDouble();
        if (radius < 0) {
            System.out.println("Invalid Radius");
            System.exit(0);
        }
    }

    private void computeArea() {
        area = Math.PI * radius * radius;
    }

    private void putData() {
        System.out.println("The area of the circle is " + area);
    }

    public void calculate() {
        getData();
        computeArea();
        putData();
    }
}

class RECTANGLE {
    private double length;
    private double breadth;
    private double area;

    public void getData() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the length and breadth:");
        length = sc.nextDouble();
        breadth = sc.nextDouble();
        if (length < 0 && breadth < 0) {
            System.out.println("Invalid Length and Breadth");
            System.exit(0);
        } else if (length < 0) {
            System.out.println("Invalid Length");
            System.exit(0);
        } else if (breadth < 0) {
            System.out.println("Invalid Breadth");
            System.exit(0);
        }
    }

    private void computeArea() {
        area = length * breadth;
    }

    private void putData() {
        System.out.println("The area of the rectangle is " + area);
    }

    public void calculate() {
        getData();
        computeArea();
        putData();
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter 'C' for Circle or 'R' for Rectangle:");
        char choice = sc.next().charAt(0);
        if (choice == 'C') {
            CIRCLE circle = new CIRCLE();
            circle.calculate();
        } else if (choice == 'R') {
            RECTANGLE rectangle = new RECTANGLE();
            rectangle.calculate();
        } else {
            System.out.println("Invalid choice");
        }
    }
}

And here is the pseudocode:

class CIRCLE
    declare private double radius
    declare private double area

    method getData
        print "Enter the radius:"
        read radius
        if radius < 0 then
            print "Invalid Radius"
            exit program
        end if
    end method

    method computeArea
        area = PI * radius * radius
    end method

    method putData
        print "The area of the circle is " + area
    end method

    method calculate
        call getData
        call computeArea
        call putData
    end method
end class

class RECTANGLE
    declare private double length
    declare private double breadth
    declare private double area

    method getData
        print "Enter the length and breadth:"
        read length and breadth
        if length < 0 and breadth < 0 then
            print "Invalid Length and Breadth"
            exit program
        else if length < 0 then
            print "Invalid Length"
            exit program
        else if breadth < 0 then
            print "Invalid Breadth"
            exit program
        end if
    end method

    method computeArea
        area = length * breadth
    end method

    method putData
        print "The area of the rectangle is " + area
    end method

    method calculate
        call getData
        call computeArea
        call putData
    end method
end class

main program
    print "Enter 'C' for Circle or 'R' for Rectangle:"
    read choice
    if choice is 'C' then
        create object circle of class CIRCLE
        call circle.calculate
    else if choice is 'R' then
        create object rectangle of class RECTANGLE
        call rectangle.calculate
    else
        print "Invalid choice"
    end if
end main program

This problem has been solved

Similar Questions

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.

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)

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

ClassCreate a class named Circle. This class will represent a circle and hold its properties and functionalities.In the class Circle, create a double-type variable named radius to represent the circle's radius.Create a method named setRadius that takes a double parameter representing the radius value and assigns it to the radius member variable.Create a method named calculateArea that takes no arguments and returns a double value.Inside the calculateArea method, use the formula area = Math.PI * radius * radius to calculate the area based on the stored radius value.

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.