Tuesday, November 30, 2010

Lab 22 -- Some More Programs

If you are done with the execution and understanding of the LinkedList Program, try to pick any problem from this list of topcoder problems and implement it.

MagicSpell was already implemented in lecture. You can see answer under the "Sample Programs" link, as usual.

Lab 22 - LinkedList.java -- with Generics -- Fill in the Function Bodies


1) Remove the quotes. I had to put in order to get the E to print.
2) First Refer to the Book to Fill in the Similar Functions and then try yourself the non-similar ones 

import java.util.ArrayList;


public class LinkedList<"E extends Comparable"<"E">>

{

 private ListNode<"E"> head;


 public LinkedList()

 {

  head = null;

 }


 public void showList()

 {

 
 }


 public int length()

 {

 
 }


 public void addNodeToStart(E addData)
 {
 
 }

 public void deleteHeadNode()
 {


 }

 public boolean onList(E target)
 {
 
 }

 public ListNode<"E"> contains(E target)
 {
 

 }

 public void addNodeAfterN(E theData, int n)
 {
 

 }

 public void deleteAfterNodeN(int n)
 {
 

 }

 public ArrayList<"E"> toArrayList()
 {
 
 
 }

}

Lab 22 -- Driver.java -- Use this to Test


1) Remove the quotes round String and Record.

public class Driver {

 
 public static void main(String[] args) 
 {
  LinkedList<"String"> stringList = new LinkedList<"String">();
  stringList.addNodeToStart("Hello");
  stringList.addNodeToStart("Good-Bye");
  stringList.showList();
  
  System.out.println("Is 'Hello' present: " + stringList.onList("Hello"));
  System.out.println("Is 'Meeee' present: " + stringList.onList("Meeee"));
  
  System.out.println("Item Added:");
  stringList.addNodeAfterN("That Me First Time",1);
  stringList.showList();
  
  System.out.println("Item Added:");
  stringList.addNodeAfterN("That Me Second Time",2);
  stringList.showList();
  
  System.out.println("Item Added:");
  stringList.addNodeAfterN("That Me 3rd Time",4);
  stringList.showList();
  
  System.out.println("Item Deleted:");
  stringList.deleteAfterNodeN(3);
  stringList.showList();
  
  System.out.println("Item Deleted:");
  stringList.deleteAfterNodeN(3);
  stringList.showList();
  
  /* *********************************************** */
  
  System.out.println(" ************************************************* ");
  
  LinkedList<"Record"> RecordList = new LinkedList<"Record">();
  Record TomRecord = new Record("Tom", 12);
  RecordList.addNodeToStart(TomRecord);
  
  Record HarryRecord = new Record("Harry", 23);
  RecordList.addNodeToStart(HarryRecord);
  RecordList.showList();
  
  Record MyRecord = new Record("Me", 14);
  System.out.println("Is 'TomRecord' present: " + RecordList.onList(TomRecord));
  System.out.println("Is 'MyRecord' present: " + RecordList.onList(MyRecord));
  
  Record Person1 = new Record("Person1", 12);
  System.out.println("Item Added:");
  RecordList.addNodeAfterN(Person1,1);
  RecordList.showList();
  
  Record Person2 = new Record("Person2", 30);
  System.out.println("Item Added:");
  RecordList.addNodeAfterN(Person2,2);
  RecordList.showList();
  
  Record Person3 = new Record("Person3", 33);
  System.out.println("Item Added:");
  RecordList.addNodeAfterN(Person3,4);
  RecordList.showList();
  
  System.out.println("Item Deleted:");
  RecordList.deleteAfterNodeN(3);
  RecordList.showList();
  
  System.out.println("Item Deleted:");
  RecordList.deleteAfterNodeN(3);
  RecordList.showList();
  
  
 }

}

Lab 22 -- ListNode


1) Remove quotes around E.

public class ListNode<"E"> 
{
 private E data;
 private ListNode<"E"> link;
 
 public ListNode()
 {
  link = null;
  data = null;
 }
 
 public ListNode(E newData, ListNode linkValue)
 {
  data = newData;
  link = linkValue;
 }

 public E getData() {
  return data;
 }

