Spring Semester 2005


Lecture Notes Six: Changing the Course of Action with if Statements.

Today in class we will solve these problems:

1. Write a program that prompts the user for two integers and then prints

  • The sum
  • The difference
  • The product
  • The average
  • The distance (absolute value of the difference)
  • The maximum (the larger of the two )
  • The minimum (the smaller of the two)
Here's a sample run with your program:

frilled.cs.indiana.edu%java Two
Please enter your first integer number, then press Enter.
3
Please enter your second integer number, then press Enter.
6
3 + 6 = 9
3 - 6 = -3
3 * 6 = 18
avg(3, 6) = 4.5
dist(3, 6) = 3
max(3, 6) = 6
min(3, 6) = 3
frilled.cs.indiana.edu%

2.

Write a program that asks a user to input

  • The number of gallons of gas in the tank
  • The fuel efficiency in miles per gallon
  • The distance the user wants to travel
Then print out
You will make it
or
You will not make it
Here's a sample run with your program:
frilled.cs.indiana.edu%java Fourteen

Please enter number of gallons then press enter.
10
Please enter fuel efficiency in miles per gallon.
20
Please enter distance in miles you want to cover.
199
You will make it.
frilled.cs.indiana.edu%java Fourteen
Please enter number of gallons then press enter.
5
Please enter fuel efficiency in miles per gallon.
20
Please enter distance in miles you want to cover.
101
You will not make it.
frilled.cs.indiana.edu%

3. Write a program that
  • reads two times in military format (0900, 1730)
  • and prints the number of hours and minutes between the two times.

Here is a sample run. User input is in color.

Please enter the first time: 0900
Please enter the second time: 1730
8 hours 30 minutes
Your program must be able to deal with the case that the first time is later than the second time:
Please enter the first time: 1730
Please enter the second time: 0900

15 hours 30 minutes
Here's a sample run with your program:
frilled.cs.indiana.edu%java Fifteen
Please enter the first time: 0920
Please enter the second time: 1025

1 hours 5 minutes
frilled.cs.indiana.edu%java Fifteen
Please enter the first time: 1025
Please enter the second time: 0920
22 hours 55 minutes
frilled.cs.indiana.edu%
4. Write a program that transforms numbers
1, 2, 3, ..., 12
into the corresponding month names
January, February, March, ..., December

Here's a sample run with your program:
frilled.cs.indiana.edu%java Eighteen
Please enter a month number from 1 to 12.
2
February  
frilled.cs.indiana.edu%java Eighteen
Please enter a month number from 1 to 12.
12
December  
frilled.cs.indiana.edu%java Eighteen
Please enter a month number from 1 to 12.

1
January   
frilled.cs.indiana.edu%java Eighteen
Please enter a month number from 1 to 12.
14
Exception in thread "main" [...]
frilled.cs.indiana.edu%

The code will be printed after the lecture.

For each program we will be using if statements to determine the course of action.

In all cases we will be thinking how we could have solved this problem without the use of if.


Last updated by Adrian German for A201/A597/I210 on Thu Jan 27 10:41:58 EST 2005