jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Point-Circle-Cylinder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Point-Circle-Cylinder" Watch "Point-Circle-Cylinder" New topic
Author

Point-Circle-Cylinder

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. 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.
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();
}
chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
It's a pretty simple fix try this and see if it's what you wanted. The problem was with having multiple Public class headers. You have all of your class headers declared as Public and Java doesn't like this.
So if you take what you have.

If you re-write it:

everything compiles happily.
Note you'll need to do that for all of your class headers which aren't your main class header.
[ November 30, 2003: Message edited by: chris czinder ]
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
Other solution - make more than one file, each file should
contain only one public class.
It's way that use in real projects.
 
wood burning stoves
 
subject: Point-Circle-Cylinder