 public void setData(E data) {
  this.data = data;
 }

 public ListNode<"E"> getLink() {
  return link;
 }

 public void setLink(ListNode<"E"> link) {
  this.link = link;
 }
 
 
}

Lab 22 - Record Class


1) Remove quotes around Record

public class Record implements Comparable<"Record">
{
 private String name;
 private int age;
 
 public Record()
 {
  this.setName("");
  this.setAge(0);
 }
 
 public Record(String n, int a)
 {
  this.setName(n);
  this.setAge(a);
 }

 public String getName() {
  return name;
 }

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

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }
 
 public int compareTo(Record that)
   {
     int returnValue;

     returnValue = 0;

     if(this.getName().compareTo(that.getName()) < 0)
     {
       returnValue = -1;
     }
     else if(this.getName().compareTo(that.getName()) > 0)
     {
       returnValue = +1;
     }
     else
     {
      if(this.getAge() < that.getAge())
       returnValue = -1;
      else if(this.getAge() > that.getAge())
       returnValue = +1;
      else
       returnValue = 0;
     }

     return returnValue;
   } 
 
 public String toString()
 {
  String s;
  s = String.format("Name: %s , Age: %d" , this.getName(), this.getAge());
  return s;
 }
 
 
}

Lab 22 - LinkedList with Generics -- Output


Good-Bye
Hello
Is 'Hello' present: true
Is 'Meeee' present: false
Item Added:
Good-Bye
That Me First Time
Hello
Item Added:
Good-Bye
That Me First Time
That Me Second Time
Hello
Item Added:
Good-Bye
That Me First Time
That Me Second Time
Hello
That Me 3rd Time
Item Deleted:
Good-Bye
That Me First Time
That Me Second Time
That Me 3rd Time
Item Deleted:
Good-Bye
That Me First Time
That Me Second Time
 ******************************************************** 
Name: Harry , Age: 23
Name: Tom , Age: 12
Is 'TomRecord' present: true
Is 'MyRecord' present: false
Item Added:
Name: Harry , Age: 23
Name: Person1 , Age: 12
Name: Tom , Age: 12
Item Added:
Name: Harry , Age: 23
Name: Person1 , Age: 12
Name: Person2 , Age: 30
Name: Tom , Age: 12
Item Added:
Name: Harry , Age: 23
Name: Person1 , Age: 12
Name: Person2 , Age: 30
Name: Tom , Age: 12
Name: Person3 , Age: 33
Item Deleted:
Name: Harry , Age: 23
Name: Person1 , Age: 12
Name: Person2 , Age: 30
Name: Person3 , Age: 33
Item Deleted:
Name: Harry , Age: 23
Name: Person1 , Age: 12
Name: Person2 , Age: 30

Test 3 Averages

The averages for test 3 are shown below. As always, we will discuss about this and the final in class.

Tuesday, November 23, 2010

Lab test 3 - send your program to these email ids

jmvidal@gmail.com, shamik.usc@gmail.com, humayun.k1@gmail.com

Lab Test 3 - section 6 - To do list

INPUT FILE FORMAT
(1) First Line : 10 -- Number of Items to be added to an ArrayList
(2) 10 Lines follow -- each line corresponds to data of an Item
Item Data Format :
--------------------------
Name;Color;Size;Price
(3) Line 12 : 3 -- Number of Items to search in ArrayList
(4) 3 Items' Name, Color and Size follows
----------------------------------------------------------------
Things To Do:
--------------------
(a) Read the data from the Input File
(b) Store information about each item in an ArrayList
(c) Read the names, colors and sizes from the file which are to be searched
(d) If items are there in the list corresponding to each Name,Color, and Size, then display their average price

Lab Test 3 - Section 6 - Input File

10
Shirt;blue;S;$40
Shirt;yellow;M;$50
Pant;black;M;$100
Shirt;blue;S;$60
Pant;grey;L;$80
Jacket;black;M;$50
Pant;grey;S;$60
Pant;black;M;$110
Shirt;blue;S;$80
T-Shirt;red;S;$60
3
Shirt;blue;S
Pant;black;M
T-Shirt;green;M