Monday, February 27, 2006

M254: TMA 03 - Results

Got my results back last night for TMA02. Although the overall mark was good, I am rather annoyed with myself as I forgot one part of a question and because of this I dropped 2 marks. Still hopeful I want make the same mistake again.

Monday, February 20, 2006

M301: TMA01 Q5(i)

Question 5 (i) of TMA01 asks us to construct a GUI front-end using the JBuilder GUI Designer. Now as I am not a fan of JBuilder and have been using BlueJ for all my Java coding, I thought I had better check with the course team as to whether it was mandatory to use the GUI Designer for this question. The answer that came back was yes we have to use the GUI Designer.

Now I am a bit disappointed with this as...

  1. I have already manually coded the GUI, and now have to redo the question.
  2. The GUI Designer generates unnecessary code for such a simple project.
  3. The course team implied, in an early FirstClass posting, that it was not a requirement to use JBuilder for our Java coding, and therefore I have been using BlueJ.
  4. With M254 TMA questions we have not been allowed to use any form of GUI Designer, and have had to code any required GUI's manually.

Friday, February 10, 2006

BlueJ ver 2.1.2

BlueJ ver 2.1.2 has just been released.
This version contains a number of minor bug fixes, including:

- a fix for a bug with display of parameter names and method
comments in call dialogues.
- a fix for a bug in specification of VM location in bluej.defs.
- a bug fix in extension API: compiler warnings are now
propagated to listeners.
- fixes to the parser.
Download from http://www.bluej.org/download/download.html

M254: Self Help Group

Tonight we have a self help group at Canterbury Christ Church University, Room number NF03, From 07:00pm - 09:00pm.

M301: TMA00

Forgot to mention yesterday thatI got my results back for TMA00 on Wednesday night. Even though it was an easy TMA and will form part of TMA01, it is always satisfying to get a good mark.

Wednesday, February 08, 2006

M301: Constructors and Inheritance

Code to demo that a subclass invokes its superclass constructor without directly calling it.

Output
Creating an instance of ChildClass
--- Running the ParentClass constructor ---
--- Running the ChildClass constructor ---
The instance variable from the ParentClass says "Hello from the ParentClass"
The instance variable from the ChildClass says "Hello from the ChildClass"


Source Code
public class ExtendTest
{
public static void main(String [] args)
{
System.out.println("Creating an instance of ChildClass");
ChildClass tempCC = new ChildClass();
System.out.println("The instance variable from the ParentClass says \"" + tempCC.pcString + "\"");
System.out.println("The instance variable from the ChildClass says \"" + tempCC.ccString + "\"");
}
} // End of class ExtendTest

public class ChildClass extends ParentClass
{
protected String ccString;

public ChildClass()
{
System.out.println("--- Running the ChildClass constructor ---");
ccString = "Hello from the ChildClass";
}
} // End of class ChildClass

public class ParentClass
{
protected String pcString;

public ParentClass()
{
System.out.println("--- Running the ParentClass constructor ---");
pcString = "Hello from the ParentClass";
}
} // End of class ParentClass

Tuesday, February 07, 2006

M301: Polyfrogism

Below is some code (based upon the Amphibian World of M206) I knocked to show a fellow student the concept of polymorpism within Java.

import java.util.StringTokenizer;

public abstract class Amphibian
{
static final int HOME_POSITION = 1; // Specifies the starting stone of an amphibian.
static final int NUM_OF_STONES = 1; // Specifies the number of stones an amphibain will move by.
static final String INITIAL_COLOUR = "Green"; // Specifies the default colour of an amphibian.

private String colour; // The colour of the amphibian.
private int position; // Represents the stone on which the amphibian is on.

public Amphibian(String colour, int position)
{
this.colour = colour;
this.position = position;
}

/**
* @return the position of the amphibian, an integer.
*/
public int getPosition()
{
return this.position;
}

/**
* @return the colour of the amphibian, a String.
*/
public String getColour()
{
return this.colour;
}

/**
* Set the stone for the amphibian to stand on.
* @param position The position the amphibian is to be at.
* @return no answer.
*/
void setPosition(int position)
{
this.position = position;
}

/**
* Set the colour of the amphibian.
* @param colour The colour the amphibian is to be.
* @return no answer.
*/
void setColour(String colour)
{
this.colour = colour;
}

/**
* subclass responsibility.
* @return no answer.
*/
public abstract void right();

/**
* subclass responsibility.
* @return no answer.
*/
public abstract void left();

/**
* Set the colour of the amphibian to Brown.
* @return no answer.
*/
public void brown()
{
this.setColour("Brown");
}

/**
* Set the colour of the amphibian to Green.
* @return no answer.
*/
public void green()
{
this.setColour("Green");
}

/**
* Set the colour of the amphibian to that of anAmphibian.
* @param anAmphibian The amphibian who's colour is to be used.
* @return no answer.
*/
public void sameColourAs(Amphibian anAmphibian)
{
this.setColour(anAmphibian.getColour());
}

/**
* @return the class name of the amphibian, a string.
*/
public String className()
{
StringTokenizer element = new StringTokenizer(this.toString(),"@");
return element.nextToken();
}

/**
* Textual representation of the amphibian.
* @return amphibians details.
*/
public String inspect()
{
return ("An instance of class " + this.className() + "(position " +
this.getPosition() + ", colour " + this.getColour() + ")");
}

/**
* subclass responsibility.
* @return no answer.
*/
public abstract void home();

} // End of class Amphibian


