Assignment 07


Use conditionals to sort through the morass of Bleegian space pirate regulations.

Textbook: 5.1 - 5.2
Concepts: conditionals; logical operators; [switch].


You have been captured by Bleegian space pirates.

The citizens of the planet Bleeg are cursed with a bloated and complicated governmental bureaucracy. Surprisingly, even those Bleegian misfits that flee their homeland to become bloodthirsty marauding space pirates have difficulty leaving their culture of rules and regulations behind. So, while every Bleegian space pirate lives only to drink, gamble, duel, and pillage, there are certain restrictions on when they are allowed to do these things. Most of these rules are imposed by each pirate vessel's Officer's Club in an attempt to maintain some control over their wild rabble crew. However, the rules can often be so complicated that the (usually drunk and belligerent) crew have difficulty remembering what activities they are allowed to do on any particular day.

Bleegian space pirate vessels tend to be ancient, ill-maintained spaceships, bereft of nearly all computational hardware. However, the crew that captured you does have an old PC. When one of the officers learns that you can program, you are hauled up from the dark and fetid slave hold and set to the task of writing a program. This program must take the day of the month as input, and then display what the crew is allowed to do on that particular day.

Good luck--since it's probably back into the slave hold (if you're lucky) or out the airlock (if you're not so lucky) should you fail!

What you need to know

You are given the following information, most of which is necessary to complete your program.

Bleegian calendar. Space pirates still follow the calendar of their home planet. The Bleegian year of 364 days is divided into 13 months of exactly 28 days each. Bleegians start counting days of the month at 0, however. So each month would include: Day 0, Day 1, Day 2, ... Day 26, Day 27.

Like us, Bleegians have a 7-day week. The days are, oddly, named the same as ours: Sunday, Monday, Tuesday, etc. Both the week and the month starts with Sunday. Because every month is exactly 4 weeks long, every Day 0 is always a Sunday, every Day 1 is a Monday, and so on.

While Day 0 is technically a Sunday by these rules, it is a special holiday for all Bleegians. Therefore, it is always called Festival. (Day 7, Day 14, and Day 21 are all normal Sundays.)

To further complicate matters, some Bleegian's prefer to number days from the end of the month, rather than the beginning. To prevent total confusion, dates in this "count-down" calendar are given as negative numbers. Therefore, Day -1 == Day 27, Day -2 == Day 26, Day -3 == Day 25, and so on. Luckily, both the standard and the count-down calendars coincide on Day 0. All official regulations are specified using the dates of the standard calendar.

Drinking. Since constant drunkenness would (somewhat) inhibit their piratey skills, the Officer's Club has ruled that space pirates may only drink on odd-numbered days (such as Day 1, Day 3, Day 5, etc), leaving even days to recover. However, since Festival (Day 0) is a holiday, pirates can always drink on Festival.

Dueling. Being drunk or hungover most of the time, space pirates tend to take offense easily. Because the crew was constantly challenging each other to duels to the death, dueling is now prohibited on any day except Sundays. (Festival counts as a Sunday for this rule.)

Gambling. Space pirates love to gamble. Because the tally of gambling debts and loans can become quite complex, the Officer's Club likes to use Tuesdays to record and consolidate everyone's debts for the week. Therefore, no new bets can be made on that day, and so gambling is prohibited on any Tuesday.

Pillaging. Of course, all these other activities just pass the time until a space pirate can go pillaging. However, given the potential deadliness of the activity, the crew tends to be a rather superstitious lot and refuses to pillage on any day divisible by four (such as Day 0, Day 4, Day 8, etc.). However, because they can't otherwise gamble on Tuesdays, the crew will disregard this rule if the day is also a Tuesday (since at least they'll get a chance to informally gamble their lives).

[Toggle Highlighting]

The program requirements

So that's all the background information you need. Here's what the Officer's Club says the program must actually do:

Sample Output

D:\TA\grading\A07>java ZtomaszeA07
Please enter the current Bleegian day of the month: 30
Sorry, but 30 is not a valid day of the month.

D:\TA\grading\A07>java ZtomaszeA07
Please enter the current Bleegian day of the month: 0
Today (Day 0) is Festival.
You may: drink duel gamble

D:\TA\grading\A07>java ZtomaszeA07
Please enter the current Bleegian day of the month: -8
Today (Day 20) is Saturday.
You may: gamble

D:\TA\grading\A07>java ZtomaszeA07
Please enter the current Bleegian day of the month: 7
Today (Day 7) is Sunday.
You may: drink duel gamble pillage

What to Submit

Submit your file to Tamarin.

Grading [6 points]

1 - Compiles
Your program compiles successfully (no errors)
0.75 - Input
You read in the Bleegian day of the week (int) from the user.
0.75 - Error-checking
You report if the entered day is out of range or not a number (and, if so, print no other output).
0.5 - Conversion
You print the day, converting any negative "count-down" dates to standard dates.
1 - Day of the week
You print the correct day of the week.
2 - Activities
You correctly print the list of activites that are allowed on the given day. (Do not print the name of the activity if it is not allowed that day.)


Sample code?
Sections 001 & 004: In lab, we went through the process of requirements -> design -> implementation -> testing. Here is the resulting code:
Any hints on how to write this?
I've seen some people manually determining which activities are possible on each day and then writing 55 separate cases (27 negative days + Festival + 27 positive days) to produce the correct output. While this gets the output you need (and so technically meets the requirements of the assignment), it is certainly not helping you think in terms of algorithms! The computer should be doing the calculations, not you. And if the program had instead asked for the day of the year, would you type out 364 (or 729!) cases?

(Note: You can use a switch structure for this assignment, if you want. But when I use the term "case" here, I'm speaking more generally as any logical branch of your code, which includes any if, else, or else if body.)

Instead of a brute force approach, try to streamline your code. Be aware that most of the rules really just require you to figure out what the input is divisible by. Break the problem into pieces and tackle them one at a time. Write the code for that piece, compile, run, and test. Once you've got it working, move on to the next piece. I recommend going in order of the requested output (though this is not required).

So, first read in the day of the month from the user. Make sure you error-check the input and print the appropriate error messages on bad input (either out of range or a String).

Only after you have the day input working should you go on to the next step: translating any negative countdown date to the corresponding positive standard date. (Technically, all the rules are based on the standard calendar, so you should be doing all the comparisons with the standard calendar day anyway.) Now you only need to worry about 28 possible cases.

However, if you use % 7 to determine which day of the week it is, you only need 7 cases--one case for each day of the week you're translating to. (Remember that some Sundays are Festival though, so there's an extra test needed within that 0 case.) (This is a good place to use a switch if you want to try it.)

At this point you should have printed all the output except the allowed activities. For this, you will need only a handful of if statements. Exceptions to the rules often just require a logical operator and a second expression.

So, while it is not required, you can streamline your code to something like this:

  //get day from user
  if (day is in range) {
    //covert day to positive if it is negative  (if)
    //determine day of the week from day of the month (switch or if/else ifs)
    //print first line of output: calendar day and day of the week.
    //print first half of "you may" line
    //use an if statement for each activity to determine if its allowed this day
  }else {
    //print error message

Of course part of this assignment is supposed to be designing a solution from the requirements. So, while I've given you most of a design here, you'll need to fill in the rest of the details on your own. And of course variations on this design or completely different designs are certainly acceptable: TMTOWTDI.