Today s topic
This presentation is the property of its rightful owner.
Sponsored Links
1 / 32

Today’s topic: PowerPoint PPT Presentation


  • 44 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

Today’s topic:

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Today s topic

Today’s topic:

  • Arithmetic expressions


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.


Arithmetic expressions1

Arithmetic expressions

  • How would we code the following?

    • Let n be 2 and k be 2n+1.


Arithmetic expressions2

Arithmetic expressions

  • How would we code the following?

    • Let n be 2 and k be 2n+1.

      int n = 2;

      int k = 2*n+1;

      Or

      int n, k;

      n = 2;

      k = 2 * n + 1;


Arithmetic expressions3

Arithmetic expressions

  • How would we code the following?

    • Let n be 2 and k be 2n+1.

      int n = 2;

      int k = 2*n+1;

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

      int 2 = n;

      Or

      2*n+1 = k;


Arithmetic expressions4

Arithmetic expressions

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

  • How would we code this in Java?


Arithmetic expressions5

Arithmetic expressions

  • Example from physics:

    • F = M A (force = mass x acceleration).

  • How would we code this in Java?

    double mass = 12.0;

    double acc = 13.9;

    double force = mass * acceleration;

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


Differences between int and double

Differences between int and double

  • int (integer) and double (real numbers) are different.

    • int is a subset of double but ints are much faster to calculate than doubles

    • So use ints whenever you can but be aware of the following:

      int n = 5 / 2;

      int k = 9 / 10;


Differences between int and double1

Differences between int and double

  • 5 is an int.

  • 5.1 is a double.

  • 5.0 is a double.

  • 5.7f is a float

  • Range of values:

    • int-2 billionto+2 billion

    • double4.9x10-324to1.8x10308

    • float1.4x10-45to3.4x1038


Converting doubles to ints

Converting doubles to ints

  • When we convert, we will typically lose something.

    • Consider converting 12.9 to an int. There is no int 12.9.

    • So the compiler will issue a message. To inform the compiler that we really wish to do this, we can use a cast.

      int i;

      double d = 12.9;

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


Converting doubles to ints1

Converting doubles to ints

inti;

double d = 12.9;

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

//how can we round instead?


Let s check what we ve learned so far using jgrasp

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

class FirstExample {

public static void main ( String[] params ) {

//arithmetic

//output

}

}


Ints and mod

ints and mod (%)

  • What is the result of the following:

    int k = 5 / 2;

    What is the remainder?

    % is used to calculate the remainder.

    int remainder = 5 % 2;


Float vs double

Float vs. double

  • Recall range of values:

    • double4.9x10-324to1.8x10+308

    • float1.4x10-45to3.4x10+38

    • Recall that we used a cast to convert a double to an int:

      • double d = 12.9;

      • inti = (int) d;

      • This is because a double won’t fit into an int. Will a double fit into a float?


Increment and decrement operators and

Increment and decrement operators (++ and --)

  • What does the following do:

    • inti = 12;

    • i = i + 2;


Increment and decrement operators and1

Increment and decrement operators (++ and --)

  • We often see code like the following:

    int value = 12;

    value = value + 1;

    value = value + 1;

    value = value + 1;


Increment and decrement operators and2

Increment and decrement operators (++ and --)

  • We often see code like the following:

    int value = 12;

    value = value + 1;

    value = value + 1;

    value = value + 1;

int value = 12;

value++; //post increment

++value; //pre increment

value++;


Increment and decrement operators and3

Increment and decrement operators (++ and --)

  • We often see code like the following:

    int value = 12;

    value = value - 1;

    value = value - 1;

    value = value - 1;

int value = 12;

value--; //post decrement

--value; //pre decrement

value--;


Increment and decrement operators and4

Increment and decrement operators (++ and --)

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

    int where = 92;

    int k = 7;

    where = k++;

    int j = --where;


Convert the following to java expressions

Convert the following to Java expressions:


Write a program that

Write a program that . . .

class MyProgramTemplate {

public static void main ( String param[] ) {

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

} //end main

} //end class


Write a program that1

Write a program that . . .

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

Calculates the circumference.

Calculates the area.

Prints out the radius, circumference, and area.

class MyProgramTemplate {

public static void main ( String param[] ) {

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

} //end main

} //end class


Write a program that2

Write a program that . . .

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

Calculates the circumference.

Calculates the area.

Prints out the radius, circumference, and area.

class MyProgramTemplate {

public static void main ( String param[] ) {

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

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

//Calculate the circumference.

//Calculate the area.

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

} //end main

} //end class


Write a program that3

Write a program that . . .

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

class MyProgramTemplate {

public static void main ( String param[] ) {

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

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

//Calculate the circumference.

//Calculate the area.

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

} //end main

} //end class


Write a program that4

Write a program that . . .

public static void main ( String param[] ) {

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

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

double radius = 12.7;

//Calculate the circumference.

//Calculate the area.

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

} //end main


Write a program that5

Write a program that . . .

public static void main ( String param[] ) {

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

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

double radius = 12.7;

//Calculate the circumference.

double circ = ?;

//Calculate the area.

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

} //end main


Write a program that6

Write a program that . . .

public static void main ( String param[] ) {

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

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

double radius = 12.7;

//Calculate the circumference.

double circ = 2 * 3.14 * radius;

//Calculate the area.

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

} //end main


Write a program that7

Write a program that . . .

public static void main ( String param[] ) {

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

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

double radius = 12.7;

//Calculate the circumference.

double circ = 2 * Math.PI * radius;

//Calculate the area.

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

} //end main


Write a program that8

Write a program that . . .

public static void main ( String param[] ) {

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

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

double radius = 12.7;

//Calculate the circumference.

double circ = 2 * Math.PI * radius;

//Calculate the area.

double area = Math.PI * radius * radius;

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

} //end main


Write a program that9

Write a program that . . .

public static void main ( String param[] ) {

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

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

double radius = 12.7;

//Calculate the circumference.

double circ = 2 * Math.PI * radius;

//Calculate the area.

double area = Math.PI * radius * radius;

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

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

?

} //end main


Write a program that10

Write a program that . . .

public static void main ( String param[] ) {

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

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

double radius = 12.7;

//Calculate the circumference.

double circ = 2 * Math.PI * radius;

//Calculate the area.

double area = Math.PI * radius * radius;

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

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

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

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

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

} //end main


Write a program that11

Write a program that . . .

public static void main ( String param[] ) {

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

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

double radius = 12.7;

//Calculate the circumference.

double circ = 2 * Math.PI * radius;

//Calculate the area.

double area = Math.PI * radius * radius;

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

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

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

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

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

} //end main


  • Login