Download presentation
Presentation is loading. Please wait.
Published byJordan Thompson Modified over 9 years ago
1
Math class services (functions) Primitive vs reference data types Scanner class 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 = -17.25; result = Math.abs(original);
2
In code, you would see something like this: double original; double result; original = -17.25; result = Math.abs(original); Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class Name of class providing service Name of the function (static method) The argument to the method (value that the method will act upon) We say that this method, abs, returns a value. In this case that value is assigned to the variable result. Appendix G – Math functions Lab will include some play with math functions MathPlay.java
3
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class Primitives int byte long short float double char boolean 5 5 int num; num = 5; num Reference Types 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. 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 "; Address to the object cityName The object that contains the character string “Charleston” Charleston
4
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class Decimal Formatter – Another class in Java Creating an object DecimalFormat twoDecimals; DecimalFormat threeDecimals; twoDecimals = new DecimalFormat(“##0.00”); threeDecimals = new DecimalFormat(“##0.000”); Data type (class name) variables We say that we are instantiating a new DecimalFormat object with the new operation. Below, we use those formats. double number1; number1 = 345.123456; System.out.println(twoDecimals.format(number1); System.out.println(threeDecimals.format(number1); Formatting.java Book Chap 3.10
5
Scanner keyboard; keyboard = new Scanner (System.in); Data type (class name)variable Standard input keyboard System.in We say that we are instantiating a new Scanner object with the new operation. Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class 23.4\n567.98\nThis little piggy\n
6
keyboard System.in Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class 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
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class 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). InputProblem.java
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.