Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Point-Circle-Cylinder

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other solution - make more than one file, each file should
contain only one public class.
It's way that use in real projects.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic