Question: /* * AP CS MOOC * Term 2 - Assignment 2, Part 1: Boxcar * A class which represents a single car on a freight
/* * AP CS MOOC * Term 2 - Assignment 2, Part 1: Boxcar * A class which represents a single car on a freight train. */
public class Boxcar { // Variables that will be initialized in the Boxcar constructors. private String cargo = ""; private int numUnits; private boolean repair;
// Default constructor that sets the boxcar to "gizmos", 5, and false. public Boxcar() { /* missing code */ }
// This constructor sets the cargo variable to the parameter c, but only if // c is "gizmos", "gadgets", "widgets", or "wadgets". The constructor ignores // the case of of the value in c. If c holds any value other than // "gizmos", "gadgets", "widgets", or "wadgets", the constructor sets cargo // to "gizmos". The numUnits variable is set to the parameter u. If u is less than // 0 or higher than the maximum capacity of 10 units, numUnits is set to 0. The repair // variable is set to the parameter r. If repair is true, numUnits is set to 0 // no matter what value is stored in the u parameter. public Boxcar(String c, int u, boolean r) { /* missing code */ }
// The toString method returns a String with the Boxcar in the format: // 5 gizmos in service // 10 widgets in service // 0 gadgets in repair // // Notice there is one space in between the number of units and the cargo // and a tab between the value for cargo and "in repair"/"in service" public String toString() { /* missing code (don't forget to update the return statement) */ return ""; }
// This method adds 1 to the number of units in the BoxCar. The maximum // capacity of a boxcar is 10 units. If increasing the number of units // would go beyond the maximum, keep numUnits at the max capacity. // If the repair variable is true, then numUnits may only be set to 0. public void loadCargo() { /* missing code */ }
// The getCargo method returns the cargo of the boxcar. public String getCargo() { /* missing code (don't forget to update the return statement) */ return ""; }
// The setCargo method sets the cargo type of the boxcar. The cargo variable is // set to c only if c is "gizmos", "gadgets", "widgets", or "wadgets". // The method ignores the case of of the value in c. If c holds any value other than // "gizmos", "gadgets", "widgets", or "wadgets" (ignoring case), the method sets cargo // to "gizmos". public void setCargo(String c) { /* missing code */ }
// The isFull method returns true if numUnits is equal to 10, false otherwise. public boolean isFull() { /* missing code (don't forget to update the return statement) */ return false; }
// The callForRepair method sets the variable repair to true, and numUnits to 0. public void callForRepair() { /* missing code */ } }
public class Student_Runner_Boxcar {
public static void main(String[] args) { //Testing various required behaviors of the Boxcar constructor System.out.println("Testing Boxcar constructors:"); Boxcar car1 = new Boxcar(); System.out.println("Printing Boxcar(): " + car1 + " "); Boxcar car2 = new Boxcar("widgets", 7, false); System.out.println("Printing Boxcar(\"widgets\", 7, false): " + car2 + " "); Boxcar car3 = new Boxcar("WaDGeTs", 7, true); System.out.println("Testing lowercase cargo and setting cargo to 0 if in repair. "); System.out.println("Printing Boxcar(\"WaDGeTs\", 7, true): " + car3 + " "); Boxcar car4 = new Boxcar("OtherStuff", 7, false); System.out.println("Testing cargo other than accepted values. "); System.out.println("Printing Boxcar(\"OtherStuff\", 7, true): " + car4 + " ");
// car2 is not burnt out. Lets call callForRepair on car2 and make sure it // gets marked for repair and set to 0 units. System.out.println("Testing callForRepair:"); car2.callForRepair(); System.out.println("Printing Boxcar called for repair: " + car2 + " ");
// Let's test the loadCargo() method. We'll make a new Boxcar with 7 gadgets, then load cargo until it reaches maximum capacity. Boxcar car5 = new Boxcar("gadgets", 7, false); car5.loadCargo(); //car5 should print out with 8 gadgets System.out.println("Printing Boxcar with 8 gadgets: " + car5 + " "); //now let's load cargo three more times. This should put the car over maximum capacity and should keep the cargo size at 10. car5.loadCargo(); car5.loadCargo(); car5.loadCargo(); System.out.println("Printing Boxcar with 10 gadgets, tried to overload: " + car5 + " "); //lastly, let's test to make sure we can't load cargo onto a Boxcar that is in repair, using car2. car2.loadCargo(); System.out.println("Printing Boxcar in repair, can't load (0 cargo): " + car2 + " ");
System.out.println("Testing isFull:"); //Let's test a full car and a non-full car to make sure they return true and false, respectively. Boxcar car6 = new Boxcar("gizmos", 10, false); Boxcar car7 = new Boxcar("widgets", 7, false); System.out.println("Printing isFull on full car: " + car6.isFull() + " "); System.out.println("Printing isFull on non-full car: " + car7.isFull() + " ");
System.out.println("Testing getCargo:"); //Let's make sure car7 returns "widgets" as its cargo. System.out.println("Printing getCargo on a \"widgets\" car: " + car7.getCargo() + " ");
System.out.println("Testing setCargo:"); //Making sure it can set cargo to "gadgets" car7.setCargo("gadgets"); System.out.println("Setting cargo to gadgets: " + car7 + " "); //Testing it will convert cargo to lowercase car7.setCargo("WADGetS"); System.out.println("Testing lowercase conversion (WADGetS -> wadgets): " + car7 + " "); //Testing it will set cargo to "gizsmos" if a nonvalid cargo is entered car7.setCargo("onions"); System.out.println("Testing invalid cargo type sets to gizmos (onions -> gizmos): " + car7 + " "); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
