Summer Institute for Computing Education - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Summer Institute for Computing Education

Description:

Repetition statements allow us to run. a statement or a block of statements multiple times ... for(int k=0; k 4; k ) { forward(len); turnRight(); Exercise 1 ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 42
Provided by: barbar224
Learn more at: http://www.cs.umb.edu
Category:

less

Transcript and Presenter's Notes

Title: Summer Institute for Computing Education


1
Lecture 7
2
Review
  • Homework 1 (sample solution)
  • Project 1 will be assigned next week
  • Draw a picture (whatever you want)in the world
    by using turtles
  • e,g,.
  • http//www.cs.umb.edu/cs110/projects/1/gallery/
  • http//www.cs.umb.edu/cs110/projects/2/gallery/

3
Review (Loops)
  • Repetition statements allow us to runa statement
    or a block of statements multiple times
  • Often we call them as loops
  • for
  • while
  • do

4
Example of for Loop
public class Turtle public void
drawSquare( int len ) for(int i0
i lt 4 i) forward( len )
turnRight()

Repeat thesestatements4 times
Count starts from 0 Add one for each repeat 4 is
not included
5
Example of for Loop
public class Turtle public void
printNumber( int num ) for(int
i0 i lt 10 i) System.out.pritnln(
num )
You can specify the printednumber when you
usethis method
6
Exercise 1 in lecture 6
public class Test public static void
main(String args) World w new
World() Turtle t new Turtle(w) for(int
k0 k lt 10 k)
t.drawSquare(100) t.turn(20)
public class Turtle public void
drawSquare(int len) for(int k0 k lt 4
k) forward(len)
turnRight()
7
Exercise 1 in lecture 6 (Another way)
public class Test public static void
main(String args) World w new
World() Turtle t new Turtle(w) t.drawP
icture()
public class Turtle public void drawPicture()
for(int k0 k lt 10 k)
drawSquare(100) turn(20) public
void drawSquare(int len) for(int k0 k lt
4 k) forward(len)
turnRight()
8
Todays topic
  • More on Arithmetic Expression
  • - Expression
  • - Operators
  • Precedence
  • Math class

9
Data type
  • Integer , -1, 0, 1,
  • e.g. int total
  • int number 5
  • Double , -1, , -0.5, , 0, , 0.5, , 1,
  • e.g. double average
  • double radius 2.4

10
Expressions
  • An expression is a combination of one or more
    operators and operands

Addition Subtraction - Multiplication Divisi
on / Remainder
  • 4
  • / 2
  • 2.3 5

operators
operands
11
Assignment
  • The result of expression is assigned toa
    variable by using symbol

int x
Variable declaration
x 5 3
Assignment
Variable x contains a value of 8
Note Somewhat different from math
12
Assignment (integer)
  • If both operands are integers, the result is an
    integer

5 7 int result result 5 7
result ?
int a 5 int b 7 int c c a b
c ?
13
Assignment (double)
  • If both operands are double (floating point), the
    result is a double

5.7 2.5 double result result 5.7 2.5
result ?
double a 5.7 double b 2.5 double c c a
b
c ?
14
Division and Remainder
  • If both operands to the division operator (/) are
    integers, the result is an integer (the
    fractional part is discarded)
  • The remainder operator () returns the remainder
    after division

14 / 3 equals
4
0
8 / 12 equals
2
14 3 equals
8
8 12 equals
15
Operator Precedence
  • Operators can be combined into complex
    expressions
  • result 3 5.1 / 2 10.1
  • Operators have a precedence which determines the
    order in which they are evaluated

16
Operator Precedence
  • Multiplication, division, and remainder are
    evaluated prior to addition and subtraction
  • result 3 5 1
  • Arithmetic operators with the same precedence are
    evaluated from left to right, but parentheses can
    be used to force the evaluation order
  • result 2 (3 5) 1

17
Examples
  • What is the order of evaluation in the following
    expressions?

a b c d e
a b c - d / e
1
4
3
2
3
2
4
1
a / (b c) - d e
2
3
4
1
a / (b (c (d - e)))
4
1
2
3
18
  • You should be careful for this difference

Math Java
10 / 2 5
10 / (2 5)
19
Assignment Revisited
  • The assignment operator has a lower precedence
    than the arithmetic operators

First the expression on the right hand side of
the operator is evaluated
answer sum / 4 10 number
1
4
3
2
Then the result is stored in the variable on the
left hand side
20
Assignment Revisited
  • The right and left hand sides of an assignment
    statement can contain the same variable

First, one is added to the original value of count
count count 1
Then the result is stored back into
count (overwriting the original value)
21
Methods of Math class
// return absolute value of num int abs(int
num) // return value of square root and
power double sqrt(double num) double pow(double
num, double power)
22
  • int abs(int num)

