Radford University ITEC

ITEC120-ibarland (incl. office hrs)infolectureslabshwsjava.lang docsjava.util docs

lab03:
I/O; variables

Sep.06 (Wed): I/O

In class, we saw that we can write strings to the screen using System.out.println, a function which takes a String and doesn't actually return a value (but it has the side-effect of changing the screen).

System.out.println( "Look at me, I'm printed on the console!" );
The complement to output is input, where we read data from the keyboard. Try the following in BlueJ's code pane (use semicolons as shown here):
java.util.Scanner s = new java.util.Scanner( System.in );  // Do this once.
s.nextInt()   // Type a number into the console window.
s.nextInt()   // Type another number. 
s.nextInt()   // (If curious: What happens if you enter a non-int?)
We'll talk more in lecture, about why two steps are needed -- a one-time creation of a Scanner (we named it “s”), followed by actually asking s for ints.

Look at one of the programs from the book's Chpt 2. Critique: Does it have repeated code?

Your task

We will write a program which compute candy costs.

I suggest you work in a pair, but you can work individually if you like. Before you leave, check off your program w/ a labby.

Donut sales calculator

Delicious SugarBomb donuts are 65 cents each. Write a program which asks the user how many donuts they would like to buy and tells the user the total cost of the donuts in dollars and cents. Be sure your program displays the dollars just like this example:

Sample run:

Program displays: Delicious SugarBomb&tm; donuts are 65 cents each!
How many would you like?
User enters: 7
Program displays: 7 donut(s) cost 4 dollars and 55 cents.
Use a constant when you write this program. What is a good name for it? Also, write this program only using ints, without any doubles ever being used!

solution

ITEC120-ibarland (incl. office hrs)infolectureslabshwsjava.lang docsjava.util docs


Sep.08 (Fri)

We'll spend today finishing the problem from the previous lab (since much of the previous lab had been spent illustrating Scanner).

Optional: different uses of variables

If you've finished the program, and want an additional challenge: The provided half-skeleton used three local variables (numDonuts, consumerInfo. and scan).

We see that local variables can serve two different purposes: If you want, you can review in the lecture notes to see examples of using local variables to temporarily remember a value for later use.

Other optional topics:


5You don't need to use all-caps, since it's not an eternal constant.      back

ITEC120-ibarland (incl. office hrs)infolectureslabshwsjava.lang docsjava.util docs


©2006, Ian Barland, Radford University
Last modified 2006.Nov.30 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme