Download Lecture 7

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
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++)
{
Repeat these
forward( len );
statements
turnRight();
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 Turtle
{
}
public void drawSquare(int len)
{
for(int k=0; k < 4; k++)
{
forward(len);
turnRight();
}
}
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);
}
Exercise 1 in lecture 6 (Another way)
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();
}
}
public class Test
{
public static void main(String[] args)
{
World w = new World();
Turtle t = new Turtle(w);
t.drawPicture();
}
}
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
+
/
%
operators
3
+ 4
4
/
2
2.3 * 5
operands
Assignment
• The result of expression is assigned to
a variable by using = symbol
int x;
x = 5 + 3;
Variable declaration
Assignment
Variable x contains a value of 8
Note: Somewhat different from math
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=?
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=?
Division and Remainder
• If both operands to the division operator (/) are
integers, the result is an integer (the fractional part
is discarded)
14 / 3
8 / 12
equals
equals
4
0
• The remainder operator (%) returns the remainder
after division
14 % 3
8 % 12
equals
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
1
2
3
4
a + b * c - d / e
3
1
4
2
a / (b + c) - d % e
2
1
4
3
a / (b * (c + (d - e)))
4
3
2
1
• You should be careful for this difference
Math
10
*
5
=
25
2
10
=1
2*5
Java
10 / 2 * 5
10 / (2 * 5)
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
=
4
sum / 4 + 10 * number;
1
3
Then the result is stored in the
variable on the left hand side
2
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)
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)
The result willItbe
takes
an integer
one integer
This method calculates the absolute of the entered number
How to use?
| -10 |
Math.abs( -10 );
Class name
Method
dot
Example
| -10 |
int negative = -10;
int value = Math.abs( negative );
System.out.println( “The result is “ + value);
The result is 10
double sqrt(double num)
The result willItbe
takes
a double
one double value or variable
This method calculates the square root of the entered
number
How to use
Math.sqrt( 6.25 );
class name
Method
Dot
6.25
Example
2.25
double area = 2.25;
double value2 = Math.sqrt( area );
System.out.println( “value2 is “ + value2);
value2 is 1.5
double pow(double num, double power)
The result will
It takes
be a double
two double values or variables
This method calculates the power of the entered number
How to use
Math.pow( 2.0, 3.0 );
Object name
Method
Dot
23
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
• 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;
You can print out whatever you want
double num2;
by using double quotation marks
num1 = 10 * 5 – 7 / 3;
System.out.println( “num1 is “ + num1 );
}
}
num2 = 2.5 * (10.1 -3.7);
System.out.println( “num2 is “ + num2 );
Exercise 2
Calculate the average score of final exams
e.g.
English
Math
Science
History
Spanish
90
86
92
78
83
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;
}
}
double?
= int
System.out.println(
average
); / int
Is this correct ?
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
3X2 = 15

X2 = 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 );
Challenge
• Let’s compute the distance between two points
P1 (4, 7)
P2 (-6, -2)
Y
10
(4,7)
-10
10
(-6, -2)
-10
X
P1 (X1, Y1)
P2 (X2, Y2)
distance = (X1 – X2)2 + (Y1 – Y2)2
Y
10
(4,7)
-10
10
(-6, -2)
-10
X
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)
distance = (X1 – X2)2 + (Y1 – Y2)2
dist = Math.sqrt( Math.pow(X1-X2,2) + Math.pow(Y1-Y2,2) )
Then print out the distance
Try to report in details like…
e.g. The distance between (-1,3) and (2, -1) is 5.0
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 );
}
}