Download presentation
Presentation is loading. Please wait.
Published byClay Bradham Modified over 10 years ago
1
Question from the last class What happens if we cast “too large” float/double to an int? int has range -2147483648..2147483647 float a=1e10f; int b=(int) a; b = 2147483647 works for long does not work for short, byte!
2
Question from the last class query: java casting large double int Searching for the answer – internet. www.google.com
3
Today flow of control if – else for while lots of examples Don’t worry about input/output.
4
Flow of Control import javax.swing.*; public class Sum { public static void main(String args[]) { int firstNumber,secondNumber,sum; firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:")); sum=FirstNumber+SecondNumber; 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; firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:")); sum=FirstNumber+SecondNumber; 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; firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:")); sum=FirstNumber+SecondNumber; JOptionPane.showMessageDialog(null,"The sum is " + sum); System.exit(0); }
7
if-else this.putOnShirt(theWhiteShirt); if (itIsRaining) bag.addItem(umbrella); door.open(); if (condition) statement; do not take seriously
8
Grouping statements if (condition) { statement;.... statement; } if (itIsRaining) { bag.addItem(umbrella); windowInLivingRoom.close(); } BLOCK
9
Conditions expressions of type boolean true false operators producting booleans, =, ==, != operands – two numbers
10
Conditions expressions of type boolean true false operators producting booleans, =, ==, != Equality testing
11
Is the number smaller than 10? import javax.swing.*; public class Sum { public static void main(String args[]) { int number; number=Integer.parseInt(JOptionPane.showInputDialog("Enter the number:")); if (Number<10) 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}? number>=1 AND number<=10 && OR || NOT !
14
Is the number from {1,2,...,10} ? import javax.swing.*; public class Sum { public static void main(String args[]) { int number; number=Integer.parseInt(JOptionPane.showInputDialog("Enter the number:")); if ((Number>=1)&&(Number<=10)) JOptionPane.showMessageDialog (null,“It is from {1,2,...,10}”); System.exit(0); } Precedence rules – book p.194.
15
Precedence - exercise Precedence rules – book p.194. ! *,/,% +,- >, = ==,!= && || false||false&&false||true
16
Is the number from {1,2,...,10} ? import javax.swing.*; public class Sum { public static void main(String args[]) { int number; number=Integer.parseInt(JOptionPane.showInputDialog("Enter the number:")); if ((Number>=1)&&(Number<=10)) JOptionPane.showMessageDialog (null,“It is from {1,2,...,10}”); System.exit(0); } 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; number=Integer.parseInt(JOptionPane.showInputDialog("Enter the number:")); if ((Number>=1) &&(Number<=10)&& (Number==(int)Number)) JOptionPane.showMessageDialog (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; number=Integer.parseInt(JOptionPane.showInputDialog("Enter the number:")); if ((Number>=1) &&(Number<=10)&& (Number==(int)Number)) JOptionPane.showMessageDialog (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
import javax.swing.*; public class Sum { public static void main(String args[]) { double b,c; c=1/3.0; b=c+c+c+c+c+c; if (b=(int)b) JOptionPane.showMessageDialog (null,“You win $1,000,000!"); System.exit(0); } Theoretically works! Practically it is not a good idea to check floating point numbers for equality! In theory practice and theory are the same. In practice they are different.
22
import javax.swing.*; public class Sum { public static void main(String args[]) { double b,c; c=1/3.0; b=c+c+c+c+c+c; if (b=(int)b) JOptionPane.showMessageDialog (null,“You win $1,000,000!"); System.exit(0); } Theoretically works! Practically it is not a good idea to check floating point numbers for equality! Check “closeness”, Math.abs(a-b)<EPSILON
23
EXERCISE #1: For what value of x is the following true? ((((x==1)||(x!=3))&&((x!=1)&&(x==3)))
24
SOLUTION #1: ((((x==1)||(x!=3))&&((x!=1)&&(x==3))) !(a||b) (!a)&&(!b) not(a and b) (not a) or (not b) deMorgan's laws
25
Are two numbers different? import javax.swing.*; public class Sum { public static void main(String args[]) { int firstNumber,secondNumber,sum; firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:")); if (firstNumber!=secondNumber) JOptionPane.showMessageDialog(null, “They are different”); System.exit(0); }
26
Is the number from {1,7,42}? EXERCISE #2:
27
Is the number from {1,7,42}? SOLUTION #2: if ((number==1)||(number==7)||(number==42)) 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 ((a+b<=c)||(a+c<=b)||(b+c<=a)) JOptionPane.showMessageDialog (null,“NO!”); if ( !((a+b<=c)||(a+c<=b)||(b+c<=a)) ) JOptionPane.showMessageDialog (null,“YES!”);
30
Is there a triangle with sides of lengths a,b,c? if ((a+b<=c)||(a+c<=b)||(b+c<=a)) JOptionPane.showMessageDialog (null,“NO!”); else JOptionPane.showMessageDialog (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 (a<b) minimum=a; else minimum=b;
33
Give me the minimum of three numbers a,b,c.
34
Give me the minimum of three numbers a,b,c. if ((a<b)&&(a<c)) minimum=a; else if (b<c) minimum=b; else minimum=c;
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. minimum=a; if (b<minimum) minimum=b; if (c<minimum) minimum=c; if (d<minimum) minimum=d; if (e<minimum) minimum=e;
37
Sort these three numbers a,b,c.
38
if ((a<=b)&&(b<=c)) OUT(a,b,c); else if (b<=a)&&(a<=c)) OUT(b,a,c); else if... JOptionPane.showMessageDialog(null,“”+a+”,“+b+” “+c);
39
Sort these three numbers a,b,c. min=a; if (b<min) min=b; if (c<min) min=c; max=a; if (b<max) max=b; if (c<max) max=c; OUT(min,a+b+c-min-max,max)
40
Sort these three numbers a,b,c. min=a; if (b<min) min=b; if (c<min) min=c; max=a; if (b<max) max=b; if (c<max) max=c; OUT(min,a+b+c-min-max,max)
41
Solve a quadratic equation. D>0 2 solutions D=0 1 solution D<0 no solution
42
Solve a quadratic equation. D=b*b-4*a*c; if (D>0) OUT(“2 solutions: “+((-b+Math.sqrt(D))/(2*a))+ “ and “+((-b-Math.sqrt(D))/(2*a))); else if (D==0) OUT(“1 solution: “+(-b/(2a))”); else OUT(“no solutions”);
43
More Complicated Flow of Control eat(); homework.solved=false; while (!homework.solved) homework.tryToSolve(); sleep(); while (condition) statement;
44
Repeating things - counters count=0; while (count<10) { makePushUp(); count=count+1; } count++ while (condition) statement;
45
Repeating things - counters count=0; while (count<10) { makePushUp(); count++; } while (condition) statement; for (count=0;count<10;count++) makePushUp(); for (init; condition; increment) statement;
46
If Gauss had a computer... Gauss 1777-1855 teacher asked him to sum numbers from 1 to 100.
47
If Gauss had a computer... teacher asked him to sum numbers from 1 to 100. for (i=1;i<=100;i++) ?
48
If Gauss had a computer... teacher asked him to sum numbers from 1 to 100. sum=0; for (i=1;i<=100;i++) sum=sum+i;
49
If Gauss had a computer... today she would asked him to sum numbers from 1 to 10. sum=0; for (i=1;i<=1000000000;i++) sum=sum+i; 9 slow
50
If Gauss had a computer... 1+2+3+4+5+6+7=S 7+6+5+4+3+2+1=S 8+8+8+8+8+8+8=2S 7*8=2S S=7*8/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 sum=0; maximum=?
54
Average, minimum, maximum int sum,maximum,minimum,score,i,numberOfStrudents; numberOfStudents=IN(); if (numberOfStudents>0) { score=IN(); maximum=minimum=sum=score; for (i=1;i<numberOfStudents;i++) { score=IN(); sum=sum+score; if (score>maximum) maximum=score; if (score<minimum) minimum=score; } OUT(maximum,minimum,sum/numberOfStudents); }
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
import java.applet.Applet; import java.awt.Graphics; public class FirstApplet extends Applet { public void paint(Graphics g) { int column; column=0; while (column<100) { g.drawLine(column,0,column,99); column=column+2; } Back to FirstApplet
58
EXERCISE #3: Modify the FirstApplet to look as follows: I.e. there is a horizontal line in every second column.
59
import java.applet.Applet; import java.awt.Graphics; public class FirstApplet extends Applet { public void paint(Graphics g) { int column; column=0; while (column<100) { g.drawLine(0,column,99,column); column=column+2; } SOLUTION #3 Correct?
60
import java.applet.Applet; import java.awt.Graphics; public class FirstApplet extends Applet { public void paint(Graphics g) { int column; column=0; while (column<100) { g.drawLine(0,column,99,column); column=column+2; } Correct? YES Confusing SOLUTION #3
61
import java.applet.Applet; import java.awt.Graphics; public class FirstApplet extends Applet { public void paint(Graphics g) { int row; row=0; while (row<100) { g.drawLine(0,row,99,row); row=row+2; } Choose right names for variables
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 x1=x1+vx1*DT; y1=y1+vy1*DT; vx1=vx1+ax1*DT; vy1=vy1+ay1*DT;
67
If Kepler had a computer... variables: x1,y1,vx1,vy1,m1,ax1,ay1 x2,y2,vx2,vy2,m2,ax2,ay2 distX=x2-x1; distY=y2-y1; denom=Math.sqrt(distX*distX+distY*distY); denom=denom*denom*denom; ax1=KAPPA*distX*m2/denom; ax2=KAPPA*distY*m2/denom;
68
If Kepler had a computer... x1=x1+vx1*DT; y1=y1+vy1*DT; vx1=vx1+ax1*DT; vy1=vy1+ay1*DT; distX=x2-x1; distY=y2-y1; denom=Math.sqrt(distX*distX+distY*distY); denom=denom*denom*denom; ax1=KAPPA*distX*m2/denom; ay1=KAPPA*distY*m2/denom; “One step of a solar system with 2 objects”
69
If Kepler had a computer... x2=x2+vx2*DT; y2=y2+vy2*DT; vx2=vx2+ax2*DT; vy2=vy2+ay2*DT; distX=x1-x2; distY=y1-y2; denom=Math.sqrt(distX*distX+distY*distY); denom=denom*denom*denom; ax2=KAPPA*distX*m1/denom; ay2=KAPPA*distY*m1/denom; 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 sx=700,sy=700; final int STEPS_BETWEEN_REPAINT=500; final double DT=0.001; final double KAPPA=1; double x1,y1,vx1,vy1,ax1,ay1,m1; double x2,y2,vx2,vy2,ax2,ay2,m2; int steps; public void init() { setBackground(Color.white); x1=200; y1=200; x2=500; y2=500; vx1=0; vy1=1; vx2=0; vy2=-1; m1=1500; m2=1500; ax1=0; ay1=0; ax2=0; ay2=0; steps=0; (new Thread(TwoPlanets.this)).start(); }
72
private void makeOneStep() { // Auxilary variables double distX,distY,denom; // First planet: x1=x1+vx1*DT; y1=y1+vy1*DT; vx1=vx1+ax1*DT; vy1=vy1+ay1*DT; // First planet, force from the second planet: distX=x2-x1; distY=y2-y1; denom=Math.sqrt(distX*distX+distY*distY); denom=denom*denom*denom; ax1=KAPPA*distX*m2/denom; ay1=KAPPA*distY*m2/denom; // Second planet: x2=x2+vx2*DT; y2=y2+vy2*DT; vx2=vx2+ax2*DT; vy2=vy2+ay2*DT; // Second planet, force from the first planet: distX=x1-x2; distY=y1-y2; denom=Math.sqrt(distX*distX+distY*distY); denom=denom*denom*denom; ax2=KAPPA*distX*m1/denom; ay2=KAPPA*distY*m1/denom; }
73
public void run() { while (true) { makeOneStep(); steps++; if (steps==STEPS_BETWEEN_REPAINT) { repaint(); steps=0; } public void paint(Graphics g) { g.setColor(Color.green); g.fillArc((int)x1,(int)y1,8,8,0,360); g.setColor(Color.red); g.fillArc((int)x2,(int)y2,8,8,0,360); }
74
Area of a polygon. y=0
75
Area of a polygon. y=0 We are given the coordinates of vertices in the order as they occur on the boundary in the counter-clockwise direction.
76
Area of a polygon. + +
77
- - -
78
+ -
80
numberOfPoints=IN(); if (numberOfPoints>0) { x1=IN(); y1=IN(); oldX=x1; oldY=y1; area=0; for (i=1;i<numberOfPoints;i++) { x=IN(); y=IN(); area+=signedArea(oldX,oldY,x,y); oldX=x; oldY=y; } area+=signedArea(oldX,oldY,x1,y1); OUT(area); }
81
Hard math – easy. Green’s formula R C
82
Hard math – easy. Green’s formula for
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.