1 / 20

Computer Science: The Third Presentation

Computer Science: The Third Presentation. For Loops, Multiple Inputs, Miscellaneous . Review. Last time we learned that “=“ is an assignment operator used to assign values to variables It’s important that we declare the variable’s type when we declare a variable

alcina
Download Presentation

Computer Science: The Third Presentation

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. Computer Science: The Third Presentation For Loops, Multiple Inputs, Miscellaneous

  2. Review • Last time we learned that “=“ is an assignment operator used to assign values to variables • It’s important that we declare the variable’s type when we declare a variable • Also learned about primitive data types- Java reserved data types • Ones you need to know: int, double, boolean

  3. While Statements • We learned about while statements, syntax: while ([something holds true) { //do something } • We have to remember to move the while loop toward termination • Eventually the [something holds true] must not be true

  4. If Statement • We also learned if statement, syntax: if ([something is true]) { //do something } • We usually follow this with an “else” statement: if this is true do this, or else do this

  5. Major Things in Programs You Wrote • Things we need to remember: • Not okay: while (condition); statement1; • From the book: It is important not to put a semicolon after while(condition). With a semicolon, the loop would have no body, only an empty statement; statement1 would be left completely out of the loop. • When intializing variables, be sure to include the variable type • Eg “count = 1;” is not valid • “double count = 1;” is

  6. A Reminder • The powerpoints are not supposed to be a replacement for the book • They are supposed to provide clarification on beginning concepts • So read the book • John Shumway, Block 1 and 2

  7. First, some additional syntax • In java, we are often adding, subtracting, etc variables • There is a shorthand way for writing • sum = sum +2; • sum +=2; • The two ways are identical • We could also use “sum -= 2;” or “sum *= 2”

  8. Incrementing • We are also often adding one to our variables- think back to our while loop for sum of squares, where we added one to count at the end of the loop • Instead of writing out “count = count + 1;” or even “count +=1;” there is an even simpler way • “count++;” • Could also use “count--;”

  9. For Loops (copied from the book) • Think back to while loops • Here’s a typical while loop • int n = 1; int sum = 0; while (n < 10) { sum += n; n++; } • Can identify three elements of this while loop

  10. Parts to a While Loop • int n = 1; • int sum = 0; • while (n < 10) • { • sum = sum + n; • n++; • } • Initialization: we must initialize the variable to be used in the condition before the loop is entered • int n = 1; • Testing: condition is tested for truthfulness before it executes another pass through the loop • Is n < 10? • Change: our condition must at some point be false, otherwise – INFINITE LOOP • n++;

  11. “For” Loops • What if we could combine the three elements from a while statement into one cohesive statement? • We can • for (initialization; test; change) { //do something }

  12. How would we approach our sum of squares method while using a for loop? • private static int sumOfSquares (int n) { int sum = 0; for (int j = 1; j <= n; j++) { sum +=j; } return sum; } • Our “counter” variable is j, we set it equal to one • Our test: is j less than or equal to n? • After each execution of the for loop, add one to j

  13. This is one of those things you just have to remember, you’ll see it very, very often on the AP exam • for (initialization; test; change) • You’re going to have to painstakingly traverse complicated for loops- look forward to it

  14. Assignment • Write a method that, using for loops and what you’ve learned so far takes n as an input and returns the nth Fibonacci number • Hints: for n=1, and n= 2 the method should return 1 without any calculation. How could you do this? • With a somewhat complicated program like this, it helps to write out/think about how you would do this before putting it in abstract computer code

  15. Simple Piece of New Info • A method can take multiple inputs • For example, say we wanted to write a method that takes two numbers a and b and multiplies them together • private static int multiplyNumbers (int a, int b) { return a*b; } • Notice there is no need to create a new variable

  16. Assignment • Write a method that takes integers a, b, and c as inputs and returns the area of a triangle with a, b and c as side lengths • Hints: • Can you test if a, b, and c are valid triangle lengths and return “0” if they are not? • If we have an arithmetic series: a + (a+d) + (a+2d) +…+(a + (n-1)d) write a program that takes integers, a (beginning term), d (difference), and n (number of terms) and returns the sum of the series

  17. Another random piece of Syntax • There is another big command out there besides adding, subtracting, dividing, and multiplying • That’s taking the mod of a number • If you don’t know what a mod is, wikipedia or google are excellent sources • Basically saying “ 7 mod 5 is congruent to 2” is equivalent to saying the remainder when we divide 7 by 5 is 2

  18. We write this as • 7 Ξ 2 (mod 5) • More mod practice: • 6 Ξ 1 (mod 5) • 17 Ξ 8 (mod 9) • 78 Ξ 8 (mod 10) • 176532 Ξ 2 (mod 10) • Notice that taking a number mod 10 gives us the last digit of that number • If you’re still confused, read a math book

  19. Java Syntax • Suppose we want to take the mod of a number in Java…for example, we wish to know the remainder when 5 is divided by 3 • We want to set the integer variable j equal to this • int j = 5%3; • Really the only time I’ve used this is to take numbers mod 10, mod 100, or mod 1000 to get the last 1, 2, or 3 digits respectively

  20. What’s Ahead • Next time, we will consider arrays and ArrayLists, Strings, and what’s happening with the rest of the programs outside the method • Eclipse • Then write a bunch of programs to learn how to write a good program • At some point we will consider the hard part of Java- classes • Apologies for the short presentation this week

More Related