//******************************************************************** // Countdown.java Author: Lewis and Loftus // // Demonstrates the difference between print and println. //******************************************************************** public class Countdown { //----------------------------------------------------------------- // Prints two lines of output representing a rocket countdown. //----------------------------------------------------------------- public static void main (String[] args) { System.out.print ("Three... "); System.out.print ("Two... "); System.out.print ("One... "); System.out.print ("Zero... "); System.out.println ("Liftoff!"); // appears on first output line System.out.println ("Houston, we have a problem."); } } //******************************************************************** // Facts.java Author: Lewis and Loftus // // Demonstrates the use of the string concatenation operator and the // automatic conversion of an integer to a string. //******************************************************************** public class Facts { //----------------------------------------------------------------- // Prints various facts. //----------------------------------------------------------------- public static void main (String[] args) { // Strings can be concatenated into one long string System.out.println ("We present the following facts for your " + "extracurricular edification:"); System.out.println (); // A string can contain numeric digits System.out.println ("Letters in the Hawaiian alphabet: 12"); // A numeric value can be concatenated to a string System.out.println ("International dialing code for Antarctica: " + 672); System.out.println ("Year in which Leonardo da Vinci invented " + "the parachute: " + 1515); System.out.println ("Speed of ketchup: " + 40 + " km per year"); } } //******************************************************************** // Addition.java Author: Lewis and Loftus // // Demonstrates the difference between the addition and string // concatenation operators. //******************************************************************** public class Addition { //----------------------------------------------------------------- // Concatenates and adds two numbers and prints the results. //----------------------------------------------------------------- public static void main (String[] args) { System.out.println ("24 and 45 concatenated: " + 24 + 45); System.out.println ("24 and 45 added: " + (24 + 45)); } } //******************************************************************** // Poem.java // // Demonstrates the use of escape sequences. //******************************************************************** public class Poem { //----------------------------------------------------------------- // Prints a poem (by Emily Dickinson) on multiple lines. //----------------------------------------------------------------- public static void main (String[] args) { System.out.println ("In this short Life\n\tThat only lasts an hour\n" + "How much -- how little -- is\n\tWithin our power"); } } //******************************************************************** // StringMutation.java // // Demonstrates the use of the String class and its methods. //******************************************************************** public class StringMutation { //----------------------------------------------------------------- // Prints a string and various mutations of it. //----------------------------------------------------------------- public static void main (String[] args) { String phrase = new String ("If you are not living life on the edge"); String mutation1, mutation2, mutation3, mutation4; System.out.println ("Original string: \"" + phrase + "\""); System.out.println ("Length of string: " + phrase.length()); mutation1 = phrase.concat (", you are taking up too much space."); mutation2 = mutation1.toUpperCase(); mutation3 = mutation2.replace ('E', 'X'); mutation4 = mutation3.substring (2, 12); // Print each mutated string System.out.println ("Mutation #1: " + mutation1); System.out.println ("Mutation #2: " + mutation2); System.out.println ("Mutation #3: " + mutation3); System.out.println ("Mutation #4: " + mutation4); System.out.println ("Mutated length: " + mutation4.length()); } } //******************************************************************** // RandomNumbers.java Author: Lewis and Loftus // // Demonstrates the import statement, and the creation of pseudo- // random numbers using the Random class. //******************************************************************** import java.util.Random; public class RandomNumbers { //----------------------------------------------------------------- // Generates random numbers in various ranges. //----------------------------------------------------------------- public static void main (String[] args) { Random generator = new Random(); int num1; float num2; num1 = generator.nextInt(); System.out.println ("A random integer: " + num1); num1 = Math.abs (generator.nextInt()) % 10; System.out.println ("0 to 9: " + num1); num1 = Math.abs (generator.nextInt()) % 10 + 1; System.out.println ("1 to 10: " + num1); num1 = Math.abs (generator.nextInt()) % 20 + 10; System.out.println ("10 to 29: " + num1); num2 = generator.nextFloat(); System.out.println ("A random float [between 0-1]: " + num2); num2 = generator.nextFloat() * 6; // 0.0 to 5.999999 num1 = (int) num2 + 1; System.out.println ("1 to 6: " + num1); } } //******************************************************************** // Echo.java // // Demonstrates the use of the Scanner class //******************************************************************** import java.util.Scanner; public class Echo { //----------------------------------------------------------------- // Reads a character string from the user and prints it. //----------------------------------------------------------------- public static void main (String[] args) { String message; Scanner scan = new Scanner(System.in); System.out.println ("Enter a line of text:"); message = scan.nextLine(); System.out.println ("You entered: \"" + message + "\""); } } //******************************************************************** // PriceOfHappiness.java // // Demonstrates the use of various Scanner and NumberFormat // methods. //******************************************************************** import java.util.Scanner; import java.text.NumberFormat; public class PriceOfHappiness { //----------------------------------------------------------------- // Calculates the final price of a purchased item using values // entered by the user. //----------------------------------------------------------------- public static void main (String[] args) { final double TAX_RATE = 0.14; // 14% sales tax int quantity; double subtotal, tax, totalCost, unitPrice; Scanner scan = new Scanner(System.in); System.out.print ("Enter the quantity of chocolate bars you want: "); quantity = scan.nextInt(); System.out.print ("Enter the unit price: "); unitPrice = scan.nextDouble(); subtotal = quantity * unitPrice; tax = subtotal * TAX_RATE; totalCost = subtotal + tax; // Print output with appropriate formatting NumberFormat money = NumberFormat.getCurrencyInstance(); NumberFormat percent = NumberFormat.getPercentInstance(); System.out.println ("Subtotal: " + money.format(subtotal)); System.out.println ("Tax: " + money.format(tax) + " at " + percent.format(TAX_RATE)); System.out.println ("Total price for satisfying your craving: " + money.format(totalCost)); } } //******************************************************************** // CircleStats.java // // Demonstrates the formatting of decimal values using the // DecimalFormat class. //******************************************************************** import java.util.Scanner; import java.text.DecimalFormat; public class CircleStats { //----------------------------------------------------------------- // Calculates the area and circumference of a circle given its // radius. //----------------------------------------------------------------- public static void main (String[] args) { int radius; double area, circumference; Scanner scan = new Scanner(System.in); System.out.print ("Enter the circle's radius: "); radius = scan.nextInt(); area = Math.PI * Math.pow(radius, 2); circumference = 2 * Math.PI * radius; // Round the output to three decimal places DecimalFormat fmt = new DecimalFormat ("0.###"); System.out.println ("The circle's area: " + fmt.format(area)); System.out.println ("The circle's circumference: " + fmt.format(circumference)); } }