+ All Categories
Home > Documents > Week 2 - Friday. What did we talk about last time? Using Scanner to get input Basic math...

Week 2 - Friday. What did we talk about last time? Using Scanner to get input Basic math...

Date post: 02-Jan-2016
Category:
Upload: audra-lindsey
View: 214 times
Download: 1 times
Share this document with a friend
Popular Tags:
22
CS 121 Week 2 - Friday
Transcript
Page 1: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

CS 121Week 2 - Friday

Page 2: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Last time

What did we talk about last time? Using Scanner to get input Basic math operations

Page 3: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Questions?

Page 4: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Project 1

Page 5: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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

Page 6: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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

Page 7: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Examples

Page 8: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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;

Page 9: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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;

Page 10: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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)

Page 11: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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

Page 12: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Advanced operations on numbers

Page 13: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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

Page 14: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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

Page 15: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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 );

Page 16: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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 );

Page 17: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

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

Page 18: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Pythagorean theorem

Compute the hypotenuse of a triangle a2 + b2 = c2

b

a

c

Page 19: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Lab 2

Page 20: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Upcoming

Page 21: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Next time…

Next time we will talk about operations on: boolean values char values String values

Page 22: Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Reminders

Keep reading Chapter 3 of the textbook

Get started on Project 1


Recommended