public class Frog extends Amphibian
{
public Frog()
{
this(INITIAL_COLOUR, HOME_POSITION);
}

public Frog(String colour)
{
this(colour, HOME_POSITION);
}

public Frog(String colour, int position)
{
super(colour, position);
}

/**
* Increase the position of the frog instance by the
* default number of stones.
* @return no answer.
*/
public void right()
{
this.setPosition(this.getPosition() + NUM_OF_STONES);
}

/**
* Decrease the position of the frog instance by the
* default number of stones.
* @return no answer.
*/
public void left()
{
this.setPosition(this.getPosition() - NUM_OF_STONES);
}

/**
* Move the frog instance to its home (start) position.
* @return no answer.
*/
public void home()
{
this.setPosition(HOME_POSITION);
}
} // End of class Frog


public class Toad extends Amphibian
{
static final int HOME_POSITION = 11;
static final int NUM_OF_STONES = 2;
static final String INITIAL_COLOUR = "Brown";

public Toad()
{
this(INITIAL_COLOUR);
}

public Toad(String colour)
{
this(colour, HOME_POSITION);
}

public Toad(String colour, int position)
{
super(colour, position);
}

/**
* Increase the position of the toad instance by the
* default number of stones.
* @return no answer.
*/
public void right()
{
this.setPosition(this.getPosition() + NUM_OF_STONES);
}

/**
* Decrease the position of the toad instance by the
* default number of stones.
* @return no answer.
*/
public void left()
{
this.setPosition(this.getPosition() - NUM_OF_STONES);
}

/**
* Move the toad instance to its home (start) position.
* @return no answer.
*/
public void home()
{
this.setPosition(HOME_POSITION);
}

} // End of class Toad


import java.util.*;

public class AmphibianWorld
{
public static void main(String [] args)
{
HashMap thePond = new HashMap(1);

thePond.put("kermit", new Frog()); // put kermit in the pond
thePond.put("toady", new Toad("red", 1)); // put a red toady in the pond on stone 1

Amphibian tempAmphib;

// lets see the pond
tempAmphib = (Amphibian) thePond.get("kermit");
System.out.println(tempAmphib.inspect());

tempAmphib = (Amphibian) thePond.get("toady");
System.out.println(tempAmphib.inspect());

System.out.println("move kermit right 3 stones");
tempAmphib = (Amphibian) thePond.get("kermit");
tempAmphib.right();
tempAmphib.right();
tempAmphib.right();
System.out.println(tempAmphib.inspect());

System.out.println("move toady right 6 stones");
tempAmphib = (Amphibian) thePond.get("toady");
tempAmphib.right(); // toady moves 2 stones at a time
tempAmphib.right();
tempAmphib.right();
System.out.println(tempAmphib.inspect());

System.out.println("now lets make both of them blue");
String name;
Set pondSet = thePond.keySet();
Iterator pondIt = pondSet.iterator();

while (pondIt.hasNext()){
name = (String) pondIt.next();
tempAmphib = (Amphibian) thePond.get(name);
tempAmphib.setColour("blue");
System.out.println(tempAmphib.inspect());
}
}
} // End of class AmphibianWorld

Wednesday, February 01, 2006

M301: TMA00

Just submitted TMA M301 00.

M301: TMA01

A new version of TMA01 has been released due to a number of issues with the supplied code files. Also there are 4 corrections to the actual TMA01 document. Just as well I haven't submitted TMA00 yet.