Skip to Main Content

Introduction to Computer Science

Computer Science Department

Conditionals and Loops – 35 course points

The purpose of this assignment is to practice conditionals and loops. You will write Java programs for the four algorithms that you wrote on Assignment 1 and for one new problem.

Refer to our Programming Assignments FAQ for instructions on how to install Sublime, how to use the command line and how to submit your assignments.

Programming
Write 5 programs and submit on Autolab. We provide this zip file containing Gas.java, TrainTicket.java, WCS111FM.java, LuckySevens.java, and TwoSmallest.java. For each problem update and submit the corresponding file. Observe the following rules:
  1. DO NOT add any import statements
  2. DO NOT add the project statement
  3. DO NOT change the class name
  4. DO NOT change the headers of ANY of the given methods
  5. DO NOT add any new class fields
  6. DO NOT use System.exit()
  7. ONLY print the result as specified by each problem. DO NOT print other messages, follow the examples for each problem.
  1. Gas (5 points). Write a program Gas.java 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 person  (5 points). Write a program TrainTicket.java 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:
      • 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 WCS111FM.java 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”
  2. Hobbyist programmers (1 – 5 hours) win a T-shirt: display the string “T-shirt”
  3. Regular programmers (6 – 400 hours) win all of the following for which they qualify:
  1. a laptop if their hours happen to end in 9 (e.g. 19, 29, 209…): display the string “Laptop”
  2. a hat if their hours are an even number: display the string “Hat”
  3. 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
TV
  1. Lucky Sevens  (10 points). Write a program LuckySevens.java 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
0
% java LuckySevens 372777
4
% java LuckySevens -2378272
2
  1. Two Smallest  (10 points). Write a program TwoSmallest.java 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
0.3
1.1
% java TwoSmallest 1.0 35.0 2.0 1.1 6.9 0.3 0.3 6.7
0.3
0.3
Before submission
  1. Collaboration policy. Read our collaboration policy here.
  2. Update @author. Update the @author tag of the files with your name, email and netid.
  3. Submitting the assignment. Submit Gas.java, TrainTicket.java, WCS111FM.java, LuckySevens.java, and TwoSmallest.java separately via the web submission system called 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 by clicking the Staff  link from the course website.