d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Having Trouble With A Professor's Assignment > Abstract Class Concept In Java
Add Reply New Topic New Poll
Member
Posts: 30,307
Joined: Sep 12 2008
Gold: 119,438.96
Sep 21 2017 08:39pm
Write a simple GUI application that allows users to resize an object. For simplicity, the object is a circle only.

You will write:
An interface called GeometricObject, which declares two abstract methods: getPerimeter() and getArea().
The abstract class Circle (with an obvious constant field), which implements the interface GeometricObject.
The interface Resizable, which declares an abstract method resize, which modifies the dimension of an object (such as the radius of a circle) by the given percentage.
The class ResizableCircle, which is a subclass of the class Circle and implements Resizable interface.
Write a GUI tester/driver program called TestResizableCircle that allows user interactively resize two different circle objects and tests the methods defined in ResizableCircle. The user should be able to draw the circles and then resize them by moving the mouse of setting the new size for the circle.
All classes should be complete, i.e. all necessary methods, which includes toString and equals.

According to the prompt, class Circle is supposed to be abstract, but according to other requirements, I can't seem to understand why Circle would need to be declared abstract. I can't think of a single abstract method that can be passed to ResizableCircle.

Any idea?
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 23 2017 03:06am
A Circle is the abstract representation of a GeometricObject. A ResizableCircle is a conceptual Circle. It is a concrete idea.

Let's think abstractly. We a concept that we want to interact with. That concept is Geometry. In order to interact with something, there needs to be an interface which bridges the gap between concrete and concept. This interface tells us how we can interact. We want to be able to interact with a GeometricObject to determine the different sizes this shape can be.

So let's define that interface to give us our interaction point
Code

//An interface called GeometricObject,
//which declares two abstract methods: getPerimeter() and getArea().
public interface GeometricObject {
double getPerimeter();
double getArea();
}


Now that we have that, we need something to interact with. It is decided that we will interact with Circles. But circles are not real. They are abstract concepts. Nothing exists as a circle. Real world things represent the abstract concept of a circle.

A Circle is an abstraction that can be interacted with as a GeometricObject because we have defined a Circle to be something that holds a shape based on a "radius" in relation to this constant PI.
Code

//The abstract class Circle (with an obvious constant field),
//which implements the interface GeometricObject.
public abstract class Circle implements GeometricObject {

protected final double PI = 3.14;
protected double radius;

public Circle(double radius){
this.radius = radius;
}

@Override
public double getPerimeter() {
return 2 * PI * radius;
}

@Override
public double getArea() {
return PI * radius * radius;
}
}



There can be many different representations of a Circle. The Circle we wish to represent is one which can be resized. So we have a second interaction point which needs another interface to bridge the gap between the concept of resizing and the actual model which is resizing

Code

//The interface Resizable,
//which declares an abstract method resize,
//which modifies the dimension of an object
//(such as the radius of a circle) by the given percentage.
public interface Resizable {
void resize(double percentage);
}


Now we can create our concrete idea: A GeometricObject which is Resizable in the form of a Circle
Code
//The class ResizableCircle, which is a subclass of the class Circle
//and implements Resizable interface.
public class ResizableCircle extends Circle implements Resizable {

public ResizableCircle(double radius) {
super(radius);
}

@Override
public void resize(double percentage) {
radius *= (1+percentage);
}
}


Just because Circle doesn't have extra abstract methods for Resizable to implement, doesn't mean it isn't an abstract class. A Circle maintains state in the form of a radius. But a Circle on its own is rather meaningless until you decide what is representing this Circle (in our case its the Resizable variant).

Good luck on the GUI part.

Also hello
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll