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

## Presentation on theme: "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)"— Presentation 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 for short, 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 test whether it is from {1,2,....,10}?

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 !

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) { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3270116/slides/slide_22.jpg", "name": "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.", "description": "Practically it is not a good idea to check floating point numbers for equality. Check closeness , Math.abs(a-b)

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

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

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;

Give me the minimum of two numbers a,b.

Give me the minimum of two numbers a,b. if (a { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3270116/slides/slide_32.jpg", "name": "Give me the minimum of two numbers a,b. if (a

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

Give me the minimum of three numbers a,b,c. if ((a { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3270116/slides/slide_34.jpg", "name": "Give me the minimum of three numbers a,b,c.", "description": "if ((a

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3270116/slides/slide_36.jpg", "name": "Give me the minimum of five numbers a,b,c,d,e.", "description": "minimum=a; if (b

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3270116/slides/slide_39.jpg", "name": "Sort these three numbers a,b,c.", "description": "min=a; if (b

Sort these three numbers a,b,c. min=a; if (b { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3270116/slides/slide_40.jpg", "name": "Sort these three numbers a,b,c.", "description": "min=a; if (b

Solve a quadratic equation. D>0 2 solutions D=0 1 solution D<0 no solution

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 numbers from 1 to 100.

If Gauss had a computer... teacher asked him to sum numbers from 1 to 100. for (i=1;i<=100;i++) ?

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;

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=S 7+6+5+4+3+2+1=S 8+8+8+8+8+8+8=2S 7*8=2S S=7*8/2

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?

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

Average, minimum, maximum sum,maximum,minimum read numberOfStudents sum=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;imaximum) maximum=score; if (score { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3270116/slides/slide_54.jpg", "name": "Average, minimum, maximum int sum,maximum,minimum,score,i,numberOfStrudents; numberOfStudents=IN(); if (numberOfStudents>0) { score=IN(); maximum=minimum=sum=score; for (i=1;imaximum) maximum=score; if (score0) { score=IN(); maximum=minimum=sum=score; for (i=1;imaximum) maximum=score; if (score

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 as follows: I.e. there is a vertical line in every second 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 as follows: I.e. there is a horizontal line in every second 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? YES Confusing 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. m

If Kepler had a computer... Gravitational force Keppler 1571-1630 Newton 1643-1727

If Kepler had a computer... Every object has location, speed, mass. Gravitational force, acceleration. 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 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 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 for second 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 vertices in the order as they occur on the boundary in the counter-clockwise direction.

Area of a polygon. + +

- - -

+ -

numberOfPoints=IN(); if (numberOfPoints>0) { x1=IN(); y1=IN(); oldX=x1; oldY=y1; area=0; for (i=1;i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3270116/slides/slide_80.jpg", "name": "numberOfPoints=IN(); if (numberOfPoints>0) { x1=IN(); y1=IN(); oldX=x1; oldY=y1; area=0; for (i=1;i0) { x1=IN(); y1=IN(); oldX=x1; oldY=y1; area=0; for (i=1;i

Hard math – easy. Green’s formula R C

Hard math – easy. Green’s formula for

Download ppt "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)"

Similar presentations