Programming in Java Spring 2006

Some Java Classes and Programs

Due Date: 2/12/06
Submit via WebCT drop box labeled HW2

Assignment

This assignment involves writing a number of Classes that correspond to shapes (like the Square class we discussed in class). Each of these classes must be capable of drawing itself using print statements (System.out.print(), System.out.println(), System.out.printf()) and each must also provide a toString method that returns a single line of text that includes the class name and any size parameters (see below for size parameters). Each of these Classes must also have a main() that allows you to test the Class by running it as a program.

You will also write a class whose purpose is to look at command like arguments and decide which type of object to create and draw. This class acts as a controller, it doesn't need to have any specific fields or methods (other than main). Note that you may find it useful to write methods other than main, but you are not required to do so.

Shape Classes

You must provide 4 (four, quattro, , quatre, tessera) different shape objects as described below. Each must be able to draw itself using just print statements. You must use the exact names given below, or the automated grading system will not be able to grade your program, you will get a bad grade, drop the course, and spend the rest of the semester wondering what seven looks like in Farsi.

Class Rectangle

You need to write a class named Rectangle that can draw any size rectangle. A Rectangle object needs to have a height and a width. The class Square discussed in class (and the lecture notes for Strings) should be a good start.

Here is an example of a 4x8 rectangle:

********
********
********
********

Note that your Rectangle class must include a main so we can test it by itself. The main should get the height and width from the command line arguments. The above example could then be produced by running: java Rectangle 4 8.

Class Triangle

You need to write a class named Triangle that can draw an isosceles triangle given just the height of the triangle. The top of the triangle should have a single "*", and each subsequent row should have two more "*"s than the previous row. For example, the picture below is a triangle of height 7 (7 rows):

       *
      ***
     *****
    *******
   *********
  ***********
 *************

Class TriangleOutline

TriangleOutline is like Triangle, except that the triangle drawn should not be filled, so a triangle of size (height) 7 looks like this:

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *************

Class Circle

Circle has a single size parameter that indicates the radius of the circle. Circle is a little harder than the others, as you need to use some math (check out the java API docs for java.lang.Math for access to functions like sin(), cos(), sqrt, etc.).

Note that since characters don't have a square shape (the height of a character is typically greater than it's width when displayed on a computer screen), you should not expect to actually see a circle, but rather an oval, something like this (circle with radius 5 produced by java Circle 5):

   *****
  *******
 *********
***********
***********
***********
***********
***********
 *********
  *******
   *****

Class DrawShape

You must also write class named DrawShape that looks at command line arguments to decide which shape class to use to draw on the screen. The first command line argument is the name of the shape (the same as the class names above). There are also command line arguments (integers) for any shape parameters. For example, if we run the program like this:

java DrawShape Rectangle 4 8

it should create a Rectangle object (with 4 rows, 8 columns) and tell it to draw itself.

We could also run it like this:java DrawShape Circle 12 or java DrawShape TrianlgeOutline 14.

Note that there should not be any drawing code in the DrawShape class, it should use the other classes you created to do the actual drawing! You can check the first command line argument with code like this:

    if (args[0].equals("Circle")



How to submit

Log in to WebCT at webct.rpi.edu using your RCS id and password. Once you get to MyWebCT click on "Programming in Java", and from there go to the homework drop boxes. Submit your files (individually, zipped, tarred or a jar file) to the drop box labeled HW2

Don't send compiled code (class files)!

Your submission must include the files Rectangle.java, Triangle.java, TriangleOutline.java,Circle.java and DrawShape.java