Question from the last class - PowerPoint PPT Presentation

1 / 82
About This Presentation
Title:

Question from the last class

Description:

KAPPA. If Kepler had a computer... variables: x1,y1,vx1,vy1,m1,ax1, ... final double KAPPA=1; double x1,y1,vx1,vy1,ax1,ay1,m1; double x2,y2,vx2,vy2,ax2,ay2,m2; ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 83
Provided by: krt85
Category:
Tags: class | kappa | last | question

less

Transcript and Presenter's Notes

Title: Question from the last class


1
Question 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
2
Question from the last class
Searching for the answer internet.
www.google.com
query java casting large double int
3
Today
  • flow of control
  • if else
  • for
  • while
  • lots of examples

Dont worry about input/output.
4
Flow 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
5
Flow 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)
6
Flow 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)
7
if-else
if (condition) statement
this.putOnShirt(theWhiteShirt) if (itIsRaining)
bag.addItem(umbrella) door.open()
do not take seriously
8
Grouping statements
if (itIsRaining) bag.addItem(umbrella)
windowInLivingRoom.close()
if (condition) statement ....
statement
BLOCK
9
Conditions
expressions of type boolean
false
true
operators producting booleans
lt, gt, lt, gt, , !
operands two numbers
10
Conditions
expressions of type boolean
false
true
operators producting booleans
lt, gt, lt, gt, , !
Equality testing
11
Is 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)
12
Is the number from 1,2,...,10 ?
Given int number, how can we test whether it is
from 1,2,....,10?
13
Is 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 !

14
Is 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.
15
Precedence - exercise
falsefalsefalsetrue
! ,/, ,- gt,lt.lt,gt ,!
Precedence rules book p.194.
16
Is 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.
17
Is the number from 1,2,...,10 ?
Given float number, how can we test whether it
is from 1,2,....,10?
18
Is 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)
19
Is 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)
20
Is 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!
21
Theoretically 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!
22
Theoretically 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!
23
EXERCISE 1
For what value of x is the following true?
((((x1)(x!3))((x!1)(x3)))
24
SOLUTION 1
((((x1)(x!3))((x!1)(x3)))
deMorgan's laws
!(ab) (!a)(!b)
not(a and b) (not a) or (not b)
25
Are 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)
26
EXERCISE 2
Is the number from 1,7,42?
27
SOLUTION 2
Is the number from 1,7,42?
if ((number1)(number7)(number42))
JOptionPane.showMessageDialog
(null,It is from 1,7,42)
28
Is 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
29
Is 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!)
30
Is 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
31
Give me the minimum of two numbers a,b.
32
Give me the minimum of two numbers a,b.
if (altb) minimuma else minimumb
33
Give me the minimum of three numbers a,b,c.
34
Give me the minimum of three numbers a,b,c.
if ((altb)(altc)) minimuma else if (bltc)
minimumb else minimumc
35
Give me the minimum of five numbers a,b,c,d,e.
36
Give me the minimum of five numbers a,b,c,d,e.
minimuma if (bltminimum) minimumb if
(cltminimum) minimumc if (dltminimum)
minimumd if (eltminimum) minimume
37
Sort these three numbers a,b,c.
38
Sort 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)
39
Sort 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)
40
Sort 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)
41
Solve a quadratic equation.
Dgt0 2 solutions
D0 1 solution
Dlt0 no solution
42
Solve 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)
43
More Complicated Flow of Control
eat() homework.solvedfalse while
(!homework.solved) homework.tryToSolve() slee
p()
while (condition) statement
44
Repeating things - counters
count
count0 while (countlt10) makePushUp()
countcount1
while (condition) statement
45
Repeating things - counters
while (condition) statement
count0 while (countlt10) makePushUp()
count
for (init condition increment) statement
for (count0countlt10count) makePushUp()
46
If Gauss had a computer...
teacher asked him to sum numbers from 1 to 100.
Gauss 1777-1855
47
If Gauss had a computer...
teacher asked him to sum numbers from 1 to 100.
for (i1ilt100i) ?
48
If Gauss had a computer...
teacher asked him to sum numbers from 1 to 100.
sum0 for (i1ilt100i) sumsumi
49
If Gauss had a computer...
today she would asked him to sum numbers from 1
to 10.
9
sum0 for (i1ilt1000000000i) sumsumi
slow
50
If Gauss had a computer...
1234567S 7654321S 88888882S 7
82S S78/2
51
Average, 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?
52
Average, 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
53
Average, minimum, maximum
sum,maximum,minimum
read numberOfStudents sum0 maximum?
54
Average, 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)
55
Back 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)
56
Back to FirstApplet
Modify the FirstApplet to look as follows
I.e. there is a vertical line in every second
column.
57
Back 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
58
EXERCISE 3
Modify the FirstApplet to look as follows
I.e. there is a horizontal line in every second
column.
59
SOLUTION 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
60
Correct? 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
61
Choose 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

62
If Kepler had a computer...
Every object has location, speed, mass.
m
63
If Kepler had a computer...
Gravitational force
Keppler 1571-1630 Newton 1643-1727
64
If Kepler had a computer...
Every object has location, speed, mass.
Gravitational force, acceleration.
for each object
65
If 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
66
If Kepler had a computer...
variables x1,y1,vx1,vy1,m1,ax1,ay1
x2,y2,vx2,vy2,m2,ax2,ay2
x1x1vx1DT y1y1vy1DT vx1vx1ax1DT vy1vy
1ay1DT
67
If 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
68
If 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
69
If 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
70
If Kepler had a computer...
initialize the variables while (true)
makeOneStep() drawPlanets()
71
import 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()
72
private 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)
74
Area of a polygon.
y0
75
Area 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
76
Area of a polygon.


77
Area of a polygon.
-
-
-
78
Area of a polygon.

-
79
Area of a polygon.
80
Area 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)
81
Hard math easy.
Greens formula
R
C
82
Hard math easy.
Greens formula for
Write a Comment
User Comments (0)
About PowerShow.com