Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Second constructor?

843789Oct 15 2009 — edited Oct 22 2009
Hello all,

I?m just doing an online beginners tutorial. Right now I?m sticking to an exercise where I don?t know what to do at all. Hopefully one of you guys is able to help me.
/*
Write a method that evaluates to true or false depending on whether a box completely fits inside another:

public boolean nests( Box outsideBox )

This is potentially a difficult method, since the box may fit or not fit depending on how it is rotated. To simplify the method, write it so that it returns true if the two boxes nest without considering rotations (so height is compared to height, length compared to length, and so on.) 
*/

class Box
{
  // Instance variables
  private double length;
  private double width;
  private double height;

  // Constructors
  Box ( double width, double height, double length )
  {
    this.width  = width;
    this.height = height;
    this.length = length;
  }

  // Methods

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

  private double faceArea()
  {
    return width * height;
  }

  private double topArea()
  {
    return width * length;
  }

  private double sideArea()
  {
    return height * length;
  }

  public double area()
  {
    return 2 * faceArea() + 2 * topArea() + 2 * sideArea() ;
  }

  public double width()
  {
    return width;
  }

  public double height()
  {
    return height;
  }

  public double length()
  {
    return length;
  }

//  public boolean nests( Box outsideBox )
//  {
//    return ?????
//  }

}

class BoxTester5
{

  public static void main ( String[] args )
  {
    // create a 1. box
    Box box1 = new  Box( 2.5, 3.0, 5.0 );
    System.out.println( "Length: " + box1.length() + " Width: " + box1.width() +
                        " Height: " + box1.height()  );
    System.out.println( "Surface area: " + box1.area() + " Volume: " + box1.volume()  );
    System.out.println();

    // create a 2. box
    Box box2 = new  Box( 5.3, 1.4, 11.7 );
    System.out.println( "Length: " + box2.length() + " Width: " + box2.width() +
                        " Height: " + box2.height()  );
    System.out.println( "Surface area: " + box2.area() + " Volume: " + box2.volume()  );
    System.out.println();
  }

}
I roughly understand that I have to construct a new Box-object and compare it within a new method. But this time I?m totally lost?

No need to write the whole program. Just a short explanation on how to proceed would be great. I appreciate your assistance!

Regards
WRE
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 19 2009
Added on Oct 15 2009
12 comments
382 views