Question

package hw2; public class SandBox {    private final double length;    // doesn't change after SandBox...

package hw2;

public class SandBox {

   private final double length;    // doesn't change after SandBox is constructed  

   private final double width; // doesn't change after SandBox is constructed

   private final double height;// doesn't change after SandBox is constructed  

   // current height of the sand within the box

   private double sandHeight; 

  

    /*

     * Constructor

     * Initially there is no sand in this SandBox

     */

    public SandBox(double length, double width, double height){

    }

   

    // Constructor

// Creates a SandBox with all dimensions 1

    // Initially there is no sand in this SandBox

    public SandBox(){

    }

// getLength: returns the length of this SandBox

// getWidth: returns the width of this SandBox

// getHeight: returns the height of this SandBox

// getVolume: returns the total volume of this SandBox

// getSandVolume: returns the volume of sand in this SandBox

   

    // toString: returns a String describing this SandBox

    //   for example, for the SandBox with length=10, width=12, height=2, sandHeight=1

    //   the String will be "SandBox(volume=240, sandVolume=120)"

@Override

    public String toString() {

        return "SandBox(volume=" + getVolume() +", sandVolume=" + getSandVolume() + ")";

    }

    // scoopSandFrom

    // Move sand from other into this SandBox.

    // If you would overflow this SandBox, *only* move the volume

    // of sand that would fill this SandBox to the top.

    public void scoopSandFrom(SandBox other){

// Don't remove these lines. They help you find errors more quickly

// when debugging.

this.checkState();

other.checkState();

    }

private void checkState() {

if (this.sandHeight > this.height) { throw new IllegalArgumentException("SandBox overflowed!"); }

if (this.sandHeight < 0) { throw new IllegalArgumentException("SandBox contains anti-sand!"); }

}

    // completely fills this SandBox with sand

    public void fillToTop() {

    }

}

here is the code on java modify it to work in such a way that output should be

ERROR: addSandBox
First box...
SandBox(volume=4.0, sandVolume=4.0)
SandBox(volume=3.0, sandVolume=3.0)
SandBox(volume=16.0, sandVolume=1.0)
source: SandBox(volume=8.0, sandVolume=0.0)
...First test passed

Second box...
SandBox(volume=4.0, sandVolume=4.0)
SandBox(volume=3.0, sandVolume=3.0)
SandBox(volume=16.0, sandVolume=16.0)
source: SandBox(volume=72.0, sandVolume=57.0)
...Second test passed

Third box...
SandBox(volume=4.0, sandVolume=4.0)
SandBox(volume=27.0, sandVolume=2.0)
source: SandBox(volume=2.0, sandVolume=0.0)
...Third test passed

Poking holes in the SandBoxes!
Time=0.5: SandBox(volume=4.0, sandVolume=3.0) SandBox(volume=27.0, sandVolume=1.75) SandBox(volume=1000.0, sandVolume=0.0)
Time=1.0: SandBox(volume=4.0, sandVolume=2.0) SandBox(volume=27.0, sandVolume=1.5) SandBox(volume=1000.0, sandVolume=0.0)
Time=1.5: SandBox(volume=4.0, sandVolume=1.0) SandBox(volume=27.0, sandVolume=1.25) SandBox(volume=1000.0, sandVolume=0.0)
Time=2.0: SandBox(volume=4.0, sandVolume=0.0) SandBox(volume=27.0, sandVolume=1.0) SandBox(volume=1000.0, sandVolume=0.0)
Time=2.5: SandBox(volume=4.0, sandVolume=0.0) SandBox(volume=27.0, sandVolume=0.75) SandBox(volume=1000.0, sandVolume=0.0)
Time=3.0: SandBox(volume=4.0, sandVolume=0.0) SandBox(volume=27.0, sandVolume=0.5) SandBox(volume=1000.0, sandVolume=0.0)
Time=3.5: SandBox(volume=4.0, sandVolume=0.0) SandBox(volume=27.0, sandVolume=0.25) SandBox(volume=1000.0, sandVolume=0.0)
Time=4.0: SandBox(volume=4.0, sandVolume=0.0) SandBox(volume=27.0, sandVolume=0.0) SandBox(volume=1000.0, sandVolume=0.0)
Time=4.5: SandBox(volume=4.0, sandVolume=0.0) SandBox(volume=27.0, sandVolume=0.0) SandBox(volume=1000.0, sandVolume=0.0)
Time=5.0: SandBox(volume=4.0, sandVolume=0.0) SandBox(volume=27.0, sandVolume=0.0) SandBox(volume=1000.0, sandVolume=0.0)

like this

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package hw2;

public class SandBox {

   private final double length; // doesn't change after SandBox is constructed
   private final double width; // doesn't change after SandBox is constructed
   private final double height;// doesn't change after SandBox is constructed
   // current height of the sand within the box
   private double sandHeight;

   /*
   * Constructor Initially there is no sand in this SandBox
   */
   public SandBox(double length, double width, double height, double sandHeight) {
       this.length = length;
       this.width = width;
       this.height = height;
       this.sandHeight = sandHeight;
   }

   // Constructor
   // Creates a SandBox with all dimensions 1
   // Initially there is no sand in this SandBox
   public SandBox() {
       length = width = height = 1;
   }

   // getLength: returns the length of this SandBox
   public double getLength() {
       return length;
   }
  
   // getWidth: returns the width of this SandBox
   public double getWidth() {
       return width;
   }
  
   // getHeight: returns the height of this SandBox
   public double getHeight() {
       return height;
   }
  
   // getVolume: returns the total volume of this SandBox
   public double getVolume() {
       return length * width * height;
   }
  
   // getSandVolume: returns the volume of sand in this SandBox
   public double getSandVolume() {
       return length * width * sandHeight;
   }

   // toString: returns a String describing this SandBox
   // for example, for the SandBox with length=10, width=12, height=2,
   // sandHeight=1
   // the String will be "SandBox(volume=240, sandVolume=120)"
   @Override
   public String toString() {
       return "SandBox(volume=" + getVolume() + ", sandVolume="
               + getSandVolume() + ")";
   }

   // scoopSandFrom
   // Move sand from other into this SandBox.
   // If you would overflow this SandBox, *only* move the volume
   // of sand that would fill this SandBox to the top.
   public void scoopSandFrom(SandBox other) {
       // Don't remove these lines. They help you find errors more quickly
       // when debugging.
       this.checkState();
       other.checkState();
   }

   private void checkState() {
       if (this.sandHeight > this.height) {
           throw new IllegalArgumentException("SandBox overflowed!");
       }
       if (this.sandHeight < 0) {
           throw new IllegalArgumentException("SandBox contains anti-sand!");
       }
   }

   // completely fills this SandBox with sand
   public void fillToTop() {
       this.sandHeight = height;
   }
  
   //main method to test with different dimension values
   public static void main(String[] args) {
       SandBox sb = new SandBox();
       System.out.println(sb.toString());
      
       System.out.println("\nFirst box...");
       SandBox sb1 = new SandBox(2,2,1,1);
       System.out.println(sb1.toString());
       SandBox sb11 = new SandBox(3,1,1,1);
       System.out.println(sb11.toString());
       SandBox sb111 = new SandBox(1,1,16,1);
       System.out.println(sb111.toString());
      
       System.out.println("\nSecond box...");
       SandBox sb2 = new SandBox(2,2,1,1);
       System.out.println(sb2.toString());
       SandBox sb22 = new SandBox(3,1,1,1);
       System.out.println(sb22.toString());
       SandBox sb222 = new SandBox(1,1,16,16);
       System.out.println(sb222.toString());
       SandBox sb2222 = new SandBox(3,1,24,19);
       System.out.println(sb2222.toString());
      
       System.out.println("\nThird box...");
       SandBox sb3 = new SandBox(2,2,1,1);
       System.out.println(sb3.toString());
       SandBox sb33 = new SandBox(2,1,13.5,1);
       System.out.println(sb33.toString());
       SandBox sb333 = new SandBox(2,1,1,0);
       System.out.println(sb333.toString());
   }
}

Add a comment
Know the answer?
Add Answer to:
package hw2; public class SandBox {    private final double length;    // doesn't change after SandBox...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following class: public class NumericValue { private double d; private static final String theValuekindName...

    Consider the following class: public class NumericValue { private double d; private static final String theValuekindName = new String("Numeri public NumericValue() { d =0.0; } public NumericValue (double x) { d = x; } // Indicates what kind of value this is. For any two values, v1 and v2 // v1.valuekind() == V2.valuekind() if and only if they are of the // same kind (e.g., two numeric values). The actual character string // pointed to by valuekind() may be anything,...

  • Directions: Follow the instructions below to explore sorting objects using boxes below. You will sort the...

    Directions: Follow the instructions below to explore sorting objects using boxes below. You will sort the boxes by the largest volume. Copy and Paste the Box method below into BlueJ public class Box { private double length, height, depth;    public Box( double length, double height, double depth ) { this.length = length; this.height = height; this.depth = depth; }    public double volume() { return length*height*depth; } // compare this Box to another Box int compareTo( Box other )...

  • departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE =...

    departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee;    } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...

  • ***USING JAVA. MUST USE A SWITCH STATEMENT. THANK YOU. Fall 2019 - Homework #3: Shipping Rates...

    ***USING JAVA. MUST USE A SWITCH STATEMENT. THANK YOU. Fall 2019 - Homework #3: Shipping Rates A client has hired your firm to implement their shipping rate rules as a Java program they can easily run. They have provided the following business rules to you. For each category, a cost and shipping time is added, as noted: Box Volume Categories: Category 1: 0.0 to 0.5 cubic feet, adds $5 to cost. Category 2: 0.5 to 2.0 cubic feet, adds $10...

  • Please USE JAVA only Write a class named InputSplitter. This class will take input from the...

    Please USE JAVA only Write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens into integers, floating point numbers, and strings. The incoming tokens will be added to one of three accumulators which start out at zero, and which keep a running total. Thus, the class will have an accumulator for integers. It starts out with the value zero. Every time another integer is read in, it is added...

  • you must implement public class Student by following the instructions as follows exactly. You may reference...

    you must implement public class Student by following the instructions as follows exactly. You may reference Chapter 7 for the similar examples to get the ideas and concepts. Each student must have the following 5 attributes (i.e., instance variables): private String   studentID ;              // student’s ID such as                         1 private String lastName   ;                         // student’s last name such as              Doe private String firstName ;             // student’s first name such as            John private double gpa...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT