public class Shape {
protected String color;
protected double surfaceArea;
protected double volume;
protected double pi = Math.PI;
boolean end = false;
public Shape(String a) {
color = a;
}
public String color(String color) {
this.color = color;
return this.color;
}
public double surfaceArea(double surfaceArea) {
this.surfaceArea = surfaceArea;
return this.surfaceArea;
}
public double volume(double volume) {
this.volume = volume;
return this.volume;
}
// Get the name of the class
public String getThisClass() {
String thisClassString = this.getClass().toString();
String newString = "";
char character;
boolean start = false;
for (int i = 0; i<thisClassString.length(); i++) {
character = thisClassString.charAt(i);
if(character == ' ') {
start = true;
}
else if(start == true) {
newString = newString + character;
}
else {
// do nothing -- continue the loop
}
}
return newString;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
// Get the name of the class (or the name of the shape that we are working with)
String thisClassIs = getThisClass();
// For a sphere
if ((thisClassIs).equals("sphere")) {
end = true;
return "SPHERE\nVolume: " + this.volume + "\nColor: " + this.color + "\nSurface Area: " + this.surfaceArea;
}
// For a cube
else if((thisClassIs).equals("Cube") && end == false) {
return "\n\nCUBE\nVolume: " + this.volume + "\nColor: " + this.color + "\nSurface Area: " + this.surfaceArea;
}
else {
return "Volume: " + this.volume + "\nColor: " + this.color + "/nSurface Area: " + this.surfaceArea;
}
}
}