CS 55 Java Programming
Week 4
Agenda
- Chapter 4 - Control Structures: Part 1
- Test 1 (see Week 3 Web page for test 1 information)
Examples
BasicStatements.java
- basic statements in Java
Sales.java - Exercise 4.13
GrossPay.java - Exercise
4.14
References
Lecture Notes - Control Structures
- based on Peter van der Linden's book Just Java
Lab04
The best way to code the following exercises is to use a while loop with an end-of-data sentinel. See the lecture notes for the syntax and
semantics of the while statement. See BasicStatements.java for
an example of a while loop with an end-of-data
sentinel.
- Exercise 4.11 Drivers are concerned with the mileage their
automobiles get. One driver has kept track of several tankfuls of gasoline
by recording miles driven and gallons used for each tankful. Develop a
Java application that will input the miles driven and gallons used for
each tankful. The program should calculate and display the miles per
gallon obtained for each tankful and print the combined miles per gallon
obtained for all tankfuls up to this point. All averaging calculations
should produce floating-point results. Use input dialogs to obtain the
data from the user. Use a while statement
to repeat the calculations for each tankful. Use an end-of-data sentinel
to terminate the loop.
- Exercise 4.12 Develop a Java application that will determine
whether a department-store customer has exceeded the credit limit on a
charge account. For each customer, the following facts are available:
a) account number
b) balance at the beginning of the month
c) total of all items charged by the customer
this month
d) total of all credits applied to the customer's
account this month
e) allowed credit limit
The program should input each of these facts from input dialogs, calculate
the new balance (balance <- balance + charges
- credits), display the new balance, and determine whether the new
balance exceeds the customer's credit limit. For those customers whose
credit limit is exceeded, the program should display the message
"Credit limit exceeded." Use a while
statement to repeat the calculations for each customer. Use an end-of-data
sentinel to terminate the loop.