Assignment 2 - Programming - MSc CS FT 2004 - 60%
Drawing Package

You are required to complete the provided code(SwingDrawingMsc.java). The program allows shapes to be placed on the screen, then filled, unfilled, moved, resized, and deleted. The shapes are: circle, rectangle, line. The shapes can be resized, so can also represent ovals. The functions of the buttons are:

Selecting a shape

The shape with its centre nearest to a mouse-click is selected.

Creating a shape.

Clicking one of the buttons causes a standard-sized shape to be placed at the top left of the drawing area.

Complete the coding, in an object-oriented manner, so that the above buttons work properly.

Advanced facility(1)

Provide a 'TextRectangle' . This is a like a square, but displays a one-line string of text inside a rectangle. Buttons - which exist already - cause the text to copied from a TextRectangle into a TextField and vice-versa, to allow editing. A TextRectangle cannot be shrunk to smaller than 50 by 50 pixels.

Provide a VerticalText class. This is like a TextRectangle, but displays its text in a vertical column, one character wide, like:
t
h
i
s
Drawing a character approx. 20 pixels lower than the previous one will suffice.

The following buttons are already provided for the advanced facilities:

Advanced facility(2)

Using the provided menu options, provide a facility for saving all the shapes on the screen into a txt file, and for reloading them from a file to the screen. The file should be capable of being edited by e.g. notepad.

Essay - OOP

Explain in detail how your particular drawing program would be different if it were to be written without using inheritance for the shapes. Evaluate and compare the 'inheritance' version with your proposed non-inheritance version, choosing suitable criteria. (Word limit: 1000 words)

Marking

Basic program - by demonstration. (Note that I will expect you to be familiar with your code. See the note on SHU plagiarism policy elsewhere.)   35%
Advanced parts (1) - by demo   10%
Advanced parts(2) - by demo   25%
Essay - differences - 15%           Evaluation - 15%   30%

Hand in:

Hand in: code listing, documentation, essay to 4th floor BITC office, by Fri 21st Jan 3pm.

The demos will take place on Thurs 20th Jan - I will tell you where...

Please note that this is an individual assignment, and the University rules on copying and plagiarism apply. I will expect you to be familiar with your coding.


Additional Material - hints

http://www.shu.ac.uk/schools/cms/teaching/mp/msc2004.htm
It is vital that you look here, and download the code, etc. There is also a link to 'Java for Students', which has code examples (e.g. use of files etc)

You are provided with a partially completed GUI application. It uses classes for shapes, and an ArrayList to store the shapes that are currently on screen.

It is expected that you ask for advice!!

You are provided with most of the GUI. We recommend that you don't alter this code. It contains some new event-handling: mouse-click. However, you do not need to alter this code. Most of your work is in writing new classes and responding to button events.

The code uses an 'interesting' class called ArrayList. here is some info:
This is a storage structure rather like an array, but more useful in this situation. It keeps track of how many items are in it, and allows items to be inserted and deleted easily. It will expand automatically when full. Unlike an array, it is a proper class, and is accessed by methods rather than an [index]. Here is some ArrayList code:


     BankAccount b;       // in tutorials...
     BetterAccount bBetter; 
     ArrayList a;
     ...
     a = new ArrayList(); 
     // an ArrayList can contain items of ANY class:
     a.add(b);       // goes at position 0
     a.add(bBetter);   // goes at position 1
     int n = v.size();     //  n is 2 (2 elements in ArrayList currently)

You can make use of size() as the limit of a for-loop, to work through each item (Items are stored at 0 upwards). ArrayList does NOT remember the class of a stored item, so you need to CAST back to the class you are using. when you have stored inherited classes as above, you must cast back to the parent class. (See the polymorphism chapter stuff in the book). So we must put:
     BankAccount bTemp; 
     bTemp = (BankAccount) v.get(1);
In general, bTemp could be a BankAccount or a BetterAccount. You might need to place 'empty' methods (e.g which return 0, or which have no code between their { ... } ) to prevent any run-time errors caused by trying to use a method of the extended class with an instance of the parent. e.g. the empty method might go in the parent. Effectively we are setting up a situation in which every class will respond to certain methods, but some responses will be to 'do nothing'. Other methods of ArrayList are:
    a.remove(int position);          //removes item at position,shifts rest to fill gap
    String s = a.toString();       // returns string of whole contents  

You do NOT need to alter any of the ArrayList code in drawAllShapes(). But you will need to look at how a new shape is added, and how a selected shape is moved, in the button-handling code of the GUI.

Constructors and Inheritance

Constructors allow you to pass parameters to an instance when you create it with new. Typically you will copy the parameters to private variables of the instance. The constructor must have the same name as the class, e.g.:
      class BankAccount {     ....       
          public BankAccount (  parameter list  ) {
         .....
     }
     ....
  }

When you extend a class, Java forces you to invoke the constructor of the parent (super) class, in this way: if the first instruction in your constructor is NOT a call of the super's constructor, then a call to the default constructor will be inserted. The default constructor is one with no parameters. This is quite involved, so here is an example:
        class BankAccount {     ....       
        public BankAccount(int initx){// constructor: NOT the default one
         .....
        }
        ....
    }
//  ----------------------------------------------

    class BetterAccount extends BankAccount {
        public BetterAccount(int initx) {
            super(initx);    // first invoke constructor of BankAccount
            etc...
        }
    }

If we omitted super(initx), then Java would try to invoke BankAccount() - the default constructor, and the only constructor it has is one with an int parameter, so we get a compilation error. BUT if BankAccount didn't have any constructor, Java would invent one behind the scenes, named BankAccount() ! So, if you never use constructors, there would be no problem. But they are useful, and are likely to be required. So remember to put a call of super(..) at the start of your constructor, to pass the appropriate parameters.
e n d