Path: p Word 20 QUESTION 11 80 pe Create a class \"Rectangle\" which subclasses
ID: 3918005 • Letter: P
Question
Path: p Word 20 QUESTION 11 80 pe Create a class "Rectangle" which subclasses the abstract "Shape" class. Rectangle" should have two instance variables of type int. length and width. . You must implement getters for both these instance variables (named "getlength" and "getwidth" o . Rectangle" should have a default constructor which sets both instance variables to 1. . Rectangle" should have a parameterized constructor which takes a length parameter and a width o If either of the given parameters have a value less than 1, Rectangle should throw an parameter (both of type int) and uses those values to initialize the instance variables llegalArgumentException0" For implementing the superclass's methods: . o The area of a rectangle is it's width multiplied by its length o The perimeter of a rectangle is the length plus the width multiplied by two. (1+w) 2) Create a class ShapeFactory which contains the following methods: A function "rectangleMaker", whichtakes no parameters and has a return type of Shapeo. This function generates three rectangles and plades them into a Shape array of length three. It then returns the array. (The ength and width of these rectangles are arbitrary) A function "getFirstRectangle". which takes a Shapefl parameter and has a return type of Rectangle. This function iterates over the given Shape array and returns the first Rectangle in the array. If there is no Rectangle object in the Array, then the function should return Null. A function "getAlRectangles". which takes a Shapel] parameter and has a return type of ArrayList. This function iterates over the given Shape array and adds rectangle to an ArrayList of rectangies. Once the given shape array has been iterated over, the resulting ArrayList of Rectangles is returned. Use the given test cares to test your project before submitting. Attach File Browse Content CollectionExplanation / Answer
here is your program : ---------->>>>>>
Shape.java : ------->>>>>
public abstract class Shape{
public Shape(){
}
public abstract int getArea();
public abstract int getPerimeter();
}
Rectangle.java : ----------->>>>>>>
public class Rectangle extends Shape{
private int length;
private int width;
public Rectangle(){
length = 1;
width = 1;
}
public Rectangle(int len,int wid)throws IllegalArgumentException{
if(len < 1 || wid < 1){
throw new IllegalArgumentException();
}
length = len;
width = wid;
}
public int getLength(){
return length;
}
public int getWidth(){
return width;
}
@Override
public int getArea(){
return length*width;
}
@Override
public int getPerimeter(){
return ((length+width) * 2);
}
}
ShapeFactory.java : ---------->>>>>>>>
import java.util.ArrayList;
public class ShapeFactory{
public Shape[] rectangleMaker(){
Shape[] shapes = new Shape[3];
for(int i = 0;i<3;i++){
shapes[i] = new Rectangle((i+3)*2,(i+2)*3);
}
return shapes;
}
public Rectangle getFirstRectangle(Shape[] shapes){
Rectangle rect = null;
for(int i = 0;i<shapes.length;i++){
if(shapes[i] instanceof Rectangle){
rect = (Rectangle)shapes[i];
}
}
return rect;
}
public ArrayList<Rectangle> getAllRectangle(Shape[] shapes){
ArrayList<Rectangle> rectangles = new ArrayList<>();
for(int i = 0;i<shapes.length;i++){
if(shapes[i] instanceof Rectangle){
rectangles.add((Rectangle)shapes[i]);
}
}
return rectangles;
}
public static void main(String[] args) {
ShapeFactory shapeFact = new ShapeFactory();
Shape[] shapes = shapeFact.rectangleMaker();
Rectangle firstRect = shapeFact.getFirstRectangle(shapes);
System.out.println("First Rectangle = ["+firstRect.getLength()+", "+firstRect.getWidth()+"] Area = "+firstRect.getArea()+" Perimeter = "+firstRect.getPerimeter());
ArrayList<Rectangle> rectangles = shapeFact.getAllRectangle(shapes);
for(int i = 0;i<rectangles.size();i++){
System.out.println("First Rectangle = ["+rectangles[i].getLength()+", "+rectangles[i].getWidth()+"] Area = "+rectangles[i].getArea()+" Perimeter = "+rectangles[i].getPerimeter());
}
}
}