Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations."— Presentation transcript:

1 Week 2 - Friday

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

3

4

5  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 double x = 5.74961; System.out.format("%.2f", x); //prints 5.75 System.out.format("Total = $%.2f", 15.7777); //prints Total = $15.78

6 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

7

8  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; int a = 31; int b = 16; int c = 1; int d = 2; a = b + c * d – a / b / d;

9  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; int a = 31; int b = 16; int c = 1; int d = 2; a = (((b + c) * d) – a / b) / d;

10  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)

11  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 double x = 2.6; int a = (int)(x + 0.5);// rounds

12

13  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

14  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

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

16 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 result = class.method( input );

17 Return typeNameJob doublesin( double theta ) Find the sine of angle theta doublecos( double theta ) Find the cosine of angle theta doubletan( double theta ) Find the tangent of angle theta doubleexp( double a ) Raise e to the power of a (e a ) doublelog( double a ) Find the natural log of a doublepow( double a, double b ) Raise a to the power of b (a b ) longround( double a ) Round a to the nearest integer doublerandom() Create a random number in [0, 1) doublesqrt( double a ) Find the square root of a doubletoDegrees( double radians ) Convert radians to degrees doubletoRadians( double degrees ) Convert degrees to radians

18  Compute the hypotenuse of a triangle  a 2 + b 2 = c 2 b a c

19

20

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

22  Keep reading Chapter 3 of the textbook  Get started on Project 1


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

Similar presentations


Ads by Google