ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws—java.lang docs—java.util docs
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!" ); |
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?) |
Look at one of the programs from the book's Chpt 2. Critique: Does it have repeated code?
We will write a program which compute candy costs.
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. |
ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws—java.lang docs—java.util docs
We'll spend today finishing the problem from the previous lab (since much of the previous lab had been spent illustrating Scanner).
If you've finished the program, and want an additional challenge: The provided half-skeleton used three local variables (numDonuts, consumerInfo. and scan).
Other optional topics:
5You don't need to use all-caps, since it's not an eternal constant. back
ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws—java.lang docs—java.util docs
©2006, Ian Barland, Radford University Last modified 2006.Nov.30 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |