Knowee
Questions
Features
Study Tools

import java.util.Scanner;class Shape { // Methods to be overridden by subclasses public double getPerimeter() { return 0; } public double getArea() { return 0; }}class Square extends Shape { private double side; public Square(double side) { this.side = side; } @Override public double getPerimeter() { return 4 * side; } @Override public double getArea() { return side * side; }}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 getPerimeter() { return 2 * (length + width); } @Override public double getArea() { return length * width; }}class Triangle extends Shape { private double side1; private double side2; private double side3; public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; } @Override public double getPerimeter() { return side1 + side2 + side3; } @Override public double getArea() { // Using Heron's formula to calculate the area of a triangle double s = (side1 + side2 + side3) / 2; return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); }}public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the dimensions for the Square (side length):"); double sideLength = scanner.nextDouble(); Square square = new Square(sideLength); System.out.println("Perimeter of Square: " + square.getPerimeter()); System.out.println("Area of Square: " + square.getArea()); System.out.println("\nEnter the dimensions for the Rectangle (length and width):"); double length = scanner.nextDouble(); double width = scanner.nextDouble(); Rectangle rectangle = new Rectangle(length, width); System.out.println("Perimeter of Rectangle: " + rectangle.getPerimeter()); System.out.println("Area of Rectangle: " + rectangle.getArea()); System.out.println("\nEnter the dimensions for the Triangle (side lengths):"); double side1 = scanner.nextDouble(); double side2 = scanner.nextDouble(); double side3 = scanner.nextDouble(); Triangle triangle = new Triangle(side1, side2, side3); System.out.println("Perimeter of Triangle: " + triangle.getPerimeter()); System.out.println("Area of Triangle: " + triangle.getArea()); scanner.close(); }}

Question

import java.util.Scanner;class Shape { // Methods to be overridden by subclasses public double getPerimeter() { return 0; } public double getArea() { return 0; }}class Square extends Shape { private double side; public Square(double side) { this.side = side; } @Override public double getPerimeter() { return 4 * side; } @Override public double getArea() { return side * side; }}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 getPerimeter() { return 2 * (length + width); } @Override public double getArea() { return length * width; }}class Triangle extends Shape { private double side1; private double side2; private double side3; public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; } @Override public double getPerimeter() { return side1 + side2 + side3; } @Override public double getArea() { // Using Heron's formula to calculate the area of a triangle double s = (side1 + side2 + side3) / 2; return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); }}public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the dimensions for the Square (side length):"); double sideLength = scanner.nextDouble(); Square square = new Square(sideLength); System.out.println("Perimeter of Square: " + square.getPerimeter()); System.out.println("Area of Square: " + square.getArea()); System.out.println("\nEnter the dimensions for the Rectangle (length and width):"); double length = scanner.nextDouble(); double width = scanner.nextDouble(); Rectangle rectangle = new Rectangle(length, width); System.out.println("Perimeter of Rectangle: " + rectangle.getPerimeter()); System.out.println("Area of Rectangle: " + rectangle.getArea()); System.out.println("\nEnter the dimensions for the Triangle (side lengths):"); double side1 = scanner.nextDouble(); double side2 = scanner.nextDouble(); double side3 = scanner.nextDouble(); Triangle triangle = new Triangle(side1, side2, side3); System.out.println("Perimeter of Triangle: " + triangle.getPerimeter()); System.out.println("Area of Triangle: " + triangle.getArea()); scanner.close(); }}

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

Solution

This is a Java program that calculates the perimeter and area of different shapes: Square, Rectangle, and Triangle. The program uses inheritance and polymorphism concepts of Object-Oriented Programming (OOP).

Here's a step-by-step explanation:

  1. A base class Shape is defined with two methods getPerimeter() and getArea(). These methods return 0 and are meant to be overridden by subclasses.

  2. Three subclasses Square, Rectangle, and Triangle are created, each extending the Shape class. Each subclass has its own attributes and overrides the getPerimeter() and getArea() methods to provide the correct calculations for each shape.

  3. In the main method, the program asks the user to input the dimensions for each shape. It then creates an object of each shape with the given dimensions, calculates the perimeter and area by calling the overridden methods, and prints the results.

  4. For the Triangle class, the area is calculated using Heron's formula.

  5. The Scanner object is closed at the end of the main method to prevent resource leaks.

This program demonstrates the power of OOP, where you can define a general behavior in a base class and have each subclass provide its own implementation.

This problem has been solved

Similar Questions

Method OverridingGiven below is the code for the Rectangle class.Define a new class named Square and override the methods area() and perimeter() to calculate the area and perimeter of a square.public class Rectangle { private double width; private double height; Rectangle(double width, double height){ this.width = width; this.height = height; } public double getWidth() { return width; } public double getHeight() { return height; } public void area() { System.out.println("Area of Rectangle is: " + (width * height) ) ; } public void perimeter() { System.out.println("Perimeter of Rectangle is: " + ( (2* width) + (2* height) ) ) ; }}

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.

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 class named ‘Shape’ which has methods ‘getArea’, ‘getPerimeter’and ‘getSide’.Make three subclasses for three different shapes - ‘Circle’, ‘Triangle’ and‘Rectangle’. These subclasses inherit the ‘Shape’ class and they have theirown ‘getArea’, ‘getPerimeter’, and ‘getSide’ methods definition.Each of these classes has its own data members likeThe circle will have a Radius,Triangle will have three sides,A rectangle will have width and heightWrite a program for the above scenario and display the solution.

What is the output of the given code?abstract class Shape{    int i = 111, j = 222;    abstract void calcArea();    abstract void calcVolume();} abstract class Quadrilateral extends Shape{    void calcArea()    {        System.out.println(i);     }}class Square extends Quadrilateral{    void calcVolume()    {        System.out.println(j);     }}public class Test{    public static void main(String[] args)    {        Square c = new Square();         c.calcArea();         c.calcVolume();              }}Select one:a.Compile time error because 'class Square' is not override all the abstract methods, so should declare it as 'abstract'b.Compile time error because trying to instantiate the 'class Square' which does not override all the abstract methodsc.Run time Errord.111222

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.