# INF 523Q Chapter 2: Objects and Primitive Data (Examples)

Post on 21-Dec-2015

215 views

Category:

## Documents

TRANSCRIPT

• Slide 1
• INF 523Q Chapter 2: Objects and Primitive Data (Examples)
• Slide 2
• Countdown.java b b //************************************************************ b b // Countdown.java Author: Lewis and Loftus b b // Demonstrates the difference between print and println. b b //************************************************************ b b public class Countdown bb{bb{ b b // Prints two lines of output representing a rocket countdown. b b public static void main (String[] args) b b { b b System.out.print ("Three... "); b b System.out.print ("Two... "); b b System.out.print ("One... "); b b System.out.print ("Zero... "); b b System.out.println ("Liftoff!"); // appears on first output line b b System.out.println ("Houston, we have a problem."); b b } bb}bb}
• Slide 3
• b b //************************************************************ b b // Facts.java Author: Lewis and Loftus b b // Demonstrates the use of the string concatenation operator and the b b // automatic conversion of an integer to a string. b b //************************************************************ b b public class Facts bb{bb{ b b // Prints various facts. b b public static void main (String[] args) b b { b b // Strings can be concatenated into one long string b b System.out.println ("We present the following facts for your " b b + "extracurricular edification:"); b b System.out.println (); b b // A string can contain numeric digits b b System.out.println ("Letters in the Hawaiian alphabet: 12"); Facts.java
• Slide 4
• b // A numeric value can be concatenated to a string b System.out.println ("International dialing code for Anarctica: " b + 672); b System.out.println ("Year in which Leonardo da Vinci invented " b + "the parachute: " + 1515); b System.out.println ("Speed of ketchup: " + 40 + " km per year"); b } Facts.java (cont.)
• Slide 5
• b //************************************************************* b // Addition.java Author: Lewis and Loftus b // Demonstrates the difference between the addition and string b // concatenation operators. b //************************************************************* b public class Addition b { b //----------------------------------------------------------------- b // Concatenates and adds two numbers and prints the results. b //----------------------------------------------------------------- b public static void main (String[] args) b { b System.out.println ("24 and 45 concatenated: " + 24 + 45); b b System.out.println ("24 and 45 added: " + (24 + 45)); b } Addition.java
• Slide 6
• PianoKeys.java b //************************************************************* b // PianoKeys.java Author: Lewis and Loftus b // Demonstrates the declaration, initialization, and use of an integer variable. b //************************************************************* b public class PianoKeys b { b //----------------------------------------------------------------- b // Prints the number of keys on a piano. b //----------------------------------------------------------------- b public static void main (String[] args) b { b int keys = 88; b b System.out.println ("A piano has " + keys + " keys."); b } PianoKeys.java
• Slide 7
• Geometry.java b //*************************************************************** b // Geometry.java Author: Lewis and Loftus b // Demonstrates the use of an assignment statement to change the b // value stored in a variable. b //**************************************************************** b public class Geometry b { b // Prints the number of sides of several geometric shapes. b public static void main (String[] args) b { b int sides = 7; // declaration with initialization b System.out.println ("A heptagon has " + sides + " sides."); b sides = 10; // assignment statement b System.out.println ("A decagon has " + sides + " sides."); b sides = 12; b System.out.println ("A dodecagon has " + sides + " sides."); b } Geometry.java
• Slide 8
• StringMutation.java b //**************************************************************** b // StringMutation.java Author: Lewis and Loftus b // Demonstrates the use of the String class and its methods. b //**************************************************************** b public class StringMutation b { b //----------------------------------------------------------------- b // Prints a string and various mutations of it. b //----------------------------------------------------------------- b public static void main (String[] args) b { b String phrase = new String ("Change is inevitable"); b String mutation1, mutation2, mutation3, mutation4; b b System.out.println ("Original string: \"" + phrase + "\""); b System.out.println ("Length of string: " + phrase.length());
• Slide 9
• StringMutation.java (cont.) b b mutation1 = phrase.concat (", except from vending machines."); b mutation2 = mutation1.toUpperCase(); b mutation3 = mutation2.replace ('E', 'X'); b mutation4 = mutation3.substring (3, 30); b b // Print each mutated string b System.out.println ("Mutation #1: " + mutation1); b System.out.println ("Mutation #2: " + mutation2); b System.out.println ("Mutation #3: " + mutation3); b System.out.println ("Mutation #4: " + mutation4); b b System.out.println ("Mutated length: " + mutation4.length()); b }
• Slide 10
• RandomNumbers.java b //**************************************************************** b // RandomNumbers.java Author: Lewis and Loftus b // Demonstrates the import statement, and the creation of pseudo- b // random numbers using the Random class. b //**************************************************************** b import java.util.Random; b public class RandomNumbers b { b // Generates random numbers in various ranges. b public static void main (String[] args) b { b Random generator = new Random(); b int num1; b float num2; b b num1 = generator.nextInt(); b System.out.println ("A random integer: " + num1); b
• Slide 11
• RandomNumbers.java (cont.) b num1 = Math.abs (generator.nextInt()) % 10; b System.out.println ("0 to 9: " + num1); b num1 = Math.abs (generator.nextInt()) % 10 + 1; b System.out.println ("1 to 10: " + num1); b num1 = Math.abs (generator.nextInt()) % 20 + 10; b System.out.println ("10 to 29: " + num1); b num2 = generator.nextFloat(); b System.out.println ("A random float [between 0-1]: " + num2); b num2 = generator.nextFloat() * 6; // 0.0 to 5.999999 b num1 = (int) num2 + 1; b System.out.println ("1 to 6: " + num1); b }
• Slide 12
• Echo.java b //**************************************************************** b // Echo.java Author: Lewis and Loftus b // Demonstrates the use of the readString method of the Keyboard class. b //**************************************************************** b import cs1.Keyboard; b public class Echo b { b // Reads a character string from the user and prints it. b public static void main (String[] args) b { b String message; b System.out.println ("Enter a line of text:"); b message = Keyboard.readString(); b System.out.println ("You entered: \"" + message + "\""); b }
• Slide 13
• Quadratic.java b //**************************************************************** b // Quadratic.java Author: Lewis and Loftus b // Demonstrates a calculation based on user input. b //**************************************************************** b import cs1.Keyboard; b public class Quadratic b { b // Determines the roots of a quadratic equation. b public static void main (String[] args) b { b int a, b, c; // ax^2 + bx + c b System.out.print ("Enter the coefficient of x squared: "); b a = Keyboard.readInt(); b System.out.print ("Enter the coefficient of x: "); b b = Keyboard.readInt(); b System.out.print ("Enter the constant: "); b c = Keyboard.readInt();
• Slide 14
• Quadratic.java (cont.) b // Use the quadratic formula to compute the roots. b // Assumes a positive discriminant. b b double discriminant = Math.pow(b, 2) - (4 * a * c); b double root1 = ((-1 * b) + Math.sqrt(discriminant)) / (2 * a); b double root2 = ((-1 * b) - Math.sqrt(discriminant)) / (2 * a); b b System.out.println ("Root #1: " + root1); b System.out.println ("Root #2: " + root2); b }
• Slide 15
• Price.java b //**************************************************************** b // Price.java Author: Lewis and Loftus b // Demonstrates the use of various Keyboard and NumberFormat methods. b //**************************************************************** b import cs1.Keyboard; b import java.text.NumberFormat; b public class Price b { b // Calculates the final price of a purchased item using values b // entered by the user. b public static void main (String[] args) b { b final double TAX_RATE = 0.06; // 6% sales tax b b int quantity; b double subtotal, tax, totalCost, unitPrice;
• Slide 16
• Price.java (cont.) b System.out.print ("Enter the quantity: "); b quantity = Keyboard.readInt(); b System.out.print ("Enter the unit price: "); b unitPrice = Keyboard.readDouble(); b subtotal = quantity * unitPrice; b tax = subtotal * TAX_RATE; b totalCost = subtotal + tax; b // Print output with appropriate formatting b NumberFormat money = NumberFormat.getCurrencyInstance(); b NumberFormat percent = NumberFormat.getPercentInstance(); b System.out.println ("Subtotal: " + money.format(subtotal)); b System.out.println ("Tax: " + money.format(tax) + " at " b + percent.format(TAX_RATE)); b System.out.println ("Total: " + money.format(totalCost)); b }
• Slide 17
• CircleStats.java b //**************************************************************** b // CircleStats.java Author: Lewis and Loftus b // Demonstrates the formatting of decimal values using the b // DecimalFormat class. b //**************************************************************** b import cs1.Keyboard; b import java.text.DecimalFormat; b b public class CircleStats b { b // Calculates the area and circumference of a circle given its radius. b public static void main (String[] args) b { b int radius; b double area, circumference; b
• Slide 18
• CircleStats.java (cont.) b b System.out.print ("Enter the circle's radius: "); b radius = Keyboard.readInt(); b area = Math.PI * Math.pow(radius, 2); b circumference = 2 * Math.PI * radius; b // Round the output to three decimal places b DecimalFormat fmt = new DecimalFormat ("0.###"); b System.out.println ("The circle's area: " + fmt.format(area)); b System.out.println ("The circle's circumference: " b + fmt.format(circumference)); b }