Title | CIS 110 notes before midterm |
---|---|
Author | Cynthia Olds |
Course | Introduction to Computer Programming |
Institution | University of Pennsylvania |
Pages | 28 |
File Size | 893.7 KB |
File Type | |
Total Downloads | 36 |
Total Views | 163 |
Download CIS 110 notes before midterm PDF
August 30 2019 In Dr. Java
1. Public class HelloWorld { 2. Public static void main(String[] args) { 3. System.out.println(“Hello world”); 4. } 5. }
1. Public class PennDrawDemo { 2. Public static void main(String[] args) { 3. 4. // set the canvas size to be 500 x 500 pixels 5. PennDraw.setCanvasSize(500,500); 6. 7. // clear the screen 8. PennDraw.clear(PennDraw.BLUE); // draw a blue sky 9. 10. // draw the sun 11. PennDraw.setPenColor(255, 255, 0); // yellow 12. PennDraw.filledCircle(0.5, 0.5, 0.1); 13. 14. // draw a green field 15. PennDraw.setPenColor(0, 170, 0); 16. PennDraw.filledRectangle(0.5, 0.25, 0.5, 0.25); 17. 18. 19. } 20. }
21.
Public class PennDrawDemo {
22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41.
Public static void main(String[] args) { // set the canvas size to be 500 x 500 pixels PennDraw.setCanvasSize(500,500); // clear the screen int blue = (int) (Math.random()*255); PennDraw.clear(0, 0, blue); // draw a blue sky // draw the sun PennDraw.setPenColor(255, 255, 0); // yellow PennDraw.filledCircle(0.5, 0.5, 0.1); // draw a green field PennDraw.setPenColor(0, 170, 0); PennDraw.filledRectangle(0.5, 0.25, 0.5, 0.25);
} }
PennDraw is “StdDraw 2.0” Colors - 0 no color - “255” = largest value you can represent with bits - Channels: red, green, blue, Alpha channel (how transparent a color is) - Fourth number to specify color - Each color represented by 32 bits - 999 largest number we can possibly represent September 4
- Ellipse for ovals (Penn wiki)
42. Public class PennDrawDemo { 43. Public static void main(String[] args) { 44. 45. // set the canvas size to be 500 x 500 pixels 46. PennDraw.setCanvasSize(500,500); 47. 48. // draw a blue sky 49. Int blue = (int) (Math.random() * 101) + 100; // [0 ... 200] 50. PennDraw.clear(0, 0, 255); 51. 52. // draw the sun 53. PennDraw.setPenColor(255, 255, 0); // yellow 54. PennDraw.filledCircle(0.5, 0.5, 0.1); 55. 56. // draw a green field 57. PennDraw.setPenColor(0, 170, 0); 58. PennDraw.filledRectangle(0.5, 0.25, 0.5, 0.25); 59. 60. 61. } 62. }
63. 64. 65. 66. 67. 68. 69. 70.
Public class PennDrawDemo { Public static void main(String[] args) { // set the canvas size to be 500 x 500 pixels PennDraw.setCanvasSize(500,500); // clear the screen int blue = (int) (Math.random()*255);
71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83.
PennDraw.clear(0, 0, blue); // draw a blue sky // draw the sun PennDraw.setPenColor(255, 255, 0); // yellow PennDraw.filledCircle(0.5, 0.5, 0.1); // draw a green field PennDraw.setPenColor(0, 170, 0); PennDraw.filledRectangle(0.5, 0.25, 0.5, 0.25);
} }
Triangle Line thickness Variables - A nameto which data can - ‘• A variable is declared as - • Names must begin with a ‘$’ and can contain ‘$’ - Use a value throughout your to be changed - • As temporary storage for result - • … etc Declare and initialize “int” means that the integer
variable
“=” stores a value in a variable
be assigned a specific data type lowercase leHer, ‘_’ or leHers, digits, ‘_’ and program,
– butallow it
a
intermediate
will
always
computed
hold an
Interactions pane Integer divided by an integer is always an integer - No fractions or decimals - 5/3 = 1 not 1.6 leaving just the integer part - Division gives the quotient - % = mod - 5%3 = 5 mod 3 - Gives the remainder - Boolean = variable that can only hold a true or false value - == equivalent - = in math but not CS - Floats and doubles store data - Double uses twice the amount of storage to create a number with deicmals - Casting allows you to divide integers with decimals and trunckates it automatically - (int) 5.0 / 3.0 = 1 not 1.67 - Math.random = any random number between [0,1) - [2,5) Random * 3 + 2 - Char = (A, B, etc) takes on a value according to ASCII standards - ! negates boolean value - || means “or” - Strings are “objects” - The other type ois Primitive data types Welcome to DrJava. Working directory is /Users/Cynthia/Desktop/cis 110 hw > run MyDrawing > int a = 1234; >a 1234 > int b; > b = 99; >b
99 > int z; >z 0 > > > >a 1234 >b 99 >a+b 1333 > int c = a + b; >c 1333 >a=0 0 >a 0 >c 1333 > int a, b; > a = 1234; > b = 99 99 > int t = a; >a=b 99 >b=t 1234 > > > >
>5+3 8 > 5 -3 2 >5*3 15 > 5/3 1 > 5%3 2 > 234876274 % 2 0 > 3919833 %2 1 > int n - 2347863 Incomplete expression > int n = 2324234 >n%2=0 Bad left expression in assignment > int n = 2324234; > n % 2 == 0 Invalid top level statement > boolean isEven = (n % 2 == 0) > isEven true > 5-7 -2 > > (int) 255.23442 255 > 5.0 / 3.0 1.6666666666666667 > 5 / 3.0 1.6666666666666667 > double a = 5.0;
> double b = 3.0; > int x = a / b Static Error: Bad types in assignment: from double to int > int x = (int)(a / b) >x 1 > int y = (int)((int) a / b) > double w = 5 / 3 >w 1.0 > Math.sqrt(2.0) 1.4142135623730951 > Math.pow(2.10) Static Error: No method in static Math with name 'pow' matches this invocation Arguments: (double) Candidate signatures: double pow(double, double) > Math.pow(2, 10) 1024.0 > Math.pow(Math.sqrt(2.0), 2.0) 2.0000000000000004 > Math.random() 0.26198115325577387 > (int) 'A' 65 > (int) 'B' 66 > 'A' + 'B' 131 > '3' - '0' 3 > (char)('A' - 'B')
'' > (char)(0) '' > "/0" "/0" > "\0" "" > true == true true > 5 == 0 false > 5 ==5 true > Math.Pi == 3.14 Static Error: No member of Math has name 'Pi' > Math.PI == 3.14 false > Math.PI 3.141592653589793 > 3.141592653589793 == Math.PI true > true != false true > Math.PI !=3.14 true > int x = 5 >5>5 false >5>6 false > 5 5 true && false
false > true && true true > > true || false true > true && false || true && false false > true && false ||true && (false && true || true) true > true ^ false true > '1' + '2' 99 > "1" + "2" "12" > "the quick brown fox" + "jumped over the lazy dog" "the quick brown foxjumped over the lazy dog" > Integer.parseInt("12") 12 > Double.parseDouble("1" + "2.0") 12.0 > Integer.parseInt("12.0") java.lang.NumberFormatException: For input string: "12.0" at java.lang.NumberFormatException.forInputString(NumberFormatException. java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) > Integer.parseInt("12" + (int)'a') 1297 > "" + Math.sqrt(2) "1.4142135623730951" > "" + 1 + 2 "12"
> 1 + 2 + "" "3" >
September 9 What we’ve done already - Primitive data types and variables - Int, long - Float, double, - Boolean - Char - String - Mathematical operators + - * / % - Comparison ⇐ ⇒ ==
Conditionals and Loops - Control flow - Programs execute one statement after another - Conditionals and loops allow us to control the flow
- Animations with PennDraw - “Double sided canvas”
- Infinite While Loop - Runs forever While (true) { Statement 1; Statement 2;
What will this do?
- PennDraw.enableAnimation(10) - 10 frames per second - PennDraw.advance() - Rotating the canvas - PennDraw.disableAnimation() - Rarely used Public static void main(String[] args) { PennDraw.setCanvasSize(500, 500); PennDraw.emableAnimation(30); While (true) { // repeats forever // draw frame of animation (your code here)
PennDraw.advance(); // display next frame } } - Equations of motion
-
September 11 - Conditionals - If statement: a common brancing structure - Evaluate a boolean expression - If true, execute some statements - If false, execute other statements
- How do we provide a number to java? - • Command-line arguments - – args[0] is the first argument, args[1] second argument and so on - – args[0] is a String - Command Line Arguments Public class Hello { Public static void main (String[] args) { String name = args[0]; System.out.printIn(“Hello + name); } }
the
- Args is going to take on value of command line arugment “john” - Concatinating two strings - Boolean expressions take on either true or false
-
1. True 2. False 3. True 4. True 5. True 6. True Booleans are only for primitive types like ints and chars, not strings Thats why - "Penn" == "Penn" - false
-
-
1. False 2. True 3. True 4. True 5. 6. False 7. True
- Conditionals - Switch statemet - Worls like a if-else statement - Convenient for large numbers of value tests Operators
- Iteration - Repetition of a program block - Iterate when a block of code is to repeated mul(ple times - Op(ons - • The while-loop - • The for-loop - While looop - The while loop - A common repetiton structure - Evaluate a boolean expression - If true, execute some statements - Repeat - Initialization, termination (when loop is going to end), modification (modify the variable)
- Break - Continue - Variable scoping - Set of code statements in which the variable is known to the compiler - Where it can be referenced in your program - Limited to the code block in which it is defined - A code block is set of code enclosed in braces {} - Application for this allows Java involved the for loop construct - Scopint and For-loop index - Can declare and initializ variablesin the heading for a loop - These variables are local to the for-loop - May be reused in other loops
- If statements only take on booleans - Array - Bundle of variables
Welcome to DrJava. Working directory is /Users/Cynthia/Desktop/cis 110 hw > double[] x = new double[10]; >x { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } > x[1] = 5; > x[9] = -1; >x { 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0 } > for (int i = 0; i < 10; i++) { x[i] = i * i; } >x { 0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81.0 } > x.length 10 > for (int i = 0; i < x.length; i++) { x[i] = i * i; } > x = new double[15]; > for (int i = 0
double ONE_PIXEL = 1.0 / 512;
(double) pennx = 0 (double) princetonx = 0 PennDraw.picture(...)
while (!pennWins && !princetonWins) { If math.random() If math.random()...