Practice SWYK check

Set up

Most of the setup is the same as before. See the previous SWYK checks if you need to reference it.

The typist should perform the following commands to download the template and test harness for this SWYK check:


The actual problem

Santa is getting prepped for what is sure to be a busy holiday season. He needs to start allocating reindeer to sleighs, deciding what toys will be loaded on which sleighs for which destinations, etc.

For this problem, you are to write three classes, called Sleigh, Reindeer, and Toy. For extra credit, you may also write a fourth class, called DeluxeSleigh, which has its own test suite. These are illustrated below:

     
Reindeer   Toy   Sleigh   Deluxe sleigh
(with turbo boosters)

How reindeer work

Reindeer each have a cute name. They are employed against their will to pull flying sleighs one day a year. Normally they work in teams. When a Reindeer is "printed," it simply prints its name with no other fanfare.

How toys work

Children all over the world play with a wide variety of toys. This is sometimes materialistic, other times not. Toys have a name (not necessarily unique — there are zillions of rag dolls and air hockey tables out there) plus a weight in kilograms. When a Toy is "printed," it simply prints its name with no other fanfare.

How sleighs work

Every sleigh has a weight and a capacity. The weight is how much the actual sleigh weighs (in kilograms) on its own, with no load. The capacity is how many kilograms of toys can fit into its cargo bay before it's completely full. (Note that the capacity is how many kilograms of toys can be loaded; the weight of the sleigh itself does not count against this number. In other words, a sleigh with a weight of 400 and a capacity of 600 can carry 600 kilograms of toys, not merely 200.)

Btw, toys are normally loaded onto sleighs bunches at a time, not individually. (This is why the .load() method takes an entire ArrayList of toys as an argument, not just one Toy.) When the .load() method is called, each of the toys in the ArrayList should be loaded onto the sleigh (assuming there is room for all of them; see below).

For propulsion, Santa's elves will harness reindeer to a sleigh. There are no limits to how many reindeer can be harnessed to a given sleigh, but the same reindeer cannot be harnessed to the same sleigh more than once. (You needn't worry about the same reindeer being harnessed to two different sleighs; that's allowed, actually, when reindeer return to the North Pole for reassignment, which happens many times on Christmas Eve.)

Finally, Santa often needs to determine the air speed of his sleighs, for strategic and planning purposes. The speed of a sleigh is related to how much load it has and how many reindeer are pulling it. Specifically: the speed of a sleigh is equal to the number of reindeer pulling it times one thousand, divided by its total weight (the total weight includes both the sleight's weight and the weight of the toys it carries).

(For example: suppose a 1200-kg sleigh was carrying three baby dolls weighing 8 kg each, plus two chainsaws weighing 50 kg each. If pulled by three reindeer, its speed would be (1000 × 3) ÷ (3 × 8 + 2 × 50 + 1200) = 2.2659 meters/second).

How deluxe sleighs work

(Note: do not work on your extra credit DeluxeSleigh class until you are completely finished with the basic SWYK check requirements, and pass all those test cases. In fact, don't even read this section yet.)

Setup

When you're ready for the extra credit, download these two additional files:

Also add this alias to your terminal session:

You can now type "runex" to run the extra credit test suite, with its own test cases.

The actual extra credit

Deluxe sleighs are a new invention of the Elves, and use jet propulsion to aid the reindeer. They work the same as regular sleighs, except for three things:

  • They always weigh exactly 3500 kilograms, always, and they always have a capacity of exactly 1200 kilograms, always.
  • They get a speed boost due to their thrusters: a deluxe sleigh's speed is 2.5 meters/second higher than the equivalent "regular" sleigh would be. (So if the speed of a regular sleigh with a given load/reindeer were 4 meters/second, the equivalent deluxe sleigh would fly at 6.5 meters/second.)
  • There's a reindeer cap: each deluxe sleigh can have no more than four reindeer harnessed to it.

  • The requirements

    Instance variables

    Your Sleigh, Toy, and Reindeer classes should probably each have instance variables. It's up to you to decide what they are. For the Sleigh class, make your inst vars protected rather than private.

    Methods

    Sleigh

    In addition to a constructor, your Sleigh class will have a .harness() method which will add an additional reindeer to the sleigh (throwing a plain-ol' Exception if the same reindeer is attempted to be added a second time.)

    Your Sleigh class will also have a .load() method to load a bunch of toys — this method can be called repeatedly (i.e., more than once) on the same Sleigh object to load additional sets of toys. This method should throw a CapacityExceededException if not all the toys in the bunch can be added (and in such a case, none of them should be added).

    Finally, Sleigh will have a .getSpeed() method to return the speed of the sleigh in meters/second if it were to depart with its current load and current reindeer.

    (Note: you might notice the Java keyword final in the template on the .load() method. Never you mind what that means, just don't change it.)

    Toy

    The Toy class takes a name in its constructor. Its weight can be get and set via accessor methods. When converted to a String, a Toy should simply give its name, with no other adornments.

    Reindeer

    The Reindeer class takes a name in its constructor. When converted to a String, a Reindeer should simply give its name, with no other adornments. Hey, they're simple, but believe me they work hard.

    DeluxeSleigh (when you get here)

    The DeluxeSleigh class will inherit from Sleigh. (I'll let you figure out what goes in there.)

    Example code and output

    Toy teddyBear = new Toy("Teddy Bear");
    teddyBear.setWeight(12);
    Toy barbie = new Toy("Barbie");
    barbie.setWeight(3);
    ArrayList<Toy> johnsonHouse = new ArrayList<Toy>();
    johnsonHouse.add(teddyBear);
    johnsonHouse.add(barbie);
    
    Toy chainsaw = new Toy("Chainsaw");
    chainsaw.setWeight(52);
    ArrayList<Toy> montgomeryHouse = new ArrayList<Toy>();
    montgomeryHouse.add(chainsaw);
    
    Reindeer dasher = new Reindeer("Dasher");
    Reindeer blitzen = new Reindeer("Blitzen");
    
    Sleigh redDazzler = new Sleigh(500, 800);
    redDazzler.load(johnsonHouse);
    redDazzler.load(montgomeryHouse);
    redDazzler.harness(blitzen);
    redDazzler.harness(dasher);
    
    System.out.println("Red Dazzler's speed: " + redDazzler.getSpeed());   <--- "Red Dazzler's speed: 3.527336"