820 likes | 991 Views
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;. works for long. does not work for short, byte!. b = 2147483647. Question from the last class .
E N D
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; works for long does not work for short, byte! b = 2147483647
Question from the last class Searching for the answer – internet. www.google.com query: java casting large double int
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 if (condition) statement; this.putOnShirt(theWhiteShirt); if (itIsRaining) bag.addItem(umbrella); door.open(); do not take seriously
Grouping statements if (itIsRaining) { bag.addItem(umbrella); windowInLivingRoom.close(); } if (condition) { statement; .... statement; } BLOCK
Conditions expressions of type boolean false true operators producting booleans <, >, <=, >=, ==, != operands – two numbers
Conditions expressions of type boolean false true 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 false||false&&false||true ! *,/,% +,- >,<.<=,>= ==,!= && || Precedence rules – book p.194.
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!
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; 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); } } Practically it is not a good idea to check floating point numbers for equality!
Theoretically works! Check “closeness”, Math.abs(a-b)<EPSILON 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); } } Practically it is not a good idea to check floating point numbers for equality!
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))) deMorgan's laws !(a||b) (!a)&&(!b) not(a and b) (not a) or (not b)
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); } }
EXERCISE #2: Is the number from {1,7,42}?
SOLUTION #2: Is the number from {1,7,42}? 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<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. 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. 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++ count=0; while (count<10) { makePushUp(); count=count+1; } while (condition) statement;
Repeating things - counters while (condition) statement; count=0; while (count<10) { makePushUp(); count++; } for (init; condition; increment) statement; for (count=0;count<10;count++) makePushUp();
If Gauss had a computer... teacher asked him to sum numbers from 1 to 100. Gauss 1777-1855
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. 9 sum=0; for (i=1;i<=1000000000;i++) sum=sum+i; 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