-3

I keep getting the error that I need to rename the file, but I don't understand what I'm doing wrong because I picked the past class and named it that. I named it ThreeDShapesDriver.java,

Here's the 4 naming errors I get:

Error: class RightCircularCylinder is public, should be declared in a file named RightCircularCylinder.java

Error: RightCircularCone is public, should be declared in a file named RightCircularCone.java

Error: class Sphere is public. Should be declared in a file named Sphere.java

ThreeDShapesDriver is public, should be declared in a file named ThreeDShapesDriver.java.

public abstract class GeometricThreeDShapes
{
          protected final double pi=3.14;

          //varible pi is delcared as constant

          protected double radius;
          protected double height;

          public GeometricThreeDShapes (double gRadius,double gHeight)  
          {
              //sets radius, height variables to parameter values
              radius=gRadius;
              height=gHeight;
          }

          abstract public double getCircumference ();
          abstract public double getTotalSurfaceArea();
          abstract public double getVolume();
}


public class RightCircularCylinder extends GeometricThreeDShapes    
{   
RightCircularCylinder (double gRadius, double gHeight)  
{   
    //sets radius, height superclass variables to parameter values
    super(gRadius,gHeight);
}   


public double getCircumference()    
{   
    //returns circumference
    return 2*pi*radius;
}       


public double getTotalSurfaceArea() 
{   
//returns area
return 2*pi*radius*(radius+height);
}   


public double getVolume()   
{   
//returns volume
return pi*radius*radius*height;
}   

public void print() 
{   

    //displays circumference, area, volume
    System.out.println ("SA Cylinder: "+getTotalSurfaceArea());
    System.out.println ("Vol Cylinder: "+getVolume());
    System.out.println ("Cir of a Cylinder: "+getCircumference());
}   
}   


public class RightCircularCone extends GeometricThreeDShapes
{   

//data member
protected double length;

RightCircularCone (double gRadius,double gHeight,double gLength)

{
    //sets radius, height superclass variables to parameter values
    super(gRadius,gHeight);

    //set length with parameter value
    length=gLength;
}




public double getCircumference()
{
//returns circumference
return 2*pi*radius;
}

public double getTotalSurfaceArea()
{
//returns area
return pi*radius*(radius+length);
}

public double getVolume()
{
//returns volume
return 0.33*pi*radius*radius*height;
}

public void print() 
{   

    //displays circumference, area, volume
    System.out.println ("Total SA Cone: "+getTotalSurfaceArea());
    System.out.println ("Vol Cone: "+getVolume());
    System.out.println ("Cir of a Cone: "+getCircumference());
}   
}   

public class Sphere extends GeometricThreeDShapes
{   
Sphere(double gRadius,double gHeight)
{
    //sets radius, height superclass variables to parameter values
    super(gRadius,gHeight);
}

public double getCircumference()
{
    //returns circumference
    return 2*pi*radius;
}

public double getTotalSurfaceArea()
{
    //returns area
    return 4*pi*radius;
}

public double getVolume()
{
    //returns volume
    return 1.33*pi*radius*radius*radius;
}

public void print() 
{   

    //displays circumference, area, volume
    System.out.println ("SA of a Sphere: "+getTotalSurfaceArea());
    System.out.println ("Vol of a Sphere: "+getVolume());
    System.out.println ("Cir of a Sphere: "+getCircumference());
}   
}   

public class ThreeDShapesDriver
{   
public static void main(String[] args)
{
    //instigates the cylinder class with initial values
    RightCircularCylinder cylinder=new
    RightCircularCylinder(7.0,14.0);

    //instigates the cone class with initial values
    RightCircularCone cone=new
    RightCircularCone(2.5,9.0,12.0);

    //instigates the sphere class with initial values
    Sphere sphere=new Sphere(3.5,7.0);

    //displays circumference, area, volume of cylinder
    cylinder.print();
    System.out.println("----------------");

    //displays circumference, area, volume of cone
    cone.print();
    System.out.println("----------------");

    //displays circumference, area, volume of sphere
    sphere.print();
    System.out.println("----------------");
}
}     
Honinbo Shusaku
  • 1,411
  • 2
  • 27
  • 45
  • Are you sure, you didn't name it *"GeometricThreeDShapes.java"*? – awesoon Sep 16 '15 at 03:18
  • I did on that last one actually but I've tried every name and I get the same series of errors (interchanged a little bit). Right now I have it named ThreeDShapesDriver.java and I still get 4 naming errors. –  Sep 16 '15 at 03:20
  • It's exactly as the error says, you cannot have more than 1 public class in a file – logee Sep 16 '15 at 03:20
  • Isn't that the whole idea of the extend command? –  Sep 16 '15 at 03:22
  • No, the extend keyword means that the class is inheriting the other class. It does NOT mean that the class is extending the file of the other class. – NoseKnowsAll Sep 16 '15 at 03:27

3 Answers3

3

Separate each class into it's own file and name it by it's Class name.

for ex. there will be a file called GeometricThreeDShapes.java with the class GeometricThreeDShapes in it and so on for the rest of them. don't put them all in the same file.

The extends command means that you are inheriting the other class. In other words that class becomes a child class, accepting all properties of the parent class. This does not mean they get to be in the same file.

Jason Gallavin
  • 403
  • 4
  • 14
3

Every public Java class must be declared in a separate file.

Please refer below question for detail.

Why is each public class in a separate file?

Community
  • 1
  • 1
Sumit Tyagi
  • 431
  • 2
  • 14
0

The error you got tells you exactly what you should do:

Error: class RightCircularCylinder is public, should be declared in a file named RightCircularCylinder.java

Error: RightCircularCone is public, should be declared in a file named RightCircularCone.java

Error: class Sphere is public. Should be declared in a file named Sphere.java

ThreeDShapesDriver is public, should be declared in a file named ThreeDShapesDriver.java.

You have declared more than one class as public within the same file, and this isn't allowed by the Java language. Either remove the public modifier from the other classes and keep only your main class, ThreeDShapesDriver, as public, or else follow what the compiler asks and put each public class in its own file with the appropriate name.

Community
  • 1
  • 1
Jason Bullers
  • 180
  • 1
  • 8
  • I ended up removing all the public's, and I got the error that the public static void main(Strings[] args) needed to be added. So I did that, and then I got 13 errors, all mentioning illegal start of expressions. –  Sep 16 '15 at 03:49
  • I'm not sure what happened there. If all that code listed above is in one file, the only places you need `public` are: `public class ThreeDShapeDriver` and `public static void main(String[] args)` – Jason Bullers Sep 16 '15 at 19:46