Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12.

Similar presentations


Presentation on theme: "Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12."— Presentation transcript:

1 Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12

2 Week 62 Wrapping up Lab Homework –Task 5: Guessing Game should be done Did you get Task 3: Fibonacci numbers? –Series: 0,1,1,2,3,5,8,13,21,34,55,89 –F(n) = F(n-1) + F (n-2); F(0) = 0; F(1) = 1; –How did you go about solving this?

3 Week 63 Mini-Quiz Ready, Set, Code

4 Week 64 Homeworks Due Thursday October 14 in class (sheet says 7 th ) – 4 problems – choose 3 of them to solve – Extra Credit: Program a rock-paper-scissors game (Hint: you can use chooseRandom from the last assignment to get 0, 1,2 and assign each to rock, paper, scissors, respectively) Gradebook homework problem – coming back to visit it this week

5 Week 65 Introduction to Programming Ms. Knudtzon C Period Thursday October 14

6 Week 66 Control Structures Know when and how to use conditionals –Nested if/else blocks –Using logical operators && || ! Know when and how to use for loops Know when and how to use increment and decrement operators Know when and how to use while loops

7 Week 67 Administrative Details Homework 6 – –hand it in for a grade (email it to me) Lab –Show me it’s all working Extra Credit –Hand it in (email or turn it in)

8 Week 68 Problem Solving – 4 phases Understanding the problem Devising a plan Carrying out the plan Looking back (testing solution) –From “How to Solve It” –By G. Polya

9 Week 69 Understanding the problem Read it – do you understand what it is asking? Think about what it is asking you to do –What are the inputs to the problem? –What are the outputs of your solution? –Are there any conditions? Draw a figure, show a sample of the problem.

10 Week 610 Devising a plan Have you seen a similar problem before? –Or have you seen something like this problem in a different form? Can you restate the problem? Can you think of a more general problem? A more special problem? Can you solve part of the problem? Draw out what you think might work Think about and name the variables you might need Think about what kind of conditional structures might be suited to the problem Pseudocode your plan

11 Week 611 Carrying out the plan Program your plan, checking each step. Is that line of code correct? Is it doing what you want/expect it to?

12 Week 612 Looking back (testing solution) Examine your solution Make sure that you get the expected output Step through your program and trace its state at each step –GRID method (for problems with looping)

13 Week 613 Method Level Problem Solving Understanding the problem –What exactly does this method need to do? –What are the inputs to the problem? –What are the outputs of your solution? –Are there any conditions? What must be true before this method starts (pre- conditions) What will be true when this method finished (post- conditions)

14 Week 614 Method Level Problem Solving Devising a plan –Is there a related method we could modify? –PSEUDOCODE Identify and name variables, Think about the conditional structures you need lay out the steps the method needs to do Carrying out the plan –Turn the pseudocode into Java code Looking Back (testing solution –Step through your program. Do the pre-conditions hold? Do the post-conditions?

15 Week 615 Calculating Powers. Given a base “b” (a double) and a power “p” calculate b to the p power How do we do this?

16 Week 616 Understanding the problem Do you understand what you are supposed to do? –b^p –Samples: 3^4 = 3 * 3 * 3 * 3 5^5 = 5 * 5 * 5 * 5 * 5 –So b^p means that we multiply b times itself p times What are your inputs? What is the output? Are there any conditions?

17 Week 617 Devising a plan Have we seen something similar? –Maybe the factorial problem… Can you restate the problem? Draw out what might work –What are the variables? –Need a loop – because we are repeating an action a number of times So what does the loop do? What kind of loop would be best for this problem? –What do we need to make the loop work? Need to hold the answer in a new variable Need a counter variable –have to count how many times we multiply b by itself (so that we can stop after we multiply it by itself p times) Start to pseudocode a solution

18 Week 618 Carrying out the plan Program your plan, checking each step public double power(double b, int p){ double answer = 1; for(int x = 1; x <= p; x++){ answer *= b; } return answer; } Why do we set answer = 1? What if we thought answer should equal b?

19 Week 619 Looking back (testing solution) Let’s test our solution with trying 5^4 bpxanswer start541 begin loop15 check x <= p ??215 check x <= p ??375 check x <= p ??4375 check x <= p ??DONE

20 Week 620 Quadratic Let's suppose we want to write a program to solve quadratic (i.e. second-order) equations. As you know, given a quadratic equation of the form: ax^2 + bx + c = 0 The quadratic formula gives the two roots (b +/- √(b^2 – 4ac))/2a Now, how do we write the program?

21 Week 621 Understanding the problem Do we know what we are being asked to do? What are the inputs? What are the outputs? Are there any conditions?

22 Week 622 Devising a plan: A Simple Plan Get the coefficients of the equation (could be passed in as parameters or could prompt a user for them) Solve the quadratic formula given those coefficients Print or return the roots

23 Week 623 Devising a plan: A More Detailed Plan We will assume we will prompt the user for input 1.Get the coefficients of the equation from the user (Need InputReader) –Prompt for the three coefficients: a, b, and c –Input the three coefficients: a, b, and c 2.Solve the quadratic formula given those coefficients (Need Java Math package) –compute the common subexpression √(b^2 – 4ac) and call it root –compute the first root: (-b + root)/2a –compute the second root: (-b - root)/2a 3.Print the roots for the user

24 Week 624 Carrying out the plan public void quadratic(){ double a, b, c; // The three coefficients double root, // The Common subexpression double x1, x2; // The 2 results // Get the coefficients from the user System.out.println(("Enter the three coefficients" + " of a quadratic equation:"); a = reader.getDoubleInput(); b = reader.getDoubleInput(); c = reader.getDoubleInput(); // Solve the quadratic formula given those coefficients // Compute the common sub expression root = Math.sqrt(b * b - 4 * a * c); // Computer the two roots x1 = (-b + root)/(2*a); x2 = (-b - root)/(2*a); // Print the roots for the user System.out.println("The roots of the equation are " + x1 + " and " + x2); }

25 Week 625 Looking back (testing solution) Is our program correct? Specifically we need to check the general correctness as well as the boundary cases. –What are boundary cases? No looping (so no need to do the grid check)

26 Week 626 Method Level Problem Solving Understanding the problem Devising a plan Carrying out the plan Looking back (testing solution) How could this help us on the lab problems? Let’s look at solving Fibonacci?

27 Week 627 Homework Method Level Problem Solving: Ask the user for an integer and tell them all the factors of the number (i.e. the numbers between 1 and the number that the number is divisible by). Do it on paper – lay out the four steps

28 Week 628 Administrative Details Turn in homework 7 Homework 6 (due 8 pm) Fibonacci Method Level Problem Solving –Understanding the problem –Devising a plan –Carrying out the plan –Looking back (testing solution) –Note: Special cases for 0 and 1 Calculating Fib for 2 and above

29 Week 629 Fibonacci 1. Understand the problem (on whiteboard) 2. Devise a plan (on whiteboard) 3. Carry out the plan (handout) 4. Looking Back – Grid (on whiteboard)


Download ppt "Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12."

Similar presentations


Ads by Google