Today’s topic:

32
Today’s topic: Today’s topic: Arithmetic expressions Arithmetic expressions

description

Today’s topic:. Arithmetic expressions. Arithmetic expressions. binary (two arguments) arithmetic operators +, -, *, /, % (mod or modulus) unary (one arg ) operators -, + Order of operations just like you’d expect: Please Excuse My Dear Aunt Sadie. Can use () to override. - PowerPoint PPT Presentation

Transcript of Today’s topic:

Page 1: Today’s topic:

Today’s topic:Today’s topic:

Arithmetic expressionsArithmetic expressions

Page 2: Today’s topic:

Arithmetic expressionsArithmetic expressions

binary (two arguments) arithmetic binary (two arguments) arithmetic operatorsoperators +, -, *, /, % (mod or modulus)+, -, *, /, % (mod or modulus)

unary (one arg) operatorsunary (one arg) operators -, +-, +

Order of operations just like you’d expect:Order of operations just like you’d expect: Please Excuse My Dear Aunt Sadie.Please Excuse My Dear Aunt Sadie. Can use () to override.Can use () to override.

Page 3: Today’s topic:

Arithmetic expressionsArithmetic expressions

How would we code the following?How would we code the following? Let n be 2 and k be 2n+1.Let n be 2 and k be 2n+1.

Page 4: Today’s topic:

Arithmetic expressionsArithmetic expressions

How would we code the following?How would we code the following? Let n be 2 and k be 2n+1.Let n be 2 and k be 2n+1.

int n = 2;int n = 2;int k = 2*n+1;int k = 2*n+1;

OrOr

int n, k;int n, k;n = 2;n = 2;k = 2 * n + 1;k = 2 * n + 1;

Page 5: Today’s topic:

Arithmetic expressionsArithmetic expressions

How would we code the following?How would we code the following? Let n be 2 and k be 2n+1.Let n be 2 and k be 2n+1.

int n = 2;int n = 2;int k = 2*n+1;int k = 2*n+1;

Note: The rhs of the equals is always evaluated first Note: The rhs of the equals is always evaluated first and is assigned to the lhs. Therefore, you cannot and is assigned to the lhs. Therefore, you cannot say:say:int 2 = n;int 2 = n;

OrOr2*n+1 = k;2*n+1 = k;

Page 6: Today’s topic:

Arithmetic expressionsArithmetic expressions

Example from physics: F = M A (force = Example from physics: F = M A (force = mass x acceleration).mass x acceleration).

How would we code this in Java?How would we code this in Java?

Page 7: Today’s topic:

Arithmetic expressionsArithmetic expressions

Example from physics:Example from physics: F = M A (force = mass x acceleration).F = M A (force = mass x acceleration).

How would we code this in Java?How would we code this in Java?

double mass = 12.0;double mass = 12.0;

double acc = 13.9;double acc = 13.9;

double force = mass * acceleration;double force = mass * acceleration;

System.out.println( "force = " + force );System.out.println( "force = " + force );

Page 8: Today’s topic:

Differences between int and doubleDifferences between int and double

int (integer) and double (real numbers) are int (integer) and double (real numbers) are different.different. int is a subset of double but ints are much int is a subset of double but ints are much

faster to calculate than doublesfaster to calculate than doubles So use ints whenever you can but be aware So use ints whenever you can but be aware

of the following:of the following:

int n = 5 / 2;int n = 5 / 2;

int k = 9 / 10;int k = 9 / 10;

Page 9: Today’s topic:

Differences between int and doubleDifferences between int and double

5 is an int.5 is an int. 5.1 is a double.5.1 is a double. 5.0 is a double.5.0 is a double. 5.7f is a float5.7f is a float

Range of values:Range of values: intint -2 billion-2 billion toto +2 billion+2 billion doubledouble 4.9x104.9x10-324-324 toto 1.8x101.8x10308308

floatfloat 1.4x101.4x10-45-45 toto 3.4x103.4x103838

Page 10: Today’s topic:

Converting doubles to intsConverting doubles to ints

When we convert, we will typically lose When we convert, we will typically lose something.something. Consider converting 12.9 to an int. There is Consider converting 12.9 to an int. There is

no int 12.9.no int 12.9. So the compiler will issue a message. To So the compiler will issue a message. To

inform the compiler that we really wish to do inform the compiler that we really wish to do this, we can use a this, we can use a castcast..

int i;int i;double d = 12.9;double d = 12.9;i = (int) d; //What do you think is in i = (int) d; //What do you think is in ii now? now?

Page 11: Today’s topic:

Converting doubles to intsConverting doubles to ints

int i;int i;

double d = 12.9;double d = 12.9;

i = (int) d; //What do you think is in i = (int) d; //What do you think is in ii now? now?

//how can we round instead?//how can we round instead?

Page 12: Today’s topic:

Let’s check what we’ve learned Let’s check what we’ve learned so far (using jGrasp).so far (using jGrasp).

class FirstExample {class FirstExample {

public static void main ( String[] params ) {public static void main ( String[] params ) {

//arithmetic//arithmetic

//output//output

}}

}}

