Montana Tech of The University of Montana
Computer Science & Software Engineering

CSCI 135
Fundamentals of Computer Science I
Fall 2016



ASSIGNMENT 1

The goal of this assignment is to use conditionals and good programming style in Java. You will be using decision constructs (if statements and switch statements) in your code, but you will also be graded on your coding style. Be sure you pay attention to your header comment and other comments, your indentation and variable names, and careful use of whitespace and curly braces. You can refer back to the style slides if you have questions on the details.


Part 1: If Statements
The first part of your assignment is to create a program that calculates to cost of mailing a package. You should name your program Shipping.java. Given the weight of the package as a command line parameter, you need to calculate just how much it would cost to ship based on the following rules:

The cost of sending a package by an express delivery service is $15.00 for the first two pounds, and $5.00 for each pound or fraction thereof over two pounds. If the package weighs more then 70 pounds, a $15.00 excess weight surcharge is added to the cost. No package over 100 pounds will be accepted.

Write a program that accepts the weight of a package in pounds as a command line argument and computes the cost of mailing the package. Be sure to handle the case of overweight packages.

Some test input cases are shown below:

For the input "135" your program would print:

Package is over 100 pounds and cannot be shipped.
 
For the input "75" your program would print:

It will cost you $395.0 to ship your package.
    

For the input "60" your program would print:

It will cost you $305.0 to ship your package.

How do I specify command line arguments in Eclipse? For reasons no one understands, in order to specify the command line arguments in Eclipse, you must first run your program without any arguments. So run your program using the green play button, or by selecting Run from the Run menu. If your program makes use of args[0], it may crash with the error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException. That's okay just ignore it.

You can now actually add the command line arguments for the Shipping Java program by going to Run -> Run Configurations.... Select the (x)= Arguments tab and type the arguments in the top box.

Note: be sure to select the Shipping program in the left area, select the correct tab, and enter the command line arguments into the text box. The four arguments can be on a single line separated by a space, or you can put them on separate lines; it doesn't really matter.

My program's output isn't quite the same as yours for the example input above. Is that okay? Not really. Learning to program involves a lot of attention to detail. You should strive to exactly match our example outputs. There will be plenty of opportunity for creativity in future assignments and extra-credit levels. The assignment page is the "contract", sticking to the exact program specification will make grading easier for the TA, and that is good for all involved!

Why do I only get one decimal place in my output when I really want two? We will look at how to format output later on in the semester. For now we can just live with some less than perfect output.

How do I declare a constant? When we get to methods and instance variable in classes, we will use the full declaration of "public static final ... ". For now, all you have to use is something like:

final double MAX_WEIGHT;
I accidently named my program Shipper or shipping or ... Is that okay? No. This will break our grading script. This is a surefire way to make your TA grumpy. Plus it is really easy to fix. Right click on the Java source file in the package explore, select Refactor -> Rename, and give it the correct name.


Part 2: Nested If Statements
Create a program Lottery.java that takes two command line arguments. Both numbers should be integers between 1 and 10, and they represent guesses on a lottery pick. Your program should generate two random numbers between 1 and 10, and compare them to the command line input. You are to use a nested if statement to determine whether the command line picks are winning numbers.

First, print out what the two randomly generated lottery numbers were, then what the command line arguments (the user's picks) were. Then, if there is no match, print out "Sorry, no match". If one number matches, print out "You matched one number! You win $100!!" Finally, if both number match print out "You matched both numbers! Congratualations! You win $1000!!"

Here is an example output given that no numbers matched:

The lottery numbers were 7 and 3.
Your picks were 2 and 8.
Sorry, no match.

Here is an example output where 1 number matched:

The lottery numbers were 4 and 2.
Your picks were 2 and 8.
You matched one number! You win $100!!

Here is an example output given that both numbers matched:

The lottery numbers were 2 and 6.
Your picks were 6 and 2.
You matched both numbers! Congratulations! You win $1000!!
How do I generate a random number between 1 and 10? Use the following line to get a random number in that range (pay attention to all the parentheses in this):
int lotteryNumber1 = (int) ((Math.random( )* 10) + 1);
Sometimes I get two random numbers that are the same. Is this OK?Yes, that can happen sometimes.

The random numbers that I get are not the same as in your examples. Is this OK?Yes, that is very likely to happen. In fact, it would be strange if your randomly generated numbers were the same as mine.

Part 3: Switch Statement
Create a program Zodiac.java that determines the Chinese zodiac sign given a year of birth. The year of birth should be an integer. Use a switch statement in your program to determine which sign to output.

The rules for the Chinese zodiac signs are as follows: It is based on a 12 year cycle. If you were born in a year that is evenly divisible by 12, your sign is Monkey. If your year of birth, when divided by 12, has a remainder of 1, your sign is Rooster. Here is the entire list:

0: Monkey
1: Rooster
2: Dog
3: Pig
4: Rat
5: Ox
6: Tiger
7: Rabbit
8: Dragon
9: Snake
10: Horse
11: Sheep

So for example, for the input "1975" your program would print:

You were born in the year of the 
Rabbit.
How do I find the remainder? Take a look back at our slides on expressions - the ones from 8/24, slide #13.



Submission. Submit all three programs Shipping.java, Lottery.java, and Zodiac.java via Moodle. Be sure each submitted source file has the required header with your name, email address, the date, and a description of the program. Also be sure to submit the .java source files and not the .class files!

Page last updated: September 02, 2016