CS 121Week 2 - Friday
Last time
What did we talk about last time? Using Scanner to get input Basic math operations
Questions?
Project 1
System.out.format()
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()
If you want, you can include other things in the formatting string
double x = 5.74961;System.out.format("%.2f", x); //prints 5.75
System.out.format("Total = $%.2f", 15.7777); //prints Total = $15.78
Scanner review
There are three parts to using Scanner for input1. 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
Examples
Complex expressions
How complex can expressions get?
What’s the value of a? 18!
int a = 31;int b = 16;int c = 1;int d = 2;
a = b + c * d – a / b / d;
Complex expressions
Order of operations holds like in math
You can use parentheses to clarify or change the precedence
Now a is 16
int a = 31;int b = 16;int c = 1;int d = 2;
a = (((b + c) * d) – a / b) / d;
Casting
You cannot directly store a double value into an int variable
However, you can cast the double value to convert it into an int
Casting tells the compiler that you want the loss of precision to happen
You can always store an int into a double
int a = 2.6; // fails!
int a = (int)2.6; // succeeds! (a = 2)
Rounding
In Java, the conversion of a double into an int does not use rounding
As in the case of integer division, the value is always rounded down
You can think of this as using the floor function from math
If you want to round normally, you can simply add 0.5 before the cast
double x = 2.6;int a = (int)(x + 0.5); // rounds
Advanced operations on numbers
Adding is great, but…
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
Methods
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
Method example with sin()
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 );
If your method takes input, you put it inside the
parentheses, if not, you leave them
empty
Next, you must give the method name that you
are calling
Unless the method is inside your class, you must supply a
class name and a dot
You can store the result of the
method, as long as the variable
matches the type that the method
gives back
Method syntax
result = class.method( input );
Other Math methodsReturn 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 ) Raise a to the power of b (ab)
long round( double a ) Round a to the nearest integer
double random() Create a random number in [0, 1)
double sqrt( double a ) Find the square root of a
double toDegrees( double radians ) Convert radians to degrees
double toRadians( double degrees ) Convert degrees to radians
Pythagorean theorem
Compute the hypotenuse of a triangle a2 + b2 = c2
b
a
c
Lab 2
Upcoming
Next time…
Next time we will talk about operations on: boolean values char values String values
Reminders
Keep reading Chapter 3 of the textbook
Get started on Project 1