Title: Question from the last class
1Question from the last class
What happens if we cast too large float/double
to an int?
int has range -2147483648..2147483647
float a1e10f int b(int) a
works for long
does not work for short, byte!
b 2147483647
2Question from the last class
Searching for the answer internet.
www.google.com
query java casting large double int
3Today
- flow of control
- if else
- for
- while
- lots of examples
Dont worry about input/output.
4Flow of Control
import javax.swing. public class Sum
public static void main(String args) int
firstNumber,secondNumber,sum
firstNumberInteger.parseInt(
JOptionPane.showInputDialog("Enter the first
number")) secondNumberInteger.parseInt(
JOptionPane.showInputDi
alog("Enter the second number"))
sumFirstNumberSecondNumber
JOptionPane.showMessageDialog(null,"The sum is "
sum) System.exit(0)
compiles
5Flow of Control
import javax.swing. public class Sum
public static void main(String args) int
firstNumber,secondNumber,sum
firstNumberInteger.parseInt(
JOptionPane.showInputDialog("Enter the first
number")) secondNumberInteger.parseInt(
JOptionPane.showInputDi
alog("Enter the second number"))
sumFirstNumberSecondNumber
JOptionPane.showMessageDialog(null,"The sum is "
sum) System.exit(0)
6Flow of Control
import javax.swing. public class Sum
public static void main(String args) int
firstNumber,secondNumber,sum
firstNumberInteger.parseInt(
JOptionPane.showInputDialog("Enter the first
number")) secondNumberInteger.parseInt(
JOptionPane.showInputDi
alog("Enter the second number"))
sumFirstNumberSecondNumber
JOptionPane.showMessageDialog(null,"The sum is "
sum) System.exit(0)
7if-else
if (condition) statement
this.putOnShirt(theWhiteShirt) if (itIsRaining)
bag.addItem(umbrella) door.open()
do not take seriously
8Grouping statements
if (itIsRaining) bag.addItem(umbrella)
windowInLivingRoom.close()
if (condition) statement ....
statement
BLOCK
9Conditions
expressions of type boolean
false
true
operators producting booleans
lt, gt, lt, gt, , !
operands two numbers
10Conditions
expressions of type boolean
false
true
operators producting booleans
lt, gt, lt, gt, , !
Equality testing
11Is the number smaller than 10?
import javax.swing. public class Sum
public static void main(String args) int
number numberInteger.parseInt(JOptionPane.s
howInputDialog("Enter the number")) if
(Numberlt10) JOptionPane.showMessageDialog
(null,It is smaller than 10!")
System.exit(0)
12Is the number from 1,2,...,10 ?
Given int number, how can we test whether it is
from 1,2,....,10?
13Is the number from 1,2,...,10 ?
Given int number, how can we test whether it is
from 1,2,....,10?
numbergt1 AND numberlt10
OR NOT !
14Is the number from 1,2,...,10 ?
import javax.swing. public class Sum
public static void main(String args) int
number numberInteger.parseInt(JOptionPane.s
howInputDialog("Enter the number")) if
((Numbergt1)(Numberlt10))
JOptionPane.showMessageDialog
(null,It is from 1,2,...,10)
System.exit(0)
Precedence rules book p.194.
15Precedence - exercise
falsefalsefalsetrue
! ,/, ,- gt,lt.lt,gt ,!
Precedence rules book p.194.
16Is the number from 1,2,...,10 ?
import javax.swing. public class Sum
public static void main(String args) int
number numberInteger.parseInt(JOptionPane.s
howInputDialog("Enter the number")) if
((Numbergt1)(Numberlt10))
JOptionPane.showMessageDialog
(null,It is from 1,2,...,10)
System.exit(0)
! ,/, ,- gt,lt.lt,gt ,!
Precedence rules book p.194.
17Is the number from 1,2,...,10 ?
Given float number, how can we test whether it
is from 1,2,....,10?
18Is the number from 1,2,...,10 ?
Given float number, how can we test whether it
is from 1,2,....,10?
test whether it is int and in 1,...,10
(number (int) number)
19Is the number from 1,2,...,10 ?
import javax.swing. public class Sum
public static void main(String args) int
number numberInteger.parseInt(JOptionPane.s
howInputDialog("Enter the number")) if
((Numbergt1) (Numberlt10)
(Number(int)Number)) JOptionPane.showMessa
geDialog (null,It is smaller than
10!") System.exit(0)
20Is the number from 1,2,...,10 ?
import javax.swing. public class Sum
public static void main(String args) int
number numberInteger.parseInt(JOptionPane.s
howInputDialog("Enter the number")) if
((Numbergt1) (Numberlt10)
(Number(int)Number)) JOptionPane.showMessa
geDialog (null,It is smaller than
10!") System.exit(0)
Theoretically works!
Practically it is not a good idea to check
floating point numbers for equality!
21Theoretically works!
In theory practice and theory are the same. In
practice they are different.
import javax.swing. public class Sum
public static void main(String args)
double b,c c1/3.0 bcccccc
if (b(int)b) JOptionPane.showMessageDialog
(null,You win 1,000,000!")
System.exit(0)
Practically it is not a good idea to check
floating point numbers for equality!
22Theoretically works!
Check closeness, Math.abs(a-b)ltEPSILON
import javax.swing. public class Sum
public static void main(String args)
double b,c c1/3.0 bcccccc
if (b(int)b) JOptionPane.showMessageDialog
(null,You win 1,000,000!")
System.exit(0)
Practically it is not a good idea to check
floating point numbers for equality!
23EXERCISE 1
For what value of x is the following true?
((((x1)(x!3))((x!1)(x3)))
24SOLUTION 1
((((x1)(x!3))((x!1)(x3)))
deMorgan's laws
!(ab) (!a)(!b)
not(a and b) (not a) or (not b)
25Are two numbers different?
import javax.swing. public class Sum
public static void main(String args) int
firstNumber,secondNumber,sum
firstNumberInteger.parseInt(
JOptionPane.showInputDialog("Enter the first
number")) secondNumberInteger.parseInt(
JOptionPane.showInputDi
alog("Enter the second number")) if
(firstNumber!secondNumber)
JOptionPane.showMessageDialog(null, They
are different) System.exit(0)
26EXERCISE 2
Is the number from 1,7,42?
27SOLUTION 2
Is the number from 1,7,42?
if ((number1)(number7)(number42))
JOptionPane.showMessageDialog
(null,It is from 1,7,42)
28Is there a triangle with sides of these lengths?
We have 3 numbers a,b,c. Is there a triangle
with sides of length a,b,c?
1,1,3 5,2,2 3,2,2 1,1,2
29Is there a triangle with sides of lengths a,b,c?
if ((abltc)(acltb)(bclta))
JOptionPane.showMessageDialog
(null,NO!)
if ( !((abltc)(acltb)(bclta)) )
JOptionPane.showMessageDialog
(null,YES!)
30Is there a triangle with sides of lengths a,b,c?
if ((abltc)(acltb)(bclta))
JOptionPane.showMessageDialog
(null,NO!) else JOptionPane.showMessageD
ialog (null,YES!)
if (condition) statement else statement
31Give me the minimum of two numbers a,b.
32Give me the minimum of two numbers a,b.
if (altb) minimuma else minimumb
33Give me the minimum of three numbers a,b,c.
34Give me the minimum of three numbers a,b,c.
if ((altb)(altc)) minimuma else if (bltc)
minimumb else minimumc
35Give me the minimum of five numbers a,b,c,d,e.
36Give me the minimum of five numbers a,b,c,d,e.
minimuma if (bltminimum) minimumb if
(cltminimum) minimumc if (dltminimum)
minimumd if (eltminimum) minimume
37Sort these three numbers a,b,c.
38Sort these three numbers a,b,c.
if ((altb)(bltc)) OUT(a,b,c) else if
(blta)(altc)) OUT(b,a,c) else if ...
JOptionPane.showMessageDialog(null,a,b
c)
39Sort these three numbers a,b,c.
mina if (bltmin) minb if (cltmin)
minc maxa if (bltmax) maxb if (cltmax)
maxc OUT(min,abc-min-max,max)
40Sort these three numbers a,b,c.
mina if (bltmin) minb if (cltmin)
minc maxa if (bltmax) maxb if (cltmax)
maxc OUT(min,abc-min-max,max)
41Solve a quadratic equation.
Dgt0 2 solutions
D0 1 solution
Dlt0 no solution
42Solve a quadratic equation.
Dbb-4ac if (Dgt0) OUT(2 solutions
((-bMath.sqrt(D))/(2a)) and
((-b-Math.sqrt(D))/(2a))) else if (D0)
OUT(1 solution (-b/(2a))) else OUT(no
solutions)
43More Complicated Flow of Control
eat() homework.solvedfalse while
(!homework.solved) homework.tryToSolve() slee
p()
while (condition) statement
44Repeating things - counters
count
count0 while (countlt10) makePushUp()
countcount1
while (condition) statement
45Repeating things - counters
while (condition) statement
count0 while (countlt10) makePushUp()
count
for (init condition increment) statement
for (count0countlt10count) makePushUp()
46If Gauss had a computer...
teacher asked him to sum numbers from 1 to 100.
Gauss 1777-1855
47If Gauss had a computer...
teacher asked him to sum numbers from 1 to 100.
for (i1ilt100i) ?
48If Gauss had a computer...
teacher asked him to sum numbers from 1 to 100.
sum0 for (i1ilt100i) sumsumi
49If Gauss had a computer...
today she would asked him to sum numbers from 1
to 10.
9
sum0 for (i1ilt1000000000i) sumsumi
slow
50If Gauss had a computer...
1234567S 7654321S 88888882S 7
82S S78/2
51Average, minimum, maximum of n numbers.
user says how many students are there enters
grades for all students wants average,maximum and
minimum
What variables do we need?
52Average, minimum, maximum of n numbers.
user says how many students are there enters
grades for all students wants average,maximum and
minimum
What variables do we need?
sum, maximum, minimum, numberOfStudents, score
53Average, minimum, maximum
sum,maximum,minimum
read numberOfStudents sum0 maximum?
54Average, minimum, maximum
int sum,maximum,minimum,score,i,numberOfStrudents
numberOfStudentsIN() if (numberOfStudentsgt0)
scoreIN() maximumminimumsumscore for
(i1iltnumberOfStudentsi) scoreIN()
sumsumscore if (scoregtmaximum)
maximumscore if (scoreltminimum)
minimumscore OUT(maximum,minimum,sum/numbe
rOfStudents)
55Back to FirstApplet
import java.applet.Applet import
java.awt.Graphics public class FirstApplet
extends Applet public void paint(Graphics g)
g.drawLine(x1,y1,x2,y2)
56Back to FirstApplet
Modify the FirstApplet to look as follows
I.e. there is a vertical line in every second
column.
57Back to FirstApplet
import java.applet.Applet import
java.awt.Graphics public class FirstApplet
extends Applet public void paint(Graphics g)
int column column0 while
(columnlt100) g.drawLine(column,0,column,99
) columncolumn2
58EXERCISE 3
Modify the FirstApplet to look as follows
I.e. there is a horizontal line in every second
column.
59SOLUTION 3
Correct?
import java.applet.Applet import
java.awt.Graphics public class FirstApplet
extends Applet public void paint(Graphics g)
int column column0 while
(columnlt100) g.drawLine(0,column,99,column
) columncolumn2
60Correct? YES
SOLUTION 3
Confusing
import java.applet.Applet import
java.awt.Graphics public class FirstApplet
extends Applet public void paint(Graphics g)
int column column0 while
(columnlt100) g.drawLine(0,column,99,column
) columncolumn2
61Choose right names for variables
import java.applet.Applet import
java.awt.Graphics public class FirstApplet
extends Applet public void paint(Graphics g)
int row row0 while (rowlt100)
g.drawLine(0,row,99,row) rowrow2
62If Kepler had a computer...
Every object has location, speed, mass.
m
63If Kepler had a computer...
Gravitational force
Keppler 1571-1630 Newton 1643-1727
64If Kepler had a computer...
Every object has location, speed, mass.
Gravitational force, acceleration.
for each object
65If Kepler had a computer...
Every object has location, speed, mass.
Gravitational force, acceleration.
variables x1,y1,vx1,vy1,m1,ax1,ay1
x2,y2,vx2,vy2,m2,ax2,ay2
constant DT (very small) KAPPA
66If Kepler had a computer...
variables x1,y1,vx1,vy1,m1,ax1,ay1
x2,y2,vx2,vy2,m2,ax2,ay2
x1x1vx1DT y1y1vy1DT vx1vx1ax1DT vy1vy
1ay1DT
67If Kepler had a computer...
variables x1,y1,vx1,vy1,m1,ax1,ay1
x2,y2,vx2,vy2,m2,ax2,ay2
distXx2-x1 distYy2-y1 denomMath.sqrt(distXdi
stXdistYdistY) denomdenomdenomdenom ax1KAP
PAdistXm2/denom ax2KAPPAdistYm2/denom
68If Kepler had a computer...
x1x1vx1DT y1y1vy1DT vx1vx1ax1DT vy1vy
1ay1DT distXx2-x1 distYy2-y1 denomMath.sqr
t(distXdistXdistYdistY) denomdenomdenomdeno
m ax1KAPPAdistXm2/denom ay1KAPPAdistYm2/de
nom
One step of a solar system with 2 objects
69If Kepler had a computer...
x2x2vx2DT y2y2vy2DT vx2vx2ax2DT vy2vy
2ay2DT distXx1-x2 distYy1-y2 denomMath.sqr
t(distXdistXdistYdistY) denomdenomdenomdeno
m ax2KAPPAdistXm1/denom ay2KAPPAdistYm1/de
nom
The same for second object
70If Kepler had a computer...
initialize the variables while (true)
makeOneStep() drawPlanets()
71import java.awt. import java.awt.event.
import java.applet. public class TwoPlanets
extends Applet implements Runnable final int
sx700,sy700 final int STEPS_BETWEEN_REPAINT5
00 final double DT0.001 final double
KAPPA1 double x1,y1,vx1,vy1,ax1,ay1,m1
double x2,y2,vx2,vy2,ax2,ay2,m2 int steps
public void init() setBackground(Color.white
) x1200 y1200 x2500 y2500 vx10
vy11 vx20 vy2-1 m11500 m21500
ax10 ay10 ax20 ay20 steps0
(new Thread(TwoPlanets.this)).start()
72private void makeOneStep() // Auxilary
variables double distX,distY,denom //
First planet x1x1vx1DT y1y1vy1DT
vx1vx1ax1DT vy1vy1ay1DT //
First planet, force from the second planet
distXx2-x1 distYy2-y1
denomMath.sqrt(distXdistXdistYdistY)
denomdenomdenomdenom ax1KAPPAdistXm2/de
nom ay1KAPPAdistYm2/denom // Second
planet x2x2vx2DT y2y2vy2DT
vx2vx2ax2DT vy2vy2ay2DT // Second
planet, force from the first planet
distXx1-x2 distYy1-y2
denomMath.sqrt(distXdistXdistYdistY)
denomdenomdenomdenom ax2KAPPAdistXm1/de
nom ay2KAPPAdistYm1/denom
73 public void run() while (true)
makeOneStep() steps if
(stepsSTEPS_BETWEEN_REPAINT)
repaint() steps0
public void paint(Graphics g)
g.setColor(Color.green) g.fillArc((int)x1,(in
t)y1,8,8,0,360) g.setColor(Color.red)
g.fillArc((int)x2,(int)y2,8,8,0,360)
74Area of a polygon.
y0
75Area of a polygon.
We are given the coordinates of vertices in the
order as they occur on the boundary in the
counter-clockwise direction.
y0
76Area of a polygon.
77Area of a polygon.
-
-
-
78Area of a polygon.
-
79Area of a polygon.
80Area of a polygon.
numberOfPointsIN() if (numberOfPointsgt0)
x1IN() y1IN() oldXx1 oldYy1 area0
for (i1iltnumberOfPointsi) xIN()
yIN() areasignedArea(oldX,oldY,x,y)
oldXx oldYy areasignedArea(oldX,oldY,
x1,y1) OUT(area)
81Hard math easy.
Greens formula
R
C
82Hard math easy.
Greens formula for