Page 13: Today’s topic:

ints and mod (%)ints and mod (%)

What is the result of the following:What is the result of the following:int k = 5 / 2;int k = 5 / 2;

What is the remainder?What is the remainder?

% is used to calculate the remainder.% is used to calculate the remainder.

int remainder = 5 % 2;int remainder = 5 % 2;

Page 14: Today’s topic:

Float vs. doubleFloat vs. double

Recall range of values:Recall range of values: doubledouble 4.9x10-3244.9x10-324 toto

1.8x10+3081.8x10+308 floatfloat 1.4x10-451.4x10-45 toto 3.4x10+383.4x10+38 Recall that we used a cast to convert a double Recall that we used a cast to convert a double

to an int:to an int: double d = 12.9;double d = 12.9; int i = (int) d;int i = (int) d; This is because a double won’t fit into an int. Will a This is because a double won’t fit into an int. Will a

double fit into a float?double fit into a float?

Page 15: Today’s topic:

Increment and decrement Increment and decrement operators (++ and --)operators (++ and --)

What does the following do:What does the following do: int i = 12;int i = 12; i = i + 2;i = i + 2;

Page 16: Today’s topic:

Increment and decrement Increment and decrement operators (++ and --)operators (++ and --)

We often see code like the following:We often see code like the following:int value = 12;int value = 12;

……

value = value + 1;value = value + 1;

……

value = value + 1;value = value + 1;

……

value = value + 1;value = value + 1;

Page 17: Today’s topic:

Increment and decrement Increment and decrement operators (++ and --)operators (++ and --)

We often see code We often see code like the following:like the following:int value = 12;int value = 12;

……

value = value + 1;value = value + 1;

……

value = value + 1;value = value + 1;

……

value = value + 1;value = value + 1;

int value = 12;int value = 12;

……

value++; //post incrementvalue++; //post increment

……

++value; //pre increment++value; //pre increment

……

value++;value++;

Page 18: Today’s topic:

Increment and decrement Increment and decrement operators (++ and --)operators (++ and --)

We often see code We often see code like the following:like the following:int value = 12;int value = 12;

……

value = value - 1;value = value - 1;

……

value = value - 1;value = value - 1;

……

value = value - 1;value = value - 1;

int value = 12;int value = 12;

……

value--; //post decrementvalue--; //post decrement

……

--value; //pre decrement--value; //pre decrement

……

value--;value--;

Page 19: Today’s topic:

Increment and decrement Increment and decrement operators (++ and --)operators (++ and --)

We can use these operators in We can use these operators in expressions as well (but don’t get carried expressions as well (but don’t get carried away):away):

int where = 92;int where = 92;

int k = 7;int k = 7;

where = k++;where = k++;

int j = --where;int j = --where;

Page 20: Today’s topic:

Convert the following to Java Convert the following to Java expressions:expressions:

2

37

3

3

x

xyd

yxc

yxb

xa

Page 21: Today’s topic:

Write a program that . . .Write a program that . . .

class MyProgramTemplate {class MyProgramTemplate {

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

} //end main} //end main

} //end class} //end class

Page 22: Today’s topic:

Write a program that . . .Write a program that . . .

1.1. Declares a variable for the radius of a circle with a value of 12.7.Declares a variable for the radius of a circle with a value of 12.7.

2.2. Calculates the circumference.Calculates the circumference.

3.3. Calculates the area.Calculates the area.

4.4. Prints out the radius, circumference, and area.Prints out the radius, circumference, and area.

class MyProgramTemplate {class MyProgramTemplate {

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

} //end main} //end main

} //end class} //end class

Page 23: Today’s topic:

Write a program that . . .Write a program that . . .

1.1. Declares a variable for the radius of a circle with a value of 12.7.Declares a variable for the radius of a circle with a value of 12.7.

2.2. Calculates the circumference.Calculates the circumference.

3.3. Calculates the area.Calculates the area.

4.4. Prints out the radius, circumference, and area.Prints out the radius, circumference, and area.

class MyProgramTemplate {class MyProgramTemplate {

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

//Declare a variable for the radius of a circle with a value of 12.7.//Declare a variable for the radius of a circle with a value of 12.7.

//Calculate the circumference.//Calculate the circumference.

//Calculate the area.//Calculate the area.

//Print out the radius, circumference, and area.//Print out the radius, circumference, and area.

} //end main} //end main

} //end class} //end class

Page 24: Today’s topic:

Write a program that . . .Write a program that . . .

class MyProgramTemplate {class MyProgramTemplate {

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

//Declare a variable for the radius of a circle with a value of 12.7.//Declare a variable for the radius of a circle with a value of 12.7.

//Calculate the circumference.//Calculate the circumference.

//Calculate the area.//Calculate the area.

//Print out the radius, circumference, and area.//Print out the radius, circumference, and area.

} //end main} //end main

} //end class} //end class

What types of things are radius, circumference, and area?

Page 25: Today’s topic:

Write a program that . . .Write a program that . . .

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

//Declare a variable for the radius of a circle with a value of 12.7.//Declare a variable for the radius of a circle with a value of 12.7.

