This week's book giveaway is in the Cloud/Virtualizaton forum.We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Inheritance in a problem...

Kelsey kelskjs
Ranch Hand
Posts: 36
Hello. I have this problem that I am working on. I have to re-write a Point-Circle-Cylinder program into a Point-Circle-Cube program.
Here is all the code that I have developed. How do I combine it in order to run this program using all the code? I am confused as to how to put it together so it will work...
Thanks...
// Exercies 9.11 Solution
// Test.java
// Driver for point, square, cube hierarchy
import javax.swing.*;
public class Test {
public static void main( String args[] )
{
Point point = new Point( 7, 11 );
Square square = new Square( 3.5, 22, 8 );
Cube cube = new Cube( 3.3, 10, 10 );
Shape[] arrayOfShapes = new Shape[ 3 ];
String result = "";
arrayOfShapes[ 0 ] = point;
arrayOfShapes[ 1 ] = square;
arrayOfShapes[ 2 ] = cube;

result += point.getName() + ": " +
point.toString();

result += "\n" + square.getName() + ": " +
square.toString();

result += "\n" + cube.getName() + ": " +
cube.toString();

for ( int i = 0; i < 3; i++ ) {
result += "\n" + arrayOfShapes[ i ].getName() +
": " + arrayOfShapes[ i ].toString();
result += "\n" + "Area = " +
arrayOfShapes[ i ].area();
result += "\n" + "Volume = " +
arrayOfShapes[ i ].volume();
}
JOptionPane.showMessageDialog(
null, result, "Shapes",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}

// Exercies 9.11 part B solution
// Point.java
// Definition of class Point
public class Point extends Shape {
protected double x, y;

public Point( double a, double b ) { setPoint( a, b ); }
public void setPoint( double a, double b )
{
x = a;
y = b;
}

public double getX() { return x; }
public double getY() { return y; }
public String toString()
{ return "[" + x + ", " + y + "]"; }

public String getName() { return "Point"; }
}
// Exercies 9.11 part B solution
// Square.java
// Definition of class Square
public class Square extends Point {
protected double side;
public Square()
{ this( 0.0, 0.0, 0.0 ); }

public Square( double s, double a, double b )
{
super( a, b );
setSide( s );
}
public void setSide( double s )
{ side = ( s >= 0 ? s : 0 ); }
public double getSide() { return side; }

public double area() { return Math.pow( side, 2 ); }
public String toString()
{ return "Corner = " + super.toString() +
"; side = " + side; }
public String getName() { return "Square"; }
}
// Exercies 9.11 part B solution
// Cube.java
// Definition of class Cylinder
public class Cube extends Square {
private double depth;
public Cube( double s, double a, double b )
{
super( s, a, b );
depth = s;
}

public double area() { return super.area() * 6; }

public double volume() { return super.area() * depth; }

public String toString()
{ return super.toString() + "; depth = " + depth; }
public String getName() { return "Cube"; }
}
// Exercies 9.11 part B solution
// Shape.java
// Definition of abstract base class Shape
public abstract class Shape {
public double area() { return 0.0; }
public double volume() { return 0.0; }
public abstract String getName();
}

Kelsey kelskjs
Ranch Hand
Posts: 36
Can anyone help me with this please? Thank you.

Kelsey kelskjs
Ranch Hand
Posts: 36
Please, someone help me on combining this so that it will work!!! Thank you!

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24211
35