1 / 25

Chapter 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 and the overflow and round off errors that can result To write arithmetic expressions in Java 

solana
Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Fundamental Data Types

  2. 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

  3. 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)...... }

  4. Number types • int: integers, no fractional part1, -4, 0 • double: floating-point numbers (double precision)0.5, -3.11111, 4.3E24, 1E-14

  5. 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

  6. Assignment operator public Purse() {      nickels = 0;      dimes = 0;      quarters = 0; }  public void addNickels(int count) {      nickels = nickels + count; }

  7. Assignment

  8. Increment/Decrement nickels++ is the same as nickels = nickels + 1 nickels-- decrements the contents of the variable.

  9. 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; }

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

  11. Syntax 3.1: Constant Definition  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

  12. File Purse.java 1/** 2   A purse computes the total value of a collection 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 …Continue

  13. 16   /** 17      Add nickels to the purse. 18      @param count the number of nickels to add 19   */ 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 add 28   */ Continue…

  14. 29public 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 add 37   */ 38   public void addQuarters(int count) 39   { 40      quarters = quarters + count; 41   } 42 …Continue

  15. 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  * QUARTER_VALUE; 51   } 52 …Continue

  16. 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

  17. 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); …Continue

  18. 12      myPurse.addQuarters(2); 13 14      double totalValue = myPurse.getTotal(); 15      System.out.print("The total is "); 16      System.out.println(totalValue); 17   } 18}

  19. 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.757 / 4 = 1 • Get the remainder with % (pronounced "modulo")7 % 4 = 3

  20. Mathematical Functions

  21. Math Function Arguments • Use online help to confirm input and output data types for math functions, most require floating point data • http://java.sun.com/j2se/1.4/docs/api/index.html

  22. Analyzing an Expression

  23. Syntax 3.2: Static Method Call 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.

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

  25. Syntax 3.3 : Cast (typeName)expression Example: (int)(x + 0.5) Purpose: To convert an expression to a different type

More Related