©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 1 Chapter 2 Fundamental Data Types
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 2 Program Coins1.java public class Coins1 { public static void main(String[] args) { int pennies = 8; // the purse contains 8 pennies, int dimes = 4; // four dimes, int quarters = 3 ; // and three quarters // compute total value of the coins double total = pennies * dimes * quarters * 0.25; // print result System.out.print("Total value = "); System.out.println(total); }
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 3 Number types int : integer double : double-precision floating-point numbers Variable declaration: int n; double total = 0.5; Quality tip: Use descriptive variable names int nickels;
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 4 Program Coins2.java public class Coins2 { public static void main(String[] args) { int pennies = 8; // eight pennies in the purse double total = pennies * 0.01; int dimes = 4; // four dimes in the purse // add value of dimes total = total + dimes * 0.10; int quarters = 3; // three quarters in the purse // add value of quarters total = total + quarters * 0.25; System.out.print("Total value = "); System.out.println(total); }
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 5 Assignment Assign a new value to a variable variableName = expression ; total = total + dimes * 0.1;
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 6 Figure 1 Assignment
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 7 Increment and decrement month++; month--; Shortcuts for month = month + 1; month = month - 1;
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 8 Figure 2 Incrementing a Variable
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 9 Type conversion In assignment, types must match. double total = "a lot"; // no Use “cast” (int) to convert floating-point values to integer values: int pennies = (int)(total * 100); Use Math.round for rounding: int dollar = (int)Math.round(total);
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 10 Static method calls ClassName. MethodName ( parameters ) Invoke a method that doesn't operate on an object Example: Math.round(3.14)
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 11 Program Volume.java public class Volume { public static void main(String[] args) { final double BOTTLE_VOLUME = 2.0; final double CAN_VOLUME = 0.355;
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 12 Constants final TypeName VariableName = Expression ; Defines a constant and assign its value Example: final double CAN_VOLUME = 0.355; Useful constants: Math.PI, Math.E Quality tip: No magic numbers
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 13 int bottles = 4; // we have four bottles int cans = 10; // and ten cans // compute total volume double total = bottles * BOTTLE_VOLUME + cans * CAN_VOLUME; // print result System.out.print("The total volume is "); System.out.print(total); System.out.println(” liters"); }
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 14 Program Coins3.java public class Coins3 { public static void main(String[] args) { final int PENNY_VALUE = 1; final int NICKEL_VALUE = 5; final int DIME_VALUE = 10; final int QUARTER_VALUE = 25; final int DOLLAR_VALUE = 100; int pennies = 8; // the purse contains 8 pennies, int nickels = 0; // no nickels, int dimes = 4; // four dimes, int quarters = 3; // and three quarters // compute total value in pennies int total = pennies * PENNY_VALUE + nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE;
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 15 // use integer division to convert to dollars, cents int dollar = total / DOLLAR_VALUE; int cents = total % DOLLAR_VALUE; System.out.print("Total value = "); System.out.print(dollar); System.out.print(” dollars and "); System.out.print(cents); System.out.println(" cents"); }
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 16 Figure 3 Analyzing an Expression
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 17 Arithmetic Operators + - * / Integer division 9 / 4 is 2 and not 2.25 ! 9 % 4 is 1 Common functions Math.pow(x,y) Math.sqrt(x)
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 18 Figure 4 On-Line Help
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 19 Program MakePassword.java public class MakePassword { public static void main(String[] args) { String firstName = "Harold"; String middleName = "Joseph"; String lastName = "Hacker"; // extract initials String initials = firstName.substring(0, 1) + middleName.substring(0, 1) + lastName.substring(0, 1); // append age int age = 19; // the age of the user String password = initials.toLowerCase() + age; System.out.println("Your password is ” + password); }
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 20 Strings String constants: "Carl" String variables: String name = "Carl"; String length: int n = name.length();
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 21 Substrings String greeting = "Clown"; String sub = greeting.substring(1, 4); Supply start and “past the end” position First position is at 0 0 C 1 l 2 o 3 w 4 n substring length = “past the end” - start
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 22 Concatenation String fname = "Harry"; String lname = "Hacker"; String name = fname + lname; name is "HarryHacker" If one operand of + is a string, the other is converted to a string: String a = "Agent"; String name = a + 7; name is "Agent7"
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 23 Converting between strings and numbers Convert to number: int n = Integer.parseInt(str); double x = Double.parseDouble(x); Convert to string: String str = "" + n; str = Integer.toString(n);
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 24 Formatting numbers NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMaximumFractionDigits(2); formatter.setMinimumFractionDi gits(2); formatter.format(tax); prints 0.30
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 25 Program Coins4.java import java.text.NumberFormat; public class Coins4 { public static void main(String[] args) { final double PENNY_VALUE = 0.01; final double NICKEL_VALUE = 0.05; final double DIME_VALUE = 0.1; final double QUARTER_VALUE = 0.25; ConsoleReader console = new ConsoleReader(System.in); System.out.println("How many pennies do you have?"); int pennies = console.readInt(); System.out.println("How many nickels do you have?"); int nickels = console.readInt(); System.out.println("How many dimes do you have?"); int dimes = console.readInt(); System.out.println("How many quarters do you have?"); int quarters = console.readInt();
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 26 double total = pennies * PENNY_VALUE + nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; // total value of the coins NumberFormat formatter = NumberFormat.getCurrencyInstance(); System.out.println("Total value = " + formatter.format(total)); }
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 27 Reading input ConsoleReader console = new ConsoleReader(System.in); int pennies = console.readInt(); Also readDouble, readLine Not a standard Java class. Include ConsoleReader.java in same directory, or paste into source file
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 28 Program Coins5.java import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Coins5 { public static void main(String[] args) { try { final double PENNY_VALUE = 0.01; final double NICKEL_VALUE = 0.05; final double DIME_VALUE = 0.1; final double QUARTER_VALUE = 0.25; InputStreamReader reader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(reader);
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 29 System.out.println("How many pennies do you have?"); String input = console.readLine(); int pennies = Integer.parseInt(input); System.out.println("How many nickels do you have?"); input = console.readLine(); int nickels = Integer.parseInt(input); System.out.println("How many dimes do you have?"); input = console.readLine(); int dimes = Integer.parseInt(input); System.out.println("How many quarters do you have?"); input = console.readLine(); int quarters = Integer.parseInt(input);
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 30 double total = pennies * PENNY_VALUE + nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; // total value of the coins System.out.println("Total value = ” + total); } catch(IOException e) { System.out.println(e); System.exit(1); }