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) ) ) ; }}
Question
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) ) ) ; }}
Solution
Sure, here is the code for the Square class in Java, which overrides the area() and perimeter() methods from the Rectangle class:
public class Square extends Rectangle {
private double side;
Square(double side) {
super(side, side);
this.side = side;
}
@Override
public void area() {
System.out.println("Area of Square is: " + (side * side));
}
@Override
public void perimeter() {
System.out.println("Perimeter of Square is: " + (4 * side));
}
}
In this code, the Square class extends the Rectangle class, meaning it inherits all of its methods. The @Override annotation is used to indicate that the following method overrides a method of the superclass. The area and perimeter of a square are calculated differently than those of a rectangle, so we need to provide new implementations for these methods in the Square class.
Similar Questions
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(); }}
Select the correct answerWhat will be the output of the following Java code?class rectangle { int width; int length; int area; void area(int width, int length) { this.width = width; this.length = length; } } class Output { public static void main(String args[]) { area obj = new area(); obj.area(5 , 6); System.out.println(obj.length + " " + obj.width); } }Options6 55 630 00 30
Create a method display() that display the length, width, perimeter and area of an object created using an instantiation on rectangle class.
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
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.
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.