## Assignment 07

Use conditionals to sort through the morass of Bleegian mining events and regulations.

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

### Scenario

The planet Bleeg is known for both its bloated governmental bureaucracy and its bloodthirsty space pirates. Through various sordid misadventures, you have been captured by such space pirates and sold into slavery. You now toil in the fetid plutoxium mines of Korvath--one of the moons of Bleeg.

The Korvathian mine overseers have a bit of a problem. Like most Bleegians, they have very strict rules about what activities they allow each other to engage in on different days. Also, due to the nature of Korvath's orbit around Bleeg, certain events--such as venting steam geysers and freezing eclipses--occur on a regular basis. Although all these events are quite predictable, your Korvathian overseers spend most of their time drunk on grog (and were probably not shining examples of Bleegian intelligence to begin with). Therefore, they've long sought a way to offload some of this unnecessary cogitation.

When one of the overseers learns that you can program (just a little), the task falls to you to write a program to track all of these things. If you do well, this could be good from you--you would be allowed to work in the upper office levels of the mine, free from the radioactive plutoxium dust of the dank mining shafts below and closer to potential escape opportunities. On the other hand, should you fail, things could be get very ugly...

Good luck!

### What you need to know

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

Bleegian calendar. The miners of the Korvath moon follow the traditional Bleegian calendar. 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 and events are specified using the dates of the standard calendar.

Work: All slaves must labor every day except for Festival.

Tremors: Korvath orbits Bleeg once very four days. On even days (so Day 0, 2, 4, ...), the moon, the planet, and the sun align with each other. This exerts exceptional gravitational forces on Korvath, which heats the moon's core. This leads to tremors--which can cause mine cave-ins and other equipment damage. Also, on hot days, it can cause super-heated steam geysers to vent in the lower shafts, usually scalding many of the slaves.

Eclipse: Every fourth day (so Day 0, 4, 8, 12, ...), Korvath passes through the shadow of Bleeg. During this eclipse, the moon's temperature drops sharply, potentially leading to hypothermia amongst the poorly-garbed slaves.

Collection: Since cold keeps the toxic dust down, the mine overseers always collect the accumulated plutoxium slag on the day after the eclipse (so Day 1, 5, 9, 13, ...).

Grog: While overseers drink nearly constantly, they have decided to distribute a diluted grog mixture to slaves every Wednesday in order to improve morale (and thus production rates). Also, in honor of Festival, slaves get grog then too.

Flogging: Since slaves tend to mine a little slower on the cold eclipse days, the overseers have decided to flog everyone on those days. This activity keeps everyone warm and motivated. However, there is an old saying on Korvath: "Flogging and grogging don't mix!" Therefore, flogging is not practiced on any day where there is also grog served to slaves.

#### The program requirements

So that's all the background information you need. Here's what the overseers say 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 the events the mine will experience that day. Events include: work, tremors, eclipse, collection, grog, flogging. If the activity is prohibited or does not occur on a 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.

Please enter the current Bleegian day of the month: 0
Today (Day 0) is Festival.
Today will be marked by: tremors eclipse grog

Please enter the current Bleegian day of the month: -9
Today (Day 19) is Friday.
Today will be marked by: work

Please enter the current Bleegian day of the month: 17
Today (Day 17) is Wednesday.
Today will be marked by: work collection grog
```

### What to Submit

Submit your `UsernameA07.java` file to Tamarin.

1 - Compiles
Your program compiles successfully (no errors)
0.5 - Input
You read in the Bleegian day of the week (int) from the user.
0.5 - 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.5 - Activities
You correctly print the list of events that will occur on the given day. (Do not print the name of the event if it does not occur that day.)

### FAQs

So what should my code look like for this one?
I've seen some people manually determining which events occur on each day and then writing 55 separate cases (27 negative days + Festival + 27 positive days) to produce the correct output. While this technically 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 events. Six `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 "Events for this day: " 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: 15 Sep 2009 ©2009 by Z. Tomaszewski.