{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# rec3 - CS 177 Week 3 Recitation Slides Basic Math...

This preview shows pages 1–13. Sign up to view the full content.

1 CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Announcements Project 1 was posted last Friday. It is due on February, 4 th at 9pm TAs started consulting hours this week Tue-Thu 6:00pm to 9:00 pm For the project, make sure to: Properly indent your code In Dr. Java, select all (CTRL+A) and then press TAB Comment the variables you use YOU WILL LOSE POINTS IF THIS IS NOT DONE!
3 QUESTIONS???

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Basic Operations Recap
Operations On Integers +, -, *, / Addition, subtraction, multiplication and division Note that division of integers drops the fractional part +=, -=, *=, /= a += 4; a = a + 4; c *= 5; c = c * 5; ++, -- a ++; a = a + 1; % Remainder of division a = 10 % 3; // a contains 1 5 b -= 3; b = b - 3; d /= 2; b = b / 2; b --; b = b - 1;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Question What are the ways we can write to increment variable a by 1? a = a + 1; a += 1; a ++; 6
7 Basic Operations w/Integers Code Example 1 public class IntegerOperations { public static void main(String[] args) { int a; int b; a = 13 + 20; // a contains 33 b = a / 11; // b contains 3 System.out.println("a = " + a); System.out.println("b = " + b); a -= 7; System.out.println("a = " + a); b = a / b; // Result = 8.66 but int type means 8 b++; // Increment b from 8 to 9 System.out.println("b = " + b); } } Output a = 33 b = 3 a = 26 b = 9

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 Basic Operations On Doubles Same operations as integers But the fractional part is retained
9 Basic Operations w/Doubles Code Example 1 public class DoubleOperations { public static void main(String[] args) { double a; double b; double c; double d; a = 4.0 / 3.0; b = a - 1; c = b + b + b * 2; d = 9 / 4; d--; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); } } Output a = 1.3333333333333333 b = 0.3333333333333333 c = 1.3333333333333333 d = 1.0 Question Variable d is a double or an integer?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 Casting in Java int a = (int) 1.8; // convert double 1.8 into an int 1 Convert one data type to another loss of precision float int, number is rounded down Let the Java compiler know that you are aware that you are going to lose some information, but you are ok with that.
11 Math Library in Java Java contains a Math class that includes a variety of math functions that can be used to perform other operations on integers and/or doubles The term “library” just means that it is a collection of predefined methods/functions that a programmer can leverage Brief example: double x = 16.0; double y; y = Math.sqrt(x); // y will be 4.0 after this line executes

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
12 Math Library in Java
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 29

rec3 - CS 177 Week 3 Recitation Slides Basic Math...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online