Tuesday, March 20, 2007

LabTest 3 Impressions

Hey Guys,
I wrote the test yesterday, relatively hard, wish i studied JUST linked lists instead of the gui and recursion.
turns out the only thing on the test was linked lists and generic class
the test before that (Monday) did linked list with an equals() method..

I found it pretty misleading of Andrew and Turpin to tell us it would cover everything when it ended up covering linked lists specifically.
worries me quite alot, because now i have no idea if i should believe them about exam materials, etc.

anyways we had to make a listNode class and a LinkedList class
make them work
using a generic object to carry data
then make a test program which proves that it works, using string and integer values.

later
Jake

Thursday, March 15, 2007

Lecture 24

Linked List
  • data structure containing nodes
  • each node points to the node after it

Adding nodes to the middle

  • find the index of the node prior to the one you must add

int position = i // position where the new node gives
ListNode a = FindNum(head, position - 1);
ListNode b = new ListNode();//new object
b.setNext(a.getNext()); // 1
a.setNext(b); //2

adding position 0 = creating a new head

listNode b = new ListNode();
b.setNext(head);
head = b;



Deleting Elements

  • deletion involves reordering next pointers

ListNode a = findNum(head, position - 1);
a.setNext((a.getNext()).getNext());

  • object must not be null
  • a.getNext must not be null

Linked List as Object

public class LinkedList
{
private ListNode head;
public LinkedList(ListNode head);
public LinkedList()
public ListNode findEnd()
public ListNode findNum()
public void addNode(ListNode, newNode, int position)
public void delNode(int position)
public void addNodeToEnd(Listnode, NewNode)
public int getSize()

Tuesday, March 13, 2007

Java Calculator GUI Lab

hey guys,
finally finished it and it works fine.. if you want some simpler code then what i'd seen on the other forum ... use mine.

i did all of this straight from the lecture notes so there shouldn't be anything too confusing
but i will admit it took me a WHILE to get this working.. rusty i guess.. heh..
later
Jake


import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.JFrame ;

public class MenuJFrame
extends JFrame
implements ActionListener
{
public static final int WIDTH = 600;
public static final int HEIGHT = 400;

// without putting these here, they are unaccessable by the actionlistener
private JPanel westPanel;
private JPanel eastPanel;
private JTextField one;
private JTextField two;
private JTextField answer;


public MenuJFrame()
{
//main window

super("Java Calculator");
this.setSize(WIDTH,HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(0,2));

//west panel

westPanel = new JPanel();
westPanel.setLayout(new GridLayout(3,0));
this.add(westPanel);

//east panel

eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(4,0));
this.add(eastPanel);


//text fields

one = new JTextField(30);
two = new JTextField(30);


westPanel.add(one);
westPanel.add(two);

//answer

answer = new JTextField("0.0");

//non-editable text field

answer.setEditable(false);
westPanel.add(answer);

//buttons for east
JButton add = new JButton("+");
JButton subtract = new JButton("-");
JButton multiply = new JButton("*");
JButton divide = new JButton("/");

//add buttons to eastPanel

eastPanel.add(add);

add.addActionListener(this);


eastPanel.add(subtract);

subtract.addActionListener(this);

eastPanel.add(multiply);

multiply.addActionListener(this);

eastPanel.add(divide) ;

divide.addActionListener(this);

//Menu Bar

//create a new menubar , this is ONLY the grey bar at the top w/ nothing in it!!!
JMenuBar myMenuBar = new JMenuBar();

//this.setJMenuBar is a method which auto positions the menu bar @ the top
this.setJMenuBar(myMenuBar);

//create two fields, File and Clear.
JMenu File = new JMenu("File");

JMenu Clear = new JMenu("Clear");

//create a menuItem quit
JMenuItem quit = new JMenuItem("Quit");

//add action listener to quit, so the program quits when clicked
quit.addActionListener(this);

// add quit to the file menu
File.add(quit);

//create menu items for clearing the fields
JMenuItem clearA = new JMenuItem("Clear A");
JMenuItem clearB = new JMenuItem("Clear B");
JMenuItem wipe = new JMenuItem("Clear All");

//add action listeners for each
clearA.addActionListener(this);
clearB.addActionListener(this);
wipe.addActionListener(this);

// add all three menu items to the menu 'Clear'
Clear.add(clearA);
Clear.add(clearB);
Clear.add(wipe);

// add file and clear to the menu bar
myMenuBar.add(File);
myMenuBar.add(Clear);






}


//ActionListener


public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();

//if the button is '+' then add the two fields and display answer in bottom box
if (cmd == "+")
{
answer.setText((Double.toString((Double.parseDouble(one.getText())) + (Double.parseDouble(two.getText())))));
}

//if the button is '-' then subtract the two fields and display answer in bottom box
if (cmd == "-")
{
answer.setText((Double.toString((Double.parseDouble(one.getText())) - (Double.parseDouble(two.getText())))));
}

//if the button is '*' then multiply the two fields and display answer in bottom box
if (cmd == "*")
{
answer.setText((Double.toString((Double.parseDouble(one.getText())) * (Double.parseDouble(two.getText())))));
}

//if the button is "/" then divide the two fields and display answer in bottom box
if (cmd == "/")
{
answer.setText((Double.toString((Double.parseDouble(one.getText())) / (Double.parseDouble(two.getText())))));
}

//if button is "Quit" then close the program
if (cmd == "Quit")
{
System.exit(0);
}

// if button is 'Clear A' then clear A
if (cmd == "Clear A")
{
one.setText("");
}

// if button is 'Clear B' then clear B
if (cmd == "Clear B")
{
two.setText("");
}

// if button is 'Clear All' then clear all fields and leave 0.0 in the answer field
if (cmd == "Clear All")
{
one.setText("");
two.setText("");
answer.setText("0.0");
}




}



}

