Slides (PPTX)

Download Report

Transcript Slides (PPTX)

Week 3 - Monday




What did we talk about last time?
Using Scanner to get input
Basic math operations
Lab 2

For Project 1, the easiest way to print out
data with 2 decimal places is put "%.2f" in
the formatting string for
System.out.format()
double x = 5.74961;
System.out.format("%.2f", x); //prints 5.75

If you want, you can include other things in
the formatting string
System.out.format("Total = $%.2f", 15.7777);
//prints Total = $15.78
There are three parts to using Scanner for
input
1. Include the appropriate import statement so
that your program knows what a Scanner
object is
2. Create a specific Scanner object with a name
you choose
3. Use the object you create to read in data

Given a temperature in Celsius, what is the
equivalent in Fahrenheit?
 TF = (9/5)TC + 32

How complex can expressions get?
int
int
int
int
a
b
c
d
=
=
=
=
31;
16;
1;
2;
a = b + c * d – a / b / d;


What’s the value of a?
18!

Order of operations holds like in math
int
int
int
int
a
b
c
d
=
=
=
=
31;
16;
1;
2;
a = (((b + c) * d) – a / b) / d;


You can use parentheses to clarify or change
the precedence
Now a is 16

You cannot directly store a double value
into an int variable
int a = 2.6; // fails!

However, you can cast the double value to
convert it into an int
int a = (int)2.6;// succeeds! (a = 2)


Casting tells the compiler that you want the
loss of precision to happen
You can always store an int into a double




There are operations beyond +, -, *, /, and %
that you probably want to do with numbers
Java has those built-in because the computer
can do those directly
A number of other operations can be done by
calling methods
Methods will end up being very important to
us later




A method is a piece of Java code that has
been packaged up so that you can use it over
and over
Usually, a method will take some input and
give some output
System.out.println() is an example
of a method
Using a method (calling a method) always
requires parentheses




The sin() method allows you to find the
sine of an angle (in radians)
This method is inside the Math class
The answer that it gives back is of type
double
To use it, you might type the following:
double value = Math.sin( 2.4 );
Unless the method is
inside your class,
you must supply a
class name and a dot
If your method takes
input, you put it inside
the parentheses, if not,
you leave them empty
result = class.method( input );
You can store the
result of the method,
as long as the variable
matches the type that
the method gives back
Next, you must give
the method name
that you are calling





In Java, the conversion of a double into an
int does not use rounding
As in the case of integer division, the
fractional part is dropped
For positive numbers, that's like using floor
For negative numbers, that's like using
ceiling
The right way to do rounding is to call
Math.round()
double x = 2.6;
int a = (int)Math.round(x);
// rounds
Return type
Name
Job
double
sin( double theta )
Find the sine of angle theta
double
cos( double theta )
Find the cosine of angle theta
double
tan( double theta )
Find the tangent of angle theta
double
exp( double a )
Raise e to the power of a (ea)
double
log( double a )
Find the natural log of a
double
pow( double a, double b )
long
round( double a )
Raise a to the power of b (ab)
Round a to the nearest integer
double
random()
double
sqrt( double a )
double
toDegrees( double radians )
Convert radians to degrees
double
toRadians( double degrees )
Convert degrees to radians
Create a random number in [0, 1)
Find the square root of a

Compute the hypotenuse of a triangle
 a2 + b 2 = c 2
c
a
b




The boolean type seems so simple
What on earth would we want to do with it?
Just like numerical types, we can combine
booleans in various ways
You might be familiar with these operations if
you have taken a course in logic


The NOT operator
Changes a true into a false or a false
into a true
x
!x
true
false
false
true


We can combine statements in logic together
to make other interesting statements
The way we combine them makes a
difference, e.g.
 Politicians lie.
 Cast iron sinks.
(True)
(True)
 Politicians lie in cast iron sinks.
(Absurd)



The AND operator
It gives back true only if both things being
combined are true
If I can swim AND the pool is not filled with
acid, then I will survive
x
y
x && y
true
true
true
true
false
false
false
true
false
false
false
false



The OR operator
It gives back true if either or both things
being combined are true
If I get punched in the face OR kicked in the
stomach, then I will be in pain
x
y
x || y
true
true
true
true
false
true
false
true
true
false
false
false



The XOR operator, sort of like what people often
mean when they say "or" in English
It gives back true if one but not both things
are true
If I get 1 apple XOR 3 oranges, then I will have an
odd number of fruit
x
y
x ^ y
true
true
false
true
false
true
false
true
true
false
false
false
(!true && (false^(false||true)))

Is this expression true or false?

It's false

In some circumstances, Java doesn't check the
whole expression:

(true || (some complicated expression))
 Ignores everything after || and gives back true

(false && (some complicated expression))
 Ignores everything after && and gives back false


Multiplication and division don't seem to
make sense
We can increment and decrement a char
char letter;
letter = 'x';
letter++;
letter++;
letter++;
//
//
//
//
letter
letter
letter
letter
contains
contains
contains
contains
'x'
'y'
'z'
?

It is possible to convert a char into an int
int number;
number = 'a';

// letter contains 97
It can be more useful to get the offset from a
starting point
char letter = 'r';
int number;
number = letter – 'a' + 1;
//number is 18





Everything in the computer is 1's
and 0's
Each character has a number
associated with it
These numbers can be listed
in tables
The ASCII table only covers 7
bits of information (0-127)
NEVER EVER TYPE THESE
NUMBERS IN CODE




Remember that we use single quotes to
designate a char literal: 'z'
What if you want to use the apostrophe
character ( ' )?
 apostrophe:
'\''
 tab:
 newline:
'\t'
'\n'
What if you want to use characters that can't be
printed, like tab or newline?
The backslash is a message that a special
command called an escape sequence is coming



You can put escape sequences into String
literals as well
You do not have to escape apostrophes in a
String
But you do have to escape quotation marks
String blanks = "\t\t\t\t\n";
String quote = "He said, \"Attack!\"";



Finish boolean and char operations
String operations
Wrapper classes


Keep reading Chapter 3 of the textbook
Get started on Project 1