Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Fundamental Data Types Goals: To understand integer and floating-point numbers To recognize the limitations of the int and double types and the.

Similar presentations


Presentation on theme: "Chapter 3 Fundamental Data Types Goals: To understand integer and floating-point numbers To recognize the limitations of the int and double types and the."— Presentation transcript:

1 Chapter 3 Fundamental Data Types Goals: To understand integer and floating-point numbers To recognize the limitations of the int and double types and the overflow and round off errors that can result To write arithmetic expressions in Java To use the String type to define and manipulate character strings To learn about the char data type To learn how to read program input To understand the copy behavior of primitive types and object references

2 Number types Integer and double –int: integers, no fractional part -1, -4, 0 –double: floating-point numbers (double precision) 0.5, -3.11111, 4.3E24, 1E-14 Example: Purse Class Interface public class Purse { public void addNickels(int count)... public void addDimes(int count)... public void addQuarters(int count)... public double getTotal(int count)...... } Implementing the getTotal Method public class Purse { public double getTotal(){ return nickels * 0.05 + dimes * 0.1 + quarters * 0.25; } private int nickels; private int dimes; private int quarters; } * = multiplication

3 Assignment operator public Purse() { nickels = 0; dimes = 0; quarters = 0; } public void addNickels(int count) { nickels = nickels + count; } Syntax variableName = exp; Example nickels=nickels+count; Purpose: To change the content of the variable to the value of the expression evaluation

4 nickels++ is the same as nickels = nickels + 1 nickels-- decrements the contents of the variable. Increment/Decrement Constants public double getTotal() { final double NICKEL_VALUE = 0.05; final double DIME_VALUE = 0.1; final double QUARTER_VALUE = 0.25; return nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; } The modifier final specifies constants Example:

5 Class Constants The modifier static specifies class fields static together with final specifies class constants Example –public class Purse{... public double getTotal(){ return nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; } private static final double NICKEL_VALUE = 0.05; private static final double DIME_VALUE = 0.1; private static final double QUARTER_VALUE = 0.25;... } In methods of other classes, the constant is Purse.NICKEL_VALUE Example: final double NICKEL_VALUE =0.05; public static final double LITERS_PER_GALLON =3.785; Purpose: To define a constant of a particular type Syntax: In a method: final typeName variableName= expression ; In a class: accessSpecifier static final typeName variableName = expression;

6 File Purse.java 1/** 2 A purse computes the total value of a c ollection of coins. 3*/ 4public class Purse 5{ 6 /** 7 Constructs an empty purse. 8 */ 9 public Purse() 10 { 11 nickels = 0; 12 dimes = 0; 13 quarters = 0; 14 } 15/** 17 Add nickels to the purse. 18 @param count the number of nickels 19 */ 20 public void addNickels(int count) 21 { 22 nickels = nickels + count; 23 } 24 /** 26 Add dimes to the purse. 27 @param count the number of dimes 28 */ 29 public void addDimes(int count) 30 { 31 dimes = dimes + count; 32 } 33 /** 35 Add quarters to the purse. 36 @param count the number of quarters 37 */ 38 public void addQuarters(int count) 39 { 40 quarters = quarters + count; 41 } 42 /** 44 Get the total value of the coins in the purse. 45 @return the sum of all coin values 46 */ 47 public double getTotal() 48 { 49 return nickels*NICKEL_VALUE+ dimes * DIME_VALUE+quarters*QUARTER_VALUE; 51 } 53 private static final double NICKEL_VALUE = 0.05; 54 private static final double DIME_VALUE = 0.1; 55 private static final double QUARTER_VALUE = 0.25; 57 private int nickels; 58 private int dimes; 59 private int quarters; 60}

7 File PurseTest.java 1/** 2 This program tests the Purse class. 3*/ 4public class PurseTest 5{ 6 public static void main(String[] args) 7 { 8 Purse myPurse = new Purse(); 9 10 myPurse.addNickels(3); 11 myPurse.addDimes(1); 12 myPurse.addQuarters(2); 13 14 double totalValue = myPurse.getTotal(); 15 System.out.print("The total is "); 16 System.out.println(totalValue); 17 } 18}

