Assignment 3 Solution


Category: Tag:


Write 5 programs and submit on Autolab. We provide this zip file containing,,,, and For each problem update and submit the corresponding file.

Observe the following rules:

    • DO NOT add any import statements

    • DO NOT add the project statement

    • DO NOT change the class name

    • DO NOT change the headers of ANY of the given methods

    • DO NOT add any new class fields

    • DO NOT use System.exit()

    • DO NOT print other messages, follow the examples for each problem

    • ONLY print the result as specified by each problem

  1. Gas (5 points)

Write a program that computes and displays the price a person will pay for gas at the gas station. The program takes three command-line arguments: two double arguments referring to the price per gallon, and the number of gallons of gas, and one boolean argument referring to whether the person pays cash or credit (true for cash, false for credit). If the person pays with a credit card, there is an extra charge of 10% of the total price.

Gas is never free. A person stopping to buy gas will always buy some amount of gas. Print the error message “Illegal input” if any of the double inputs is zero or negative, and end the program.

% java Gas 3.40 15.0 false 56.1

% java Gas 3.40 15.0 true 51.0

% java Gas 3.40 0 true “Illegal input”

  1. Train Ticket for One (5 points)

Write a program that takes two command-line arguments: an int referring to the person’s age and a boolean referring to whether or not the ticket was bought at the train station (true for a ticket bought at the train station). The program computes and displays the ticket price the person needs to pay for the train ride according to the following rules:

    • A child under 7 years old rides free.

    • If the ticket is bought at the train station:

    1. A person over 65 years old pays $7.50. Everyone else pays $13.20.

  • If ticket is bought inside the train, there is an extra charge of 20% of the price paid if the ticket was bought at the train station.

Print the error message “Illegal input” if a persons’ age is not within the range of 0 to 120 years inclusive and end the program.

      • java TrainTicket 23 true 13.20

      • java TrainTicket 23 false 15.84

      • java TrainTicket 230 false “Illegal input”

  1. WCS111 FM (5 points). Suppose you work at WCS111 FM, the radio station by computer scientists for computer scientists. The station wishes to run a contest whereby listeners win prizes based on how many hours they spend programming in Java. When a listener calls in to the radio station, the listener will state how many hours a month he/she spends programming in Java. Write a program that takes the number of hours the listener spends programming as an int argument. Based on the number of hours spent programming, display the listener prize according to:

    1. Non-programmers do not win any prize (aka those who have 0 hours): display the string “Nothing”

    1. Hobbyist programmers (1 – 5 hours) win a T-shirt: display the string “T-shirt”

  1. Regular programmers (6 – 400 hours) win all of the following for which they qualify:

    • a laptop if their hours happen to end in 9 (e.g. 19, 29, 209‚etc;): display the string “Laptop”

    • a hat if their hours are an even number: display the string “Hat”

    • a TV if their hours are divisible by 3: display the string “TV”

  1. Elite programmers (> 400 hours) will win a cat: display the string “Cat”

NOTE 1: Display one prize per line.

NOTE 2: assume the number of hours will never be negative.

    • java WCS111FM 3 T-shirt

    • java WCS111FM 9 Laptop


  1. Lucky Sevens (10 points)

Write a program that takes an int as a command-line argument and displays how many digits in the integer number are 7s.

NOTE: the number can be negative.

    • java LuckySevens 3482


    • java LuckySevens 372777


    • java LuckySevens -2378272


  1. Two Smallest (10 points)

Write a program that takes a set of double command-line arguments and prints the smallest and second-smallest number, in that order. It is possible for the smallest and second-smallest numbers to be the same (if the sequence contains duplicate numbers).

NOTE: display one number per line.

*HINT: Double.MAX_VALUE is the largest real number that can be stored in a double variable.

  • java TwoSmallest 17.0 23.0 5.0 1.1 6.9 0.3



  • java TwoSmallest 1.0 35.0 2.0 1.1 6.9 0.3 0.3 6.7