Presentation is loading. Please wait.

Presentation is loading. Please wait.

Making Decisions uCode: October 2014. Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.

Similar presentations


Presentation on theme: "Making Decisions uCode: October 2014. Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real."— Presentation transcript:

1 Making Decisions uCode: October 2014

2 Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real world. o Methods o Parameter o Variables o Classes

3 Overview Today we will talk about: Part I 1.Reading code/comments 2.Designing a program Part 2 1.Making decisions in code

4 Reading Code What do you do when you want to know how to build a rocket? o Look it up on Google o Ask Mom, Dad, or Rocket Scientist o Go to the library and check out a book on building rockets o Take apart a rocket similar to the one you want to build The point is : If you do not know how to do something, you ask for help. o Reading the code from someone else can help you find your solution. o Good code will have comments – read them

5 Designing a Program It is much easier to go somewhere when you know where you are going. o Understand your problem o Design a solution o Implement and test your solution o Re-evaluate your solution “Designing a solution is messy and does not follow a straight road.”

6 Understand Your Problem Figure out what it is that you want to do o Ask lots of questions! o Write things down o Draw pictures

7 Understand the Problem Problem : We are going to build a number guessing game program. Player 1 will select a secret random value between 0 and some number and write it down so that player 2 cannot see it. Player 2 will make guesses one number at a time. Player 1 can only say too high, too low or correct. To Do : Play the game several times with your partner. Switch places every so often.

8 Design a Solution This is point at which you start putting your program together on paper. o A problem can break down into smaller problems (guess what; smaller problems are easier to solve!!!). o Look for similar solutions to parts of your problem. To Do : Make an ordered list of steps describing how to play the game from start to finish.

9 Implement & Test Your Code Once you have an idea where you are going to go, you may open BlueJ and start working. o Write code for a small problem then test it to make sure it works. Vocabulary Implement – write the code to solve your solution. Test – run your program to see if the code works as you expect.

10 Guessing Game Plan To play one round of the Guessing Game 1.Get a maximum range value from the player. 2.Get the random number. 3.Tell the user a number between 0 and the maximum range value the player inputted. 4.Play the game until the player guesses the secret number. 1.Get the player’s guess. 2.Evaluate the guess and tell the player whether their guess is too big, too small, or just right. 5.Exit the program.

11 Consider Alternatives Quite simply, given the step you just wrote into code, does your code work the way you expect??? o Yes? Great move on to the next step. o No? Well take a look at what you have done and do something different. To Do : 1.Select a random number and store it. 2.Print out the number to guess to test it. You will need to remove this later.

12 Solution to first task public void playGame() { // Assume player will play at least one game. // Initialize value to y for yes. play = 'y'; while(play == 'y'){ // If the user wants to play // Get the random number to guess and assign it to // the variable that will hold the secret number. secretNumber = getRandomNumber(maxNumToGuess); // … code has been removed … }

13 Summary Part I Up to now we have discussed getting help and the process for solving a problem. Reading someone else’s code that solves a similar problem can help you a lot. Following a step by step process can help you to face a problem with confidence and knowledge that you can solve it. Understand ing a problem will give you the ability to break it into steps. Each step is a smaller problem that will lead you to a solution to the original problem. Design ing a solution to a small problem is easier than finding a solution to a larger one. Implement and test your code according to the steps in your list. Evaluate your program to make sure it works as you expect.

14 Part II: Making Decisions What do you do when you want to buy the latest iPhone 6? A.Ask mom or dad if they can buy it? B.Check to see if you have enough money in your wallet to buy it. They key is asking a question to which the answer is either “yes” or “no”. So your question might look something like this: If “money in my wallet” greater or equal to $600 then Buy the iPhone 6.

15 Part II: Making Decisions So your code might look something like this: Pseudo-Code : If “money in my wallet” greater or equal to $600 then Buy the iPhone 6. Code : Line 1: int cashOnHand = 650; Line 2: if( cashOnHand >= 600 ){ Line 3: buy(“iPhone 6”); Line 4: }

16 Asking the Question Questions have to be asked so that you get a yes/no answer. Relational Operators You can ask a yes/no question by comparing values:, ≠, =  you know these from math. The good news you can use them when programming. MathJava Equivalent << >> ≤<= ≥>= === ≠!=

17 Exercise 10 > 5 yes girl == Girl no y != n yes 40 <= 41 yes 3 < 3 no 800 != 800 no 55 >= 55 yes int grade = 85; grade > 90 no 33,333 == 33,333 yes Answer yes/no to answer the given questions?


Download ppt "Making Decisions uCode: October 2014. Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real."

Similar presentations


Ads by Google