Assignment 2: Conditional love!

Due Thursday, September 22, before midnight

The goals of this assignment are:

  • Work with conditional statements

  • Work with boolean types

  • Work with both command line arguments and interactive input

1. What are the years and weeks?

Implement a program Years.java which inputs the number of days and outputs the number of years, weeks, and days in a human friendly text. Note that when the number of years (or weeks or day is one), we print year and not years (or week instead of week or day instead of days!

$ java Years 67
0 years 9 weeks and 4 days

$ java Years 453
1 year 12 weeks and 4 days

$ java Years 360
0 years 51 weeks and 3 days

$ java Years 61
0 years 8 weeks and 5 days

You may assume that the input to the program is a positive integer, i.e. a whole number greater than 0. You can also assume no leap years and that every year has the same number of weeks and days.

2. Rock, Paper, Scissors — Shoot!

Rock, Paper Scissors Shoot is a popular game where two players choose either rock, paper, or scissors. According to Wikipedia there are even ''professional Rock Paper Scissors Tournaments all around the world!'' and it’s even been used to settle court cases (who knew!).

Write a program in RPSS.java that simulates one round of Rock, Paper, Scissors, Shoot. Your program should ask the user to choose "rock", "paper", or "scissors", randomly choose an option for the computer player, and then determine if the user or computer wins, or if there is a tie. If the user chooses something that is not "rock", "paper", or "scissors", make sure to end the game.

The basic rules are as follows (here’s a short youtube video describing the rules as well):

  • "rock" beats "scissors", "scissors" beats "paper", and "paper" beats "rock"

  • if the user and computer both choose the same option, then print out a message saying there is a tie.

Here are a few examples of how your program should work:

$ java RPSS
Choose rock, paper, or scissors": rock
The computer chose rock.
Winner: Tie

$ java RPSS
Choose rock, paper, or scissors": paper
The computer chose rock.
Winner: You win - hooray!

$ java RPSS
Choose rock, paper, or scissors": paper
The computer chose scissors.
Winner: Computer

$ java RPSS
Choose rock, paper, or scissors": banana
banana is an invalid option, goodbye.

Hints:

  • Use the Math library to randomly choose an integer between 0 (inclusive) and 3 (exclusively)

  • Create conditionals statements to map 0, 1, and 2 to "rock", "paper" and "scissors" respectively.

  • You will likely need to use nested if statements.

3. Speeding fines!

credit: Lisa Meeden

Write a program called Speeding.java that calculates speeding fines based on the traffic laws of the state of Pennsylvania. Here is a short description of the fine structure:

For most speeding violations, the fine is $28 plus $2.50 for every mile in excess of 5 mph over the limit. However, if the maximum limit is 60 mph or higher, the fine is $47.50 plus $4 for every mile in excess of 5 mph over the limit.

The goal of this law is to discourage motorists from driving significantly over the posted limits. If the motorist is traveling just slightly over the limit (within 5 mph), then only a base fine is applied. However, if the motorist’s speed exceeds the limit by more than 5 mph, then an extra fine is applied.

See the table below for some example scenarios.

Table 1. Example Fines

Speed

Limit

Over

Excess > 5

Base fine

Extra fine

Total fine

45

50

0

0

0.00

0.00

0.00

52

50

2

0

28.00

0.00

28.00

65

50

15

10

28.00

25.00

53.00

69

65

4

0

47.50

0.00

47.50

75

65

10

5

47.50

20.00

67.50

Below are some examples of the running program. User input is shown in bold.

$ java Speeding

Enter speed limit: 55
Enter clocked speed: 45

Motorist is within the limit

$ java Speeding

Enter speed limit: 55
Enter clocked speed: 58

Motorist is over the limit by 3 mph
Total fine is: $28.00

$ java Speeding

Enter speed limit: 55
Enter clocked speed: 63

Motorist is over the limit by 8 mph

Base fine is: $28.00
Additional $2.50 per 3 miles in excess of 5 mph over limit
Total fine is: $35.50

$ java Speeding

Enter speed limit: 65
Enter clocked speed: 69

Motorist is over the limit by 4 mph

Total fine is: $47.50

4. What to hand-in

  1. The programs, Years.java, RPSS.java, and Speeding.java.

  2. Make sure each program has a header containing your name, date, and purpose of the program

  3. A brief write-up containing your name, assignment number, and a few sentences about how long you spent on the assignment and any interesting bugs you solved. This file should be saved in a .txt file (not a word doc file)

4.1. How to hand-in

  1. Copy your programs to your dropbox, into the folder called A2.