Decision Structures | Evaluating Expressions | Loops | Review | Resources
All programming languages have statements that help you perform basic operations. These statements handle all programmed activity. Sometimes in programs it is necessary to skip lines of code if they do not apply to the conditions that are given. What if you want a program to make a decision with multiple results based on different preliminary conditions? Many times in programming it is necessary to repeat a segment of code multiple times. It would be very tedious to write lines of code over and over again and it would not be very efficient. The for loop and the while loop allow you to repeat segments of code many times.
Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Directions: Complete the if multiple choice worksheet in itsLearning.
Directions: Complete the if-else multiple choice worksheet in itsLearning.
Directions: Complete the relational operators worksheet in itsLearning.
Directions: As activity director at Lake LazyDays Resort, it is your job to suggest appropriate activities to guests based on the weather:
Download the Lake LazyDays Resort lab for the full instructions. Follow your teacher's instructions for submitting the code and a screenshot from the BlueJ terminal window.
Directions: The MinMax guessing game will pick a random number between 1 and 100 (inclusive), then keep asking the user to guess the number. On each guess, it reports to the user that he is correct or his guess is high or low. It keeps accepting guesses until the user guesses correctly or quits. It uses a sentinel value to determine whether the user wants to quit and counts the number of guesses reporting the value when the user guesses correctly. At the end of the each game, it asks whether the user wants to play again. It keeps playing games until the user chooses to stop. Download the worksheet and follow your teacher's instructions for submitting the answers.
Directions: Download the handout: if / strings Free Response. Complete parts A and B in the handout. Responses must be hand-written. Follow your teacher's directions for submitting your work.
Directions: Create a program that takes as input an employee's salary and a rating of the employee's performance and computes the raise for the employee. The performance rating here is being entered as a String-the three possible ratings are "Outstanding", "Acceptable", and "Poor". An employee who is rated excellent will receive a 9.9% raise, one rated good will receive a 6% raise, and one rated poor will receive a 1.4% raise.
Add the if... else... statements to program Salary to make it run as described above. Note that you will have to use the equals method of the String class (not the relational operator ==) to compare two strings.
Download the Raise lab for the full instructions and the Raise source file. Follow your teacher's instructions for submitting the code and a screenshot from the BlueJ terminal window.
Short circuit evaluation means that the second condition is not necessarily checked if the result from the first condition is enough to tell if the result is true or false. Both && (and) and || (or) use short circuit evaluation. In a complex conditional with a logical && both conditions must be true, so if the first is false, then it is not necessary to evaluate the second. If the complex conditional uses a logical || and the first condition is true, then the second condition will not be executed, since only one of the conditions is required to be true and the first condition satisfied the requirement.
On the AP Computer Science Exam, you must be able to evaluate complex compound conditionals.
DeMorgan's Law can help you decipher ones that fit certain criteria. An easy way to remember how to apply DeMorgan's Law is to think of the distributive property from algebra, but with a twist. Distribute the ! to both the conditionals and also change the logical operator to its opposite.
Directions: This program reads input from the user to determine if the user can ride the rollercoasters and swim in the pools at the amusement park. The program computes 2 boolean expressions in order to determine what the user is allowed to do:
boolean cannotRide = !(oldEnough && tallEnough);
boolean cannotSwim = !(canSwim || hasLifeJacket);
Convert these two lines into their equivalent DeMorgan style boolean expressions. Negate the AND in the first statement and negate the OR in the second statement using DeMorgan's Laws. Your resulting boolean expression for cannotRide should include both oldEnough and tallEnough. Your resulting boolean expression for cannotSwim should include both canSwim and hasLifeJacket. The resulting program should still be able to successfully determine if the user can ride the rides and swim in the pool.
Directions: Complete the conditional worksheet in itsLearning.
Directions: Complete the Boolean & Logical Operators Review worksheet in itsLearning.
Directions: Complete the boolean logic practice worksheet in itsLearning. My best advice to solving some of the questions is to assign values to the variables and trace them through.
Directions: Take the boolean operators quiz according to the instructions provided by your teacher.
Repetition statements allow us to execute a statement multiple times and are often referred to as loops. A loop statement allows us to execute a statement or group of statements multiple times. Like conditional statements, loops are controlled by boolean expressions. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
While loops are indefinite loops because they do not have a predetermined number of iterations. A while Loop will keep iterating until the boolean condition that it evaluates is false, in other words, it repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. When the condition becomes false, program control passes to the line immediately following the loop.
Some while loops will contain a sentinel value. The sentinel value, often -1 or -999 is a value that a user can enter to stop the loop. For example, if you were creating a program where users were entering student grades, the program might ask users to enter -1 when they were finished. The boolean expression would be looking for a grade that was obviously greater than or equal to zero so a negative value would stop the loop.
Directions: Write a while loop. When the condition becomes false, program control passes to the line immediately following the loop.
Directions: Download the While Loops worksheet. Work through each while loop scenario and then input your answers into the itsLearning While Loops Worksheet Answer Document.
Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
Directions: Write a for loop that prints Hello North! exactly 1000 times.
Directions: Write a for loop that prints the odd numbers from 1 to 100.
Directions: Write a for loop. Write a program that prints a factorial to the screen. The factorial of a number is found by multiplying itself by all positive integers less than it (excluding 0). For example, 4 factorial, written as 4!, can be computed as follows:
4! == 4 * 3 * 2 * 1 == 24
After taking user input for the factorial they want to find, print out the factorial to the screen.
What number would you like to compute the factorial for? 4 (answer: 24)
Directions: Complete the for Loop Identification worksheet in itsLearning.
This is used to iterate over an array or collection. The general form of the loop is:
for (SomeType of element : collection)
Directions: Write a foreach loop that prints all possible values of an enumerated type called Month.
This is mainly used to traverse collection of elements including arrays.
Directions: Explain the differences between a "for loop" and a "while loop." What conditions would be most beneficial for using each loop? Summarize your thoughts in ~50 words and submit directly to the itsLearning assignment textbox. Do not attach a separate document and proof read before submitting.
Directions: The program in LoveCS.java prints "I love Computer Science!!" 10 times. Copy it to your directory and compile and run it to see how it works. Modify the program according to the Counting & Looping instructions. Submit your code and a screenshot from BlueJ to the itsLearning assignment. Make sure your identification is included in the comments.
Directions: The program in rock.java contains a skeleton for the game Rock, Paper, Scissors. Open it and copy the Java code to your directory. Add statements to the program as indicated by the comments so that the program asks the user to enter a play, generates a random play for the computer, compares them and announces the winner (and why). For example, one run of your program might look like this:
Note that the user should be able to enter either upper or lower case r, p, and s. The user's play is stored as a string to make it easy to convert whatever is entered to upper case. Figure out a way to convert the randomly generated integer for the computer's play to a string. (Note, for bonus points add Lizard and Spock to your game).
Modify the program according to the Rock, Paper, Scissors, Lizard, Spock instructions. Submit your code and a screenshot from BlueJ to the itsLearning assignment. Make sure your identification is included in the comments.
Directions: The method compareTo() is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns a positive or negative value. The result is positive if the first string is lexicographically greater (comes before) than the second string else the result would be negative.
Here the comparison is between string literals. For e.g. string1.compareTo(string2) where string1 and string2 are String literals. Complete the worksheet in itsLearning
Directions: Complete the Unit 3 Review worksheet in itsLearning to review for the unit assessment.
To prepare for the unit assessment, you should utilize the stude guide.
Flash Card Deck created by sarush with GoConqr
Flow of Control Presentation
Increment & Decrement Presentation
Logical Operators Presentation
While Loops Presentation
For Loops Presentation
Loop Architecture image: https://www.tutorialspoint.com/java/java_loop_control.htm
Decision Making Structure: https://www.tutorialspoint.com/java/java_decision_making.htm
Relational Operators Image: https://corejava25hours.com/category/java/
Rock, Paper, Scissors, Lizard, Spoke image: public domain