Top Banner
Lecture 7
41

Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Lecture 7

Page 2: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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/

Page 3: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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

Page 4: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Example of for Loop

public class Turtle{

public void drawSquare( int len ) {

for(int i=0; i < 4; i++) { forward( len );

turnRight(); }

} }

Repeat thesestatements

4 times

Count starts from 0Add one for each repeat

4 is not included

Page 5: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Example of for Loop

public class Turtle{

public void printNumber( int num ) {

for(int i=0; i < 10; i++) {

System.out.pritnln( num ); }

} }

You can specify the printednumber when you use

this method

Page 6: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Exercise 1 in lecture 6public class Test{ public static void main(String[]

args) { World w = new World();

Turtle t = new Turtle(w);

for(int k=0; k < 10; k++)

{ t.drawSquare(100); t.turn(20);}

}}

public class Turtle{

public void drawSquare(int len){

for(int k=0; k < 4; k++)

{ forward(len); turnRight();}

}}

Page 7: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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.drawPicture();

}}

public class Turtle{

public void drawPicture() {

for(int k=0; k < 10; k++)

{ drawSquare(100); turn(20);}

}

public void drawSquare(int len){

for(int k=0; k < 4; k++){ forward(len); turnRight();}

}}

Page 8: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Today’s topic

• More on Arithmetic Expression

- Expression

- Operators

- Precedence

- Math class

Page 9: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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;

Page 10: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Expressions

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

Addition +Subtraction -Multiplication *Division /Remainder %

3 + 4

4 / 2

2.3 * 5

operators operands

Page 11: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Assignment

• The result of expression is assigned toa variable by using = symbol

x = 5 + 3;

int x; Variable declaration

Assignment

Note: Somewhat different from math

Variable x contains a value of 8

Page 12: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Assignment (integer)

• If both operands are integers, the result is an integer

5 + 7

int result;result = 5 + 7;

int a = 5;int b = 7;int c;c = a + b;

result = ?

c = ?

Page 13: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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;

double a = 5.7;double b = 2.5;double c;c = a + b;

result = ?

c = ?

Page 14: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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 equals8 / 12 equals

40

14 % 3 equals8 % 12 equals

28

Page 15: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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

Page 16: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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;

Page 17: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Examples

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

a + b + c + d + e1 432

a + b * c - d / e3 241

a / (b + c) - d % e2 341

a / (b * (c + (d - e)))4 123

Page 18: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

• You should be careful for this difference

10 2 * 5 = 25

102 * 5

= 1 10 / (2 * 5)

10 / 2 * 5

Math Java

Page 19: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Assignment Revisited

• The assignment operator has a lower precedence than the arithmetic operators

First the expression on the right handside of the = operator is evaluated

Then the result is stored in thevariable on the left hand side

answer = sum / 4 + 10 * number;14 3 2

Page 20: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Assignment Revisited

• The right and left hand sides of an assignment statement can contain the same variable

First, one is added to theoriginal value of count

Then the result is stored back into count(overwriting the original value)

count = count + 1;

Page 21: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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)

Page 22: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

int abs(int num)

This method calculates the absolute of the entered number

How to use?

It takes one integer

| -10 |

The result will be an integer

Class name Method

dot

Math.abs( -10 );

Page 23: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Example

int negative = -10;

int value = Math.abs( negative );

System.out.println( “The result is “ + value);

The result is 10

| -10 |

Page 24: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

double sqrt(double num)

This method calculates the square root of the entered number

How to use

Math.sqrt( 6.25 );

It takes one double value or variableThe result will be a double

class name Method

Dot

6.25

Page 25: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Example

double area = 2.25;

double value2 = Math.sqrt( area );

System.out.println( “value2 is “ + value2);

value2 is 1.5

2.25

Page 26: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

double pow(double num, double power)

This method calculates the power of the entered number

How to use

Math.pow( 2.0, 3.0 );

It takes two double values or variablesThe result will be a double

Object name Method

Dot

23

Page 27: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Example

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

pow method takes two variables32

Page 28: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

• Exercise!!!

Page 29: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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 );

} }

Page 30: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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 wantby using double quotation marks

Page 31: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Exercise 2

Calculate the average score of final exams

e.g.English 90Math 86Science 92History 78Spanish 83

Average ?

Page 32: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Exercise 2 (sample codes)

public class Test{

public static void main(String args[]) {

int num = 5; double average; average = (90+86+92+78+83) / num;

System.out.println( average ); }

}

Is this correct ?

double? = int / int

Page 33: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Exercise 2 (sample codes)

public class Test{

public static void main(String args[]) {

int num = 5; double average, total; total = 90+86+92+78+83; average = total / num;

System.out.println( “Average is “ + average ); }

} Is this correct ?

Page 34: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Exercise 3

Let’s calculate the following equations

(2.3)2 + (3.5)3

How to write in Java program?

Page 35: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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 ); }

}

Page 36: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Exercise 4

Let’s solve the following quadratic equations

3X2 = 15

X2 = 15 / 3

X = 5

How to write in Java program?

Page 37: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Exercise 4 (sample codes)

public class Test{

public static void main(String args[]) {

int num1; double num2; num2 = Math.sqrt( 5 );

System.out.println( “3*X^2 = 15“); System.out.println( “X is “ + num2 );

} }

Page 38: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

10

10

-10

-10

X

Y

(4,7)

(-6, -2)

Challenge

• Let’s compute the distance between two pointsP1 (4, 7)

P2 (-6, -2)

Page 39: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

P1 (X1, Y1)P2 (X2, Y2)

distance = (X1 – X2)2 + (Y1 – Y2)2

10

10

-10

-10

X

Y

(4,7)

(-6, -2)

Page 40: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

So, how to compute the distance in Java ???

The result will be double number, let’s store the result into double variable (it should be declared before)

Then print out the distanceTry 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) )

Page 41: Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

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 ); }}

Sample codes