Siena CSIS 120 - Numeric Types & Arithmetic Strings - Reading & Writing

Unformatted text preview:

Chapter 4Numeric TypesPrimitive TypesNumber Types: Floating-point TypesQuestionConstants: finalConstants: static finalSyntax 4.1 Constant DefinitionCashRegister ExampleSlide 10Slide 11Driver or Test ClassOutput of Driver/Test ClassSlide 14Arithmetic OperatorsIncrement and DecrementIncrement & Decrement OperatorsInteger DivisionPowers and RootsAnalyzing an ExpressionMathematical MethodsCast and RoundSlide 23Division & ModuloOrder of operationMath.Pow(base, exponent)What is a and b?Calling Static MethodsPrimative types vs. ObjectsThe String ClassConcatenationConverting between Strings and NumbersSubstringsSlide 34German KeyboardSlide 36Chinese IdeographsReading InputScanner ClassCashRegisterSimulator.javaSlide 41Slide 42Typical inputReading Input From a Dialog BoxChapter 4Numeric types & arithmeticStrings: reading & writingNumeric Types• int: integers, no fractional part:1 -4 0 32483204 • double: floating-point numbers (double precision):0.5 -3.11111 4.3E24 1E-14 • A numeric computation overflows if the result falls outside the range for the number type: int n = 1000000;System.out.println(n * n); // prints -727379968Type Description Size int The integer type, with range -2,147,483,648 . . . 2,147,483,647 4 bytes byteThe type describing a single byte, with range -128 . . . 127 1 byte shortThe short integer type, with range -32768 . . . 32767 2 bytes longThe long integer type, with range -9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,8078 bytes doubleThe double-precision floating-point type, with a range of about ±10308 and about 15 significant decimal digits 8 bytes floatThe single-precision floating-point type, with a range of about ±1038 and about 7 significant decimal digits4 bytes charThe character type, representing code units in the Unicode encoding scheme 2 bytes booleanThe type with the two truth values false and true 1 bit Primitive TypesNumber Types: Floating-point TypesRounding errors occur when an exact conversion between numbers is not possible:double f = 4.35;// prints 434.99999999999994 System.out.println(100 * f); Java: Illegal to assign a floating-point expression to an integer variable:double balance = 13.75;int dollars = balance; // ErrorQuestionWhich of the following initializations are incorrect, and why?a. int dollars = 100.0; b. double balance = 100;Constants: final•A final variable is a constant •Once its value has been set, it cannot be changed •Named constants make programs easier to read and maintain •Convention: Use all-uppercase names for constantsfinal double QUARTER_VALUE = 0.25; final double DIME_VALUE = 0.1; final double NICKEL_VALUE = 0.05; final double PENNY_VALUE = 0.01; payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE + nickels * NICKEL_VALUE + pennies * PENNY_VALUE;Constants: static finalIf constant values are needed in several methods, declare them together with the instance fields of a class and tag them as static and final Give static final constants public access to enable other classes to use them public class Math { . . . public static final double E = 2.7182818284590452; public static final double PI = 3.1415926535897932; }double circumference = Math.PI * diameter;Syntax 4.1 Constant DefinitionCashRegister Example 1 /** 2 A cash register totals up sales and computes change due. 3 */ 4 public class CashRegister 5 { 6 public static final double QUARTER_VALUE = 0.25; 7 public static final double DIME_VALUE = 0.1; 8 public static final double NICKEL_VALUE = 0.05; 9 public static final double PENNY_VALUE = 0.01; 10 11 private double purchase; 12 private double payment; 13 14 /** 15 Constructs a cash register with no money in it. 16 */ 17 public CashRegister() 18 { 19 purchase = 0; 20 payment = 0; 21 } 22 Continued23 /** 24 Records the purchase price of an item. 25 @param amount the price of the purchased item 26 */ 27 public void recordPurchase(double amount) 28 { 29 purchase = purchase + amount; 30 } 31 32 /** 33 Enters the payment received from the customer. 34 @param dollars the number of dollars in the payment 35 @param quarters the number of quarters in the payment 36 @param dimes the number of dimes in the payment 37 @param nickels the number of nickels in the payment 38 @param pennies the number of pennies in the payment 39 */ 40 public void enterPayment(int dollars, int quarters, 41 int dimes, int nickels, int pennies) 42 { 43 payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE 44 + nickels * NICKEL_VALUE + pennies * PENNY_VALUE; 45 } 46 Continued47 /** 48 Computes the change due and resets the machine for the next customer. 49 @return the change due to the customer 50 */ 51 public double giveChange() 52 { 53 double change = payment - purchase; 54 purchase = 0; 55 payment = 0; 56 return change; 57 } 58 }1 /** 2 This class tests the CashRegister class. 3 */ 4 public class CashRegisterTester 5 { 6 public static void main(String[] args) 7 { 8 CashRegister register = new CashRegister(); 9 10 register.recordPurchase(0.75); 11 register.recordPurchase(1.50); 12 register.enterPayment(2, 0, 5, 0, 0); 13 System.out.print("Change: "); 14 System.out.println(register.giveChange()); 15 System.out.println("Expected: 0.25"); 16 17 register.recordPurchase(2.25); 18 register.recordPurchase(19.25); 19 register.enterPayment(23, 2, 0, 0, 0); 20 System.out.print("Change: "); 21 System.out.println(register.giveChange()); 22 System.out.println("Expected: 2.0"); 23 } 24 }Driver or Test ClassOutput of Driver/Test ClassProgram Run: Change: 0.25 Expected: 0.25 Change: 2.0 Expected: 2.0QuestionWhat is the difference between the following two statements?final double CM_PER_INCH = 2.54;public static final double CM_PER_INCH = 2.54;Arithmetic Operators•Basic operators:•addition: + •subtraction: - •multiplication: * •division: / •modulo (remainder of division): %•Parentheses control the order of sub-expression computation:(a + b) / 2Increment and Decrement•items++ is the same as items = items + 1 •items-- subtracts 1 from itemsWhat is the value of n after the following sequence of statements? int n = 0; n--; n++;


View Full Document

Siena CSIS 120 - Numeric Types & Arithmetic Strings - Reading & Writing

Download Numeric Types & Arithmetic Strings - Reading & Writing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Numeric Types & Arithmetic Strings - Reading & Writing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Numeric Types & Arithmetic Strings - Reading & Writing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?