CS210 Programming Assignment #5 Solution

$30.00

Category:

Description

  1. 1. (5 points) Modify the balls.java program so that all the five up-and-down motion balls have different size and different colors. Also, add 5 left-and-right motion balls that have different size, color, and speed. You have to use the random class and the DrawingPanelclass. To use DrawingPanel.java, you’ll need to 1) Download it from the Canva 2) Put it in the same folder as the programs you write (make sure it’s called DrawingPanel.java). 3) Compile DrawingPane. Your program should be stored in a file called balls.java. Submit your java file balls.java only. (Do not submit DrawingPanel.java)

 

  1. (20points) This question will give you practice with while loops and pseudorandom numbers. You are going to write a program that allows the computer to play a simple guessing game in which you think an integer and allows the computer to make guesses until the computer gets it right. For each incorrect guess you will tell the computer whether the right answer is h for higher or l for lower.

 

At a minimum, your program should have the following static methods in addition to method main:

  • a method that introduces the game to the computer
  • a method to play one game with the computer

You may define more methods than this if you find it helpful, although you will find that the limitation that methods can return only one value will tend to limit how much you can decompose this problem.

You are to define a class constant for the maximum number used in the guessing game. The sample log shows the user making guesses from 1 to 100, but the choice of 100 is arbitrary. By introducing a constant for 100, you should be able to change just the value of the constant to make the program play the game with a range of 1 to 50 or a range of 1 to 250 or some other range starting with 1.

 

When you ask the user whether or not to play again, you should use the “next()” method of the Scanner class to read a one-word answer from the user. You should continue playing if this answer begins with the letter “y” or the letter “Y”. Notice that the user is allowed to type words like “yes”. You are to look just at the first letter of the user’s response and see whether it begins with a “y” or “n” (either capitalized or not) to determine whether to play again.

 

Here are a few helpful hints to keep in mind.

  • To deal with the yes/no response from the user, you will want to use some of the String class methods described in section 3.3 and 4.1 of the book or the lecture slides for the lecture on Strings. You should use the next() method of the Scanner class to read a word from the console.
  • The chapter 5 case study is a particularly relevant example for this assignment.

For this assignment you are limited to the language features in Chapters 1-5 shown in lecture or the textbook. Be sure to once again include a short comment at the beginning of your program as well as a short comment for each method describing what it does. Your program should be stored in a file called

Guess.java

 

Log of execution (user input bold and underlined)

 

This program allows computer to play a guessing game. I will think of a number between 1 and

100 and will allow computer to guess until computer get it. For each guess, I will tell computer whether the right answer is higher (=h) or lower (=l) than computer’s guess.

 

I’m thinking of a number between 1 and 100… My guess: 50

l

My guess: 25

l

My guess: 12

l

My guess: 6

correct

 

Do you want to play again? Y

I’m thinking of a number between 1 and 100… My guess: 50

l

My guess: 25

l

My guess: 12

h

My guess: 18

correct

 

Do you want to play again? No


error: Content is protected !!