CPTR 124 Fundamentals of Programming
In this lab you will write programs that use loops.
- Teams
You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.
- Preparing your working folder
Make a subfolder named Lab10 within your H:\cptr124 folder.
- Part 1: Guessing Game.
Write a text-based Java program that plays a guessing game with the user. The program's user is the player. The player is supposed to guess an integer in the range 1-100. The program should generate a number in this range at random. The program's number is the correct answer. The player's guess is compared to the correct number:
- If the player's number is less than the correct number, then the program should print "Too low" and allow the player to guess again.
- If the player's number is greater than the correct number, then the program should print "Too high" and allow the player to guess again.
- If the player's number is the same as the correct number, the game is over.
Name your class GuessingGame.
To obtain input from the user, use a Scanner object.
Hint: While you are developing your program you can print the answer (secret number to guess) each time a new guess is requested. That way you can better tell if your program is working properly; that is, giving you the correct feedback. Be sure to disable this "feature" in your final version, or it will not be much of a game!
- Part 2: Pythagorean Triples.
A Pythagorean triple consists of three integers that could be the lengths of the sides of a right triangle. A right triangle is a triangle that contains a 90-degree angle. (A 90-degree angle is called a right angle.) In the figure, the little square indicates the right angle.
The hypotenuse of a right triangle is the side that is opposite (that is, directly across from) the right angle.The Pythagorean theorem states that if the lengths of the sides are a, b, and c, where c is the hypotenuse of the triangle, then the following equation must be satisfied:
a2 + b2 = c2 Write a Java program that generates all Pythagorean triples up to a maximum a or b value. Your program should request the maximum value from the user. For example, if the user entered 10, then your program would consider all combinations of as and bs up to 102 + 102. Only the Pythagorean triples would be displayed. Your program should then indicate the number of triples that it displayed.Details:
- The values within each triple should be displayed in ascending
order; e.g., display (3, 4, 5),
not (4, 3, 5).
- The set of triples should be listed in ascending order by the
first element; for example, if the user
enters 15, the triples are printed as
(3, 4, 5) (5, 12, 13) (6, 8, 10) (8, 15, 17) (9, 12, 15) Total: 5 - You may find it helpful to write a class method named
checkforIntegerSquareRoot() that accepts a single
integer argument. The method should return the square root of
its argument if its argument is a perfect square. A perfect
square is an integer with an integer square root. For
example, 25 is a perfect square, since its square root is 5,
but 26 is not a perfect square, since its square root is not
an integer.
If the parameter passed to the method is
not a perfect square, the method should return -1.
Since -1 is not a valid square root for real numbers, your
code can use the method to determine whether or not to
print certain combinations of values.
If you use any floating-point values, variables, or methods (for example, Math.sqrt() is a floating point method) within your program, they must be limited to this checkforIntegerSquareRoot() method. All computation, variables, and values outside of this method must use integers.
- Your program should be able to handle input values up to 30,000.
Note that it may take considerable time to generate all
the triples when large values are entered!
- Your program should run continuously until the user enters
a value less than 1.
- Don't forget to display the number of triples that were
generated.
- You should plan your algorithm on paper first. When you believe you have a strategy that can be readily translated into Java source code (and you think it will work correctly!), you can begin coding. Unless you are a fairly confident programmer, it will be a waste of your time to do otherwise.
- The values within each triple should be displayed in ascending
order; e.g., display (3, 4, 5),
not (4, 3, 5).
- Check out
Your finished program will be evaluated for correctness and compliance. When approved, you should submit a printout of your code. Be sure your name and your partner(s) names are included on the printout.