{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ch3 - Age.java Author Lewis/Loftus Demonstrates the use of...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
//******************************************************************** // 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
Image of page 1

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

View Full Document Right Arrow Icon
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. //******************************************************************** import cs1.Keyboard; public class MinOfThree { //----------------------------------------------------------------- // Reads three integers from the user and determines the smallest // value. //----------------------------------------------------------------- public static void main (String[] args) {
Image of page 2
int num1, num2, num3, min = 0; System.out.println ("Enter three integers: "); num1 = Keyboard.readInt(); num2 = Keyboard.readInt(); num3 = Keyboard.readInt(); if (num1 < num2) if (num1 < num3) min = num1; else min = num3; else if (num2 < num3) min = num2; else min = num3; System.out.println ("Minimum value: " + min); } } //******************************************************************** // GradeReport.java Author: Lewis/Loftus // // Demonstrates the use of a switch statement. //******************************************************************** import cs1.Keyboard; public class GradeReport { //----------------------------------------------------------------- // Reads a grade from the user and prints comments accordingly. //----------------------------------------------------------------- public static void main (String[] args) { int grade, category; System.out.print ("Enter a numeric grade (0 to 100): "); grade = Keyboard.readInt(); category = grade / 10; System.out.print ("That grade is "); switch (category) { case 10: System.out.println ("a perfect score. Well done."); break; case 9: System.out.println ("well above average. Excellent."); break; case 8: System.out.println ("above average. Nice job."); break; case 7: System.out.println ("average.");
Image of page 3

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

View Full Document Right Arrow Icon
break; case 6: System.out.println ("below average. You should see the"); System.out.println ("instructor to clarify the material " + "presented in class."); break; default: System.out.println ("not passing."); } } } //******************************************************************** // Counter.java Author: Lewis/Loftus // // Demonstrates the use of a while loop.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}