CS 305J Assignment 7, While Loops

Programming Assignment 7 Pair Assignment. You may work with one other person (You make work with one other person on this assignment using the pair programming technique.. You may work with anyone in the class. They do not have to be in the same discussion section as you or the same partner as on previous assignments. One solution will be turned in for the pair. Once you start working with one partner on an assignment you may not switch partners. If you do not wish to work with a partner after starting on an assignment you must both complete it individually. The intent here is you work on the assignment together, at the same time, at the same computer. Do not simply try to work on different parts independently and then try to put it together. You may choose to work alone if you wish.)

Placed online: October 16
20 points, ~2% of total grade
Due: no later than 11 pm, Thursday, October 23
General Assignment Requirements

Description The purposes of this assignment are:
  1. To practice creating a structure for a complex program.
  2. To write more methods that use parameters.
  3. To practice using conditional execution.
  4. To practice using  indefinite loops.
  5. To practice error checking user input.

For this assignment you are limited to the language features in chapters 1 through 5 of the textbook. You will also use the DrawingPanel class and the following built in Java classes: Graphics, Scanner, Color, Rectangle, and Random.

Write a program that prompts the user for the size of a target square. The target size must be a minimum of 10 and a maximum of 100. You may assume the user will enter integers, but you must error check their response to ensure the value that enter is greater than or equal to 10 and less than or equal to 100. If not prompt the user again. You must repeatedly prompt the user until they enter a correct integer for the size of the target square.

The program then creates a DrawingPanel that is 600 by 600 pixels with a red square randomly placed in the DrawingPanel. The square is the size specified by the user. It is placed at a random location in the panel. The entire square must be in the window. It cannot be clipped in any way.

In order to be able to remember all of the information about the target you can have the method that picks the location for the target and draw it, return a Rectangle object. Rectangle is a built in class like String, Scanner, Random, and File. Here is a sample line of code for creating a Rectangle.

Rectangle result = new Rectangle(xCoord, yCoord, size, size);
// arguments are for the x coordinate, y coordiante, width, and height of the rectangle.

By returning the Rectangle object you can pass back 4 pieces of information: the x coordinate, the y coordinate, the width, and the height. (Since we are making a square the width and height will be the same.)

Once the Rectangle is created the information can be accessed via methods. Assume target is a Rectangle object.

target.getX()  //returns the x coordinate of the rectangle
target.getY() // returns the y coordinate of the rectangle
target.getWidth() // returns the width of the rectangle
target.getHeight() // returns the height of the rectangle;

To use the Rectangle class you must have the line import java.awt.Rectangle; at the top of your program.

After creating the DrawingPanel and adding the target, pick a random location in the window that is not inside the target square. You will have to pick a random x and y that are not in the bounds of the target square. The initial location cannot be in the target, but your program should allow every other position in the window to be a possible starting location. Draw a black box of size 1 at that location. (When testing you may want to draw a bigger box to see that your placement is correct.)

After picking the initial location perform a random walk The x coordinate for the next location can either be one less than the current x coordinate, the same as the current x coordinate, or one more than the current x coordinate. The next y coordinate can also be 1 more, the same, or less than the current y cooridnate.

The one limit is that the resulting x and y must be in the window. In other words Neither x or y can be less than 0 nor can either  be greater than or equal to 600.

 Note, it is possible that the next location will be the same as the current location.

When the new location is picked draw a square of size 1 at the new location.

This simulates a random walk around the window. Continue the random walk until it hits the edge of the target square.

While outputs will vary you usually get something interesting such as this:

Tips:

  • This is not an easy program because there are so many things to think about and it involves random actions. Create a plan for your program before you start coding.
  • Implement your program in stages. Write the part to get user input first. Output what you read in even though that output won't be part of the final program. After reading in the size of the target correctly create the initial DrawingPanel and target. Test this before moving on.
  • After creating the initial DrawingPanel figure out how to pick a random x and y that are in the window, but not in the target. Think about this before coding. Work it out on paper first. When testing this part draw a square bigger than 1 by 1 to see where the initial location is. Test this many times with a large target square so you are sure it is never in the target.
  • Next work on picking the new x and y. Try printing out the values while testing. Don't worry about hitting the target until you get the random walk part done.
  • Finally figure out when to stop.
  • Don't forget the good techniques we have learned thus far.

    Use a global constant where appropriate: The window size of 600 is an excellent candidate.

    Provide structure to the program by breaking it up into methods.

    Pass parameters to move information from one method to another.

    Use descriptive variable names.

    Format your program properly, indenting where necessary.

When finished turn in your RandomWalk.java program using the turnin program. Do not turn in the class file. do not turn in a file with a tilde (~) in the file name. Doing either of these will result in a zero on the assignment. If you are working with another person, turn the assignment in to only one person's account, but ensure the header is filled in with both of your names and the unique class IDs for both of your sections.

Files
File Responsibility
RandomWalk.java (A shell file with a main method and the header information.) Me and you, mostly you.
DrawingPanel.java Provided by me.
Checklist Did you remember to:
  • review the general assignment requirements?
  • worked on the assignment with at most one other person?
  • fill in the header in your file RandomWalk.java?
  • complete the RandomWalk.java so it performs the simulation correctly?
  • ensure you wrote the program using good style?
  • ensure your program does not suffer a compile error or runtime error?
  • turn in your Java source code in files named RandomWalk.java to the proper account in the Microlab via the turnin program before 11 pm, Thursday, October 23?

Back to the CS 305j homepage.