Assignment 07

Task

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 for 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.


Your Korvath overseers want the following events tracked by your program:

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

Tremors: Korvath orbits Bleeg once very four days. On odd days (so Day 1, 3, 5, ...), 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 four days, starting with Day 1 (so Day 1, 5, 9, 13, ...), 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 2, 6, 10, 14, ...).

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.

[Toggle Highlighting]

The program requirements

So that's all the background information you need. Here's what the overseers say 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.
Today will be marked by: grog

D:\TA\grading\A07>java ZtomaszeA07
Please enter the current Bleegian day of the month: -7
Today (Day 21) is Sunday.
Today will be marked by: work tremors eclipse flogging

D:\TA\grading\A07>java ZtomaszeA07
Please enter the current Bleegian day of the month: 17
Today (Day 17) is Wednesday.
Today will be marked by: work tremors eclipse grog

D:\TA\grading\A07>java ZtomaszeA07
Please enter the current Bleegian day of the month: 18
Today (Day 18) is Thursday.
Today will be marked by: work collection

What to Submit

Submit your UsernameA07.java file to Tamarin.

Grading [7 points]

1 - Compiles
Your program compiles successfully (no errors)
0.5 - Input
You read in the Bleegian day of the week (int) from the user.
1 - 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.
3 - 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

Sample code?
Sections 001 & 004: In lab, we went through the process of requirements -> design -> implementation -> testing. Here is the resulting code: ChineseZodiac.java
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. This is a good place to use a switch if you want to try it. (Remember that some Sundays are Festival though, so there's an extra test needed within that 0 case.)

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. Like determining the day, most events can quickly be determined by using the % operator and looking at the value of the remainder. (For example, day % 4 == 1 might tell you something useful.) 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 "Today will include:" 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.