<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chris Ford &#187; Things I&#8217;ve Learnt</title>
	<atom:link href="http://www.chrisfordblog.com/category/things-ive-learnt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chrisfordblog.com</link>
	<description>Just another blog</description>
	<lastBuildDate>Thu, 17 Dec 2009 21:30:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>OU M257 Revision notes</title>
		<link>http://www.chrisfordblog.com/2009/06/ou-m257-revision-notes/</link>
		<comments>http://www.chrisfordblog.com/2009/06/ou-m257-revision-notes/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 08:37:30 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Things I've Learnt]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[M257]]></category>
		<category><![CDATA[Open University]]></category>
		<category><![CDATA[OU]]></category>

		<guid isPermaLink="false">http://www.chrisfordblog.com/?p=394</guid>
		<description><![CDATA[Next week I&#8217;m sitting an Open University exam on the Java course M257.  I&#8217;ve made some notes on most of the units which you can see below: Unit 3 Implement classes in which you have specified the access level of methods and instance variables; private access level: never inherited, so can never be accessed by [...]]]></description>
			<content:encoded><![CDATA[<p>Next week I&#8217;m sitting an Open University exam on the Java course M257.  I&#8217;ve made some notes on most of the units which you can see below:</p>
<p><span style="font-size: 12pt; font-family: &quot;Times New Roman&quot;; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: RU; mso-fareast-language: RU; mso-bidi-language: AR-SA;" lang="RU"><strong>Unit 3</strong></span></p>
<ul type="disc">
<li><strong>Implement classes in which you have specified the access level of methods and</strong> <strong>instance variables;</strong></li>
</ul>
<ul type="disc">
<li>private access level: never inherited, so can never be accessed by subclasses as if they were part of the subclass.</li>
<li>Public access level: If you precede an instance variable or a method of a public class with the keyword public, then any class can access that member using a reference and a dot notation. Any public members are inherited by all subclasses, so they can be accessed within those classes without a dot notation.</li>
<li>Protected access level: available only to classes in the same package. Any subclass inherits any protected and public members of its superclass.</li>
</ul>
<ul type="disc">
<li><strong>Understand the difference between instance variables and class variables, and be</strong> <strong>able to decide when to use each;</strong></li>
</ul>
<p>Instance variables are defined in the class above the constructor. Values relates to that object. Class variable are defined as static, only one copy of the &#8216;variable&#8217; is created for all instances of the class. Reasons to use class variables:</p>
<ul type="disc">
<li>Constants: A common use is to define some constant associated with the class. For example, the Java package associated with mathematical processing implements the mathematical constant PI in this way.</li>
<li>One value for all objects: A second use of class variables and methods is to implement data that has only one value for every object of that class. One common example of this is to keep count of the number of objects of a class type that have been created. It might lead to errors if each object kept its own version of this count, as each object would have to update the count every time a new object came into existence and it would be easy to forget to update some object&#8217;s copy of this variable.</li>
</ul>
<p>private int numOfSpots; //instance variable</p>
<p>private static int numOfLegs; //class variable</p>
<p>Often but not always static variables are constants. To declare a variable as a constant the keyword final is used and for readability the name is in CAPS</p>
<p>static final int MAX_VALUE = 12;</p>
<p>Methods can also be declared as static. If you try to access a nonstatic variable from a static method then you will get a syntax error.</p>
<p><strong> </strong></p>
<ul type="disc">
<li><strong>Write constructors for classes;</strong></li>
</ul>
<p>Constructor is a method with the same name as the class. Multiple constructors can exist for the same class if they have different arguments. A constructor sets the default variable values. The constructor of the superclass can be called in a constructor using super(); if it has zero arguments. Otherwise arguments can be pass to the superclass constructor like this.</p>
<p>public FourInt (int a, int b, int c, int d)</p>
<p>{</p>
<p>super(a, b, c);</p>
<p>s = d;</p>
<p>}</p>
<ul type="disc">
<li><strong>Use inheritance to develop new classes;</strong></li>
</ul>
<p>To inherit from a class the class header needs to include the keyword extends followed by the class name.</p>
<p>public class PrivilegedUser extends User</p>
<ul type="disc">
<li><strong>Discuss the nature of the Java class hierarchy and the purpose of the Object class</strong> <strong>as the root of this hierarchy;</strong></li>
</ul>
<p>All Java classes inherit from the object class as it is at the top of the hierarchy. The object class therefore has no superclass.</p>
<ul type="disc">
<li><strong>Write an equals method for a class;</strong></li>
</ul>
<p>By default the equals method compares object references not object contents.</p>
<p>Writing another equals method means we can compare contents as well.</p>
<p>public boolean equals (User u) // this method overloads the equals method.</p>
<p>{</p>
<p>return userID.equals(u.userID); // compares if userID&#8217;s are the same.</p>
<p>}</p>
<p>public boolean equals (User u) // this method overloads the equals method.</p>
<p>{</p>
<p>return userID.equals(u.userID) &amp;&amp; emailAddress.equals(u.emailAddress) &amp;&amp;               numOfAccesses == u.numOfAccesses; // compares userID&#8217;s, emails and number of accesses.</p>
<p>}</p>
<p>public boolean equals (Object o) // this method overrides the equals method</p>
<p>{</p>
<p>// cast the Object argument to a User type</p>
<p>User u = (User) o;</p>
<p>return userID.equals(u.userID);</p>
<p>}</p>
<p>Use like this: emma.equals(maria);</p>
<ul type="disc">
<li><strong>Write a toString method for a class.</strong></li>
</ul>
<p>toString class by default returns class name followed by @ followed by a by a numeric hexadecimal value. To make a more useful toString class override method returning more useful info from constructor for example.</p>
<p>public String toString ()</p>
<p>{</p>
<p>return &#8220;User &#8221; + userID + &#8220;, Email &#8221; + emailAddress;</p>
<p>}</p>
<p>To use a toString method. Call println on an object.</p>
<p>User a = new User(&#8220;Tembo&#8221;, &#8220;<a href="mailto:zamby@lusaka.zm">zamby@lusaka.zm</a>&#8220;);</p>
<p>System.out.println(a);</p>
<p>produces the output: User Tembo, Email <a href="mailto:zamby@lusaka.zm">zamby@lusaka.zm</a></p>
<p><strong>Unit 4</strong></p>
<h2><em>Explain the concept of a stream; </em></h2>
<p>A stream for providing its input/output facilities. A stream, sometimes referred to as a byte stream, is essentially a sequence of bytes, representing a flow of data from a source to a destination. This includes the keyboard, screen and various sorts of data files, as well as between networked computers. They also allow manipulation of the data on the way: for example, by converting the data to different formats or by storing it temporarily (buffering).</p>
<h2><em>Use the basic Java facilities for input and output; </em></h2>
<p>//Read from Keyboard</p>
<p>public void hello()</p>
<p>{</p>
<p>Scanner sc = new Scanner(System.in);</p>
<p>//Ask what is your name</p>
<p>String name = sc.nextLine();</p>
<p>//Ask what is your age</p>
<p>int age = sc.nextInt();</p>
<p>//Display name and age here</p>
<p>sc.close();//take care</p>
<p>}</p>
<p>//Read from a file</p>
<p>public void hello()</p>
<p>{</p>
<p>Scanner sc = Scanner sc = new Scanner (new File (&#8220;source/use.txt&#8221;));</p>
<p>//Ask what is your name</p>
<p>String name = sc.nextLine();</p>
<p>//Ask what is your age</p>
<p>int age = sc.nextInt();</p>
<p>//Display name and age here</p>
<p>sc.close();//take care</p>
<p>}</p>
<p>//Read from a datafile.</p>
<p>FileInputStream fis = new FileInputStream(&#8220;payroll.dat&#8221;);</p>
<p>DataInputStream dataIn = new DataInputStream(fis);</p>
<p>//Output to text file</p>
<p>Public void hello()</p>
<p>{</p>
<p>PrintWriter pw = new PrintWriter(&#8220;Hello.txt&#8221;); //creates new file</p>
<p>pw.println(&#8220;The first line&#8221;);</p>
<p>pw.println(&#8220;The second line&#8221;);</p>
<p>pw.close();</p>
<p>}</p>
<p>//output to datafile</p>
<p>FileOutputStream fos = new FileOutputStream(&#8220;payroll.dat&#8221;);</p>
<p>DataOutputStream dataOut = new DataOutputStream(fos);</p>
<h2><em>Distinguish text input/output from binary input/output; </em></h2>
<ul type="disc">
<li>Text input/output: human readable using a simple text editor or word processor.</li>
<li>Binary Input/Output: machine readable, more efficient.</li>
</ul>
<h2><em>Identify when it is appropriate to use exceptions; </em></h2>
<p>If the code within a method may throw a checked exception, then you must do one of two things:</p>
<ul type="disc">
<li>Declare in the method header that an exception may be thrown. in this case you do not handle the exception within the method, but simply pass it on;</li>
<li>Catch the exception and deal with it within the method, using a try-catch statement.</li>
</ul>
<h2><em>Distinguish between checked and unchecked exceptions; </em></h2>
<p>Examples of checked exceptions.</p>
<ul type="disc">
<li>EOFException occurs when a program attempts to read past the end of a file.</li>
<li>FileNotFoundException can occur when a program attempts to open or write to an existing file. If the file cannot be found, then this exception is thrown.</li>
<li>MalformedURLException indicates that an invalid form of URL (such as a website address) has occurred.</li>
</ul>
<p>Examples of unchecked exceptions, as they are subclasses of RunTimeException.</p>
<ul type="disc">
<li>ArrayIndexOutOfBoundsException occurs when a program tries to access an array with an index outside the valid limits.</li>
<li>ArithmeticException arises when an illegal arithmetic condition occurs, such as an attempt to divide an integer by zero.</li>
<li>NumberFormatException can be caused by an application expecting to read a number of some sort, when the data does not have the appropriate format.</li>
<li>NullPointerException happens when an application attempts to use null in a case where an object is required: for example, invoking a method on a null reference.</li>
</ul>
<h2><em> </em></h2>
<h2><em>Define and use new types of exception; </em></h2>
<p>DEFINE</p>
<p>class FileFormatException extends IOException</p>
<p>{</p>
<p>private int errorPosition;</p>
<p>public FileFormatException () {}</p>
<p>public FileFormatException (String message, int bytesRead)</p>
<p>{</p>
<p>super(message);</p>
<p>errorPosition = bytesRead;</p>
<p>}</p>
<p>public int getErrorPosition ()</p>
<p>{</p>
<p>return errorPosition;</p>
<p>}</p>
<p>}</p>
<p>USE</p>
<p>try</p>
<p>{</p>
<p>// code to be checked</p>
<p>if (checkFormat(theNewFile))</p>
<p>{</p>
<p>&#8230;</p>
<p>}</p>
<p>}</p>
<p>Catch (FileFormatException exception)</p>
<p>{</p>
<p>String reason = exception.getMessage();</p>
<p>int errLoc = exception.getErrorPosition();</p>
<p>System.out.println(reason+&#8221; at &#8220;+errLoc);</p>
<p>}</p>
<h2><em>Deal with error conditions in a Java program, by means of exceptions or otherwise; </em></h2>
<p>public void loadFiles ()</p>
<p>{</p>
<p>&#8230;</p>
<p>Try</p>
<p>{</p>
<p>If (checkFormat(&#8220;File1.dat&#8221;))</p>
<p>{</p>
<p>// code to process file data</p>
<p>}</p>
<p>}</p>
<p>catch (EOFException eofEx)</p>
<p>{</p>
<p>// code to handle this exception</p>
<p>// eofEx refers to EOFException object</p>
<p>}</p>
<p>}</p>
<h2><em>Use the StringTokenizer class to split string data into substrings. </em></h2>
<p>String = new String(<a href="mailto:T.F.Jones@coolmail.com">T.F.Jones@coolmail.com</a> Tom Jones);</p>
<p>StringTokenizer em = new StringTokenizer( eMailAddress, &#8220;@.&#8221;);</p>
<p>while (em.hasMoreTokens())</p>
<p>{</p>
<p>System.out.println(em.nextToken());</p>
<p>}</p>
<p>OUTPUTS:</p>
<p>T</p>
<p>F</p>
<p>Jones</p>
<p>Coolmail</p>
<p>com Tom Jones</p>
<h2><em>Defensive programming. </em></h2>
<p>The defensive programming style of coding is appropriate for certain kinds of common error conditions. However, it has some drawbacks:</p>
<ul type="disc">
<li>If there is a lot of error checking and handling code, this can obscure the main purpose of the method;</li>
<li>It is somewhat ad-hoc &#8211; different programmers or Java library code may take different approaches to signaling or handling errors;</li>
<li>Some serious errors cannot easily be handled within the method where they occur &#8211; they may need to be passed to higher level code;</li>
<li>It may not be possible for a method to return a value indicating that an error has occurred &#8211; for example, if the method already returns a result.</li>
</ul>
<p><strong>Unit 5</strong></p>
<h2><em>Use library classes from the standard Java packages; </em></h2>
<p>First import them.</p>
<p>Import java.util.*; // The * means that the whole contents of java.util is available to use.</p>
<p>java.util and java.sql both contain a class called date. To use the date class from java.sql we can include an import at the top of our classes like below. Otherwise well get a name clash that will not compile.</p>
<p>Import java.util.*;</p>
<p>Import java.sql.*;</p>
<p>Import java.sql.Date;</p>
<p>You never need to import java.lang</p>
<h2><em>Broadly understand the contents of the java.util package; </em></h2>
<p>1.       Contain Java Collection Framework: LinkedList, ArrayList, HashSet, TreeSet, HashMap and TreeMap</p>
<p>2.       &#8216;Legacy&#8217; collection classes</p>
<p>3.       Random class</p>
<p>4.       StringTokenizer class</p>
<h2><em>Use some of the important collection classes provided within the java.util package; </em></h2>
<p>HashSet</p>
<p>Set&lt;Integer&gt; s= new HashSet&lt;Integer&gt;(); Using the Set interface.</p>
<p>if (s.isEmpty())</p>
<p>{</p>
<p>System.out.println(&#8220;Set initiallyempty&#8221;);</p>
<p>}</p>
<p>s.add(23);</p>
<p>s.add(25);</p>
<p>s.add(11);</p>
<p>// three integers added</p>
<p>System.out.println(&#8220;The set has &#8221; + s.size() + &#8220;elements&#8221;);</p>
<p>s.remove(23);</p>
<p>// there are nowonly two integers in the set</p>
<p>System.out.println(&#8220;The set now has &#8220;+ s.size() + &#8220;elements&#8221;);</p>
<p>LinkedList</p>
<p>LinkedList&lt;Integer&gt;numList =new LinkedList&lt;Integer&gt;();</p>
<p>numList.add(99);</p>
<p>numList.add(103);</p>
<p>numList.add(88);</p>
<p>// there are now three items in the list</p>
<p>System.out.println(&#8220;First item is &#8220;+numList.getFirst());</p>
<p>System.out.println(&#8220;Lastitem is &#8220;+numList.getLast());</p>
<p>ArrayList</p>
<p>ArrayList &lt;Integer&gt; holder = new ArrayList &lt;Integer&gt;();</p>
<p>holder.add(3);</p>
<p>System.out.println(holder.remove(0)+2); //returns 3 then adds on 2</p>
<p>HashMap</p>
<p>HashMap &lt;String,String&gt; hm =new HashMap&lt;String, String&gt;();</p>
<p>System.out.println(&#8220;Hello there&#8221;);</p>
<p>hm.put(&#8220;Dave&#8221;,&#8221; London &#8220;);</p>
<p>hm.put(&#8220;Robert&#8221;, &#8221; Sheffield &#8220;);</p>
<p>hm.put(&#8220;Darrel&#8221;, &#8221; Milton Keynes &#8220;);</p>
<p>System.out.println(&#8220;Darrelisassociated with &#8220;+hm.get(&#8220;Darrel&#8221;));</p>
<p>for(String name: hm.keySet()) // The code hm.keySet returns the collection of keys associated with hm.</p>
<p>{</p>
<p>System.out.println (name+&#8221; is associated with the city &#8220;+hm.get(name));</p>
<p>}</p>
<h2><em>Define your own packages for structuring large programs; </em></h2>
<p>To define a collection of classes as a package, you need to precede the source code of each of the classes with the keyword package followed by the name of the package, as follows:</p>
<p>package testPackage;</p>
<ul type="disc">
<li> * explain the role of the Object class in heterogeneous collections;</li>
<li> * explain the concept of an interface and its role in relation to multiple inheritance;</li>
</ul>
<p>An Interface is like a class that has method headers but very little method bodies. An interface declares what has to be done but not how to do it. Interfaces do not contain variables but can contain constants. Interfaces are not classes.</p>
<p>publicinterface Comparable&lt;T&gt; //T is the type being compared.</p>
<p>{</p>
<p>int compareTo(T o); // method header only in this example there are no constants defined. Methods in interfaces are know as abstract methods.</p>
<p>}</p>
<p>A class that implements the above class start like this and must contain a method called compareTo.</p>
<p>class Employee implements Comparable &lt;Employee&gt;</p>
<h2><em>Understand the concept of an abstract class; </em></h2>
<p>Abstract classes are useful in defining groups of classes related by inheritance. Abstract classes normally have at least one abstract method. An abstract method has header, but has no implementing code associated with the header. The implementation code must be supplied by subclasses of the abstract class. The keyword also used to identify these methods. Note the difference from interfaces, which have abstract methods but do not require the word abstract in the declaration.</p>
<ul>
<li>cannot be instantiated;</li>
<li>may have instance variables, finals, static variables and concrete methods.</li>
</ul>
<p>An abstract class may be useful because:</p>
<ul>
<li>it can be used to factor out common features of a number of classes, which then</li>
</ul>
<p>inherit from the abstract class and implement or override methods as required;</p>
<p>Abstract methods</p>
<p>1.       Boolean hasNext() &#8211; returns true if the iteration has more elements</p>
<p>2.       E next() &#8211; returns the next element in the iteration</p>
<p>3.       Void remove() &#8211; removes from the underlying collection the last element returned by the iterator (optional operation)</p>
<h2><em>use polymorphism in connection with abstract classes; </em></h2>
<p>If Employee is an abstract class of WeeklyEmployee and MonthlyEmployee and both contain a different method called payment. When staff is an instance of Employee we can call either of the payment methods like this.</p>
<p>staff.payment();</p>
<p>If the staff object is a WeeklyEmployee it will use its class, if the object is MonthlyEmployee it will use the payment method of the MonthlyEmplyee class. This is polymorphism.</p>
<h2><em>Contrast interfaces and abstract classes; </em></h2>
<p><span style="text-decoration: underline;">Interface:</span></p>
<p>1.      An interface is not a class.</p>
<p>2.      All its methods are abstract and, apart from its abstract methods, it may contain only constants. The methods are implicitly treated as abstract and the constants are treated as if declared as public static final.</p>
<p>3.      An interface can be implemented by any number of unrelated classes, which declare this using the implements keyword. A class may implement any number of interfaces.</p>
<p><span style="text-decoration: underline;">Abstract class:</span></p>
<p>1.      An abstract class uses the keyword abstract in the class header.</p>
<p>2.      It normally has at least one abstract method, either defined within the class or inherited from a superclass, and its abstract methods must be explicitly declared abstract. It can also have concrete methods (that is, it may be fully implemented) and instance variables, unlike an interface.</p>
<p>3.      It can only constrain its own concrete subclasses, by requiring them to implement its abstract methods &#8211; it cannot constrain any other classes.</p>
<p><span style="text-decoration: underline;">The similarities between an interface and an abstract class in Java:</span></p>
<p>1.      They can both place requirements on objects of other classes.</p>
<p>2.      Both can have abstract methods (although neither need have abstract methods).</p>
<p>3.      You cannot create objects of an abstract class type or an interface type. You can,</p>
<p>4.      however, create reference variables of either type. These are normally used to refer to an object of a subclass of the abstract type or to an object of a class that implements the interface, respectively.</p>
<p>5.      Both can inherit: the abstract class from another class; the interface only from another interface.</p>
<h2><em>Use the Iterator interface defined for Java collection classes. </em></h2>
<p>Example</p>
<p>ArrayList&lt;String&gt; userList =new ArrayList&lt;String&gt;();</p>
<p>&#8230;</p>
<p>Iterator &lt;String&gt;listIt =userList.iterator();</p>
<p>System.out.print(&#8220;The list is [");</p>
<p>while (listIt.hasNext())</p>
<p>{</p>
<p>System.out.print(listIt.next() +" ");</p>
<p>}</p>
<p>System.out.println(" ]&#8220;);</p>
<p><strong> </strong></p>
<p><strong>Unit 6</strong></p>
<p>GUI element need a JFrame, class extends JFrame.</p>
<p>Buttons:</p>
<p>JButton offButton = new JButton( ); // unlabeled</p>
<p>JButton onButton = new JButton(&#8220;Cancel&#8221;); // labeled</p>
<p>offButton.setText(&#8220;Press Me&#8221;);  // set label</p>
<p>String offButtonLabel = offButton.getText( ); // name a variable the label of an exciting button</p>
<p>Labels:</p>
<p>JLabel testLb = new JLabel(&#8220;Verbose Interface Cluster&#8221;);</p>
<p>testLb.setText(&#8220;Good Bye&#8221;);</p>
<p>Check Box:</p>
<p>JCheckBox noviceUserType = new JCheckBox(&#8220;Novice&#8221;); // labeled Novice state is true or false</p>
<p>boolean b = noviceUserType.isSelected( ); // will set b to true or false</p>
<p>Radio Buttons:</p>
<p>// first create the button group</p>
<p>ButtonGroup language = new ButtonGroup( );</p>
<p>// then create the buttons</p>
<p>JRadioButton frenchButton = new JRadioButton(&#8220;French&#8221;, true);</p>
<p>JRadioButton englishButton = new JRadioButton(&#8220;English&#8221;, false);</p>
<p>// then add the buttons to the button group</p>
<p>language.add(frenchButton);</p>
<p>language.add(englishButton);</p>
<p>Combo Box:</p>
<p>JComboBox computerChoice = new JComboBox( );</p>
<p>computerChoice.addItem(&#8220;PC&#8221;);</p>
<p>computerChoice.addItem(&#8220;Mac&#8221;);</p>
<p>computerChoice.setSelectedItem(&#8220;Mac&#8221;); // default Variable</p>
<p>Text Fields:</p>
<p>JTextField txtA = new JTextField( ); // default</p>
<p>JTextField txtB = new JTextField( 24); // 25em width</p>
<p>JTextField txtC = new JTextField(&#8220;Text Field Type here&#8221;); // default contents</p>
<p>JTextField txtD = new JTextField(&#8220;This is for typing&#8221;, 20); // default contents and width</p>
<p>Text Areas:</p>
<p>JTextArea ta = new JTextArea (&#8220;This is some text to start off with. \n&#8221; +</p>
<p>&#8220;Do you like it ? yes or no ?&#8221;, 4, 20);</p>
<p>// same as text field but has has extra argument for box length.</p>
<p>Scroll bars:</p>
<p>JScrollBar scbV = new JScrollBar(JScrollBar.VERTICAL);</p>
<p>JScrollBar scbH = new JScrollBar(JScrollBar.HORIZONTAL);</p>
<p>Menu bars:</p>
<p>JMenuBar mb = new JMenuBar( ); // new menu bar</p>
<p>JMenu m = new JMenu(&#8220;File&#8221;); // new menu section</p>
<p>m.add(new JMenuItem(&#8220;close&#8221;)); // new menu item in section</p>
<p>m.add(new JMenuItem(&#8220;save&#8221;)); // new menu item in section</p>
<p>mb.add(m); // add menu section to bar</p>
<p>setJMenuBar(mb); // set bar</p>
<p>Layout FlowLayout: Default</p>
<p>/ * Place some buttons. */</p>
<p>Container cp = getContentPane( );</p>
<p>cp.setLayout(new FlowLayout( ));</p>
<p>Layouts: GridLayout:</p>
<p>/ * Place some buttons. */</p>
<p>Container cp = getContentPane( );</p>
<p>// set size of grid &#8211; rows then columns</p>
<p>cp.setLayout(new GridLayout(3, 2)); // 3 tall by 2 wide grid.</p>
<p>Layout BorderLayout:</p>
<p>/ * Place some buttons. */</p>
<p>Container cp = getContentPane( );</p>
<p>cp.add( buttons [0 ], BorderLayout.NORTH);</p>
<p>cp.add( buttons [1 ], BorderLayout.SOUTH);</p>
<p>cp.add( buttons [2 ], BorderLayout.EAST);</p>
<p>cp.add( buttons [3 ], BorderLayout.WEST);</p>
<p>cp.add( buttons [4 ], BorderLayout.CENTER);</p>
<p>Class to display your code</p>
<p>public class FrameDemoTest</p>
<p>{</p>
<p>public static void main (String [] args)</p>
<p>{</p>
<p>FrameDemo fd = new FrameDemo(&#8220;We Love Java&#8221;);</p>
<p>fd.setVisible(true);</p>
<p>}</p>
<p>}</p>
<p><strong>Unit 7 </strong></p>
<h2><em>Work within an event-driven programming environment; </em></h2>
<p>Non linear. Event-driven programs are programs that respond to events initiated by the user, such as mouse clicks and key presses. Events can also be initiated through software coding, using for example the Timer class,</p>
<h2><em>Attach code to visual components to capture user input in the form of events such as mouse presses and text input; </em></h2>
<p><span style="text-decoration: underline;">FOR A BUTTON</span></p>
<p>buttonUp = new JButton(&#8220;Press Me Up&#8221;); // define a new button buttonUp.addActionListener(new ButtonWatcher()); // attaches ActionListener to button</p>
<p>/* Define the listener inner class implementing the interface ActionListener. */</p>
<p>private class ButtonWatcher implements ActionListener</p>
<p>{</p>
<p>/* Here is the code which is executed when the button is clicked. Note the class implements */</p>
<p>public void actionPerformed (ActionEvent a)</p>
<p>{</p>
<p>Object buttonPressed =a.getSource();</p>
<p>if (buttonPressed.equals(buttonUp))</p>
<p>{</p>
<p>buttonClicks++;</p>
<p>}</p>
<p>if (buttonPressed.equals(something else))</p>
<p>{</p>
<p>buttonClicks&#8211;;</p>
<p>}</p>
<p>label.setText(buttonClicks + &#8220;&#8221;);</p>
<p>}</p>
<p>}</p>
<p><span style="text-decoration: underline;">FOR A MOUSE CLICK etc&#8230;</span></p>
<p>c = new JPanel();</p>
<p>c.addMouseListener(new MouseEventer()); // attaches MouseListener to previously define panel called c.</p>
<p>/* Define the listener inner class implementing the adapter class</p>
<p>. */</p>
<p>private class MouseEventer extends MouseAdapter</p>
<p>{</p>
<p>/* Here is the code which is executed when the mouse is clicked. Note the class extends */</p>
<p>public void mouseClicked (MouseEvent a)</p>
<p>{</p>
<p>int xCoordinate = e.getX();</p>
<p>int yCoordinate = e.getY();</p>
<p>yCoordLabel.setText(yCoordinate + &#8220;&#8221; );</p>
<p>xCoordLabel.setText(xCoordinate + &#8220;&#8221; );</p>
<p>}</p>
<p>}</p>
<h2><em>Use inner classes; </em></h2>
<p>Inner classes are nested inside of normal classes. advantages of using inner classes is that the inner class has access to all of the methods and data fields of the surrounding class.</p>
<h2><em> </em></h2>
<h2><em>Create simple animations; </em></h2>
<p>One class sets up panels/window/frame that animation happens in. paint method is overridden.</p>
<p>public void paint (Graphics g)</p>
<p>{</p>
<p>super.paint(g);</p>
<p>myBall.paint(g);</p>
<p>}</p>
<p>// class contains move method that trigger another move methods in another class.</p>
<p>public void move ()</p>
<p>{</p>
<p>while (true)</p>
<p>{</p>
<p>myBall.move(); // not yet defined</p>
<p>repaint();</p>
<p>try</p>
<p>{</p>
<p>Thread.sleep(50);</p>
<p>} catch (InterruptedException e)</p>
<p>{</p>
<p>System.exit(0);</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>class that  builds object above calls move method of above.</p>
<p>public class MovingBallTest</p>
<p>{</p>
<p>public static void main (String[]args)</p>
<p>{</p>
<p>MovingBall world = new MovingBall(&#8220;Moving Ball&#8221;);</p>
<p>world.setVisible(true);</p>
<p>world.move();</p>
<p>}</p>
<p>}</p>
<p>another class in this case called ball must include it own paint method and move method. Move method should contain code to change size, direction, shape, speed, coordinates etc&#8230;</p>
<h2><em>Use the graphical facilities within Java. </em></h2>
<p>Here we override the method paint to create what we want to appear on screen. */</p>
<p>public void paint (Graphics g)</p>
<p>{</p>
<p>super.paint(g);</p>
<p>g.drawRect(50, 50, 100, 75);</p>
<p>}</p>
<p>With shapes the first 2 variables are the distance in pixels from the top left corner of the window/panel.</p>
<p>topleft at (x, y)</p>
<p>Rectangle testRectangle = new Rectangle(x, y, width, height);</p>
<p>Fonts example</p>
<p>public void paintComponent(Graphics g)</p>
<p>{</p>
<p>Font someFont = new Font(&#8220;Serif&#8221;, Font.BOLD, 12);</p>
<p>g.setFont(someFont);</p>
<p>g.drawString(&#8220;Some Text!&#8221;, 50, 50); // draws, some Text! 50&#215;50 from top left in bold serif font</p>
<p>FontMetrics someFontMetric = g.getFontMetrics(someFont);</p>
<p>int wordLength = someFontmetric.stringWidth(&#8220;Hello&#8221;); // gives length in pixels of the graphic word Hello</p>
<p>}</p>
<p>assigned an image to a variable</p>
<p>private Image picture = null;</p>
<p>picture =getToolKit().getImage(&#8220;mypicture.gif&#8221;);</p>
<p>g.drawImage(picture, 0, 0, this);</p>
<p><strong> </strong></p>
<p><strong>Unit 8 </strong></p>
<h2><em>Understand how threads can help to make more effective use of a computer&#8217;s resources; </em></h2>
<p>Threads are not the actual static code itself but rather they are the dynamic process of executing that code. The threads share the CPU, the operating system will allocate small blocks of time to each thread.</p>
<h2><em>Define, create and use threads; </em></h2>
<p>Define</p>
<p>Create</p>
<p>public class WhereAmIRun extends Thread</p>
<p>{</p>
<p>int thisThread;</p>
<p>// constructor</p>
<p>public WhereAmIRun (int number)</p>
<p>{</p>
<p>thisThread = number;</p>
<p>}</p>
<p>// implement the run method</p>
<p>public void run ()</p>
<p>{</p>
<p>for (int i =0;i&lt; 100; i++)</p>
<p>{</p>
<p>System.out.println(&#8220;I&#8217;m in thread &#8221; + thisThread);</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>You can also use the Runnable Interface which allows classes to inherit from more than just the threads class. The runnable inferface has one method called run. The creation of a threadable class is the same as defined above but has a slightly different first line.</p>
<p>public class WhereAmIRun implements Runnable</p>
<p><span style="text-decoration: underline;">USE</span></p>
<p>public class ThreadTester // for classes that extend the Thread class</p>
<p>{</p>
<p>public static void main (String [] args)</p>
<p>{</p>
<p>// create the threads</p>
<p>WhereAmI place1 = new WhereAmI(1);</p>
<p>WhereAmI place2 = new WhereAmI(2);</p>
<p>WhereAmI place3 = new WhereAmI(3);</p>
<p>// start the threads</p>
<p>place1.start();</p>
<p>place2.start();</p>
<p>place3.start();</p>
<p>}</p>
<p>}</p>
<p>public class ThreadTesterRun // for classes that use the runnable interface we need to create thread objects</p>
<p>{</p>
<p>public static void main (String [] args)</p>
<p>// create a runnable object</p>
<p>WhereAmIRun place1 = new WhereAmIRun(1);</p>
<p>// now create a thread to run it</p>
<p>Thread thread1 = new Thread(place1);</p>
<p>// repeat for two further objects</p>
<p>WhereAmIRun place2 = new WhereAmIRun(2);</p>
<p>Thread thread2 = new Thread(place2);</p>
<p>WhereAmIRun place3 = new WhereAmIRun(3);</p>
<p>Thread thread3 = new Thread(place3);</p>
<p>// start the threads</p>
<p>thread1.start();</p>
<p>thread2.start();</p>
<p>thread3.start();</p>
<p>}</p>
<p>}</p>
<h2><em> </em></h2>
<h2><em>Describe the various states that threads can be in; </em></h2>
<p>A thread is in one of five states at any moment in time. These are:</p>
<p>1. initial state;</p>
<p>2. runnable state;</p>
<ul type="disc">
<li>
<ul type="circle">
<li>
<ul type="square">
<li>
<ul type="disc">
<li>ready to be used by the JVM</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>3. running state;</p>
<p>4. blocked state;</p>
<ul type="disc">
<li>
<ul type="circle">
<li>
<ul type="square">
<li>
<ul type="disc">
<li>it is waiting for an I/O process to complete;</li>
<li>some other part of the program has not completed its processing;</li>
<li>it is not being given access to some shared data.</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>5. finished state.</p>
<h2><em>Appreciate some of the problems that can occur when threads access shared resources; </em></h2>
<p>Multiple threads accessing shared resources can cause problems as they can block each other, or change variables that other threads are relying on.</p>
<h2><em>Understand how locks affect the execution of code. </em></h2>
<p>Threads can lock a shared resources to avoid problems. To do this any thread methods that access shared data need to be defined with the static variable, synchronized (with a &#8216;Z&#8217;). This stops any other threads using this method and put them in the blocked state until the resource is unlocked. To prevent the locking of shared resources creating new potential problems follow points.</p>
<ul type="disc">
<li>If two or more threads modify a common object, then declare the methods that do the modifying as being synchronized.</li>
<li>If a thread needs to wait for an object to change, make sure that it waits inside the thread. It does this by entering a synchronized method and calling wait.</li>
</ul>
<p>When a method has changed an object, it should call the notify method or the notifyAll method before terminating. This gives other methods a chance to access the object. notifyAll(); or notify();</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisfordblog.com/2009/06/ou-m257-revision-notes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fix: CPU is unworkable or has been changed.</title>
		<link>http://www.chrisfordblog.com/2009/04/fix-cpu-is-unworkable-or-has-been-changed/</link>
		<comments>http://www.chrisfordblog.com/2009/04/fix-cpu-is-unworkable-or-has-been-changed/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 21:17:44 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Everything Else]]></category>
		<category><![CDATA[Things I've Learnt]]></category>

		<guid isPermaLink="false">http://www.chrisfordblog.com/2009/04/fix-cpu-is-unworkable-or-has-been-changed/</guid>
		<description><![CDATA[Today I fixed a friends computer that was suddenly displaying the message &#8220;CPU is unworkable or has been changed&#8221; at boot time and performing very slowly. After running one of my diagnostic tools I found that the CPU, an AMD athon 2800+ was running at a speed of 1.25Ghz. The processor should normally run at [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" title="CPU" src="http://www.novopc.com/wp-content/uploads/2008/10/cpu-1433z.jpg" alt="" width="303" height="303" /></p>
<p>Today I fixed a friends computer that was suddenly displaying the message &#8220;CPU is unworkable or has been changed&#8221; at boot time and performing very slowly. After running one of my diagnostic tools I found that the CPU, an AMD athon 2800+ was running at a speed of 1.25Ghz. The processor should normally run at just over 2.00Ghz.</p>
<p>The solution was to enter the BIOS and reset the front side bus number. Which for some reason was suddenly 100 with a multiplier of 12.5 (100 X 12.5 = 12500). When I changed the FSB to 166 it starting working as normal again (166 X 12.5 = 2075).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisfordblog.com/2009/04/fix-cpu-is-unworkable-or-has-been-changed/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Emailing Play.com</title>
		<link>http://www.chrisfordblog.com/2008/12/emailing-playcom/</link>
		<comments>http://www.chrisfordblog.com/2008/12/emailing-playcom/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 15:18:19 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Things I've Learnt]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[play.com]]></category>

		<guid isPermaLink="false">http://www.chrisfordblog.com/?p=294</guid>
		<description><![CDATA[If you ever need to email play.com You&#8217;ll find that on there website you can&#8217;t find an email address. After a bit of googling I found this email address for them customercare@play.com which I&#8217;ve used and had a response from. I don&#8217;t know why they don&#8217;t put this on there website so I&#8217;ve shared it here.]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-352" title="email_clipart_computer" src="http://chrisfordblog.com/wp/wp-content/uploads/2008/12/email_clipart_computer.gif" alt="email_clipart_computer" width="200" height="140" /></p>
<p>If you ever need to email <a href="http://www.play.com">play.com</a> You&#8217;ll find that on there website you can&#8217;t find an email address. After a bit of googling I found this email address for them <a href="mailto:addresscustomercare@play.com">customercare@play.com</a> which I&#8217;ve used and had a response from. I don&#8217;t know why they don&#8217;t put this on there website so I&#8217;ve shared it here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisfordblog.com/2008/12/emailing-playcom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to speed up Mac OS X</title>
		<link>http://www.chrisfordblog.com/2008/12/how-to-speed-up-mac-os-x/</link>
		<comments>http://www.chrisfordblog.com/2008/12/how-to-speed-up-mac-os-x/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 12:57:16 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Things I've Learnt]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[faster]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[OSX]]></category>
		<category><![CDATA[speed]]></category>

		<guid isPermaLink="false">http://www.chrisfordblog.com/?p=288</guid>
		<description><![CDATA[I&#8217;ve had an iMac for about 2 years and have recently noticed that it&#8217;s getting a little slower at booting up. So I&#8217;ve spent so time trying to improve it. This is what I found and how I recommend speeding up a mac in the future. Check login items for your user account and remove programs [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-354" title="apple-2007" src="http://chrisfordblog.com/wp/wp-content/uploads/2008/12/apple-2007-303x213.jpg" alt="apple-2007" width="303" height="213" /></p>
<p>I&#8217;ve had an iMac for about 2 years and have recently noticed that it&#8217;s getting a little slower at booting up. So I&#8217;ve spent so time trying to improve it. This is what I found and how I recommend speeding up a mac in the future.</p>
<ul>
<li>Check login items for your user account and remove programs that you don&#8217;t need. This can be found thorough systems.app then user accounts. I found that I still had software running at start up for a scanner that I didn&#8217;t have anymore.</li>
<li>Time spent loading with grey and blue boot screens aren&#8217;t usually related to programs running in the background.</li>
<li>Use a maintenance program like <a href="http://www.apple.com/downloads/macosx/system_disk_utilities/cocktail_maintain.html">Cocktail</a> or <a href="http://www.apple.com/downloads/macosx/system_disk_utilities/leopardcachecleaner.html">Leopard Cache Cleaner</a> both are shareware and can be used without paying for although I&#8217;d recommended buying 1 of them if you find it useful to support the developers. Both programs are very similar but <a href="http://www.apple.com/downloads/macosx/system_disk_utilities/cocktail_maintain.html">Cocktail</a> has a built in scheduler to run maintenance scripts and power down your system which is a nice feature. <a href="http://www.apple.com/downloads/macosx/system_disk_utilities/leopardcachecleaner.html">Leopard Cache Cleaner</a> can run an antivirus scanner so you may want to run bits of both programs.<span id="more-288"></span></li>
<li>Delete programs, movies, albums that you no longer want. A hard drive more than 75% full seems to slow down the system a little. Using a program like <a href="http://www.apple.com/downloads/macosx/system_disk_utilities/tidyup.html.">Tidy Up</a> can make this easier.</li>
<li>Reboot the system then run a program called console straight away. It come with the mac. Just search for it using spotlight and click run. This will give you a log of what your system has been doing. I noticed that there were about 20 log entries for VMware fusion, a program that I&#8217;d removed a few months earlier. A quick google search and download later and I had an uninstaller program to remove VMware fusion properly from my system.</li>
<li>Run the activing monitor program that came with your Mac and watch for programs that use alot of CPU or RAM of anykind. If you don&#8217;t need the program or can find an alternative that uses less resources remove the program from your system.</li>
<li>Unplug any devices attached to your Mac except for your mouse and keyboard. Restart and see if this improves boot time, if so then reattach items one by one rebooting in between until you find which item is causing the problem. I found that my USB hub added an extra 20 seconds to the boot time. If this happens to you consider getting another USB hub or just be happy knowing that you&#8217;ve found part of the problem and that it&#8217;s nothing to worry about.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisfordblog.com/2008/12/how-to-speed-up-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
