Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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()
- {
- cargo = "gizmos";
- numUnits = 5;
- repair = false;
- }
- // 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)
- {
- if (c.equalsIgnoreCase("gizmos") || c.equalsIgnoreCase("gadgets") || c.equalsIgnoreCase("widgets") || c.equalsIgnoreCase("wadgets"))
- cargo = c.toLowerCase();
- else
- cargo = "gizmos";
- if (u > 0 && u < 10)
- numUnits = u;
- else
- numUnits = 0;
- repair = r;
- if (repair)
- numUnits = 0;
- }
- // 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()
- {
- String sr = "";
- if (repair)
- sr = "in repair";
- else
- sr = "in service";
- return numUnits + " " + cargo + "\t" + sr;
- }
- // 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() {
- if (numUnits < 10)
- numUnits++;
- if (repair)
- numUnits = 0;
- }
- // The getCargo method returns the cargo of the boxcar.
- public String getCargo()
- {
- /* missing code (don't forget to update the return statement) */
- return cargo;
- }
- // 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)
- {
- if (c.equalsIgnoreCase("gizmos") || c.equalsIgnoreCase("gadgets") || c.equalsIgnoreCase("widgets") || c.equalsIgnoreCase("wadgets"))
- cargo = c.toLowerCase();
- else
- cargo = "gizmos";
- }
- // The isFull method returns true if numUnits is equal to 10, false otherwise.
- public boolean isFull()
- {
- if (numUnits == 10)
- return true;
- else
- return false;
- }
- // The callForRepair method sets the variable repair to true, and numUnits to 0.
- public void callForRepair()
- {
- repair = true;
- numUnits = 0;
- }
- }
- -----------------------------------------------------
- /*
- * AP CS MOOC
- * Term 2 - Assignment 2, Part 2: FreightTrain
- * A class which represents a freight train with multiple boxcars.
- */
- import java.util.ArrayList;
- public class FreightTrain
- {
- // An ArrayList that stores a train with multiple boxcars
- private ArrayList<Boxcar> train = new ArrayList<Boxcar>();
- // Default constructor that sets train to an ArrayList holding one
- // boxcar containing 5 gizmos, that is not in repair.
- public FreightTrain()
- {
- train.add(new Boxcar());
- }
- // A constructor that sets train to an ArrayList of size n, holding
- // n boxcars, that are all hold 5 gizmos and are not in repair. If n <= 0,
- // then the train should be set to size one, with a single boxcar containing
- // 5 gizmos and not in repair.
- public FreightTrain(int n)
- {
- if (n > 0)
- for (int i = 0; i < n; i++)
- {
- train.add(i, new Boxcar());
- }
- else
- {
- train.add (new Boxcar());
- }
- }
- // This method returns a String representation of the
- // Boxcar objects in the ArrayList, one per line. For example,
- // here is the String returned when toString is called on a
- // FreightTrain with 5 boxcars:
- //
- // 3 gadgets in service
- // 2 wadgets in service
- // 0 gizmos in repair
- // 7 gadgets in service
- // 0 gadgets in repair
- //
- // Note: there is one space between the number of units and
- // the cargo type, and a tab between the cargo type and
- // "in repair"/"in service".
- public String toString()
- {
- String r = "";
- String s = "";
- for (Boxcar x: train)
- {
- s = s + x.toString() + "\n";
- }
- return s;
- }
- // This method sets the cargo type of all the boxcars in the entire train.
- public void setCargo(String c)
- {
- for (Boxcar x: train)
- x.setCargo(c);
- }
- // This method sets the boxcars to the pattern "gizmos", "gadgets", "widgets",
- // "wadgets", "gizmos", "gadgets", "widgets", "wadgets", ...
- // until the end of the train.
- public void setMultiCargo()
- {
- int counter = 0;
- ArrayList<String> s = new ArrayList<String>();
- s.add("gizmos"); s.add("gadgets"); s.add("widgets"); s.add("wadgets");
- for (Boxcar x: train)
- {
- x.setCargo(s.get(counter));
- counter++;
- if(counter == 4)
- counter = 0;
- }
- }
- // This method fills every boxcar in the train to max capacity of 10.
- // Each individual boxcar can only hold cargo if its
- // repair variable is false.
- public void fillTrain()
- {
- for (Boxcar x: train)
- {
- while (!x.isFull())
- x.loadCargo();
- }
- }
- // This method sets the Boxcar at location i’s repair variable to true.
- public void callForRepair(int i)
- {
- train.get(i).callForRepair();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement