Title: Nuts and Bolts Basic Stuff In Java
1CHAPTER 4 Nuts and Bolts Basic Stuff In Java
2Java Arithmetic Operators
- Addition
- Subtraction
- Multiplication ?
- Division /
- Remainder (modulus )
- Increment
- Decrement --
3Java Arithmetic Operators
The common addition, subtraction, and
multiplication operators are straight forward.
The division, modulus, increment, and decrement
operators do require more discussion.
4Division in Java
Example of division issues 10 / 3 3 10.0 / 3
3.33333 As you can see, if you divide two
integers you get an integer result. If one or
both operands is a floating-point value you get a
floating-point result.
5Modulus in Java
Modulus generates the remainder when you divide
two integer values. 53 2 54 1 550 510
5 Modulus is only defined for integers. If you
attempt to use the modulus operator on
floating-point values you will get garbage!
6Order of Precedence
- ( ) evalutated first, inside-out
- ?, /, or evaluated second, left-to-right
- , ? evaluated last, left-to-right
7Example
The equation to convert degrees Fahrenheit to
degrees Celsius is C 5/9 X (F-32). If you coded
this as C 5/9 (F-32) what would happen? C
would always be 0 because Java will always
calculate 5/90. Whats the right way to write
this? C 5.0/9 (F-32) or C (F-32) 5/9
8Standard Arithmetic Methods
- Java Math class
- more advanced mathematical operations
- make sure the argument is the correct data type
as specified by the method definition. - any variable to which the method is assigned must
be defined as the same data type returned by the
method.
9Standard Arithmetic Methods
exp(x) Returns ex. log(x) Returns the natural
log of x. min(x,y) Returns the smallest of x and
y. max(x,y) Returns the largest of x and
y. pow(x,y) Returns x raised to the power of
y. round(x) Rounds x to the nearest integer
value. sqrt(x) Returns the square root of x. and
many more (pg 127).
10Standard Arithmetic Methods
- Use the Math class name to call a method in the
Math class. - Math.sqrt(9) returns the value 3.0
- Math.pow(2,3) returns the value 8.0
- Because these methods return a value, you must
either assign it to a variable, or have it as
part of a writeInfo statement.
11Increment/Decrement Shorthand
- x or x is equivalent to x x1
- x-- or --x is equivalent to x x-1
- Can be applied to integer, floating-point, and
character variables
12Increment/Decrement Shorthand
- x
- Pre-increment/decrement is executed before the
expression is evaluated
x Post-increment/decrement is executed after
the expression is evaluated
13Increment/Decrement Shorthand
It only matters if you use pre- or
post-increment/decrement operation as part of an
expression. Examples value value
produce the same value for value. number
value number value produce two
different values for number.
14Example
StaugIO io new StaugIO() int x
1 io.writeInfo(2- x)What does this output?
0 int x 1 io.writeInfo(2- x)What does this
output? 1 What is the value of x now? 2
15Increment/Decrement Shorthand
To avoid potential problems, its suggested that
you do not use or ? ? within expressions, even
within simple assignment and output
statements. Only use or ? ? when a variable is
being incremented/decremented as a statement by
itself.
16Assignment Operators
Assignment operators store a value in memory.
The value is stored at a location in memory that
is identified by the variable on the left-hand
side of the assignment operator. A Java
assignment operator assigns the value on the
right side of the operator to the variable
appearing on the left side of the operator.
17Assignment Operators
Example x x 1 means to assign the value
of x1 to x.
You should be aware that does not mean equal in
Java. is used to assign something to a
variable is used for equal in Boolean
comparisons and are not the same thing!
18Assignment Operators
- Simple assignment
- Addition/assignment
- Subtraction/assignment -
- Multiplication/assignment
- Division/assignment /
- Remainder/assignment
- You dont need to use the shorthand notation.
Regular notation is usually easier.
19Consumer Loan Case Study
Formula for calculating monthly payment Payment
principal rate/(1-(1 rate)-term) Input
date, customer name, values for loan
principal, interest rate, and term. Processing
formula above Output monthly loan payment along
with the date, customer name, loan principal,
interest rate, and term used in the
calculation.