# 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.

## Presentation on theme: "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."— Presentation transcript:

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 e,g,. http://www.cs.umb.edu/cs110/projects/1/gallery/ http://www.cs.umb.edu/cs110/projects/2/gallery/

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

Example of for Loop public class Turtle { public void drawSquare( int len ) { for(int i=0; i < 4; i++) { forward( len ); turnRight(); } Repeat these statements 4 times Count starts from 0 Add one for each repeat 4 is not included

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 printed number when you use this method

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

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

Today’s topic More on Arithmetic Expression - Expression - Operators -Precedence -Math class

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;

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 operatorsoperands

Assignment The result of expression is assigned to a variable by using = symbol x = 5 + 3; int x; Variable declaration Assignment Note: Somewhat different from math Variable x contains a value of 8

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 = ?

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 = ?

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 8 / 12 equals 4 0 14 % 3 equals 8 % 12 equals 2 8

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

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;

Examples What is the order of evaluation in the following expressions? a + b + c + d + e 1432 a + b * c - d / e 3241 a / (b + c) - d % e 2341 a / (b * (c + (d - e))) 4123

You should be careful for this difference 10 2 * 5 = 25 10 2 * 5 = 1 10 / (2 * 5) 10 / 2 * 5 Math Java

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 Then the result is stored in the variable on the left hand side answer = sum / 4 + 10 * number; 1432

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 Then the result is stored back into count (overwriting the original value) count = count + 1;

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)

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

Example int negative = -10; int value = Math.abs( negative ); System.out.println( “The result is “ + value); The result is 10 | -10 |

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

Example double area = 2.25; double value2 = Math.sqrt( area ); System.out.println( “value2 is “ + value2); value2 is 1.5 2.25

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 2323

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 variables 3 2

Exercise!!!

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

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

Exercise 2 Calculate the average score of final exams e.g. English90 Math86 Science92 History78 Spanish83 Average ?

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

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 ?

Exercise 3 Let’s calculate the following equations (2.3) 2 + (3.5) 3 How to write in Java program?

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

Exercise 4 Let’s solve the following quadratic equations 3X 2 = 15  X 2 = 15 / 3  X = 5 How to write in Java program?

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

10 -10 X Y (4,7) (-6, -2) Challenge Let’s compute the distance between two points P1 (4, 7) P2 (-6, -2)

P1 (X 1, Y 1 ) P2 (X 2, Y 2 ) distance = (X 1 – X 2 ) 2 + (Y 1 – Y 2 ) 2 10 -10 X Y (4,7) (-6, -2)

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 distance Try to report in details like… e.g. The distance between (-1,3) and (2, -1) is 5.0 distance = (X 1 – X 2 ) 2 + (Y 1 – Y 2 ) 2 dist = Math.sqrt( Math.pow(X1-X2,2) + Math.pow(Y1-Y2,2) )

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

Download ppt "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."

Similar presentations