I am writing a box program, with various extras. I understand that proper coding requires properties to be private, but I now need to add to this program getTotalEdgeLength, getSurfaceArea, and getVolume methods. Would I put these methods in the main class? I do not know a way to only use "get" functions and keep the properties private instead of making them public and using "getters" in my class. Any solutions on how to keep my properties private, but use getters for TotalEdgeLength, etc. Thanks, here is my class so far:

private class Box {
private Double length = 0.0;
private Double width = 0.0;
private Double height = 0.0;
public Box(Double boxlen, Double boxwid, Double boxhei)
{
    length = boxlen;
    width = boxwid;
    height = boxhei;
}
public void setLength(Double boxlen)
{
    length = boxlen;
}
public Double getLength()
{
    return length;
}
public void setWidth(Double boxwid)
{
    width = boxwid;
}
public Double getWidth()
{
    return width;
}
public void setHeight(Double boxhei)
{
    height = boxhei;
}
public Double getHeight()
{
    return height;
}
}

Recommended Answers

All 2 Replies

I think those methods belong in the Box class itself. For example,

private class Box {
      private double length = 0.0;
      private double width = 0.0;
      private double height = 0.0;

      public Box(double boxlen, double boxwid, double boxhei)
      {
        length = boxlen;
        width = boxwid;
        height = boxhei;
      }

      public void setLength(Double boxlen)
      {
        length = boxlen;
      }

      public double getLength()
      {
        return length;
      }

      public void setWidth(Double boxwid)
      {
        width = boxwid;
      }

      public double getWidth()
      {
        return width;
      }

      public void setHeight(Double boxhei)
      {
        height = boxhei;
      }

      public double getHeight()
      {
        return height;
      }

      public double getVolume()
      {
        return length * height * width;
      }
}

After all, volume, surface area and total edge length are all properties of the Box, not of a Main.

Also notice that I have spelt double with a lower case 'd'. You are not using the class Double, but the primitive double, to store the values of height, width and length.

thanks, sometimes the answer is so obvious that you don't even realize it, ive spend hours on this, but now it makes so much sense, thanks for your help. :D

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.