chapter 3 fundamental data types. chapter goals to understand integer and floating-point numbers to...

37
Chapter 3 Fundamental Data Types

Upload: crystal-may

Post on 12-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

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

Chapter 3

Fundamental Data Types

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

Chapter 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

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

Purse Class Interfacepublic class Purse{

public void addNickels(int count) . . .public void addDimes(int count) . . .public void addQuarters(int count) . . .public double getTotal(int count) . . .. . .

}

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

Number types

• int: integers, no fractional part1, -4, 0

• double: floating-point numbers (double precision)0.5, -3.11111, 4.3E24, 1E-14

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

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

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

Assignment operator

public Purse(){

      nickels = 0;      dimes = 0;      quarters = 0;

}   public void addNickels(int count){

      nickels = nickels + count;}

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

Assignment

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

Increment/Decrement

nickels++ is the same as nickels = nickels + 1

nickels-- decrements the contents of the variable.

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

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;

}

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

Class Constantspublic 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 isPurse.DIME_VALUE

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

Syntax 3.1: Constant DefinitionExample:  Purpose:   In a method:

final typeName variableName= expression ;In a class:

accessSpecifier static final typeName variableName = expression;

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

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

File Purse.java1/**2   A purse computes the total value of a collection of coins.3*/4public class Purse5{6   /**7      Constructs an empty purse.8   */9   public Purse()10   {11      nickels = 0;12      dimes = 0;13      quarters = 0;14   }15 …Continue

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

16   /**17      Add nickels to the purse.18      @param count the number of nickels to add19   */20   public void addNickels(int count)21   {22      nickels = nickels + count;23   }2425   /**26      Add dimes to the purse.27      @param count the number of dimes to add28   */

Continue…

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

29   public void addDimes(int count)30   {31      dimes = dimes + count;32   }3334   /**35      Add quarters to the purse.36      @param count the number of quarters to add37   */38   public void addQuarters(int count)39   {40      quarters = quarters + count;41   }42 …

Continue

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

43   /**

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 

50   + dimes * DIME_VALUE + quarters * QUARTE

R_VALUE;

51   }

52

…Continue

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

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;

56

57   private int nickels;

58   private int dimes;

59   private int quarters;

60}

61

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

File PurseTest.java1/**2   This program tests the Purse class.3*/4public class PurseTest5{6   public static void main(String[] args)7   {8      Purse myPurse = new Purse();910      myPurse.addNickels(3);11      myPurse.addDimes(1); …Continue

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

12      myPurse.addQuarters(2);

13

14      double totalValue = myPurse.getTotal();

15      System.out.print("The total is ");

16      System.out.println(totalValue);

17   }

18}

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

Division and Remainder

• / is the division operator • If both arguments are integers, the result is

an integer. The remainder is discarded • 7.0 / 4 = 1.75

7 / 4 = 1 • Get the remainder with % (pronounced

"modulo")7 % 4 = 3

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

Mathematical Functions

Math.sqrt(x) square root

Math.pow(x, y) power xy

Math.exp(x) ex

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

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

Analyzing an Expression

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

Syntax 3.2: Static Method CallClassName. methodName( Tparameters)

Example:Math.sqrt(4)

Purpose:To invoke a static method (a method that doesn'toperate on an object) and supply its parameters.

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

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);Cast discards fractional part.

• Use Math.round for rounding:int dollar =   (int)Math.round(total);

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

Syntax 3.3 : Cast

(typeName)expression

Example: (int)(x + 0.5)

(int)Math.round(100 * f)

Purpose:To convert an expression to a different type

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

Strings

• String constants: "Carl"

• String variables:String name = "Carl"; 

• String length:int n = name.length();

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

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"      

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

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);

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

Substrings

• String greeting = "Clown";String sub = greeting.substring(1, 4);

• Supply start and “past the end” position

• First position is at 0

0C1l2o3w4 n

• substring length = “past the end” - start

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

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 chapter 15 • Add

System.exit(0)to the main method of any program that uses JOptionPane

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

An Input Dialog

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

File InputTest.java1import javax.swing.JOptionPane;23/**4   This program tests input from an input dialog.5*/6public class InputTest7{8   public static void main(String[] args)9   {10      Purse myPurse = new Purse();

…Continue

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

1112      String input = JOptionPane.showInputDialog("How ma

ny nickels do you have?");13      int count = Integer.parseInt(input);14      myPurse.addNickels(count);1516      input = JOptionPane.showInputDialog("How many di

mes do you have?");17      count = Integer.parseInt(input);18      myPurse.addDimes(count);1920      input = JOptionPane.showInputDialog("How many qua

rters do you have?"); …Continue

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

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}

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

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 gets character from a string"Hello".charAt(0) is 'H'

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

Copying Numbers

• double balance1 = 1000;double balance2 = balance1;balance2 = balance2 + 500;

• Change in balance2 does not affect balance1

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

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

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

Copying Object References