ap computer science a -- unit 3 · lab: lake lazydays resorts directions: as activity director at...

14
AP Computer Science A -- Unit 3 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 Structures | Evaluating Expressions | Loops | Review Decision Structures (if and if/else) 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.

Upload: others

Post on 10-Jul-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

AP Computer Science A -- Unit 3

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 Structures | Evaluating Expressions | Loops | Review

Decision Structures (if and if/else)

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.

Assignment: if Worksheet

Directions: Complete the if multiple choice worksheet in itsLearning.

Assignment: if-else Worksheet

Directions: Complete the if-else multiple choice worksheet in itsLearning.

Assignment: Relational Operators worksheet

Directions: Complete the relational operators worksheet in itsLearning.

Lab: Lake LazyDays Resorts

Directions: As activity director at Lake LazyDays Resort, it is your job to suggest appropriate activities to guests based on the weather:

temp >= 80: swimming 60 >= temp < 80: rollerblading 40 >= temp < 60: hiking temp < 40: cross-country Skiing

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.

Assignment: MinMax Guessing Game

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.

Assignment: if / strings Free Response

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.

Lab: Raise

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.

Evaluating Expressions

Increment & Decrement

Logical Operators & Truth Tables

Short Circuit Evaluation

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.

DeMorgan's Law of Negation

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.

Practice: Amusement Park

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);

and

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.

Assignment: Conditional Worksheet

Directions: Complete the conditional worksheet in itsLearning.

Assignment: Boolean & Logical Operators Review

Directions: Complete the Boolean & Logical Operators Review worksheet in itsLearning.

Assignment: Boolean Logic Practice Worksheet

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.

Assignment: Boolean Operators Quiz

Directions: Take the boolean operators quiz according to the instructions provided by your

teacher.

Loops

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

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.

Practice: Even Numbers - While Loop

Directions: Write a while loop. When the condition becomes false, program control passes to the line immediately following the loop.

Assignment: While Loops Worksheet

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.

For Loops

Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.

Practice: Repeat 1000 Times - For Loop

Directions: Write a for loop that prints Hello North! exactly 1000 times.

Practice: Print Odd Numbers - For Loop

Directions: Write a for loop that prints the odd numbers from 1 to 100.

Practice: Factorial - For Loop

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.

Example Output:

What number would you like to compute the factorial for? 4 (answer: 24)

Assignment: for Loop Identificaton Worksheet

Directions: Complete the for Loop Identification worksheet in itsLearning.

Enhanced for Loop in Java

This is used to iterate over an array or collection. The general form of the loop is:

for (SomeType of element : collection)

{

Statement

}

Practice: Enumerated type called Month - Foreach Loop

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.

Assignment: Question for Thought 3.1

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.

Lab: Counting & Looping

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.

Lab: Rock, Paper, Scissors (Lizard, Spock)

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:

$ java Rock Enter your play: R, P, or S r Computer play is S Rock crushes scissors, you win!

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.

Assignment: Comparing Strings using compareTo method Worksheet

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

Assignment: Unit 3 Review Worksheet

Directions: Complete the Unit 3 Review worksheet in itsLearning to review for the unit assessment.

Review

Flash Card Deck created by sarush with GoConqr

Resources

Flow of Control Presentation

Increment & Decrement Presentation

Logical Operators Presentation

While Loops Presentation

For Loops Presentation

Sources

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