8 Division and Remainder / is the division operator –If both arguments are integers, the result is an integer. The remainder is discarded. e.g. 7 / 4 = 1 –If one argument is double, the result is also double. e.g. 7.0 / 4 = 1.75 % (pronounced "modulo") is to get the remainder, e.g. 7 % 4 = 3

9 Mathematical Functions Math.sqrt(x)square root Math.pow(x, y)power x y Math.exp(x)exex Math.log(x)natural log Math.sin(x), Math.cos(x), Math.tan(x) sine, cosine, tangent (x in radian) Math.round(x)closest integer to x

10 Analyzing an Expression

11 Syntax 3.2: Static Method Call Syntax: ClassName. methodName( parameters) Example: Math.sqrt(4) Purpose: To invoke a static method (a method that doesn't operate on an object) and supply its parameters. Recall other method calls –Instance (object) method call Syntax: objectName. methodName( parameters) Example: myAccount.getBalance() Purpose: To invoke a method of a specific object (instance) –Implicit method call Syntax: methodName( parameters) Example: getBalance() Purpose: To invoke a method within the same object. This is a short form of this.methdName(parameters)

12 Type Conversion In assignment, types must match. double total = "a lot"; // no Two types match if –they are the same; or –the type in the right side can be converted to the type of the left side Two type conversions –Implicit type conversion if the conversion doesn’t cause information lost done automatically Example: double x = 1; Explicit type conversion –if the conversion causes information lost –done by the programmer by using “cast” Syntax 3.3 : Cast Syntax: (typeName)expression Example: (int)(x + 0.5) (int)Math.round(100 * f) Purpose: To convert an expression to a different type –More Examples: Use “cast” (int) to convert floating-point values to integer values: int pennies = (int)(total * 100); // Cast discards fractional part. Use Math.round for rounding: int dollar = (int)Math.round(total);

13 Strings String constants: "Carl" String variables: String name = "Carl"; String length: int n = name.length(); 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" 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 The result is a string consisting of characters from position start to “past the end” -1 substring length = “past the end” - start

14 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); Example: Reading Input –String input = JOptionPane.showInputDialog(prompt) –Convert strings to numbers if necessary: int count = Integer.parseInt(input); –Conversion throws an exception if user doesn't supply a number--see chap. 15 –Add System.exit(0) to the main method of any program that uses JOptionPane An Input Dialog

15 File InputTest.java 1import javax.swing.JOptionPane; 2 3/** 4 This program tests input from an inp ut dialog. 5*/ 6public class InputTest 7{ 8 public static void main(String[] args) 9 { 10 Purse myPurse = new Purse(); 12 String input = JOptionPane.showInputDialog( "How many nickels do you have?"); 13 int count = Integer.parseInt(input); 14 myPurse.addNickels(count); 15 16 input = JOptionPane.showInputDialog( "How many dimes do you have?"); 17 count = Integer.parseInt(input); 18 myPurse.addDimes(count); 19 20 input = JOptionPane.showInputDialog( "How many quarters do you have?"); 21 count = Integer.parseInt(input); 22 myPurse.addQuarters(count); 23 24 double totalValue = myPurse.getTotal(); 25 System.out.println( "The total is " + totalValue); 26 27 System.exit(0); 28 } 29}

16 Characters char: character type—a single Unicode character Character constants use single quotes: 'A', '\n', '\u00E9' 'A’ is not the same as "A" charAt method is a string method which gets character from a string, e.g. –"Hello".charAt(0) is 'H' Copying Numbers double balance1 = 1000; double balance2 = balance1; balance2 = balance2 + 500; Change in balance2 does not affect balance1

17 Copying Object References BankAccount account1 = new BankAccount(1000); BankAccount account2 = account1; account2.deposit(500); Change through account2 is also visible through account1 Object variables hold references, not objects


Download ppt "Chapter 3 Fundamental Data Types Goals: To understand integer and floating-point numbers To recognize the limitations of the int and double types and the."

Similar presentations


Ads by Google