This method calculates the absolute of the
entered number
-10
How to use?
Math.abs( -10 )
23
Example
-10
  • int negative -10
  • int value Math.abs( negative )
  • System.out.println( The result is value)

The result is 10
24
  • double sqrt(double num)

This method calculates the square root of the
entered number
How to use Math.sqrt( 6.25 )
25
Example
  • double area 2.25
  • double value2 Math.sqrt( area )
  • System.out.println( value2 is value2)

2.25
value2 is 1.5
26
  • double pow(double num, double power)

This method calculates the power of the entered
number
How to use Math.pow( 2.0, 3.0 )
23
27
Example
pow method takes two variables 32
  • int three 3
  • double square Math.pow( three, 2 )
  • double cube Math.pow( three, 3.0 )
  • System.out.println( 3 square is square)
  • System.out.println( 3 cube is cube)

3 square is 9.0
3 cube is 27.0
28
  • Exercise!!!

29
Exercise 1 (Test.java)
public class Test public static
void main(String args)
int num1 double num2 num1
10 5 7 / 3 System.out.println( num1
) num2 2.5 (10.1 -3.7)
System.out.println( num2 )
30
Exercise 1 (Test.java)
public class Test public static
void main(String args)
int num1 double num2 num1
10 5 7 / 3 System.out.println( num1
is num1 ) num2 2.5 (10.1
-3.7) System.out.println( num2 is
num2 )
You can print out whatever you want by using
double quotation marks
31
Exercise 2
Calculate the average score of final
exams e.g. English 90 Math 86 Science
92 History 78 Spanish 83 Average ?
32
Exercise 2 (sample codes)
public class Test public static
void main(String args) int num
5 double average average
(9086927883) / num
System.out.println( average )
double? int / int
Is this correct ?
33
Exercise 2 (sample codes)
public class Test public static
void main(String args) int num
5 double average, total total
9086927883 average total / num
System.out.println( Average is average
)
Is this correct ?
34
Exercise 3
  • Lets calculate the following equations
  • (2.3)2 (3.5)3
  • How to write in Java program?

35
Exercise 3 (sample codes)
public class Test public static
void main(String args)
int num1 double num2 num2
Math.pow( 2.3, 2 ) Math.pow( 3.5, 3 )
System.out.println( The result is num2 )

36
Exercise 4
  • Lets solve the following quadratic equations
  • 3X2 15
  • ? X2 15 / 3
  • ? X 5
  • How to write in Java program?

37
Exercise 4 (sample codes)
public class Test public static
void main(String args)
int num1 double num2 num2
Math.sqrt( 5 ) System.out.println( 3X2
15) System.out.println( X is num2
)
38
Challenge
  • Lets compute the distance between two points
  • P1 (4, 7)
  • P2 (-6, -2)

39
P1 (X1, Y1)P2 (X2, Y2) distance (X1 X2)2
(Y1 Y2)2
40
  • So, how to compute the distance in Java ???
  • The result will be double number, lets store the
    result into double variable (it should be
    declared before)
  • Then print out the distance
  • Try to report in details like
  • e.g. The distance between (-1,3) and (2, -1) is
    5.0

distance (X1 X2)2 (Y1 Y2)2
dist Math.sqrt( Math.pow(X1-X2,2)
Math.pow(Y1-Y2,2) )
41
Sample codes
  • public class Test public static void
    main(String args) // declare
    variables and // assign values into
    variables representing each point P1 and P2
    int x1 4 int y1 7 int x2
    -6 int y2 -2
  • double dist // caluclate the
    distance and print out the result dist
    Math.sqrt( Math.pow(x1-x2, 2) Math.pow(y1-y2,
    2) )
  • System.out.println(The distance between
    P1( x1 , y1 )
    and P2( x2 , y2 ) is
    dist )
Write a Comment
User Comments (0)
About PowerShow.com