Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week91 APCS-A: Java Problem Solving November 2, 2005.

Similar presentations


Presentation on theme: "Week91 APCS-A: Java Problem Solving November 2, 2005."— Presentation transcript:

1 week91 APCS-A: Java Problem Solving November 2, 2005

2 week92 Test Cases A way to verify that your code is correct (Show off in BlueJ how it all works)

3 week93 How you problem solve… What do you do when faced with a new problem?

4 week94 Today We are going to look at a general method of problem solving  You can compare this with what you already do This may or may not improve what you already instinctively do, and if nothing else, it might help you help a friend who’s struggling with a problem

5 week95 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

6 week96 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.

7 week97 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

8 week98 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?

9 week99 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  Follow through your code and make sure you didn’t stop earlier, and that your variables get the value that you expected  For problems with loops, draw a grid and trace each step through the code

10 week910 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)  Could assert they are true before the method continues: What will be true when this method finished (post- conditions)

11 week911 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?

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

13 week913 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?

14 week914 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

15 week915 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?

16 week916 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

17 week917 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?

18 week918 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?

19 week919 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

20 week920 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  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

21 week921 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 Scanner reader = new Scanner (System.in); // Get the coefficients from the user System.out.println(("Enter the three coefficients" + " of a quadratic equation:"); a = reader.nextDouble(); b = reader.nextDouble(); c = reader.nextDouble(); // 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); }

22 week922 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)

23 week923 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 and homework problems?  Did we all finish the looping lab from last week? Factorial, Fibonacci and Next perfect square

24 week924 Problem Solving We’ve looked at problem solving at the method level What about at the higher levels?  The Object level  The Project level (multiple objects)

25 week925 Project Level Problem Solving Program/project level: HIGHEST level Essentially making a model, deciding what parts are related  What are the key components of this problem?  What kind of information needs to be known?  What objects do you need to do the task?  How do they interact?  Where is your key data going to be stored?

26 week926 Problem Solving What are the key components of this problem? What kind of information needs to be known? What objects do you need to do the task? How do they interact? Where is your key data going to be stored?

27 week927 Problem Solving Lay out the big picture (high level design) Break that into objects (object level design) Break each object into components (method level design)

28 week928 APCS-A: Java Pass by Value and Pass by Reference November 3, 2005

29 week929 Passing data to methods We’ve all passed values to methods to be used in calculation (parameters) We’ve returned item back from methods But how does this all work?

30 week930 Pass by Value When we pass an argument by value to a method, a copy of the argument’s value is made and passed to the called method public void method1(){ int x = 5; System.out.println(“X is: “ + x); method2(x); System.out.println(“X is: “ + x); } public void method2(int param){ param = 20; }

31 week931 Pass by Value x int int x = 7; 00000111 Declare an int value and assign it the value ‘7’. The bit pattern for 7 goes into the variable named x. z int void go(int z) { }Declare a method with an int parameter named z. x int 00000111 z int 00000111 Copy of x Call the go() method, passing the variable x as the argument. The bits in x are copied, and the copy lands in z. go(x);

32 week932 Confusion A lot of you are still unclear about variables, parameters and return values  If a variable is not a class-level (instance) variable then it is not visible to other methods AND NOTE: only things that logically make sense in the model of the object should be made instance variables  So anytime we call a method with a primitive data type, a copy of the variable is made The receiving method can call the incoming number (or boolean, etc) anything they want Similarly, when a number is returned from a method, the caller of the method can name the returned value anything it wants

33 week933 But now…. We are going to confuse matters even more… Only primitives (and Strings, which are immutable) are passed by value. Objects are passed by reference  So what I’ve said still holds true, but what happens under the scenes is different You don’t have to return an Object to change the data - when you pass an Object to a method, anything you do to the Object in that method happens to the real Object, not to a copy of it.

34 week934 Pass by reference When we pass by reference, the caller gives the called method the ability to access the caller’s data directly and possibly modify that data (it is not copied) -- Basically a pointer (the address) to the object is given to the method public class PassByReferenceExample { public void sampleMethod() { Kendra k = new Kendra(); System.out.println(k); changeKendra(k); System.out.println(k); } public void changeKendra(Kendra kk){ kk.changeName(); kk.x++; } public class Kendra { public int x = 0; private String name="kendra"; public void changeName(){ name = "kendra walther"; } public String toString(){ return "Object: " + name + " x is: " + x; } The variables k and kk both point to the same Kendra Object

35 week935 Where we go from here We’ll talk even more about this next week, but I’m hoping that this will start to clear things up for you. Finish your codebreaker assignment and send it to me Saturday. I will email you a “receipt” at some point Saturday afternoon, so if you don’t get that, please re- send your project. Then enjoy the rest of your weekend.


Download ppt "Week91 APCS-A: Java Problem Solving November 2, 2005."

Similar presentations


Ads by Google