Exercise #4 in Software1


The purpose of this assignment is to practice the use of objects.


Part 1:  Turtle drawing


In this section you will write a program that draws a simple picture. You are not required to learn anything about graphics, instead you will use a Turtle class supplied by us that implements turtle graphics as described below.

Introduction - LOGO and Turtle Graphics

LOGO is a simple programming language that is often used to introduce programming concepts as well as planar geometry concepts to children. A LOGO environment consists of a window representing a plane and a turtle that lives in this plane. The turtle has a tail that can be up or down. If the turtle is walking when its tail is down, it leaves behind it a line. When it walks while its tail is up, no line is left behind. The purpose of LOGO is to be able to draw/define various figures by giving instructions to the turtle.

The turtle has a location and a direction. You can give the turtle instructions to change its location and direction causing it to draw some figures along the way. For example, the instruction 'forward 30' tells the turtle to advance 30 units forward in the direction it is looking at. The instruction 'left 45' tells the turtle to turn 45 degrees counter-clockwise (i.e., change its direction by 45 degrees).

Here is a representative list of instructions you can give the turtle of LOGO:

   forward x 

  advance x units forwards

   backwards x

  move x units backwards

   left x 

  turn x degrees counter-clockwise

   right x

  turn x degrees clockwise

   tail up

  lifts the turtle's tail

   tail down

  lowers the turtle's tail

What You Should Do

You are not expected to implement the emulation of LOGO by yourself. For this purpose we give you a Java class Turtle. Recall that this class defines all the behaviors of a LOGO turtle. Your program should only create a turtle object and give it instructions by invoking methods on it. The table below lists the methods of a Turtle object. You are encouraged to look at the API documentation of class Turtle.


  advance x units forwards


  move x units backwards


  turn x degrees counter-clockwise


  turn x degrees clockwise


  lifts the turtle's tail


  lowers the turtle's tail


Pentagon.java is an example of a program that uses class Turtle to draw a 'pentagon' figure (Below is the skeleton of the program with some additional comments).

class Pentagon {

  public static void main(String[] args) {
    Turtle leonardo = new Turtle(); // Creates the turtle and named leonardo
    leonardo.tailDown();            // Start painting
    leonardo.moveForward(100);      // Advances the turtle forward
                                    // by 100 units
                                    // ...

Recall that an Euler drawing is a drawing that can be done without lifting the pencil from the paper. In other words it is a figure that is drawn without going over any line twice. This is an example of an Euler drawing with our turtle.

You should write a class called TurtleDrawing in package ex4.turtle that draws the above figure a number of times. After each figure is drawn the turtle rotates so that at the end it will complete a full cycle. The user will specify the number of figures to draw. You can assume that this number is a divisor of 360.  In order to receive input from the user you should use the LineInput class (located in the simple_io_NoSrc.jar file). For example this is the output of TurtleDrawing when it is called to draw 12 figures.

 And this is how the output would look to 4 figures:

At the end of the drawing, hide the turtle using the method hide().

Technical Details

In order to use our Turtle class you should include turtle_NoDemoNoSrc.jar in your CLASSPATH.  This is done by Project properties->java build path->libraries->add external libraries. An explanation about jar files can be found in the Eclipse handouts on the course web-site.
You should also add the simple_io_NoSrc.jar to your CLASSPATH as well in order to use the LineInput class.


Part 2:  ACME Company - creation and manipulation


On the course web-site you will find acme.zip with a set of classes representing a simple model of a company. There are two classes: Department and Worker (both found on package ex4.acme). They each have a set of attributes that describe them and methods that maintain the relationship between them.


The Acme company is founded with the following initial structure:


The class AcmeCompany (in package ex4.acme) contains a single member field, m_acme, that points to a Department object which is the root department of the ACME company.



What You Should Do

Your job is to implement following methods in the class AcmeCompany:


1.      found(). After found() is called, m_acme points to a Department object that represents the entire company, as shown in the diagram above. This method should construct all the required objects and connect them as appropriate.

2.      findWorker(String name) and findDepartment(String name). The method findWorker  returns a worker given his/her name. The method findDepartment returns a department given its name. The implementation of these two method should use recursion.

3.      moveUgi(). This method moves the worker Ugi from Expenses to Income.

4.      fireKermit(). This method removes Kermit from the company.

5.      reorganize (). This method closes the Glue department, fires Miki and Donald, and moves Pluto to the Coyotes department.