Create a subclass of the Rectangle class, called CustomRectangle. The CustomRect
ID: 3535815 • Letter: C
Question
Create a subclass of the Rectangle class, called CustomRectangle.
The CustomRectangles constructor accepts a width and a height. Conveniently, the parent class Rectangle also has a constructor that accepts width and height:
Rectangle(int width, int height)
Constructs a new Rectangle whose upperleft corner is at (0, 0) in the coordinate space, and whose width and height are specified by the arguments of the same name.
Also, in CustomRectangle, implement the following method:
public boolean isAreaGreaterThan(int n)
This method returns true if the rectangles area is greater than n, or false if it is less than or equal to n. Area of a rectangle is L x W.
Please note that height and width are public fields in the Rectangle class.
Explanation / Answer
public class MyRectangle { private double width = 1; private double height = 1; private static String color = "white"; public MyRectangle(){ width++; height++; } public MyRectangle (double widthParam, double heightParam, String colorParam){ width = widthParam; height = heightParam; color = colorParam; width++; height++; } public double getWidth (){ return width; } public void setWidth (double widthParam){ width = (widthParam >= 0) ? widthParam : 0; } public double getHeight (){ return height; } public void setHeight (double heightParam){ height = (heightParam >= 0) ? heightParam : 0; } public String getColor (){ return color; } public static void setColor (String colorParam){ } public double findArea (){ return width * height; } } public class MyRectangle { public static void main(String[] args){ Rectangle myRectangle = new Rectangle (9); System.out.println ("The area of my rectangle" + myRectangle.findArea + "is" + myRectangle.findArea()); Rectangle myRectangle = new Rectangle (4); System.out.println ("The area of my rectangle" + myRectangle.findArea + "is" + myRectangle.findArea()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.