Modify the code to use inheritance


Assignment:

Modify the code to use inheritance, an interface, and aggregation by completing the following steps:

1. In the Shape class, eliminate the concrete methods, area() and perimeter(); create an instance variable of type String called name; and make a constructor that requires a String, shapeName, as the name of the new Shape object. Keep the method getLocation(), and keep the Shape class abstract.

2. Create an interface, Shape2D, which will require the methods, area() and perimeter(), which were elimated from Shape.

3. Create a new class, Point, which inherits from Shape and contains two private instance variables of type int, x and y, which store the coordinates for the Point. Point should have both a default constructor and a constructor that takes two int values for the coordinates x and y. For the default constructor, set both x and y to zero.

4. Modify the class, Circle, to inherit from Shape as well as use the interface, Shape2D. Circle should have only two private instance variables: a double named, radius, and a Point object reference named, center. Circle should have two constructors, a default constructor and a constructor that takes a double as a new radius value and two ints as the x and y coordinates of the center. Circle does not store the x and y values as ints, but instead creates a Point object with these coordinates. The location of a Circle is the location of its center, Point.

5. Use the TestCircle.java program to test your classes until they are error-free .import java.text.DecimalFormat;

public class TestCircle
{
public static void main(String[] args)
{
DecimalFormat showTwoDecimals = new DecimalFormat( "0.00" );
Circle c1 = new Circle(2.5, 22, 44);
double newRadius = 6.0;

System.out.println("This shape is a " + c1.getName() +
"\nlocated at " + c1.getLocation() +
"\nRadius is " + c1.getRadius());

System.out.println("The shape area is: " + showTwoDecimals.format(c1.area())+"\n");

System.out.println("The shape perimeter (circumference) is: " + showTwoDecimals.format(c1.perimeter())+"\n");

System.out.println("After changing the radius to "+newRadius);
c1.setRadius(newRadius);
System.out.println(", it is a"+ c1 + "\nArea is " + showTwoDecimals.format(c1.area())+"\n");
System.out.println("The shape perimeter (circumference) is: "+ showTwoDecimals.format(c1.perimeter())+"\n");

}
}

public class Circle extends Shape
{
private int x=0, y=0; // coordinates of the center
private double radius;

// Constructors
public Circle()
{
setRadius(0);
}

public Circle(double r, int x, int y)
{
//x = x;
this.x=x;
//y = y;
this.y=y;
setRadius(r);
}

// Get radius of Circle
public double getRadius()
{
return radius;
}

// Set radius of Circle
public void setRadius(double r)
{
radius = ( r >= 0 ? r : 0 );
}

// Calculate area of Circle
public double area()
{
return Math.PI * radius * radius;
}

// convert the Circle to a String
public String toString()
{
return "Circle with center at (" + x + ", " + y + ")" +
" and radius = " + radius;
}

// return the class name
public String getName()
{
return "Circle";
}

// return the center location
public String getLocation()
{
//return "X = " + X + ", Y = " + Y;
return "X = " + x + ", Y = " + y;
}
}

abstract public class Shape
{
public double area()
{
return 0.0;
}

public double perimeter()
{
return 0.0;
}

abstract public String getName();
abstract public String getLocation();
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Modify the code to use inheritance
Reference No:- TGS01935214

Now Priced at $25 (50% Discount)

Recommended (92%)

Rated (4.4/5)