Back to 111 Main Page

Mobius strip

Assignment 07

Task

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

Textbook: 5.1 - 5.2
Concepts: logical operators; comparing Strings/objects (.equals); [switch].

Scenario

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 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:

  • The program should ask the user to enter the current day of the month. It needs to support both the standard and the count-down calendar. (So any value between -27 and 27 is valid.)
  • If the user enters an invalid number, give an error message (and no other output!).
  • Print the current day in the standard calendar.
  • Print the current day of the week. (Remember Day 0 is Festival, Day 1 is Monday, Day 2 is Tuesday, and so on.)
  • Then list which activities the crew can perform today. Activities include: drinking, dueling, gambling, and pillaging. If the activity is prohibited or cannot be performed for the given day, the word for that activity should not appear anywhere in your output.

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 UsernameA07.java 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 (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.)

FAQs

So what should my code look like for this one?
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 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. So, first of all, translate 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. Four if statements will give you those. Note that all the rules can really be boiled down to "what is the day divisible by?". 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:

  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.



~ztomasze Index : TA Details: ICS111: A07
http://www2.hawaii.edu/~ztomasze
Last Edited: 09 Feb 2009
©2009 by Z. Tomaszewski.