//******************************************************************** // BusRide.java // // Demonstrates the use of an if statement. //******************************************************************** import java.util.Scanner; public class BusRide { //----------------------------------------------------------------- // Reads the number of passengers and prints comments accordingly. //----------------------------------------------------------------- public static void main (String[] args) { final int CAPACITY = 56; Scanner scan = new Scanner(System.in); System.out.print ("Enter the number of people that want to get on: "); int passengers = scan.nextInt(); if (passengers > CAPACITY) System.out.println ("We need " + (passengers - CAPACITY) + " volunteers for economy class: To the roof."); System.out.println ("Have a nice ride!"); } } //******************************************************************** // Wages.java Author: Lewis and Loftus // // Demonstrates the use of an if-else statement. //******************************************************************** import java.text.NumberFormat; import java.util.Scanner; 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; Scanner scan = new Scanner(System.in); System.out.print ("Enter the number of hours worked: "); int hours = scan.nextInt(); 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 // // Demonstrates the use of a block statement in an if-else. //******************************************************************** import java.util.Scanner; public class Guessing { //----------------------------------------------------------------- // Plays a simple guessing game with the user. //----------------------------------------------------------------- public static void main (String[] args) { final int MAX = 10; int answer, guess; Scanner scan = new Scanner(System.in); answer = (int) (Math.random() * MAX) + 1; System.out.print ("I'm thinking about a number between 1 and " + MAX + ". Guess what it is: "); guess = scan.nextInt(); if (guess == answer) System.out.println ("Wow! You are the MOST amazing person I know!"); else { System.out.println ("Sorry, you owe me a thousand bucks."); System.out.println ("The number was " + answer); } } } //******************************************************************** // MinOfThree.java Author: Lewis and Loftus // // Demonstrates the use of nested if statements. //******************************************************************** import java.util.Scanner; public class MinOfThree { //----------------------------------------------------------------- // Reads three integers from the user and determines the smallest // value. //----------------------------------------------------------------- public static void main (String[] args) { int num1, num2, num3, min = 0; Scanner scan = new Scanner(System.in); System.out.println ("Enter three integers: "); num1 = scan.nextInt(); num2 = scan.nextInt(); num3 = scan.nextInt(); 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); } } //******************************************************************** // AgeInLife.java // // Demonstrates the use of a switch statement. //******************************************************************** import java.util.Scanner; public class AgeInLife { //----------------------------------------------------------------- // Reads an age from the user and prints comments accordingly. //----------------------------------------------------------------- public static void main (String[] args) { int age, category; Scanner scan = new Scanner(System.in); System.out.print ("Enter an age : "); age = scan.nextInt(); category = age / 10; switch (category) { case 0: System.out.println ("NO! I want THAT one!"); break; case 1: System.out.println ("School is cool"); System.out.println ("School is cool"); System.out.println ("School is cool"); System.out.println ("School is ..."); break; case 2: System.out.println ("I'll take on the world!"); break; case 3: System.out.println ("The world is a big place you know..."); break; case 4: System.out.println ("That new ferrari looks very appealing."); break; case 5: System.out.println ("Live your life and forget your age."); break; case 6: System.out.println ("What was that?"); break; default: System.out.println ("Old age isn't so bad when you consider the alternative."); } } } //******************************************************************** // Drinks.java // // Demonstrates the use of a switch statement. //******************************************************************** import java.util.Scanner; public class Drinks { //----------------------------------------------------------------- // Prints out a menu for the user to choose from, and prints // comments accordingly //----------------------------------------------------------------- public static void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Here is the drinks menu : "); System.out.println ("1.\tOrange juice"); System.out.println ("2.\tMilk"); System.out.println ("3.\tWater"); System.out.println ("4.\tWine"); System.out.println ("5.\tBeer"); System.out.print ("What will it be ? "); int choice = scan.nextInt(); switch (choice) { case 1: System.out.println ("Vitamin C!"); case 2: System.out.println ("Your bones will thank you."); break; case 3: System.out.println ("The classics never die."); break; case 4: System.out.print ("Red or white ? "); String type = scan.next(); boolean isRed = (type.toLowerCase()).equals("red"); if (isRed) System.out.println ("Good for your heart."); else System.out.println ("Good for your lungs."); break; case 5: System.out.println ("Watch that belly!"); break; default: System.out.println ("That's not going to quench your thirst..."); } } } //******************************************************************** // Wages2.java // // Demonstrates the use of the ternary and logical operators. //******************************************************************** import java.text.NumberFormat; import java.util.Scanner; public class Wages2 { //----------------------------------------------------------------- // 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 boolean isProf; // is the worker a professor or not? double pay = 0.0; Scanner scan = new Scanner(System.in); System.out.print ("Enter the number of hours worked: "); int hours = scan.nextInt(); System.out.print ("Are you a professor (Y/N)? "); String answer = scan.next(); if ( answer.equalsIgnoreCase("Y") ) { isProf = true; System.out.println("Sorry...Overtime does not apply to YOUR kind."); } else isProf = false; // Pay overtime at "time and a half" only if worker is not a professor pay = (hours > STANDARD && !isProf) ? STANDARD*RATE+(hours-STANDARD)*(RATE*1.5) : hours*RATE; NumberFormat fmt = NumberFormat.getCurrencyInstance(); System.out.println ("Gross weekly earnings: " + fmt.format(pay)); } } //******************************************************************** // Counter.java Author: Lewis and Loftus // // Demonstrates the use of a while loop. //******************************************************************** public class Counter { //----------------------------------------------------------------- // Prints integer values from 1 to a specific limit. //----------------------------------------------------------------- public static void main (String[] args) { final int LIMIT = 5; int count = 1; while (count <= LIMIT) { System.out.println (count); count = count + 1; } System.out.println ("Done."); } } //******************************************************************** // BusSpeed.java // // Demonstrates the use of a while loop, a sentinel value, and a // running sum. //******************************************************************** import java.text.DecimalFormat; import java.util.Scanner; public class BusSpeed { //----------------------------------------------------------------- // Computes the average speed of a bus on a trip. // The sum is printed as the numbers are entered. //----------------------------------------------------------------- public static void main (String[] args) { int sum = 0, value, count = 0; double average; Scanner scan = new Scanner(System.in); System.out.print ("Enter the number of kilometers covered " + "in hour 1 (-1 to quit): "); value = scan.nextInt(); while (value >= 0) // sentinel value to terminate loop { count++; sum += value; System.out.println ("The total distance so far is " + sum + " km."); System.out.print ("In hour " + (count+1) + " (-1 to quit): "); value = scan.nextInt(); } System.out.println (); System.out.println ("The trip lasted " + count + " hours."); average = (double)sum / count; DecimalFormat fmt = new DecimalFormat ("0.###"); System.out.println ("The average speed was " + fmt.format(average) + " km/h."); } } //******************************************************************** // BusPercentage.java // // Demonstrates the use of a while loop for input validation. //******************************************************************** import java.text.NumberFormat; import java.util.Scanner; public class BusPercentage { //----------------------------------------------------------------- // Computes the percentage of seats filled in a bus. //----------------------------------------------------------------- public static void main (String[] args) { final int NUM_SEATS = 56; int passengers; double ratio; Scanner scan = new Scanner(System.in); System.out.print ("Enter the number of passengers (0 to " + NUM_SEATS + "): "); passengers = scan.nextInt(); while (passengers < 0 || passengers > NUM_SEATS) { if (passengers > NUM_SEATS) System.out.print ("We're not in Mexico...Please reenter: "); else System.out.print ("You're a liar...Please reenter: "); passengers = scan.nextInt(); } ratio = (double)passengers / NUM_SEATS; NumberFormat fmt = NumberFormat.getPercentInstance(); System.out.println (); System.out.println ("The bus is " + fmt.format(ratio) + " full."); } } //******************************************************************** // Abyss.java // // Demonstrates an INFINITE LOOP. WARNING!! //******************************************************************** public class Abyss { //----------------------------------------------------------------- // Prints ever decreasing integers in an INFINITE LOOP! //----------------------------------------------------------------- public static void main (String[] args) { int count = 1; System.out.println ("I'm going in..."); while (count <= Integer.MAX_VALUE) { System.out.println (count); count = count - 1; } System.out.println ("Found the bottom of the abyss!"); //never reached } } //******************************************************************** // PalindromeTester.java Author: Lewis and Loftus // // Demonstrates the use of nested while loops. //******************************************************************** import java.util.Scanner; public class PalindromeTester { //----------------------------------------------------------------- // Tests strings to see if they are palindromes. //----------------------------------------------------------------- public static void main (String[] args) { String str, another = "y"; int left, right; Scanner scan = new Scanner (System.in); while (another.equalsIgnoreCase("y")) // allows y or Y { System.out.println ("Enter a potential palindrome:"); str = scan.nextLine(); left = 0; right = str.length() - 1; while (str.charAt(left) == str.charAt(right) && left < right) { left++; right--; } System.out.println(); if (left < right) System.out.println ("That string is NOT a palindrome."); else System.out.println ("That string IS a palindrome."); System.out.println(); System.out.print ("Test another palindrome (y/n)? "); another = scan.nextLine(); } } } //******************************************************************** // Counter2.java Author: Lewis and Loftus // // Demonstrates the use of a do loop. //******************************************************************** public class Counter2 { //----------------------------------------------------------------- // Prints integer values from 1 to a specific limit. //----------------------------------------------------------------- public static void main (String[] args) { final int LIMIT = 5; int count = 0; do { count = count + 1; System.out.println (count); } while (count < LIMIT); System.out.println ("Done."); } } //******************************************************************** // ReverseNumber.java Author: Lewis and Loftus // // Demonstrates the use of a do loop. //******************************************************************** import java.util.Scanner; public class ReverseNumber { //----------------------------------------------------------------- // Reverses the digits of an integer mathematically. //----------------------------------------------------------------- public static void main (String[] args) { int number, lastDigit, reverse = 0; Scanner scan = new Scanner (System.in); System.out.print ("Enter a positive integer: "); number = scan.nextInt(); do { lastDigit = number % 10; reverse = (reverse * 10) + lastDigit; number = number / 10; } while (number > 0); System.out.println ("That number reversed is " + reverse); } } //******************************************************************** // Counter3.java Author: Lewis and Loftus // // Demonstrates the use of a for loop. //******************************************************************** public class Counter3 { //----------------------------------------------------------------- // Prints integer values from 1 to a specific limit. //----------------------------------------------------------------- public static void main (String[] args) { final int LIMIT = 5; for (int count=1; count <= LIMIT; count++) System.out.println (count); System.out.println ("Done"); } } //******************************************************************** // Multiples.java Author: Lewis and Loftus // // Demonstrates the use of a for loop. //******************************************************************** import java.util.Scanner; public class Multiples { //----------------------------------------------------------------- // Prints multiples of a user-specified number up to a user- // specified limit. //----------------------------------------------------------------- public static void main (String[] args) { final int PER_LINE = 5; int value, limit, mult, count = 0; Scanner scan = new Scanner(System.in); System.out.print ("Enter a positive value: "); value = scan.nextInt(); System.out.print ("Enter an upper limit: "); limit = scan.nextInt(); System.out.println (); System.out.println ("The multiples of " + value + " between " + value + " and " + limit + " (inclusive) are:"); for (mult = value; mult <= limit; mult += value) { System.out.print (mult + "\t"); // Print a specific number of values per line of output count++; if (count % PER_LINE == 0) System.out.println(); } } } //******************************************************************** // Stars.java Author: Lewis and Loftus // // Demonstrates the use of nested for loops. //******************************************************************** public class Stars { //----------------------------------------------------------------- // Prints a triangle shape using asterisk (star) characters. //----------------------------------------------------------------- public static void main (String[] args) { final int MAX_ROWS = 10; for (int row = 1; row <= MAX_ROWS; row++) { for (int star = 1; star <= row; star++) System.out.print ("*"); System.out.println(); } } } //******************************************************************** // Christmas.java // // Demonstrates the use of nested for loops. //******************************************************************** public class Christmas { //----------------------------------------------------------------- // Prints a Christmas tree shape using asterisk (star) characters. //----------------------------------------------------------------- public static void main (String[] args) { final int MAX_ROWS = 10; for (int row = 1; row <= MAX_ROWS; row++) { for (int space = 1; space <= MAX_ROWS-row; space++) System.out.print (" "); for (int star = 1; star <= row*2; star++) System.out.print ("*"); System.out.println (); } for (int trunc=3; trunc>0; trunc--) { for (int space = 1; space <= MAX_ROWS-1; space++) System.out.print (" "); System.out.println("**"); } System.out.println("Merry late Christmas!"); } }