This is Java.
- Programming Exercises
- Regular 100/80/60 Quizzes
- Short Answer Quizzes
- Unit Exam
- Game Project (15% of final mark)
Resources
Do this:
- Read this w3 While Loop.
- Watch this Java While Loop
- Do these problems: Control Flow Statement Questions and Exercises.
- Write a while loop that prints 10 random numbers (all between 1 and 10).
- Modify the loop from question 1 so that if one of the random numbers is 5, it prints "You win" and ends the loop.
- Write a while loop that prints the digits -100 up to 100.
- Write a while loop that prints the digits 100 down to -100.
a) Monty Hall
- Watch The Monty Hall Problem.
- Watch Monty Hall's Denial of the problem.
- Download and play this working version of MontyHall.java
- Modify this game so that the user can play repeatedly in a single run of the program. Count the number of times the player wins, and print the percentage of wins at the end of the program.
- Watch this video on Ending a Loop with a Sentinel Value (C++)
- Write a program that reads any number of grades in numerical format, averages them,
and then displays the average to the user. Use a
while
ordo...while
loop to enter grades and a sentinel variable to indicate when to stop entering grades. Allow the program user to determine when to stop entering the grades.
- Watch this video on the For Loop
- Read this Going Loopy Tutorial.
- Complete these Loops Exercises **on paper**.
- When you are done, confirm your answers by running the code.
c) A Single For Loop
- Write a program to find the sum of first 42 positive numbers using a
for
loop.
- Write and run a program that reads a positive integer
n
and then prints a triangle of asteriks in that number of rows. Use two nestedfor
loops. For example, if n is 4, then the output would be:* ** *** ****
Hints to solve this algorithm:
1) Print a single star usingSystem.out.print("*")
.
2) Get the program to print n stars on one line (a single for loop).
3) Then get it to print an nxn square of stars (another for loop around the first for loop). UseSystem.out.println("");
to add a hard return where you need one.
4) Now change only one thing in the program will give you the triangle you seek.
NOTE: Solutions with two for loops that are not nested, or with only one for loop will not be accepted.
Read Hurray for Arrays.
Write a program that
- Creates an array of 200 integers
- Loads the array with 200 random values between 1 and 100 (Hint: use a for loop)
- Prints the elements of the array in order from index 0 to index 199. (Hint: use another for loop)
- Prints the elements of the array in REVERSE order from index 199 to index 0. (Hint: use a 3rd for loop)
When a variable with primitive data type is passed to a method, it is 'pass by value', which means a copy of the variable is passed into the method.
When an array or object is passed to a method, it is 'pass reference by value', which means a copy of its reference is passed into the method. If the method modifies
an array that has been passed in, the original array gets modified. Extra help: Pass-by-Value, Passing Arrays to Methods
In Class Exercise:
In Class Exercise:
- Download this incomplete program.
- Complete the method
absoluteValues()
that replaces all the negative numbers in an array of integers with their absolute values. - Complete the method
indexOfMinimum()
that returns the index of the minimum value stored in an array.
- Declare a 10x10 2D array of integers.
- Populate the array with random integers between (and including) 0 and 9.
- Call a method named
print2DArray
and pass it the above array. The method will print the array by line as shown below:1 2 3 4 0 9 4 3 2 6 4 3 2 1 5 3 4 2 0 9 5 6 7 8 5 3 7 5 8 1 8 7 6 5 0 8 9 5 7 3 5 6 7 8 5 3 7 5 8 1 0 9 8 0 5 3 2 5 3 7 4 3 2 1 5 3 4 2 0 9 7 2 0 8 9 2 6 4 6 5 1 0 7 9 3 5 7 4 6 3 0 5 4 9 4 9 0 5 3 5
- Call a method named
findSum
and pass it the above array. The method will find the sum of the numbers in the array and print it.
In this assignment, you will learn to read data from a file, process the data, and write data to a file. The skills learned here will be required to complete Assignment 2.
Watch Ms Wear's Lesson on File I/O (Input/Output) to complete this exercise.
- Create a new Eclipse Project. Call your main class
FileInputOutput
. - Copy and paste File Input and Output Code into your main class.
- Copy and paste this list of numbers. Save it into a text file called
nums.txt
in the bin folder of your Eclipse project. - Run the code. You should see the contents of nums.txt print to the console.
- Look into the Eclipse Project folder using Windows Explorer (the file manager on Windows) and notice a new file called
output.txt
has appeared. Open it, and you should see a duplicate of the numbers in nums.txt. This file was created by your program! - Now uncomment line 21, and write the method
sumIntegers()
. Run the program. - It should print the sum of the values in the console.
Watch this video lesson on Writing the Dictionary to write a method called
isValidWord()
which accepts one string and returns true if it is a valid 4 letter English word.
- Put this four letter list in a file called dictionary.txt.
- Format the dictionary so that the first line contains all the words beginning with a, the second line contains all the words beginning with b, and so on.
Write a method called
getRandomWord()
the uses the dictionary from the IsValidWord Exercise to return a random 4 letter word. Display the word to the user.
Write a method called
sameLetters
which accepts two strings and returns true if they contain the same letters (case insensitive).
Watch Fisher-Yates algorithm.
Implement the Fisher-Yates algorithm in a method called
shuffle
that accepts a string and returns the same string with its letters shuffled.
You will be asked to create a program that utilizes the methods written in earlier programs: sameLetters, isValidWord, getRandomWord, and shuffle
.
Resources allowed: non-shared notes, Java API, previous exercises you have written for this course. No other resources allowed.
Evaluation
Worth 10% of FINAL MARK.
39 Multiple Choice Questions
Focuses on material and algorithms from this unit
39 Multiple Choice Questions
Focuses on material and algorithms from this unit
- Loops Exercises
- Methods
- Arrays (1d and 2d)
- File I/O
- Pass by Value vs Pass Reference by Value
- Nested for loops (stars program)
- Array manipulation
- Sum a number of values (Grades Average)
- Sentinel Value (Grades Average)
- Character Arrays (Assignment 2)
- All algorithms from Assignment 2
Examine and run this code:
