Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
Increment and Decrement
Operators
– ++ increments the value of its operand by 1
– -- decrements the value of its operand by 1
– Syntax
Pre-increment: ++variable
Post-increment: variable++
Pre-decrement: --variable
Post-decrement: variable--
Relational Operators
-Allow you to make comparisons in a
program
Relational Operators and the Unicode
Coating Sequence
Logical (Boolean) Operators
Mixed Expressions
– Operands of different types
– Examples
2 + 3
...
9
– Integer operands yield an integer result;
floating-point numbers yield floating-point
results
– If both types of operands are present, the
result is a floating-point number
– Precedence rules are followed
Precedence of Operators
Output
– Standard output object: System
...
out
...
out
...
out
...
54;
final int NO_OF_STUDENTS = 20;
final char BLANK = ' ';
final double PAY_RATE = 15
...
02 * 1000;
first = 'D';
str = "It is a sunny day
...
in object
– Standard input device
– Normally the keyboard
– Access using the Scanner class
– Scanner object
– Breaks input into units called tokens
– To read data:
1
...
Use the methods such as next, nextLine, nextInt, and
nextDouble
Using the Scanner Class to Accept
Keyboard Input
Table 2-5: Selected
Scanner class methods
Method
Description
nextDouble()
Retrieves input as a double
nextInt()
Retrieves input as an int
nextLine()
Retrieves the next line of data and returns it as a String
next()
Retrieves the next complete token as a String
nextShort()
Retrieves input as a short
nextByte()
Retrieves input as a byte
nextFloat()
Retrieves input as a float
...
The F is used only with constants coded within a program
...
Note that when you enter an input value that will be stored as a
long, you do not type an L
...
Using the Scanner Class to
Accept Keyboard Input
Pitfall: Using nextLine()
Following One of the Other
Scanner Input Methods
– There is a problem when using one numeric Scanner class retrieval method
or next()method before using the nextLine()method
– Keyboard buffer
– Location in memory that stores all keystrokes, including Enter
– To avoid issues, add an extra nextLine()method call to retrieve the
abandoned Enter key character after numeric or next() inputs
Packages, Classes, Methods,
and the import Statement
– Package: collection of related classes
– Class: consists of methods
– Method: designed to accomplish a specific task
import Statement
– Used to import the components of a package into a
program
– Reserved word
– import java
...
*;
– Imports the (components of the) package java
...
The Math class contains
methods for finding the maximum or minimum of two
values, rounding values, logarithmic functions, square
root, and trigonometric functions (sin, cos, tan etc
...
lang package, and
not in the java
...
Thus, the fully qualified
class name of the Math class is java
...
Math
...
abs()
The Math
...
The absolute value is the
positive value of the parameter
...
examples:
int abs1 = Math
...
abs(-20); // abs2 = 20
Math
...
ceil() function rounds a floating point value
up to the nearest integer value
...
example:
double ceil = Math
...
343);//ceil=8
...
floor()
The Math
...
The rounded
value is returned as a double
...
floor(7
...
0
Math
...
min() method returns the smallest of two
values passed to it as parameter
...
min(10, 20);
Math
...
max() method returns the largest of two
values passed to it as parameter
...
max(10, 20);
Math
...
round() method rounds
a float or double to the nearest integer using normal
math round rules (either up or down)
...
round(23
...
round(23
...
random()
The Math
...
Of course the number is not fully random,
but the result of some calculation which is
supposed to make it as unpredictable as
possible
...
random();
To get a random value between 0 and e
...
100,
multiply the value returned
by Math
...
g
...
Here is an example of how that might
look:
double random = Math
...
Exponential and Logarithmic
Math Functions
Math
...
exp() function returns e (Euler's number)
raised to the power of the value provided as parameter
...
exp(1);
System
...
println("exp1 = " + exp1);
double exp2 = Math
...
out
...
log()
The Math
...
The base for the logarithm is i(Euler's
number)
...
log() provides the reverse
function of Math
...
example:
double log1 = Math
...
out
...
log(10);
System
...
println("log10 = " + log10);
Math
...
log10 method works like
the Math
...
example:
double log10_1
= Math
...
out
...
log10(100);
System
...
println("log10_100 = " + log10_100);
Math
...
pow() function takes two parameters
...
example:
double pow2 = Math
...
out
...
pow(2,8);
System
...
println("pow8 = " + pow8);
Math
...
sqrt() method calculates the square root
of the parameter given to it
...
sqrt(4);
System
...
println("sqrt4 = " + sqrt4);
double sqrt9 = Math
...
out
...
PI
The Math
...
You will
often need the Math
...
Math
...
sin() method calculates the sine value
of some angle value in radians
...
sin(Math
...
out
...
cos()
The Math
...
example:
double cos = Math
...
PI);
System
...
println("cos = " + cos);
Math
...
tan() method calculates the tangent
value of some angle value in radians
...
tan(Math
...
out
...
asin()
The Math
...
example
double asin = Math
...
0);
System
...
println("asin = "+ asin);
Math
...
acos() method calculates the arc
cosine value of a value between 1 and -1
...
acos(1
...
out
...
atan()
The Math
...
example:
double atan = Math
...
0);
System
...
println("atan = " + atan);
Math
...
If you need this method, please read the JavaDoc
...
Math
...
sinh() method calculates the
hyperbolic sine value of a value between 1 and -1
...
sinh(1
...
out
...
cosh()
The Math
...
example:
double cosh = Math
...
0);
System
...
println("cosh = " + cosh);
Math
...
tanh() method calculates the hyperbolic
tangens value of a value between 1 and -1
...
tanh(1
...
out
...
toDegrees()
The Math
...
example:
double degrees = Math
...
PI);
System
...
println("degrees = "+ degrees);
Math
...
toRadians() method converts an angle in
degrees to radians
...
toRadians(180);
System
...
println("radians = "+ radians;