Upload
jordan-thompson
View
216
Download
1
Embed Size (px)
Citation preview
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);
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 functionsLab will include some play with
math functions
MathPlay.javaMathPlay.java
Math class services (functions)
Primitive vs reference data types
Scanner class
Math class services (functions)
Primitive vs reference data types
Scanner class
Primitivesintbytelongshortfloatdoublecharboolean
55
int num;num = 5;
num
Reference Types
Reference types take up 2 chunks of memory, onethat 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 thecharacter string “Charleston”
Charleston
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 DecimalFormatobject 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.javaFormatting.java
Book Chap 3.10
Scanner keyboard;keyboard = new Scanner (System.in);
Data type (class name) variableStandard input
keyboard
System.in
We say that we are instantiating a new Scannerobject 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\n23.4\n567.98\nThis little piggy\n
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 methodsint: nextInt()double: nextDouble()String: next()String: nextLine()
Examples – assume all variableshave been initialized to the appropriate types
abc = keyboard.nextInt();text = keyboard.nextLine();
MPG.javaMPG.java
Book chapter 2.13
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 nexttoken.
If the input is not correct, such as using nextInt and the userhas 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.javaInputProblem.java