IT1090cLab3

docx

School

University of Cincinnati, Main Campus *

*We aren’t endorsed by this school

Course

1090

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

8

Uploaded by MegaRainMandrill31

Report
IT 1090C Computer Programming I Prof. Tom Wulf Lab 3 Looping 15 pts / 6 extra Mini-lecture: We will continue to refine our pseudo code. Now, we will look at looping We looked at looping in class. Java has a while loop and a for loop. These are both pre-test loops. The for loop is a definite loop and the while loop is an indefinite loop. (Java also has a post-test indefinite loop: do..while. Here is the flowchart and the pseudo code for an indefinite loop that uses a sentinel to terminate the repetition of the loop. In this variation the sentinel is the value “N” which the user enters when they are prompted if they want to continue or not. Here is the general format for the while loop in pseudo code: while BOOLEAN EXPRESSIO statements here that will be repeated endWhile As discussed, Boolean expressions are expressions that evaluate to true or false. These are formed by relational operators (<. >,< =,>=, !=) and the logical operators AND && and OR ||. Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
For loops: For loops are definite pre-test loops. Use these when you want to repeat something some fixed number of times. Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
Pseudo code for the for loop: for count = 1 to 5 step 1 these statements repeat endFor Pseudo code for the do .. while loop do these statements repeat while BOOLEAN EXPRESSION Which to use? Use a while loop when you don’t know exactly how many times the loop will repeat (indefinite loop) Use a for loop when you know exactly how many times the loop will repeat (definite loop) Both these loops are pre-test loops which means that if the first test fails (is false) then the statements in the loop body are never executed. The do while loop is also indefinite but is post test so that loop runs the code block at least once before the first test. Every time you get input you do this. You collect data and plan to loop back only to correct it! Lab: 1. Just submit this document (See directions below.) and insert your work after each task description. 2. For each of the following tasks, provide the complete pseudo code including all elements that we have been using (i.e. class, end class, main return, output prompt). Be sure to declare your variables and use symbolic constants where appropriate. Determine if each task requires a definite or indefinite loop and use the correct one. Use the do while loop for loops that you want to execute at least once, like getting input. a. Task 1 (3 pts): an application program where the user enters the price of a series of items (assume at least one.) (Prompt user for additional items until they are done, so we don’t know ahead how many items there will be. This is the looping part! ;) ) The program computes shipping costs. Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
If the total price of all items is $100 or more, then shipping is free otherwise it is 2% of the price. The program should output the total price of all items, the shipping cost, and the total cost. (Indefinite loop – do while) Class ShipCost Main() // Declare variables Double total_price = 0.0 Double item_price = 0.0 Char should_continue = "Y" // Loop to add item prices Do Output "Please enter the item price:” // Input item price Input item_price total_price = total_price + item_price Output "Anything else? (Y/N)" Input should_continue While should_continue = "Y" // Calculate shipping cost If total_price >= 100 Output "Shipping is free." Else shipping_cost = total_price * 0.02 final_cost = total_price + shipping_cost Output "Your total is " + total_price + " and shipping cost is " + shipping_cost + “so you owe “ + End Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
b. Task 2 (4 pts): A program that prompts the user for an initial balance and a monthly interest rate and then displays each new monthly balance for a 12 month period. (Interest is compounded. Print inside the loop so you display each of the 12 balances not just the final.) (Definite loop – for loop) Class MonthlyBalance Main() //declarations initialBalance, monthlyInterest, finalBalance Output: “Enter your initial balance” Input: initialBalance Output: “Enter your monthly interest rate ” Input intrestRate // looping 12 times For month 1-12 // calculate new balance Final_balance = initial_balance * (1 + interestRate) // display new balance and month Output month + “ “ + new_balance // update initial balance Initial_balance = new_balance End for end c. Task 3 (3 pts): A program that collects a series of grades (ranging 0 – 100) until the user indicaes that they don’t want to continue entering values useing a sentinel value. -1 is a good choice for a sentinel here.) The program should display the average of the grades entered. (Sentinel Value) Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
Class GradeAverage Main() LoopValue = 1 i = 0 GradeTotal = 0 While LoopValue == 1: GradeValue = 0 //output Ouput “Please enter the grade from 0-100:” //input Input GradeValue // Update GradeTotal GradeTotal = GradeTotal + GradeValue //update number of values entered i += 1 //output Output “Do you have anymore values to enter (1 – yes 0 – no) Input LoopValue End While Loop //output Output “The average of all the entered grades is:” + (GradeTotal / i) end Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
d. Task 4 (5 pts): Rock Paper Scissors: The game repeats as long as the user enters Y to a prompt to continue. Get a move from player A and then from player B. Assume that the move is valid so it has to be one of the 3 choices (R P or S). Your program should compute the winner with an appropriate output string: “Rock Breaks Scissors Player A wins, etc... And then prompt to play again… ( cross product) Class HandGame Main() //declarations LoopValue = “Y” While LoopValue == “Y” PlayerA_Choice = “” PlayerB_choice = “” //output Output “The game is rock paper scissors each player will enter( R-Rock, P-Paper, or S-Scissors)” Output “PlayerA enter your move:” Input PlayerA_Choice Output “PlayerB Enter your move:” Input PlayerB_Choice If (PlayerA_Choice == R && PlayerB_Choice == R) || (PlayerA_Choice == P && PlayerB_Choice == P) || (PlayerA_Choice == S && PlayerB_Choice == S) //output Output “Game is a draw would you like to play again? (Y o N)” Input LoopValue Restart or End Loop Else if (PlayerA_Choice == R && PlayerB_Choice == S) || (PlayerA_Choice == P && PlayerB_Choice == R) || (PlayerA_Choice == S && PlayerB_Choice == P) //output Output “Player A wins the game and Player B loses would you like to play again?(Y o N)” Input LoopValue Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
Restart/End Loop Else if (PlayerB_Choice == R && PlayerA_Choice == S) || (PlayerB_Choice == P && PlayerA_Choice == R) || (PlayerB_Choice == S && PlayerA_Choice == P) //output Output “Player B wins the game and Player A loses would you like to play again?(Y o N)” Input LoopValue Restart/End Loop end Extra Credit Options: e. Task 5 (4 pts): Code a number guessing game. The user picks a number between 1 and 10 and the computer ‘guesses’ the value. Each time, the player indicates if the guess is larger or smaller by entering a plus or a minus sign. If the computer guesses the value correctly then the user enters a exclamation point !. Assume in this case that the player/user it truthful. f. Task 6 (2 pts): A program that displays the 12 by 12 multiplication table. This is a bit of a challenge as it requires a nested loop so I’ll grant you extra credit if you complete it! 3. Submitting your work: carefully check your work. Rename your word file as Lastname_Firstname_Lastname_Firstname Lab03.docx using your name. Submit this file using the Canvas assignment mechanism. For The Extra Credit resubmit the entire lab again using the second submission link provided. 4. Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.