$r5sj64f

15
1 The Player Program with Notes Figure 1: The first step is to create the environment for your project. Figure 2: Once the top of the "tree" is created, then we do a right mouse click for creating Java files. Figure 3: Screen shot of creating a Java class file.

Upload: michaelkattan

Post on 26-Sep-2015

227 views

Category:

Documents


2 download

DESCRIPTION

1

TRANSCRIPT

1

The Player Program with Notes

Figure 1: The first step is to create the environment for your project.

Figure 2: Once the top of the "tree" is created, then we do a right mouse click for creating Java files.

Figure 3: Screen shot of creating a Java class file.

In time, you will create several Java class files. Look at the following:

Figure 4: The "tree" structure.In class, we built these files in an order that went from player object to the player node to the linked list. We also created the supporting background structure. Then we worked on the GUI.Of course, for the GUI, we used the JFrame menu.

If your code looks messy, hold down the Alt key, then the Shift key, and then the F key.

package my.playerlinkedlist;

/** * * @author Fred L. Strickland */public class DataStructureException extends java.util.NoSuchElementException {

public DataStructureException(String s) { super(s); }}

package my.playerlinkedlist;

/** * * @author Fred L. Strickland */public class Player {

private int id; // You may see a light blub on this line. You may ignore it. private String name;I messed you. You know how Mondays can be. Notice that the key word this is on the left side. Please correct your code.

private String game;

public Player(int id, String name, String game) { this.id = id; this.name = name; this.game = game; }

public int getID() { return id; }

public String getName() { return name; }

public String getGame() { return game; }

public void setID(int id) { this.id = id; }

public void setName(String name) { this.name = name; }

public void setGame(String game) { this.game = game; }

public boolean equals(Object p) { if (p instanceof Player) { Player objPlayer = (Player) p; return (id == objPlayer.id && name.equals(objPlayer.name) && game.equals(objPlayer.game)); } else { return false; } }

@Override public String toString() { // You may see opposite the header a light blub with a downward // pointing arrow. It is linked with the yellow line under "toString." // NetBeans is telling you that you are overriding (over writing) the // built in toString() method.When I had a problem with my code, I started adding a bunch of statements. Notice all of these are Sytem.out.println entries. I use this approach to debug my code. After I did this, I realized that the object was not being seen. Look on the next page for the source of my error.

System.out.println("I am in the toString method."); return ("id: " + id + "\tname: " + name + "\tgame: " + game + "\n"); }

}

package my.playerlinkedlist;

/** * * @author Fred L. StricklandI had mentioned in class that a good Java programmer would use words not letters for variables. I commented that I did not like what the author did. He used letters for variables.As a result, I missed the letter p in the first line of the insert method. As a result, the player was never added to the player node and that is why the code did not work.

*/public class PlayerLinkedList extends ShellLinkedList {

public PlayerLinkedList() { super(); }

public void insert(Player player) { PlayerNode pn = new PlayerNode(player); System.out.println("Inside the insert method"); pn.setNext(head); head = pn; numberOfItems++; }

public Player delete(int searchID) throws DataStructureException { PlayerNode current = head; PlayerNode previous = null;

while (current != null && current.getPlayer().getID() != searchID) { previous = current; current = current.getNext(); }

if (current == null) { throw new DataStructureException(searchID + " not found. " + " Cannot delete the ID. \n"); } else { if (current == head) { head = head.getNext(); // Delete the head. } else { previous.setNext(current.getNext()); }

numberOfItems--; return current.getPlayer(); }

}

public Player peek(int searchID) throws DataStructureException { PlayerNode current = head; while (current != null && current.getPlayer().getID() != searchID) { current = current.getNext(); } if (current == null) // Not found { throw new DataStructureException(searchID + " not found; cannot be delete. \n"); } else { return current.getPlayer(); } }

}

package my.playerlinkedlist;

/** * * @author fstrickland */public class PlayerNode { private Player player; private PlayerNode next; public PlayerNode () { player = null; next = null; } This is how the author of the book that I am using wrote his code. Do you see how hard it is to follow? Now you know why Java programmers tend to use words for a variable.My approach would be to replace the variable p with the word player.

public PlayerNode (Player p) { setPlayer ( p ); next = null; } public Player getPlayer () { System.out.println ("I am in the getPlayer method."); return player; } public PlayerNode getNext () { return next; } public void setPlayer (Player p) { player = p; } public void setNext (PlayerNode pn) { next = pn; } }

package my.playerlinkedlist;

/** * * @author fstrickland */public abstract class ShellLinkedList { protected PlayerNode head; protected int numberOfItems; public ShellLinkedList () { head = null; numberOfItems = 0; System.out.println ("Inside the ShellLinkedList constructor."); } public int getNumberOfItems () { return numberOfItems; } public boolean isEmpty() { return (numberOfItems == 0); }

public String toString () { System.out.println (Inside the toString method.); String listString = ""; PlayerNode current = head; while (current != null) { listString += current.getPlayer().toString() + "\n"; // I forgot to correct this error. current = current.getNext(); } return listString; }}

private void afterButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String name; The first four lines could have been written as follows:String name = nameTextField.getText();String game = gameTextField.getText();Which approach do you like better?

String game; name = nameTextField.getText(); game = gameTextField.getText(); ID_Number++; players.insert(new Player(ID_Number, name, game)); jTextArea1.append("\nNumber of players in the list: " + players.getNumberOfItems() + "\n" + players.toString()); }

private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String entry = removeTextField.getText(); int intEntry = 0; Player temp;

try { intEntry = Integer.parseInt(entry); jTextArea1.append("You have provided an ID number of " + entry + ".\n"); try { temp = players.delete(intEntry); jTextArea1.append ("Player deleted: " + temp + "\n"); } catch (DataStructureException dataStructureExceptionMessage) { jTextArea1.append(dataStructureExceptionMessage.getMessage() + "\n"); }

} catch (NumberFormatException nfe) { jTextArea1.append("You have provided a player's name of " + entry + ".\n"); } }

Add before the other variables:// Local defined variables. int ID_Number = 0; PlayerLinkedList players = new PlayerLinkedList (); PlayerLinkedList players;

In class, we worked just on the add button. (Yes, in class I called the first button to be the after button. It really does not matter as long as you understand what you are doing.) Now look at the whole system of Java files. How would a delete travel through the files?YOUR HOMEWORK ASSIGNMENT: Code up the delete serial or ID number approach. Code up the rest of the program.