CS 161 Assignment #4

Due Friday 2/16 by 11:59pm
(Not accepted after 2/18)

Introduction

This week you'll build a class that simulates an ORCA Card, which is used to pay for bus, train, and ferry trips in Pierce and King counties. When using the real card, you add funds to it and can then swipe the card to charge rides on the various services. In addition to keeping track of the balance and paying for rides, "our" ORCA card will also keep track of things like the number of trips that have been taken and the amount of tax that has been collected. This assignment will give you some additional practice with fields, methods, constructors, conditionals, and output.

The Assignment

An object-oriented programming language like Java is an excellent tool for this task: We can build an OrcaCard class with state representing the actual state of a card, and behaviors modeling the kinds of things we expect to do with the card. The most important state information is the balance (dollar amount) remaining on the card, though for this assignment you'll also need to store the number of trips taken. (As you work through the assignment you may decide to store additional information as well.) By the time you're done with the steps below, your class will have a constructor and the five methods (behaviors) shown on the menu below.
For full credit, your class should contain all of the methods described below. The methods should have exactly the same names as shown, take the correct number and type of inputs, and have the specified return types and values. (I will run a program that creates instances of your class and tests them, and if your names or other details differ, my testing code won't compile.)
  1. Start by downloading the OrcaCard BlueJ project. (If you don't start with this project, you won't be able to submit via BlueJ.) The project contains an OrcaCard class, but there's currently not much in the class.
  2. Your class needs to be able to keep track of how much money is currently stored on the card. Create a field of the appropriate type to hold the balance. The balance should be set by the constructor, which should take a single input, a double, specifying the initial balance on the card.
  3. Your class should have a topUp method that takes a single input (the amount to add to the current balance) and adjusts the balance but doesn't return anything. For full credit, it should verify that the specified amount is positive before adjusting the balance. (If the value is negative your method shouldn't update the balance.) It doesn't need to complain to the user if the input is negative — silently ignoring them is fine — but feel free to insult the user if desired if they try to add a negative amount to the balance.
  4. We'll simulate the process of "swiping" the card via the buyTrip method. It should take a double as a parameter (the cost of the trip), but we're required to pay tax on the cost of the trip as well. Thus, in the body of your buyTrip method you'll need to calculate how much we owe in tax, and decrease the balance by the cost of the trip plus the cost of the tax. (Assume the tax rate is 6.9%, but use a constant at the top of your class to hold this, so that we can change it easily in the future.) For full credit, you should use a conditional in buyTrip that checks whether you can afford the trip (including the tax). If the card doesn't have enough money, print an error message and leave the balance unchanged. If there are sufficient funds for the trip, print a success message that includes the remaining balance on the card.
  5. We'll also add a getAverageTripCost method. It doesn't need any inputs, but should return the average cost of all of the trips paid for by this card. (That is, the total cost divided by the number of trips.) Ignore the tax we pay on the trips when computing the average. (Hint: You'll need some extra state to support this method, though some of the info you need was probably added to support earlier methods.)
  6. The total tax collected on trips has to be reported to the IRS at the end of the year, so we'll need a getTax method. It shouldn't take any inputs, but should return the total amount of tax collected during "swipes" of the card. (Hint: You can use the state you added for the previous method to help you out here as well, or find some other way to keep track of the total amount of tax collected.)
  7. Finally, write a void printSummary method that prints information about the ORCA card object. Feel free to personalize this as you see fit, but the string should contain at least the card's current balance and the number of trips taken.
  8. For full credit, your code should contain comments. There should be a Javadoc-style (/** */) comment at the top of the class containing your name and a sentence or two explaining what it's about, and Javadoc-style comments above each of the methods in your class. You should add a brief comment (using //) for each of the instance variables and constants you use.

Some sample interactions with an OrcaCard in BlueJ's codepad are shown below. Notice that when you work with doubles, the math doesn't always come out precisely. That's to be expected, and the results on your computer might differ a little from what I'm showing below, but the first few significant digits of your results should agree with the values below.

> OrcaCard oc = new OrcaCard(20.50);
> oc.printSummary();
$20.5 left after 0 trip(s).
> oc.buyTrip(10.00);
Success: Ticket purchased.  $9.85 remaining.
> oc.getTax()
0.65   (double)
> oc.buyTrip(5);
Success: Ticket purchased.  $4.5249999999999995 remaining.
> oc.getAverageTripCost()
7.5   (double)
> oc.getTax()
0.9750000000000001   (double)
> oc.printSummary();
$4.5249999999999995 left after 2 trip(s).
> oc.buyTrip(4.50);
Fail: You can't afford this trip.
> oc.topUp(1.00);
> oc.buyTrip(4.50);
Success: Ticket purchased.  $0.7324999999999995 remaining.
> oc.printSummary();
$0.7324999999999995 left after 3 trip(s).

Extras

Looking for additional challenges? Add code to buyTrip so that it also prints out a simulated ticket, showing the cost, the amount paid in tax, and the remaining balance on the card. You could add a cheatIRS method that moves the amount you've collected as tax over to the balance of the card. Look into ways to tidy up the dollar amounts so that they always have two digits after the decimal point. In my output, I printed trip(s) so that it sounded ok whether there had been one trip or more. It would look even better if you added some code that looked at the number of trips and either used trip or trips as appropriate.

Grading

This assignment will be graded out of a total of 75 points.

Submitting

Before submitting, make sure your code compiles and is commented properly, and test each of your methods thoroughly. When you're convinced that it's ready to go, submit the assignment using BlueJ's submission tool as described here.


Brad Richards, 2024