File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Inheritance in a problem... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance in a problem..." Watch "Inheritance in a problem..." New topic
Author

Inheritance in a problem...

Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
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

Joined: Nov 07, 2003
Posts: 36
Can anyone help me with this please? Thank you.
Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
Please, someone help me on combining this so that it will work!!! Thank you!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

What's your question exactly?


[Jess in Action][AskingGoodQuestions]
Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
I was able to do different parts of the code separately with the help of my friends, but we do not know how to put the program together. When we combine all of these codes together, like so...we cannot compile. If you can look at the overall program, and maybe arrange them and fix any errors I have, the program might work....please help. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance in a problem...
 
Similar Threads
Polymorphism issue in the test code
code is complete! but whats wrong here??
constructor
the instance variable shapes
Point-Circle-Cylinder