ulrail.gif

Lab Quiz 1

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Lectures | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif

Pledged Lab Quiz

This lab quiz is pledged.  You may use your textbook, and NOTHING ELSE.  This includes any files in your home directory, previous assignments, slides, etc.  However, you should still save your file into your home directory.

You will need to submit a single file called LabQuiz1.java.  You may download the skeleton code here: LabQuiz1.java.  You must also save the Rectangle.class file to the same directory that your LabQuiz1.java file is in.  For both files, right click on the link and select "save as" (or "save target as").

The TAs cannot help you with Java-related issues on this quiz.  If you are having problems getting JCreator to work, can't log in to the machines, etc., then you are welcome to ask the TAs for assistance.  If you do not understand what the lab is asking, the TAs may be able to help you (meaning if it's a problem with our explanation, then they can help you -- if you've been skipping the lectures so far and have no idea what any of this means, you are on your own).

If you get an error in JCreator that states, "cannot find symbol class Rectangle", then you have not saved the Rectangle.class file properly.  You may as a TA to help fix this problem.

Advice

  • To maximize your points, make sure your programs compile successfully.
     
  • If towards the end things are not going as well as you would want (i.e., the program does not compile), it might be best to try commenting out errant statements (i.e., put a // in front of them)
     
  • If you run out of time, submit what you have -- there will be partial credit for this quiz.  Also remember that you can submit a file as many times as you want -- only the most recent submission is graded.  So once you have it working, submit what you have.

Rectangle Class

For this quiz, you will need to use the Rectangle class that was discussed in lecture, and also in the textbook (chapter 3, pages 109-114).  The relevant methods are as follows.

  • Rectangle (x, y, width, height): The constructor, it allows you to create a new Rectangle object.  All parameters are ints.
  • setLocation (x, y): Changes the location of the Rectangle to the passed values.
  • grow (w, h): increases the width of the rectangle by w, and the height by h

Lab Quiz

To get full credit on this quiz, your code must complete the following eight steps:

  1. Read in four integers from the keyboard: x, y, width, and height.  Be sure to ask for them in the specified order!  You don't have to prompt for the inputs -- add the prompts if there is time when everything else is working.
  2. Create a new Rectangle using those four values.
  3. Print the rectangle out.  If you created a Rectangle r, then you can just print it out via the command System.out.println(r);
  4. Read in two more integer values.  Again, you don't have to prompt for them.
  5. Set the location of the rectangle to the last two read in values.
  6. Print out the rectangle.
  7. Grow the rectangle by the same last two values read in.
  8. Print out the rectangle.

For testing your code, it will be easier to use "small" values (i.e. less than 10).  If your rectangle is too big, or too far from the origin, then it will not display correctly.

How to Proceed

Get the steps working in order!  Make sure step 1 works before you move onto step 2.  If you don't follow this, you will end up with a mess of code that doesn't work, and you will not be able to fix it within the lab quiz time.  There will be partial credit given for this lab quiz -- so if you only get steps 1-6 working, you will get a good amount of points.  But if you have a large mess of code that doesn't work, you will get very few points.

Sample output

Below is a sample run of the program.  The text in red is what was input by the user.  The text in green is optional (prompts, legends, and such) -- add it only if you have sufficient time.

Rectangle manipulation

Enter four values for the rectangle: x, y, width, and height
5
10
5
10
 +--------------------------------------------------------------------------+
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |          +--------+                                                      |
 |          |        |                                                      |
 |          |        |                                                      |
 |          |        |                                                      |
 |          |        |                                                      |
 |          |        |                                                      |
 |          |        |                                                      |
 |          |        |                                                      |
 |          |        |                                                      |
 |          +--------+                                                      |
 |                                                                          |
 +--------------------------------------------------------------------------+

Enter two more integer values
2
5
Rectangle at 2, 5:
 +--------------------------------------------------------------------------+
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |    +--------+                                                            |
 |    |        |                                                            |
 |    |        |                                                            |
 |    |        |                                                            |
 |    |        |                                                            |
 |    |        |                                                            |
 |    |        |                                                            |
 |    |        |                                                            |
 |    |        |                                                            |
 |    +--------+                                                            |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 +--------------------------------------------------------------------------+

Rectangle grown by 2, 5:
 +--------------------------------------------------------------------------+
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |                                                                          |
 |    +------------+                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    |            |                                                        |
 |    +------------+                                                        |
 |                                                                          |
 +--------------------------------------------------------------------------+

Other Requirements

You are only required to follow a few of the good programming practices discussed in HW J1.  If you have time, feel free to do the others -- but due to the time constraints of the quiz, you don't have to do them all.  The ones you must do are listed below.

  • Whitespace: A line or two between separate elements of the code (methods, code segments, etc.) and good indentation.
     
  • Variable names: All final variables should be in all caps with underscores between the words, such as BOX_WIDTH.  Non-final variable names should have the firstLetterOfEachWordCaptilized in the variable name (excepting the very first letter). Variable names should be relevant and informative, such as mileage and not m.
     
  • Line length: The lines should not go over about 72 characters. Essentially, when displayed on an 80-character wide screen (what we are using), the line should not go past the end of the screen. You can have a println() statement, for example, use multiple lines (as long as each string starts and ends on the same line). JCreator tells you the column that the cursor is currently in (at the bottom on the status bar), so you can use that to gauge how long your lines are.

Submission

When you are finished, submit your LabQuiz1.java file.  You will have to "sign" a pledge on the submission.

 

spacer.gif
spacer.gif footer-middle.gif spacer.gif