public class JavaCalculator
{
public static void main(String[] args)
{
MenuJFrame MyWindow = new MenuJFrame();
MyWindow.setVisible (true);

}

}

Monday, March 12, 2007

Lab 4 and 5

Post your ideas / code / questions for these labs here

Friday, March 9, 2007

Lecture 23

Linked Lists

  • alternative method for producing an array of arbitrary size
    • a collection of objects of the same type
    • contained in the wrapper object, which points (or contains a reference) to the next object in the list
  • declare public class myObject
public class ListNode
{
private myObject item;
private ListNode next;
public ListNode(){...}
public ListNode getNext() {return next}
public void setNext(ListNode next) {this.next = next}
}

  • initially the list is empty
    • ListNode head = null;
  • add elements to the end as required
    • first element:
      • head = new ListNode(); /*head points to head of chain*/
    • every subsequent slement
      • let ListNode tail be reference to the last element in the list
        • tail.setNext(new ListNode(...));
        • tail = tail.getNext(); // not mandatory to keep track of tail
  • if the list is empty then the head must be 'null'
  • next pointer in the last element must be null

Transversing the List

  • Items are arranged in order
    • the only way to get something from the list is to first access all previous elements

Example - retrieve the ith element
  • pass through i next pointers to find the one you want

ListNode = head;
int j;
for (j=0; (j

Example - find the last element

ListNode n = head;
if (n != null)
{
while (n.getNext() ! = null)
{n = n.getNext() }
}


Lecture 22

Generic Methods


  • method with a type parameter
    • independent of a generic class
    • can be used in a non-generic class
    • can be used in a generic class with a different type parameter

public class myClass
{
public void myMethod(T x)
{
System.out.println(x);
}
}

  • syntax for calling the method
myClass m = new myClass();
m.myMethod("Hello");
m.myMethod(6);


Inheritance

  • inheritance is allowed with generics
public class Parent
{
}


public class Child extends Parent
{
public class A {...}
public class myGeneric
{...}
myGeneric//these two have
myGeneric//No Relationship

ArrayList
Example

Create an array of 5 Strings using ArrayList


ArrayList myList = new ArrayList(5);
// = generic type parameter

ArrayList myList = new ArrayList(0);//size is 0
myList.add("x"); // size is now 1
myList.add("y"); // puts y at index 1, size is now 2


Wednesday, March 7, 2007

Lecture 21

Generic programming
public class x
{
private String s;
public x(String s){this.s = s;}
public String getS(){return s;}
}
if we need many classes with the same function but different instance field type.
we can
1. declare them all individually
2. use a generic class(better)

a generic is a class that takes a type as a parameter

public class x <T>
{
private T s;
public x(T s){this.s=s;}
public T getS(){return s;}
}

x<String&lgt; myX = new x<String&lgt;("hello");

Note:
-constructor declared in the class as
public x(......)

but called as x<String&lgt;(....)
-The type associated with the generic can be any reference type
(i.e. any object type)

e.g. String, GregorianCalendar, JFrame and so on.

-Cannot use primitives
e.g. x<int&lgt; myX = new x<int&lgt;(36) is illegal

-There exist "wrapper" subjects for every primitive type.
int -&lgt; Integer
char -&lgt; Character
double -&lgt; Double
and so on...

x<Integer&lgt; myX = new x<Integer&lgt;(36); is legal

more illegal uses of the type parameter "T"

T myT = new T(); not okay
T[] myTArray = new T[10]; not okay

cannot create an array of generics.
x<String&lgt;[] myXarray = new x <String&lgt;[10] is illegal

can a generic take more than one type ?
the answer is yes.

public class TwoTypeGeneric <FirstType, SecondType&lgt;
{
codes here
}

restricting Types
you may wish to constrain the type parameter as follows
1.the child of a given class
2. implements a give interface
why?
can use methods associated with the ancestor/interface
usage: the extends keyword is used for both interfaces and ancestors.

Require that T implements myInterface

public class x<T extends myInterface&lgt;

require that T is a descendant of ancestor
public class x<T extends ancestor&lgt;

Also legal to require more than one
at most one is a class and the rest can be interface
_ use ampersands (&) to be seperator
T -&amp;lgt; descendant of class B
implement interface C,D
public class x<T extends B&C&D&lgt;

Monday, March 5, 2007

Lecture 20

Menus

  • Important classes are

    • JMenuBar

    • JMenu

    • JMenuItem

  • all three of these are children of Component

  • all implement the add() method



Creating a Menu

  1. Create a Menubar

    • JMenuBar myMenuBar = new JMenuBar();

  2. Create menus and add them to the menubar

    • JMenu = new JMenu(“File”);

    • MyMenuBar.add(myMenu);

  3. Create and add menu items to the menu

    • JMenuItem Save = new JMenuItem(“Save”);

    • MyMenu.add(Save);

  4. Add action listener to each menu item

    • Save.addActionListener(this);

  5. Add menuBar to the JFrame

    • add (myMenuBar);//Bad!!

    • Set JMenuBar(myMenuBar); // Good!!





Windows and Inner Classes



  • For Complicated window components it makes sense to use inner classes

Recall:

Public class OuterClass;

{

Private class InnerClass

{

}

}

  • A complicated panel within a JFrame

Public class myJframe extends JFrame

{

Public myJPanel()

{//appearance of the panel;}

}

Public JFrame()

{

Add ( new myJPanel());

}

Private class myJPanel extends JPanel implements ActionListener

{

Public myJPanel()

{

JButton b = new JButton(“Button”);

b.addActionListener(this); /* this refers to myJPanel*/

add(b);

}

Public void ActionPerformed(ActionEvent e){…}



Text Field

  • JTextField

    • Constructor takes an integer size argument

    • JTextField t = newJTextField(20); /* 20 chars wide*/

      • public String getText()

      • public void setText(String S)

      • public void setEditable(boolean e)