double radius = 12.7;double radius = 12.7;

//Calculate the circumference.//Calculate the circumference.

//Calculate the area.//Calculate the area.

//Print out the radius, circumference, and area.//Print out the radius, circumference, and area.

} //end main} //end main

Page 26: Today’s topic:

Write a program that . . .Write a program that . . .

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

//Declare a variable for the radius of a circle with a value of 12.7.//Declare a variable for the radius of a circle with a value of 12.7.

double radius = 12.7;double radius = 12.7;

//Calculate the circumference.//Calculate the circumference.

double circ = ?;double circ = ?;

//Calculate the area.//Calculate the area.

//Print out the radius, circumference, and area.//Print out the radius, circumference, and area.

} //end main} //end main

Page 27: Today’s topic:

Write a program that . . .Write a program that . . .

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

//Declare a variable for the radius of a circle with a value of 12.7.//Declare a variable for the radius of a circle with a value of 12.7.

double radius = 12.7;double radius = 12.7;

//Calculate the circumference.//Calculate the circumference.

double circ = 2 * 3.14 * radius;double circ = 2 * 3.14 * radius;

//Calculate the area.//Calculate the area.

//Print out the radius, circumference, and area.//Print out the radius, circumference, and area.

} //end main} //end main

Page 28: Today’s topic:

Write a program that . . .Write a program that . . .

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

//Declare a variable for the radius of a circle with a value of 12.7.//Declare a variable for the radius of a circle with a value of 12.7.

double radius = 12.7;double radius = 12.7;

//Calculate the circumference.//Calculate the circumference.

double circ = 2 * Math.PI * radius;double circ = 2 * Math.PI * radius;

//Calculate the area.//Calculate the area.

//Print out the radius, circumference, and area.//Print out the radius, circumference, and area.

} //end main} //end main

Page 29: Today’s topic:

Write a program that . . .Write a program that . . .

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

//Declare a variable for the radius of a circle with a value of 12.7.//Declare a variable for the radius of a circle with a value of 12.7.

double radius = 12.7;double radius = 12.7;

//Calculate the circumference.//Calculate the circumference.

double circ = 2 * Math.PI * radius;double circ = 2 * Math.PI * radius;

//Calculate the area.//Calculate the area.

double area = Math.PI * radius * radius;double area = Math.PI * radius * radius;

//Print out the radius, circumference, and area.//Print out the radius, circumference, and area.

} //end main} //end main

Page 30: Today’s topic:

Write a program that . . .Write a program that . . .

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

//Declare a variable for the radius of a circle with a value of 12.7.//Declare a variable for the radius of a circle with a value of 12.7.

double radius = 12.7;double radius = 12.7;

//Calculate the circumference.//Calculate the circumference.

double circ = 2 * Math.PI * radius;double circ = 2 * Math.PI * radius;

//Calculate the area.//Calculate the area.

double area = Math.PI * radius * radius;double area = Math.PI * radius * radius;

//Print out the radius, circumference, and area.//Print out the radius, circumference, and area.

System.out.println( " radius = " + radius );System.out.println( " radius = " + radius );

??

} //end main} //end main

Page 31: Today’s topic:

Write a program that . . .Write a program that . . .

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

//Declare a variable for the radius of a circle with a value of 12.7.//Declare a variable for the radius of a circle with a value of 12.7.

double radius = 12.7;double radius = 12.7;

//Calculate the circumference.//Calculate the circumference.

double circ = 2 * Math.PI * radius;double circ = 2 * Math.PI * radius;

//Calculate the area.//Calculate the area.

double area = Math.PI * radius * radius;double area = Math.PI * radius * radius;

//Print out the radius, circumference, and area.//Print out the radius, circumference, and area.

System.out.println( " radius = " + radius );System.out.println( " radius = " + radius );

System.out.println( " circumference = " + circ );System.out.println( " circumference = " + circ );

System.out.println( " area = " + area );System.out.println( " area = " + area );

System.out.println( " Bye.");System.out.println( " Bye.");

} //end main} //end main

Page 32: Today’s topic:

Write a program that . . .Write a program that . . .

public static void main ( String param[] ) {public static void main ( String param[] ) {

System.out.println( "hi there" );System.out.println( "hi there" );

//Declare a variable for the radius of a circle with a value of 12.7.//Declare a variable for the radius of a circle with a value of 12.7.

double radius = 12.7;double radius = 12.7;

//Calculate the circumference.//Calculate the circumference.

double circ = 2 * Math.PI * radius;double circ = 2 * Math.PI * radius;

//Calculate the area.//Calculate the area.

double area = Math.PI * radius * radius;double area = Math.PI * radius * radius;

//Print out the radius, circumference, and area.//Print out the radius, circumference, and area.

System.out.print( " radius = " + radius + ", " );System.out.print( " radius = " + radius + ", " );

System.out.print( " circumference = " + circ + ", " );System.out.print( " circumference = " + circ + ", " );

System.out.println( " area = " + area + "." );System.out.println( " area = " + area + "." );

System.out.println( " Bye.");System.out.println( " Bye.");

} //end main} //end main