ch3 -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: //******************************************************************** // Age.java Author: Lewis/Loftus // // Demonstrates the use of an if statement. //******************************************************************** import cs1.Keyboard; public class Age { //----------------------------------------------------------------- // Reads the user's age and prints comments accordingly. //----------------------------------------------------------------- public static void main (String args) { final int MINOR = 21; System.out.print ("Enter your age: "); int age = Keyboard.readInt(); System.out.println ("You entered: " + age); if (age < MINOR) System.out.println ("Youth is a wonderful thing. Enjoy."); System.out.println ("Age is a state of mind."); } } //******************************************************************** // Wages.java Author: Lewis/Loftus // // Demonstrates the use of an if-else statement. //******************************************************************** import java.text.NumberFormat; import cs1.Keyboard; public class Wages { //----------------------------------------------------------------- // Reads the number of hours worked and calculates wages. //----------------------------------------------------------------- public static void main (String args) { final double RATE = 8.25; // regular pay rate final int STANDARD = 40; // standard hours in a work week double pay = 0.0; System.out.print ("Enter the number of hours worked: "); int hours = Keyboard.readInt(); System.out.println (); // Pay overtime at "time and a half" if (hours > STANDARD) pay = STANDARD * RATE + (hours-STANDARD) * (RATE * 1.5); else pay = hours * RATE; NumberFormat fmt = NumberFormat.getCurrencyInstance(); System.out.println ("Gross earnings: " + fmt.format(pay)); } } //******************************************************************** // Guessing.java Author: Lewis/Loftus // // Demonstrates the use of a block statement in an if-else. //******************************************************************** import cs1.Keyboard; import java.util.Random; public class Guessing { //----------------------------------------------------------------- // Plays a simple guessing game with the user. //----------------------------------------------------------------- public static void main (String args) { final int MAX = 10; int answer, guess; Random generator = new Random(); answer = generator.nextInt(MAX) + 1; System.out.print ("I'm thinking of a number between 1 and " + MAX + ". Guess what it is: "); guess = Keyboard.readInt(); if (guess == answer) System.out.println ("You got it! Good guessing!"); else { System.out.println ("That is not correct, sorry."); System.out.println ("The number was " + answer); } } } //******************************************************************** // MinOfThree.java Author: Lewis/Loftus // // Demonstrates the use of nested if statements....
View Full Document

Page1 / 15

ch3 -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online