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

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

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

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

Week 6 1

Introduction to Programming

Ms. Knudtzon

C Period

Tuesday October 12

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

Week 6 2

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?

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

Week 6 3

Mini-Quiz

Ready, Set, Code

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

Week 6 4

Homeworks

• Due Thursday October 14 in class (sheet says 7th)– 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

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

Week 6 5

Introduction to Programming

Ms. Knudtzon

C Period

Thursday October 14

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

Week 6 6

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

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

Week 6 7

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)

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

Week 6 8

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

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

Week 6 9

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.

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

Week 6 10

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

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

Week 6 11

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?

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

Week 6 12

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)

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

Week 6 13

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)

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

Week 6 14

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?

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

Week 6 15

Calculating Powers.

Given a base “b” (a double) and a power “p” calculate b to the p power

How do we do this?

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

Week 6 16

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?

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

Week 6 17

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

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

Week 6 18

Carrying out the plan

• Program your plan, checking each steppublic 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?

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

Week 6 19

Looking back (testing solution)Let’s test our solution with trying 5^4

b p x answer

start 5 4 1

begin loop 1 5

check x <= p ?? 2 15

check x <= p ?? 3 75

check x <= p ?? 4 375

check x <= p ?? DONE

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

Week 6 20

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?

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

Week 6 21

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?

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

Week 6 22

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

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

Week 6 23

Devising a plan: A More Detailed Plan

We will assume we will prompt the user for input1. 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

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

Week 6 24

Carrying out the planpublic 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);

}

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

Week 6 25

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)

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

Week 6 26

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?

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

Week 6 27

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

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

Week 6 28

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

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

Week 6 29

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)