Assignment 2 (40 points)

Implementation Notes

  • YOU MAY only update the methods with the WRITE YOUR CODE HERE line. 
  • COMMENT all testing print statements you have written from Welcome.java, Multiples.java, RoadTrip.java
  • DO NOT add any instance variables to the Welcome.java, Multiples.java, RoadTrip.java class.
  • DO NOT add any public methods to the Welcome.java, Multiples.java, RoadTrip.java class.
  • DO NOT add/rename the project or package statements.
  • DO NOT change the class name.
  • DO NOT use System.exit()

 

1. Welcome (10 points)

Programming

Write a program, Welcome.java, that prints out the message: Welcome to CS111.

Note:

  • The Autograder is case and punctuation sensitive.

Executing and Debugging

From the folder containing Welcome.java:

javac Welcome.java
java Welcome

Expected output:

Welcome to CS111.

 

2. Multiples (15 points)

Write a program, Multiples.java, without using conditionals, that reads 2 whole numbers and displays TRUE if:

  • the first number is 0
    OR
  • the second number is 0
    OR
  • the first number is a multiple of the second number

Or displays FALSE otherwise.

Note: You MUST check the above conditions in the given order. 

Hint:

  • num1 (first number) is a multiple of num2 (second number) if num1 = num2 * x, where x is any integer value
  • Use boolean variables instead of conditionals.

Example:

Inputs

Expected Outputs

[5, 6]

false

[8,4]

true

[8, 0]

true

Precondition:

  • Inputs are both greater than or equal to 0, and there will always be exactly two numbers

Executing and Debugging

From the folder containing Multiples.java:

javac Multiples.java
java Multiples 5 6

Expected output:

false

 

3. Road Trip (15 points)

You will use conditionals to compute and display a car’s trip given the current traffic conditions.

Programming

Write a program, RoadTrip.java, that reads the following six command‑line arguments in this order:

  • gallons (double) – Fuel currently in the tank
  • mpg (double) – Vehicle’s base miles‑per‑gallon on a flat, dry highway
  • isRaining (boolean) – true if it is raining, false otherwise
  • hasMountainPass (boolean) – true if the route has a mountain pass, false otherwise
  • numPassengers (int) – Number of people in the car, driver included
  • isWeekend (boolean) – true if the trip starts on a weekend, false otherwise

Once the command-line arguments have been read, the program is expected to calculate and display the car trip (in miles) before refueling, subject to the rules below.

Fuel-efficiency rules

  1. Weekend traffic
    If isWeekend is true, the traffic slows down the trip, multiplying the current mpg by 0.70.
  2. Weather slows down traffic
    If isRaining is true, the wet roads slow down the trip, multiplying the current mpg by 0.85.
  3. Terrain detour
    If hasMountainPass is true, multiply the current mpg by 0.90. The detour leads to less efficiency.
  4. Car‑pool boost
    If numPassengers ≥ 3, multiply the current mpg by 1.05 due to the ability to use HOV lanes.
  5. Range calculation
    distance = gallons * mpg, where mpg has been updated according to the specifications above.

Expected Output

Print exactly one line:
Expected range: XX.XX miles

Pre-conditions

  • gallons and mpg are positive real numbers.
  • numPassengers is an integer ≥ 1.
  • All booleans are literal true or false.

You may assume the inputs follow the pre-conditions, no validation code is required.

Note

  • The value of mpg may be modified multiple times throughout the program

Executing and Debugging

From the folder containing RoadTrip.java:

javac RoadTrip.java
java RoadTrip 12.0 30.0 true false 4 true

Expected output:

Expected range: 224.91 miles

Computation details (for your understanding, not printed by the program):

  • Weekend toll → 30.0 × 0.70 = 21mpg
  • Rain penalty → 21.0 × 0.85 = 17.85mpg
  • Car‑pool boost → 17.85 × 1.05 = 18.7425mpg

Range → 12 × 18.7425 = 224.91 miles

Created by: Matthew Specht

 

Guidelines

Implementation Notes

  • YOU MAY only update the methods with the WRITE YOUR CODE HERE line. 
  • COMMENT all testing print statements you have written from Welcome.java, Multiples.java, RoadTrip.java
  • DO NOT add any instance variables to the Welcome.java, Multiples.java, RoadTrip.java class.
  • DO NOT add any public methods to the Welcome.java, Multiples.java, RoadTrip.java class.
  • DO NOT add/rename the project or package statements.
  • DO NOT change the class name.
  • DO NOT use System.exit()

VSCode Extensions

You can install the VSCode extension pack for Java. Take a look at this tutorial.

We suggest:

Importing VSCode Project

  1. Download Assignment2.zip from Autolab Attachments.
  2. Unzip the file by double clicking.
  3. Open VSCode
    • Import the folder to a workspace through File > Open Folder

Before submission

Collaboration policy. Read our collaboration policy here.

Submitting the assignment. Submit the .java files via Autolab. To do this, click the Assignments link from the course website; click the Submit link for that assignment.

Getting help

If anything is unclear, don’t hesitate to drop by office hours or post a question on Piazza.

  • Find instructors office hours here
  • Find tutors office hours in Canvas -> Tutoring, RU CATS
  • Find head TAs office hours here
  • POST on Piazza in Canvas -> Piazza
  • In addition to office hours we have the CSL (Coding and Social Lounge), a community space staffed with lab assistants which are undergraduate students further along the CS major to answer questions.
  • Refer to our Programming Assignments FAQ for instructions regarding our assignments and policies.

Submitting

Write the program and submit on Autolab.

  • We provide a zip file (find it under Welcome on Autolab) containing Welcome.java, Multiples.java, RoadTrip.java.
  • For this assignment UPDATE and SUBMIT the corresponding file.