Radford University, ITEC 224, Fall 2003

Programming Assignment 3

Due Date: Midnight, Tuesday, November 25, 2003


Problem Specification (100 points)

  • Implement the class represented by the UML diagram show below.
    • In your class, every method except isEmpty() must be recursive (or use a recursive helper function).

TreePkg

- Root: TreeNode
+ IN_ORDER: int
+ PRE_ORDER: int
+ POST_ORDER: int

+ isEmpty(): Boolean
+ isInTree(name:String): Boolean


+ add(name, tel, email: String)
+ phoneNumberByName(name: String): String
+ emailByName(name: String): String
+ del(name: String)

+ toString(order: int): String
+ print(order: int)

+ numberOfNodes(): int
+ heightOfTree(): int
+ level(name: String): int

+ minName(): String
+ maxName(): String

- findParent(n: TreeNode): TreeNode

    • Method isInTree(name: String) returns true if the name is in the tree and false otherwise.  The values of the phone number and email do not affect the value returned by the method.
    • Precondition for add: name is not in the tree.
    • Condition for del: if name is not in the tree, the routine should have no visible effect.
    • Precondition for level: name is in the tree.  Example: level should return 0 when passed the name that is in the root of the tree.
    • Precondition for findParent: n is in the tree.
    • For phoneNumberByName and emailByName, if the name is not in the tree, return "no matching data".
    • You may not add any fields to the tree.  You may add other private methods.
    • Any of the methods can call recursive helper functions.
    • Method print cannot call toString; it must either be recursive or call a recursive helper that does the printing.
  • Class TreePkg should have an internal class TreeNode defined as follows:

 

private TreeNode
private class TreeNode
{
   private String name;
   private String tel;
   private String email;
   private TreeNode left, right;
   private TreeNode(String name, String tel, String email)
   {
      ...
   }
   public String getName(){...}
   public String getTel(){...}
   public String getEmail(){...}
   public String void setName(...){...}
   public String void setTel(...){...}
   public String void setEmail(...){...}
   public TreeNode getLeft(){...}
   public TreeNode getRight(){...}
   public TreeNode void setLeft(TreeNode n){...}
   public TreeNode void setRight(TreeNode n){...}
}
  • Driver program
    • You should create a driver program to test your TreePkg class.  This program should read from standard input and should implement the following commands:
      • is = isEmpty
      • a = add
      • pn = phoneNumberByName
      • en = emailByName
      • d = del
      • toIn = toString(TreePkg.IN_ORDER)
      • toPre = toString(TreePkg.PRE_ORDER)
      • toPost = toString(TreePkg.POST_ORDER)
      • pIn =print(TreePkg.IN_ORDER)
      • pPre =print(TreePkg.PRE_ORDER)
      • pPost = print(TreePkg.POST_ORDER)
      • n = numberOfNodes
      • h = heightOfTree
      • l = level
      • min = minName
      • max = maxName
      • q = quit
    • Methods should prompt for parameters, as needed and as defined in the previous assignment
    • Call this program treeDriver.java.
    • As an alternative, you may creat an GUI driver. If you do this, call your driver GUIDriver.java.
  • Extra credit: (15 points)
    • Save the data in the tree in a file to retain the data between runs of your program.  The driver program can read this file, if it exists, and restore the data when the driver begins execution.  You should call your data file data.dat.

What to submit

  • Source code ...  // please include javadoc as a comment in your source code
  • All the softcopy should be submitted at

 \\neelix\dropbox\ITEC\ITEC224\hlee3\Submissions\ITEC224-02-fall\<your_RU_id>\RU03

 

Reference: The idea behind part of this assignment came from ECE 242 at the University of Massachusetts.