week03

# week03 - Week 3 Basic Operations CS 177 1 What did we talk...

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

CS 177 Week 3: Basic Operations 1

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

View Full Document
What did we talk about last week? Data representation Built-in types int double boolean char String Literals 2
Basic operations In Java , each data type has a set of basic operations you are allowed to perform It is not possible to define new operations or change how the operations behave Some programming languages allow this, but not Java 3

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

View Full Document
Operations for each type We are going to consider the basic operations for numerical types: int double 4
The + Operator for int Use the + operator to add two int s together int a; int b; a = 5 + 6; // a contains 11 b = a + 3; // b contains 14 a + b; // not allowed, does nothing a = a + 1; // a contains 12, and b? 5

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

View Full Document
Shortcuts Some expressions are used so often that Java gives us a short cut x = x + y; can be written x += y; x = x + 1; can be written x++; int x; x = 6; // x contains 6 x += 4; // x contains 10 x++; // x contains 11 6
The - Operator for int Exactly like + except performs subtraction int a; int b; a = 6 - 5; // a contains 1 b = 3 - a; // b contains 2 a -= 10; // shortcut for a = a – 10; a--; // shortcut for a = a – 1; 7

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

View Full Document
The * Operator for int The * operator performs multiplication int a; int b; a = 5 * 6; // a contains 30 b = a * 3; // b contains 90 a *= 2; // shortcut for a = a * 2; 8
The / Operator for int The / operator performs integer division when used with two int s Not the same as regular division The factional part is dropped, not rounded int a; int b; a = 9; // a contains 9 b = a / 2; // b contains 4 a /= 2; // shortcut for a = a / 2; 9

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

View Full Document
The % Operator for int The % operator is the mod operator It returns the remainder after division int a; int b; a = 38; // a contains 38 b = a % 5; // b contains 3 a %= 2; // shortcut for a = a % 2; 10
Area example Compute the area of a rectangle area = width * length; length width Area 11

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

View Full Document
Conversion example If you run 26 miles, how many feet is that? feet = 26 * 5280; 12
The + Operator for double Exactly the same as + for int, except now you can have fractional parts double a; double b; a = 3.14159; // a contains 3.14159 b = a + 2.1; // b contains 5.24159 a += 1.6; // shortcut for a = a + 1.6; a++; // shortcut for a = a + 1.0; 13

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

View Full Document
The and * Operator for double No surprises here They do subtraction and multiplication double a; double b; a = 3.14159; // a contains 3.14159 b = a - 2.1; // b contains 1.04159 a = b * 0.5; // a contains 0.520795 14
The / Operator for double Unlike int , this division does have fractional parts double a; double b; a = 9; // a contains 9.0 b = a / 2; // b contains 4.5 b = 9 / 2; // b contains 4.0 15

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

View Full Document
Area example Compute the area of a triangle area = 1.0/2.0 * base * height; base height Area 16
Given a temperature in Celsius, what is the equivalent in Fahrenheit? TF = (9/5)TC + 32

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 49

week03 - Week 3 Basic Operations CS 177 1 What did we talk...

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

View Full Document
Ask a homework question - tutors are online