Create a class named Shape: Create 1 private variables named length. Create a co
ID: 3684835 • Letter: C
Question
Create a class named Shape: Create 1 private variables named length. Create a constructor for shape class with parameters Create get and set function for length variable. Create a class named Rectangle inherits Shape class: Create 1 private variables named width Create a constructor for Rectangle class with parameters Create getArea and getPerimeter Create get and set function for width variable Create a printRectangle function that prints the Area and Perimeter Create a class named Square that inherits Shape class: Create a constructor for Square class with parameters Create getArea and getPerimeter Create a printSquare function that prints the Area and Perimeter In main function: Create 2 objects named rect1 and rect2 for rectangle class Set value you like for rect1 using constructor update the value for rect1 using set function Set value you like for rect2 using constructor Display all the values of using print function Create 2 objects named sqr1 and sqr2 for rectangle class Set value you like for sqr1 using Constructor Update the value for sqr1 using set function Set value you like for sqr2 using constructor Display all the values of using print function
Explanation / Answer
package com.tcs.ignite.cep.util;
class Shape {
private float length;
Shape(float length){
this.length = length;
}
public float getLength(){
return length;
}
public void setLength(float length){
this.length = length;
}
}
class Rectangle extends Shape{
private float width;
Rectangle(float length, float width){
super(length);
this.width = width;
}
public double getArea() {
return width * getLength();
}
public double getPerimeter() {
return 2 * (width + getLength());
}
public void printRectangle(){
System.out.format("Area of Rectangle is - %.2f%n", getArea());
System.out.format("Perimeter of Rectangle is - %.2f%n", getPerimeter());
}
}
class Square{
float radius;
public Square(float radius){
this.radius = radius;
}
public float getRadius() {
return radius;
}
public void setRadius(float radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public void printSquare(){
System.out.format("Area of Square is - %.2f%n", getArea());
System.out.format("Perimeter of Square is - %.2f%n", getPerimeter());
}
}
public class MainClass {
public static void main(String[] args) {
Rectangle rect1 = new Rectangle(10, 5); // creating constructor
rect1.setLength(12); // changing length value to 12 by calling setter method.
rect1.printRectangle(); // printing rectangle area and perimeter.
Rectangle rect2 = new Rectangle(20, 10); // creating constructor
rect2.printRectangle(); // printing rectangle area and perimeter.
Square square1 = new Square(10); // creating constructor
square1.setRadius(12); // changing length value to 12 by calling setter method.
square1.printSquare(); // printing rectangle area and perimeter.
Square square2 = new Square(20); // creating constructor
square2.printSquare(); // printing rectangle area and perimeter.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.