question from the last class what happens if we cast “too large” float/double to an int? int has...

82
ion from the last class happens if we cast “too lar t/double to an int? int has range - 2147483648..2147483647 float a=1e10f; int b=(int) a; b = 2147483647 works for long does not work fo short, byte!

Upload: clay-bradham

Post on 14-Dec-2015

230 views

Category:

Documents


0 download

TRANSCRIPT

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 forshort, byte!

Question from the last class

query: java casting large double int

Searching for the answer – internet.

www.google.com

Today • flow of control• if – else• for• while• lots of examples

Don’t worry about input/output.

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

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

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

if-else

this.putOnShirt(theWhiteShirt);if (itIsRaining) bag.addItem(umbrella);door.open();

if (condition) statement;

do not take seriously

Grouping statements

if (condition) { statement; .... statement;}

if (itIsRaining) { bag.addItem(umbrella); windowInLivingRoom.close();}

BLOCK

Conditions expressions of type boolean

true false

operators producting booleans<, >, <=, >=, ==, !=

operands – two numbers

Conditions expressions of type boolean

true false

operators producting booleans

<, >, <=, >=, ==, !=

Equality testing

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

Is the number from {1,2,...,10} ?

Given int number, how can we testwhether it is from {1,2,....,10}?

Is the number from {1,2,...,10} ?

Given int number, how can we testwhether it is from {1,2,....,10}?

number>=1 AND number<=10

&&OR ||NOT !

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.

Precedence - exercise

Precedence rules – book p.194.

!*,/,%+,->,<.<=,>===,!=&&||

false||false&&false||true

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.

!*,/,%+,->,<.<=,>===,!=&&||

Is the number from {1,2,...,10} ?

Given float number, how can we test whether it is from {1,2,....,10}?

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)

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

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!

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.

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

EXERCISE #1:

For what value of x is the following true?

((((x==1)||(x!=3))&&((x!=1)&&(x==3)))

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

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

Is the number from {1,7,42}?

EXERCISE #2:

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

Is there a triangle with sides ofthese lengths?

We have 3 numbers a,b,c. Is there a triangle with sides of length a,b,c?

1,1,35,2,23,2,21,1,2

Is there a triangle with sides oflengths 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!”);

Is there a triangle with sides oflengths 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;

Give me the minimum of two numbers a,b.

Give me the minimum of two numbers a,b.

if (a<b) minimum=a;else minimum=b;

Give me the minimum of three numbers a,b,c.

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;

Give me the minimum of five numbers a,b,c,d,e.

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;

Sort these three numbers a,b,c.

Sort these three numbers a,b,c.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);

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)

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)

Solve a quadratic equation.2 0ax bx c

2 4D b ac

D>0 2 solutions

D=0 1 solution

D<0 no solution

( ) /(2 )b D a

( ) /(2 )b D a /(2 )b a

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

More Complicated Flow of Control

eat();homework.solved=false;while (!homework.solved) homework.tryToSolve();sleep();

while (condition) statement;

Repeating things - counters

count=0;while (count<10) { makePushUp(); count=count+1;}

count++

while (condition) statement;

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;

If Gauss had a computer...

Gauss 1777-1855

teacher asked him to sum numbersfrom 1 to 100.

If Gauss had a computer...

teacher asked him to sum numbersfrom 1 to 100.

for (i=1;i<=100;i++) ?

If Gauss had a computer...

teacher asked him to sum numbersfrom 1 to 100.

sum=0;for (i=1;i<=100;i++) sum=sum+i;

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

If Gauss had a computer...

1+2+3+4+5+6+7=S7+6+5+4+3+2+1=S8+8+8+8+8+8+8=2S7*8=2SS=7*8/2

Average, minimum, maximum of n numbers.

user says how many students are thereenters grades for all studentswants average,maximum and minimum

What variables do we need?

Average, minimum, maximum of n numbers.

user says how many students are thereenters grades for all studentswants average,maximum and minimum

What variables do we need?

sum, maximum, minimum,numberOfStudents, score

Average, minimum, maximum sum,maximum,minimum

read numberOfStudentssum=0; maximum=?

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

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

Back to FirstApplet

Modify the FirstApplet to look asfollows:

I.e. there is a vertical line in everysecond column.

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

EXERCISE #3:

Modify the FirstApplet to look asfollows:

I.e. there is a horizontal line in everysecond column.

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?

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

SOLUTION #3

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

If Kepler had a computer...

Every object has location, speed, mass..dl v dt

����������������������������

ml

v

If Kepler had a computer...

Gravitational force2 1

1 1 23

2 1

l lF m m

l l

������������������������������������������

����������������������������

Keppler 1571-1630Newton 1643-1727

If Kepler had a computer...

.dl v dt����������������������������

Every object has location, speed, mass.Gravitational force, acceleration.

.dv a dt����������������������������

/a F m������������� �

2 11 23

2 1

l la m

l l

����������������������������

����������������������������

for each object

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

If Kepler had a computer...variables: x1,y1,vx1,vy1,m1,ax1,ay1 x2,y2,vx2,vy2,m2,ax2,ay2

.dl v dt����������������������������

.dv a dt����������������������������

/a F m������������� �

x1=x1+vx1*DT;y1=y1+vy1*DT;vx1=vx1+ax1*DT;vy1=vy1+ay1*DT;

If Kepler had a computer...variables: x1,y1,vx1,vy1,m1,ax1,ay1 x2,y2,vx2,vy2,m2,ax2,ay2

2 11 23

2 1

l la m

l l

����������������������������

����������������������������

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;

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”

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 forsecond object

If Kepler had a computer...

initialize the variables;while (true) { makeOneStep(); drawPlanets();}

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

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

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

Area of a polygon.

y=0

Area of a polygon.

y=0

We are given the coordinates of verticesin the order as they occur on the boundaryin the counter-clockwise direction.

Area of a polygon.

+ +

Area of a polygon.

- - -

Area of a polygon.

+ -

Area of a polygon.

Area of a polygon.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);}

Hard math – easy.

( , )( , )

C R

P x yP x y dx dx dy

y

Green’s formula

R

C

( ) 1R

Area R dx dy

Hard math – easy.

Green’s formula for ( , )P x y y

1 ( )C R

y dx dx dy Area R

( )C

Area R y dx