1 / 82

Question from the last class

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 .

rosalind
Download Presentation

Question from the last class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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; 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 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 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 <, >, <=, >=, ==, != operands – two numbers

  10. Conditions expressions of type boolean false true 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 false||false&&false||true ! *,/,% +,- >,<.<=,>= ==,!= && || 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; 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. 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!

  22. 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!

  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))) deMorgan's laws !(a||b) (!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; 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. EXERCISE #2: Is the number from {1,7,42}?

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

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

  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++ count=0; while (count<10) { makePushUp(); count=count+1; } while (condition) statement;

  45. 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();

  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 (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. 9 sum=0; for (i=1;i<=1000000000;i++) sum=sum+i; 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

More Related