![]() |
CS 111 - Introduction to Computer Science
Homework #9 |
1. Patterns: In a class Patterns.java, use nested loops and appropriate conditions to print the following patterns:
(a)
+++++ +++++ ----- ----- -----(b)
-+--- -+--- -+--- -+--- -+---(c)
--+-- --+-- +++++ --+-- --+--(d)
+++++ -++++ --+++ ---++ ----+(e)
----+ ---+- --+-- -+--- +----(f) Hint: Consider the row and column sum for each position.
+-+-+ -+-+- +-+-+ -+-+- +-+-+
For example, to make the pattern:
----- ----- +++++ ----- -----one could write the code:
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
if (row == 2)
System.out.print("+");
else
System.out.print("-");
}
System.out.println();
}
or the equivalent code (using the selection operator):
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++)
System.out.print((row == 2) ? "+" : "-");
System.out.println();
}
2. Guessing Game: Exercise 6.33. Additionally, before the computer picks a number, prompt "Enter a maximum value: " and allow the user to specify the maximum number that might be chosen. Call your game class Guess.java. Assume the user provides valid input (i.e. no error handling necessary).