Assignment Part 2 - Winston Knoll Collegiate

advertisement
Java Outline (Upto Exam 2)
Part 4 IF’s (Branches) and Loops
Chapter 12/13 (The if Statement)
Hand in Program Assignment#1 (12 marks):
Create a program called “Ifs” that will do the following:
1. Ask the user a skill testing question with a word answer. Tell them if they are right or
wrong. (Ex. What is the capital of Sask?)
2. Ask the user a skill testing question with a number answer. Tell them if they are right or
wrong. (Ex. What is 12 x 6?)
3. Ask the user for their favourite colour. Deal with 4 different colours as answers. Be sure
to deal with the situation that none of the 4 was chosen.
(Ex. If their colour is blue, then tell them they must be cold.
If their colour is red, then tell them they must be hot.
If their colour is green, then tell them they must be naïve.
If their colour is black, then tell them they must be mad.
For all other colours, then tell them that you don’t know what they are.)
Suggestion: Test each section one part at a time then move on to the next part.
Marks (for Assignment#1……“Ifs” program)
1. /2 for proper comments (header + other meaningful comments)
2. /5 for Skill Testing Questions
3. /5 for Colours
Hand in Program Assignment#2 (7 marks):
In a new role-playing fantasy game players must design their character by picking a point value
for each of three characteristics:
Strength, from 1 to 10
Health, from 1 to 10
Luck, from 1 to 10
Write a program called “Fantasy” that asks for a name for the character and asks for the point
value of for each of the three characteristics. However, the total points must be less than 15. If
the total exceeds 15, then 5 points are assigned to each characteristic.
An example of how the game would work is given below:
Welcome to Yertle's Quest
Enter the name of your character: Chortle
Enter strength (1-10): 8
Enter health (1-10): 4
Enter luck (1-10): 6
You have given your character too many points! Default values have been assigned:
Chortle has a strength value of 5, a health value of 5, and a luck value: 5
Marks (for Assignment#2…“Fantasy” program)
1. /2 for proper comments (header + other meaningful comments)
2. /3 for inputs and variables used
3. /2 for outputs (including proper if statement)
Notes Assignment Chapter 12/13 (8 marks)
Briefly define the following terms in your own words (do not copy/paste from the website):
1. two-way decisions
2. block statement
3. boolean expression
4. difference between using the == vs just the = symbols (2 marks)
5. three-way decisions
6. nested if statements
7. danger of comparing floating point amounts (Ex if dlbNum == 5.5)
For an exam, know how to:
 use the if statement
 use the if … else …
 use block if statements, ie using the { }
 use all the Relational Operators (see below)
A == B is A equal to B
A < B is A less than B
A <= B is A less than or equal to B
A > B is A Greater than B
A >= B is A Greater than or equal to B
A != B is A not equal to B
 use nested if statements
 use the if … else if … else (See Mr. Kerbrat about this one if needed!)
 use the .equals( ) method for Strings
 use constants (final + declare variable)
Chapter 14 (Boolean Expressions and Variables)
Hand in Program Assignment (10 marks):
Create a program called “Guess” that will do the following:
1. Ask the user for a number from 1 to 100 (3 are correct).
2. If the number is 33 or 45 or 67 then tell them they guessed one of the right numbers.
3. If the number is within 3 or any one number, tell them they are very close.
(For example, 30, 31, 32, 34, 35, 36 would be very close)
4. If it is any other number between 1 and 100, tell them they are not very close.
5. Numbers less than 1 or numbers greater than 100 should not be allowed. Tell them
they should read the instructions next time.
Marks (for “Guess” program)
1. /1 for proper comments (header + other meaningful comments)
2. /2 for the code to guess the correct number (Must use || in code for full marks)
3. /3 for the code to deal with numbers that are very close to the correct answers
4. /2 for the use of the && Boolean operator in code
5. /2 for dealing with numbers outside of the 1 to 100 range
Notes Assignment Chapter 14 (9 marks)
Briefly define the following terms in your own words (do not copy/paste from the website):
 logical operators (make a list of them) /3
 relational operators (make a list of them) /6
For an exam, know how to:
 create Boolean expressions using the and-operator (&&)
Key Use: To check between two amounts (range testing) (Ex Between 10 and 20)
 create Boolean expressions using the or-operator (||)
Chapter 15-19 (Loops and the While Statement, Counters)
Hand in Program Assignment (20 marks):
Create a program called “Loops1” that will do the following:
1. Use a “while” loop to print out the pattern: 3 4 5 6 7 8 9 10 11 12
2. Use a “while” loop to print out the pattern: 20 18 16 14 12 10
3. Use “while” loops to print out the pattern: 0 1 4 9 16 25 36 49 64 81 100
4. Use “while” loops, counters and multiple variables to print out the following:
The first 10 Fibonacci numbers are: 0 1 1 2 3 5 8 13 21 34
Note: Sequence is derived by adding 2 numbers to get the next in the list.
5. Use a “while” loop that asks for marks. The loop must add up all the marks together.
The loop ends when the user types in any mark that is not between 0 and 100. When
the loop is done, it must calculate the average of the marks and print it out. Take care of
all possibilities (don’t let it crash!).
Marks (for “Loops1” program)
1. /2 for proper comments (header + other meaningful comments)
2. /2 pattern 3,4,5 pattern
3. /2 pattern 20,18,16 pattern
4. /3 pattern 0,1,4,9,16… pattern
5. /5 Fibonacci sequence
6. /6 Marks program
Notes Assignment Chapter 15-19 (6 marks)
Briefly define the following terms in your own words (do not copy/paste from the website):
 loop
 counting loop
 initialization
 increment
 iteration
 infinite loop
For an exam, know how to:
 use the “while” statement as a loop
 use counters (intCount = intCount + 1 OR intCount ++)
 increment loops by values other than 1
(count = count + 2 OR count +=2)
Chapter 41 (The For Loop)
Hand in Program Assignment (6 marks):
Create a program called “Loops2” that will create the patterns below. You must use the “for”
loop.
Marks (for “Loops2” program)
1. /2 pattern 3,4,5 pattern
2. /2 pattern 20,18,16 pattern
3. /2 pattern 0,1,4,9,16 pattern
Chapter 20 (Random)
Hand in Program Assignment#1 (9 marks):
Create a program called “GuessRandom” that does the following:
1. Picks a random number from 1 to 10.
2. Allows the user three guesses. As soon as the user enters the correct number the
program writes a winning message and exits.
3. If the user fails to enter the correct number in three guesses, the program writes a
failure message that includes what the correct number was and then it exits.
Marks (for “GuessRandom” program)
1. /2 for proper comments (header + other meaningful comments)
2. /3 for allowing only 3 guesses
3. /4 for correct messages if right or wrong
Hand in Program Assignment#2 (Bonus of 20% towards Assignments!):
(This is an OPTIONAL program!)
Create a program called “Pig” that does the following:
1. Player and Computer start out with zero points.
2. The winner is the first player to reach 100 points or more.
3. In each round, the computer rolls first, followed by the player.
4. The computer rolls three dice and the sum of spots is added to its score.
5. At the start of the player's turn, a working sum is initialized to zero.
6. The player then rolls a single die as many times as desired.
- If the die shows a 1 (on the first roll or any roll), then the player's turn is over and
nothing is added to the player's score.
- For rolls other than 1, the roll is added to the working sum.
- If the player stops rolling before a 1 occurs, then the working sum is added to the
player's score.
Hints:
 You will need to use multiple loops
 You will need to use loops inside of loops (nested loops)
 Use a press enter to continue section (See Extras folder) to slow down the computer so
that you can see the output one round at a time, rather than in one giant lump
Marks (for Assignment#2…“Pig” program))
1. /2 for proper comments (header + other meaningful comments)
2. /4 for computer’s score
3. /4 for player’s score after 1st round
4. /2 for dealing with 1’s rolled (ending the round)
5. /4 for dealing with loops (nested)
6. /4 for final scores and who wins
For an exam, know how to:
 use the Random class to generate random numbers
 use nextInt(), nextDouble()
 calculate a random number between a range of numbers (eg. between 3 and 10)
EXTRA BONUS (Towards Final Exam…Possible 2 marks added to Final)
Do the PIG Program with a GUI format (Talk to Mr. K. about this Bonus first!)
Download