Task 1 We want to define our Superclass ‘Shape’, which requires two instance var
ID: 3714899 • 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
package inheritanceEx;
class Shape
{
int x,y;
public Shape(int x2, int y2) {
this.x= x2;
this.y= y2;
// TODO Auto-generated constructor stub
}
public void setX(int x)
{
this.x=x;
}
public void setY(int y)
{
this.y=y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
}
class TwoDimensionalShape extends Shape
{
int dimension1,dimension2;
public TwoDimensionalShape(int x,int y,int dimension1,int dimension2)
{
super(x,y);
this.dimension1 = dimension1;
this.dimension2 = dimension2;
}
public void setDimension1(int dimension1)
{
this.dimension1=dimension1;
}
public void setDimension2(int dimension2)
{
this.dimension2=dimension2;
}
}
class Circle extends TwoDimensionalShape
{
double pi=3.17;
//int radius;
public Circle(int x,int y,int radius)
{
super(x,y,radius,0);
}
double area()
{
return pi*getRadius()*getRadius();
}
public void setRadius(int radius)
{
radius=radius;
}
int getRadius()
{
return getRadius();
}
}
class Rectangle extends TwoDimensionalShape
{
public Rectangle(int x,int y,int length,int width)
{
super(x,y,length,width);
}
double area()
{
return getLength() * getWidth();
}
public void setLength(int length)
{
length=length;
}
int getLength()
{
return getLength();
}
public void setWidth(int width)
{
width=width;
}
int getWidth()
{
return getWidth();
}
}
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());
}
}
I have run it but getting error Could not find or load class. But you may not get it if your class path is OK.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.