Hi am working on a shape inheritance hierarchy and am having trouble with this particular class I have two errors that state class, interface, or enum expected: on the lines indicated in red everything else seems fine I have attached the whole project If you need to check out other parts to it. Here is my code for this particular class

// Exerciae 10.9: SphereShape.java
// Sphere Shape class extends ThreeDimensionalShape.

public class SphereShape extends ThreeDimensionalShape
{
   private double radius; // sphere radius

   // constructor
   public SphereShape(double radius )
   {
      super("SphereShape");
	  this.radius = radius;

      double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 );

   } // end method sphereVolume
   } // end constructor

  // return String representation of Sphere object

   public String toString()
   {
      return String.format( "This is a sphere. It's radius = %f. It's volume = %f\n", radius, volume);
      
} // end method toString
} // end class SphereShape

Dani AI

Generated

A quick note that expands on the replies from and : the immediate compile message "class, interface, or enum expected" was caused by the extra closing brace — removing it fixes that error. The posted code also contains a separate logic/scoping issue worth addressing: a local volume variable declared inside the constructor cannot be referenced from toString(), so that will produce a "cannot find symbol" error or similar when the code is changed.

Two simple, safer patterns avoid that problem:

  • Keep only radius as the object's stored state and compute volume on demand with a getVolume() method (preferred unless caching is needed).
  • Or store volume as a private field (e.g., final) computed in the constructor if caching is required.

A compact, idiomatic implementation using the first approach:

public class SphereShape extends ThreeDimensionalShape {
    private final double radius;

    public SphereShape(double radius) {
        super("SphereShape");
        this.radius = radius;
    }

    public double getVolume() {
        return (4.0 / 3.0) * Math.PI * radius * radius * radius;
    }

    @Override
    public String toString() {
        return String.format("Sphere: radius=%.3f, volume=%.3f", radius, getVolume());
    }
}

Additional practical tips: enable an IDE’s auto-format/brace-matching to spot stray braces quickly; use @Override so signature mistakes on toString() are caught; prefer radius*radius*radius to Math.pow(radius, 3) for simple cubes (micro-optimization and clarity); avoid embedding a newline inside toString(); and keep end-of-block comments accurate (misleading comments like "// end method sphereVolume" hide the real structure and make debugging harder). Thanks to and for the helpful diagnostics.

Recommended Answers

All 4 Replies

You need to remove: } // end method sphereVolume.
This is an extra brace which is causing the error.

let me know if it fix the issue

} // end method sphereVolume
   } // end constructor

There's no method "sphereVolume", so one of these ends the constructor and the other ends the class.

A Taimoor Rana I removed the extra } and the error is gone. Thanks

you are welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.