Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 Introduction to Programming
Ms. Knudtzon C Period Monday Oct 4 – Thursday Oct 7 Week 5

2 In Class Lab Monday, Tuesday & Wednesday class periods
Work on the lab assignment, getting help from Ms.K as necessary Week 5

3 Thursday Lab Review Week 5

4 What’d we learn? Task 0 and Task 1 are working for everyone (I think)
But could you do it again without looking at your code? Did you understand what you did and why you did it? Let’s look in detail at what we did Week 5

5 Lab: Task 0 Looping until the user says to quit
Draw out the algorithm (steps) for the code What does it look like in Java? Task 0: Reading, beginning to lay out the code. Need to understand that we are making a class, that we have a variable of type InputReader that is handling the user input for us (declaring that variable), making the constructor and creating the InputReader object (with the new command), making the driver method for our code – “runMenu” which presents the user with the options of what the program does Also need to understand calling another method (getTasks) from inside runMenu and how that works in Java Algorithm for runMenu: Print the menu of choices ask the user to choose one while the user hasn’t said they want to quit do the choice the user wants print the menu again ask the user to choose again when the user quits say bye My code to do this (which calls other methods): public void runMenu(){ int choice = 0; printWelcome(); printMenu(); choice = reader.getPositiveInt(); while(choice != 1){ doChoice(choice); System.out.println("Press enter to continue"); reader.getStrInput(); //get the enter before printing the menu } System.out.println("Bye."); Week 5

6 Task 1: Factorial Draw out the algorithm (steps) for the code
What does it look like in Java? Algorithm: Get the number the user wants to compute the factorial of multiply ((1 * 2) * 3) … [number user inputted]  indicates we want a for loop starting at 1 and ending at number  we want to store the first multiplication and multiply that result times the next number in the serier (In class exercise – draw all the variable “containers” and see what they hold at each step My Java code to do this: (Note: There are multiple ways to accomplish this task!) /* * This method computes the factorial of the integer provided by a user */ private int factorial(){ int num = reader.getPositiveInt(); int fact = 1; for(int i=1; i <=num; i++){ fact *= i; } return fact; Week 5

7 Task 2: Additive Factorial
Draw out the algorithm (steps) for the code What does it look like in Java? Same algorithm as in factorial, only this time we are adding the next number to the result My code to do this: private int addFact(){ int num = reader.getPositiveInt(); int fact = 0; for(int i=1; i <=num; i++){ fact += i; } return fact; Week 5

8 Task 4: Next Perfect Square
Draw out the algorithm (steps) for the code What does it look like in Java? Algorithm (do as an in class exercise) Get number from the user have a count variable that increases by 1 each time store the current square value (count * count) before we increase the count variable, see if the user’s number equals or is less than the square value if it is  we’ve found the next perfect square if not  we need to increase count and keep going My code to do this: (Note: There are multiple ways to accomplish this task!) (In class, also have Austin show the way he did this code, since he used a for loop) private int nextSquare(){ int num = reader.getPositiveInt(); boolean found = false; int counter = 1; int square = 1; while(!found){ if(num == square){ //the number entered was a perfect square found = true; } else if(num < square){ //we've found the next perfect square else{ //the squares we've found are still less than the user's input counter++; square = counter*counter; return square; Week 5

9 Weekend Homework Task 5 – Guessing Game Task 3: Fibonacci numbers
3 iterations of guessing game Shouldn’t be too difficult Task 3: Fibonacci numbers Series: 0,1,1,2,3,5,8,13,21,34,55,89 A little tricky, do your best – if you can’t get the program to work, turn in a pseudo-code algorithm laying out what you think might work We’ll have a mini-quiz on Tuesday in class Making sure you understand the problem-solving techniques and code that you did in the lab Week 5

10 Test if (testScore < 90 && studentWantsToImproveGrade){
doExtraCredit(); } public void doExtraCredit(){ //See Ms. K for what to do Week 5


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google