Download MathObjectsScanner

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Suppose you want to find the absolute value of a number.
Math class services
(functions)
Primitive vs reference
data types
Scanner class
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;
Appendix G – Math functions
Lab will include some play with
math functions
original = -17.25;
result = Math.abs(original);
The argument to the
method (value that the
method will act upon)
Name of class
Name of the function
providing service (static method)
We say that this method, abs,
returns a value. In this case that
value is assigned to the variable
result.
Math class services
(functions)
MathPlay.java
Primitive vs reference
data types
Scanner class
Primitives
int
byte
long
short
float
double
char
boolean
int num;
num = 5;
num
5
Reference Types
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 ";
The object that contains the
character string “Charleston”
cityName
Math class services
(functions)
Address to the object
Charleston
Primitive vs reference
data types
Scanner class
Reference types take up 2 chunks of memory, one
that is the named variable that contains an address or
Reference and the other that contains the data contents
of the 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”);
Formatting.java
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);
Book Chap 3.10
Math class services
(functions)
Primitive vs
reference data
types
Scanner class
keyboard
System.in
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.
23.4\n567.98\nThis little piggy\n
Math class services
(functions)
keyboard
Primitive vs
reference data
types
System.in
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.
Examples – assume all variables
Scanner methods
have been initialized to the
int:
nextInt()
appropriate types
double: nextDouble()
String: next()
abc = keyboard.nextInt();
String: nextLine()
text = keyboard.nextLine();
MPG.java
Book chapter 2.13
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)
InputProblem.java
Primitive vs
reference data
types
Scanner class