Todd
Todd

Reputation: 129

Calculating volume and surface area using java

I am working on a project for my Java I class. I have included the program I’ve written. My formula seems to be working but my output isn’t. This is the project- “Write a class called Sphere that contains instance data that represents the sphere's diameter. Define the sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface are of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called Multisphere, whose main method instantites and updates several Sphere objects.” Here’s what I have written:

public class Sphere 
{

  private double diameter;
  private double calcVol;
  private double calcSA;


  //----------------------------------------------------------------------------------------------
  //Constructor
  //----------------------------------------------------------------------------------------------

  public Sphere(double diameter)
  {
    this.diameter = diameter;
  }
  public void setDiameter(double diameter)
  {
    this.diameter = diameter;
  }
  public double getDiameter(double diameter)
  {
    return diameter;
  }
  public double calcVol()
  {
    return ((Math.PI) * (Math.pow(diameter, 3.0) / 6.0));   
  }
  public double calcSA()
  {
    return ((Math.PI) * Math.pow(diameter, 2.0));   
  }
  public String toString()
  {
    return "Diameter: " + diameter + " Volume: " + calcVol + " Surface Area: " + calcSA;
  }
}

public class MultiSphere 
{

  public static void main(String[] args) 
  {


    Sphere sphere1 = new Sphere(6.0);
    Sphere sphere2 = new Sphere(7.0);
    Sphere sphere3 = new Sphere(8.0);d



    sphere1.calcVol();
    sphere2.calcVol();
    sphere3.calcVol();

    sphere1.calcSA();
    sphere2.calcSA();
    sphere3.calcSA();

    System.out.println(sphere1.toString());

    System.out.println(sphere2.toString());

    System.out.println(sphere3.toString());
  }
}

Upvotes: 0

Views: 2070

Answers (2)

Łukasz
Łukasz

Reputation: 8673

private double calcVol;
private double calcSA;

these are lines you should remove, you declared new fields that have the same names as methods that you also have.

In toString you should call your methods like this

return "Diameter: " + diameter + " Volume: " + calcVol() + " Surface Area: " + calcSA();

also in your main() you have an extra d in the end of this line

Sphere sphere3 = new Sphere(8.0);d 

Upvotes: 0

christopher
christopher

Reputation: 27356

Include methods that calculate and return the volume and surface are of the sphere.

This is an important line in your homework assignment. There is no mention of any internal state for the volume and the surface area of the sphere, so keeping field values for this doesn't make sense. Your methods are correct, but your toString should just call those methods:

public String toString()
{
    return "Diameter: " + diameter + " Volume: " + calcVol() + " Surface Area: " + calcSA();

}

That way you don't need to call the methods first and your toString will always represent the up to date surface area and volume, should the diameter change.

Upvotes: 1

Related Questions