Computer Engineering Department

Bilkent University

CS101: Algorithms and Programming I

Assignment No. 11

April 10, 2002

 

Notes: 1. Please READ the assignment.  2. Always bring your textbook to the lab for reference purposes.  3. Study the programs before coming to the lab and try to solve them as much as possible.  Keep them in your H drive so that it would be easy to access them in the labs.  You may also keep them in diskette.  4. Your assistants may slightly change the programs as you work on them.

 

A. Purpose of Assignment

More experience with object-oriented programming.

 

B. Description of Assignment

Implement the Box and Ball classes as defined below and test all of their methods.

 

Box and Ball Objects.

Implement the following Box Class:  (implementation of some of the methods are given). 

Note that you will implement all of the classes of this assignment inside the same project; therefore, there is no need to use “import” for them.

 

public class Box

{

    private double height;

    private double length;

    private double width;

    private String type;

 

    public Box()  // sets all dimensions equal to 1 and sets the type as “Heavy Duty”

    public Box(double h, double l, double w, String t)  // sets height= h, and so on

    public double volume( )  // returns the volume of box

    public double area( )

    public boolean equals(Box otherBox) // if all the dimensions and types are the sme returns true

    public boolean areasEqual(Box otherBox) // returns true if areas of the boxes are the same

    {

        return( this.area( ) == otherBox.area( ) );

    }

    public boolean volumesEqual(Box otherBox)

    public boolean fitsInside(Box otherBox) // returns true if all of the length of this box is smaller than

                                                                           // the length of otherBox and width of this box is smaller ...

    public double getLength( ) // returns length of box

    public double getWidth( )

    public double getHeight( )
    public String getType( )

    public void displayBoxInfo( ) // display width, length, height, typeof box

    public boolean volumesEqual(Ball aBall, double threshold) // returns true if the volume difference

//  between box and aBall are <= threshold

    {

        return( Math.abs(this.volume( ) - aBall.volume( )) <= threshold );

    }

   public boolean areasEqual(Ball aBall, double threshold)  // returns true if the area difference

                                // between box and aBall <= threshold

}

 

 

Public Class Ball

{

    private double radius;

    private String type;

 

    public Ball( )  // sets radius as 1 and type as “Football”

    public Ball(double r, String t)    // sets radius as r and type as t

    public double volume( )

    {

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

        return(result);

    }

    public double area( )

    public boolean equals(Ball otherBall)  // returns true if radius and type of this Ball and

                                                                     // otherBall have thesamevalues

    public boolean fitsInside(Ball otherBall) // if this ball fits inside otherBall returns true

                                                                    // decided by checking ther adius values

   public double getRadius( )

   public String getType( )

   public void displayBallInfo( )  // display radius and type information

   public boolean volumesEqual(Box aBox, double threshold) 

    {

        return( Math.abs(this.volume( ) - aBox.volume( )) <= threshold );

    }

   public boolean areasEqual(Ball aBox, double threshold)

}

 

 

 

C. When and How to Submit Your Work

Make sure that your programs are all seen and approved by your assistant.  In this assignment hard coded main( ) method is acceptable (i.e., if there is no user interaction it is OK); however, you have to make sure that each method of both classes are tested at least once.  An incomplete tester class is given in the following box, please modify and use it as needed.

 

public class testBoxBall

{

    public static void main(String [ ] args)

    {

        Box myBox= new Box(1.0, 2.0, 3.0, "Packaging");

        myBox.displayBoxInfo( );

        System.out.println("Volume of myBox: " + myBox.volume( ));

        System.out.println("Area of myBox: " + myBox.area( ));

        System.out.println("Width of myBox: " + myBox.getWidth( ));

        System.out.println("Length of myBox: " + myBox.getLength( ));

        System.out.println("Height of myBox: " + myBox.getHeight( ));

 

        Box yourBox= new Box(.5, 1.5, 2.5, "Heavy Duty");

        yourBox.displayBoxInfo( );

        System.out.println("Volume of yourBox: " + yourBox.volume( ));

        System.out.println("Area of yourBox: " + yourBox.area( ));

 

        if(yourBox.fitsInside(myBox))

            System.out.println("yourBox fits inside my box.");

        else

            System.out.println("yourBox does not fit inside myBox.");

   

        //===================================================================

 

        Ball herBall= new Ball(2, "Football");

        herBall.displayBallInfo( );

        System.out.println("Area of herBall: " + herBall.area( ));

 

        if(myBox.areasEqual(herBall, 100.0))

            System.out.println("Areas of myBox and herBall are the same.");

        else

            System.out.println("Areas of myBox and herBall are different.");

 

    }  

}