week03

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

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

View Full Document Right Arrow Icon
CS 177 Week 3: Basic Operations 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
What did we talk about last week? Data representation Built-in types int double boolean char String Literals 2
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Operations for each type We are going to consider the basic operations for numerical types: int double 4
Background image of page 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
Area example Compute the area of a rectangle area = width * length; length width Area 11
Background image of page 11

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

View Full DocumentRight Arrow Icon
Conversion example If you run 26 miles, how many feet is that? feet = 26 * 5280; 12
Background image of page 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
Background image of page 13

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 15

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

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

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

View Full DocumentRight Arrow Icon
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online