Download presentation
Presentation is loading. Please wait.
1
Math class services (functions)
Primitive vs reference data types Scanner class Suppose you want to find the absolute value of a number. You could square it and then take the square root. OR You can ask Math for some help. Math is a class of services automatically provided to help us with a range of math services. To request Math’s help in this problem, we essentially say, Math, calculate the value of this number. In code, you would see something like this: double original; double result; original = ; result = Math.abs(original);
2
Appendix G – Math functions
In code, you would see something like this: double original; double result; original = ; result = Math.abs(original); Appendix G – Math functions Lab will include some play with math functions The argument to the method (value that the method will act upon) Name of class providing service Name of the function (static method) Math class services (functions) Primitive vs reference data types Scanner class We say that this method, abs, returns a value. In this case that value is assigned to the variable result. MathPlay.java
3
The object that contains the character string “Charleston”
Reference Types Primitives int byte long short float double char boolean int num; num = 5; When a variable references an object, it contains the memory address of the object’s location. Then it is said that the variable references the object. String cityName = "Charleston "; num 5 The object that contains the character string “Charleston” Math class services (functions) Primitive vs reference data types Scanner class cityName Address to the object Charleston Reference types take up 2 chunks of memory, one that is the named variable that contains an address or referenceand the other that contains the data contents of the object.
4
Decimal Formatter – Another class in Java Creating an object
Math class services (functions) Primitive vs reference data types Scanner class Decimal Formatter – Another class in Java Creating an object variables Data type (class name) DecimalFormat twoDecimals; DecimalFormat threeDecimals; twoDecimals = new DecimalFormat(“##0.00”); threeDecimals = new DecimalFormat(“##0.000”); We say that we are instantiating a new DecimalFormat object with the new operation. Below, we use those formats. Formatting.java double number1; number1 = ; System.out.println(twoDecimals.format(number1); System.out.println(threeDecimals.format(number1); Book Chap 3.10
5
23.4\n567.98\nThis little piggy\n
Math class services (functions) Primitive vs reference data types Scanner class Data type (class name) variable Standard input Scanner keyboard; keyboard = new Scanner (System.in); We say that we are instantiating a new Scanner object with the new operation. keyboard 23.4\n567.98\nThis little piggy\n System.in
6
Examples – assume all variables have been initialized to the
Math class services (functions) Primitive vs reference data types Scanner class keyboard System.in If I want to read in data, I will use keyboard’s methods to do so, since I want to read from standard input and keyboard references the Scanner object which will read from standard input. Scanner methods int: nextInt() double: nextDouble() String: next() String: nextLine() Examples – assume all variables have been initialized to the appropriate types abc = keyboard.nextInt(); text = keyboard.nextLine(); MPG.java Book chapter 2.13
7
Scanner methods (except for nextLine()) treat “whitespace”
as a delimiter. For each next method, it will pluck off the next token. If the input is not correct, such as using nextInt and the user has entered “abc”, you will get a runtime error. If you try to read in a String after you have read in a number, you must “consume” the new line character prior to reading the String. See page 88 (purple) or 86 (green). Math class services (functions) Primitive vs reference data types Scanner class InputProblem.java
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.