Task 1 We want to define our Superclass ‘Shape’, which requires two instance var
ID: 3718885 • Letter: T
Question
Task 1
We want to define our Superclass ‘Shape’, which requires two instance variables x and y.
We want to define setter and getter methods for both instance variables.
Task 2
We now want to create the subclass TwoDimensionalShape, which inherits the Shape class.
Our class definition includes the definition, along with extends Shape (to inherit Shape) class TwoDimensionalShape extends Shape
This class requires two additional instance variables: dimension1 and dimension2.
The x and y variables are used from super class where dimension1 and dimension2 are defined in this class.
Do not declare any other variables besides dimension1 and dimension2.
We need to define a Constructor which accepts initializing parameters for the two instance variables for Shape and the two instance variables for TwoDimensionalShape.
We then need to define the setter and getter methods for the new instance variables.
The setter and getter methods of the Shape class are inherited, thus included in TwoDimensionalShape.
We now want to define the two additional subclasses; Circle and
Rectangle. Both of these subclasses extend the TwoDimensionalShape class.
Task 3
1. Define the class Circle, which inherits TwoDimensionalShape.
Circle inherits all instance variables and methods from
Shape and all instance variables and methods from TwoDimensionalShape.
2. No new Instance variables should be defined for this class, you must use the instance variables from the inherited classes.
3. Define a Constructor, which accepts an x and y coordinate, along with the radius of the circle. This constructor should call the superclass constructor where the radius should be assigned to dimension1of the superclass. Dimension2 is not needed so you can initialize it with 0.
4. Define a method called area, which has no parameters and returns a double. This method returns the area of the circle (PI * radius * radius).
Define setter and getter methods for setting the radius; setRadius and getRadius.
Task 4
1. Define the class Rectangle, which inherits TwoDimensionalShape. Rectangleinherits all instance variables and methods from Shape and allinstance variables and methods from TwoDimensionalShape.
2. No new instance variables should be defined for this class, you must use the instance variables from the inherited classes.
3. Define a Constructor, which accepts an x and y coordinate, along with the length and width of the rectangle. This constructor should call the superclass constructor where the length of the rectangle should be assigned to dimension1 and the width of the rectangle should be assigned to dimension2 of the superclass.
4. Define a method called area, which has no parameters and returns a double. This method returns the area of the rectangle (length x width)
5. Define setter and getter methods for setting both the length and width of the rectangle; getLength, getWidth, setLength and setWidth.
Use the following test harness to test your
subclasses
public class ShapeTest
{
public static void main(
String[] args
)
{
Circle c = new Circle( 1, 1, 5 );
Rectangle r = new Rectangle( 1, 1, 4, 3 );
System.out.println( "Area of the Circle is " + c.area());
System.out.println( "Area of the Rectangle is " + r.area());
}
}
Which will result in the following output:
Area of the Circle is 78.53981633974483
Area of the Rectangle is 12
Explanation / Answer
Shape.java
public class Shape {
//Declaring instance variables
private double x;
private double y;
//Parameterized constructor
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
// getters and setters
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
__________________
TwoDimensionalShape.java
public class TwoDimensionalShape extends Shape {
//Declaring instance variables
private double dimension1;
private double dimension2;
//Parameterized constructor
public TwoDimensionalShape(double x, double y, double dimension1,
double dimension2) {
super(x, y);
this.dimension1 = dimension1;
this.dimension2 = dimension2;
}
// getters and setters
public double getDimension1() {
return dimension1;
}
public void setDimension1(double dimension1) {
this.dimension1 = dimension1;
}
public double getDimension2() {
return dimension2;
}
public void setDimension2(double dimension2) {
this.dimension2 = dimension2;
}
}
___________________
Circle.java
public class Circle extends TwoDimensionalShape {
//Parameterized constructor
public Circle(double x, double y, double radius) {
super(x, y,radius,0.0);
}
//This method will calculate the area of the circle
public double area()
{
return Math.PI*getDimension1()*getDimension1();
}
}
_________________
Rectangle.java
public class Rectangle extends TwoDimensionalShape {
//Parameterized constructor
public Rectangle(double x, double y, double length, double width) {
super(x, y, length, width);
}
//This method will calculate the area of the Rectangle
public double area() {
return getDimension1() * getDimension2();
}
}
_________________
Test.java
public class Test {
public static void main(String[] args) {
Circle c = new Circle(1, 1, 5);
Rectangle r = new Rectangle(1, 1, 4, 3);
System.out.println("Area of the Circle is " + c.area());
System.out.println("Area of the Rectangle is " + r.area());
}
}
________________
Output:
Area of the Circle is 78.53981633974483
Area of the Rectangle is 12.0
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.