Results 1 to 12 of 12
  1. #1
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default Need help accessing data in LinkedList

    I am having trouble figuring out how to access the data that is stored inside a LinkedList. I have set the LinkedList to contain objects of type Box and Letter. These two classes extend to the super class Package. The Box constructor is made up of tracking number, weight, length, width, and height. The letter constructor is made up of tracking number, weight, length and width. What I need to do is make a getter to access all the things that make up the Box and Letter objects that i can access inside another class Truck. The whole purpose of this is to simulate the activities of a delivery truck transporting packages from one city to another. Please note that the load method in the Truck class is incomplete because I don't know how to access the data within the LinkedList. Below is the code. This is for an assignment that is VERY late so help is much appreciated...

    LinkedList class
    Java Code:
    @SuppressWarnings({ "rawtypes" })
    
    public class LinkedList
    {
       protected static class Node
       {
          protected Comparable data;
          protected Node next;
    
          protected Node ( Comparable data )
          {
             this.data = data;
             this.next = null;
          }// end constructor
    
          protected Node ( Comparable data, Node next )
          {
             this.data = data;
             this.next = next;
          }// end constructor
    
          public void setData ( Comparable data )
          {  
    			this.data = data;
    		}// end setData
    
          public void setNext ( Node next )
          { 
    			this.next = next;
    		}// end setNext
    
          public Comparable getData ()
          {
    			return this.data;
    		}// end getData
    
          public Node getNext ()
          {
    			return this.next;
    		}// end getNext
          
    		
       }// end Node class
    
       protected Node head = null;
       
       public void add(Comparable string)
       {
    	   Node newNode = new Node(string);
    	   if (size == 0)
    	   {
    		   head = newNode;
    	   }
    	   else
    	   {
    		   Node curr = head;
    		   while (curr.next != null)
    		   {
    			   curr = curr.next;
    		   }
    		   curr.next = newNode;
    	   }
    	   size++;
       }
    }// end LinkedList class
    Package, Box, and Letter classes
    Java Code:
    public class Package implements Comparable <Package>
    {
    	String tracking, weight;
    	
    	public Package(String tracking, String weight)
    	{
    		this.tracking = tracking;
    		this.weight = weight;
    	}
    	
    	public int getTracking() {
    		return Integer.parseInt(tracking);
    	}
    
    	public int getWeight() {
    		return Integer.parseInt(weight);
    	}
    
    	@Override
    	public int compareTo(Package arg0) 
    	{
    		// TODO Auto-generated method stub
    		return 0;
    	}
    }
    
    
    public class Letter extends Package
    {
    	private String length, width, type;
    	
    	public Letter(String tracking, String weight, String length, String width)
    	{
    		super(tracking, weight);
    		this.length = length;
    		this.width = width;
    		this.type = "Letter";
    	}
    
    }
    
    
    public class Box extends Package 
    {
    	private String length, width, height, type;
    
    	public Box(String tracking, String weight, String length, String width, String height) 
    	{
    		super(tracking, weight);
    		this.length = length;
    		this.width = width;
    		this.height = height;
    		this.type = "Box";
    	}
    
    }
    And finally the Truck class
    Java Code:
    //Try making the truck constructor just put it into a LinkedList and then when calling load method in truck class, try to load first and then cast the type
    
    import java.io.*;
    import java.util.Scanner;
    
    public class Truck
    {
    	String driver;
    	int maxPackages, boxWeight, letterWeight;
    	LinkedList packs = new LinkedList();
    	LinkedList loaded = new LinkedList();
    	Scanner fin;
    	
    	public Truck(String string)
    	{
    		try{
    			fin = new Scanner(new File(string));
    			if(fin.hasNextLine())
    			{
    				this.driver = fin.nextLine();
    				this.maxPackages = fin.nextInt();
    				//fin.nextLine();
    			}
    			while(fin.hasNextLine())
    			{
    				String tracking = fin.next();
    				fin.nextLine();
    				if(tracking.endsWith("0"))
    				{
    					//String tracking = fin.toString();
    					String weight = fin.nextLine();
    					String length = fin.nextLine();
    					String width = fin.nextLine();
    					Package n = new Letter(tracking, weight, length, width);
    					packs.add(n);
    				}
    				else if(tracking.endsWith("1"))
    				{
    					//String tracking = fin.nextLine();
    					String weight = fin.nextLine();
    					String length = fin.nextLine();
    					String width = fin.nextLine();
    					String height = fin.nextLine();
    					Package m = new Box(tracking, weight, length, width, height);
    					packs.add(m);
    				}
    				//packs.add(fin.nextLine());
    			}
    		}catch(IOException io){
    			io.printStackTrace();
    		}
    	}
    	
    	public void load() 
    	{
    		//System.out.println(packs.head.getData());
    		LinkedList temp = packs;
    		while(temp.head.data != null)
    		{
    			if(temp.head.)
    			{
    				//temp.head.data;
    				temp.head = temp.head.next;
    			}
    			else if(temp.head.getData().toString().contains("Box"))
    			{
    				System.out.println("Box");
    				temp.head = temp.head.next;
    			}
    		}
    		
    		
    		
    	}
    
     
    	
    }// end Truck class

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Need help accessing data in LinkedList

    How do you execute the code for testing? I don't see a main method.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default Re: Need help accessing data in LinkedList

    Thanks for the response! Main is just a simple tester that instantiates a truck object:

    Java Code:
    public static void main(String [] args)
    {
           Truck theTruck = new Truck("manifest.txt");
           theTruck.load();
           theTruck.drive(); //doesn't really do anything...
           theTruck.unload();
    }//end main

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Need help accessing data in LinkedList

    I get several compiler errors. Can you fix them so the code will execute?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default Re: Need help accessing data in LinkedList

    Please note that the load method doesn't work...

    Truck class
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Truck
    {
    	String driver;
    	int maxPackages, boxWeight, letterWeight;
    	LinkedList packs = new LinkedList();
    	LinkedList loaded = new LinkedList();
    	Scanner fin;
    	
    	public Truck(String string)
    	{
    		try{
    			fin = new Scanner(new File(string));
    			if(fin.hasNextLine())
    			{
    				this.driver = fin.nextLine();
    				this.maxPackages = fin.nextInt();
    				//fin.nextLine();
    			}
    			while(fin.hasNextLine())
    			{
    				String tracking = fin.next();
    				fin.nextLine();
    				if(tracking.endsWith("0"))
    				{
    					//String tracking = fin.toString();
    					String weight = fin.nextLine();
    					String length = fin.nextLine();
    					String width = fin.nextLine();
    					Package n = new Letter(tracking, weight, length, width);
    					packs.add(n);
    				}
    				else if(tracking.endsWith("1"))
    				{
    					//String tracking = fin.nextLine();
    					String weight = fin.nextLine();
    					String length = fin.nextLine();
    					String width = fin.nextLine();
    					String height = fin.nextLine();
    					Package m = new Box(tracking, weight, length, width, height);
    					packs.add(m);
    				}
    				//packs.add(fin.nextLine());
    			}
    		}catch(IOException io){
    			io.printStackTrace();
    		}
    	}
    	
    	public void load() 
    	{
    		//System.out.println(packs.head.getData());
    		LinkedList temp = packs;
    		while(temp.head.data != null)
    		{
    			if(temp.head.)
    			{
    				//temp.head.data;
    				temp.head = temp.head.next;
    			}
    			else if(temp.head.getData().toString().contains("Box"))
    			{
    				System.out.println("Box");
    				temp.head = temp.head.next;
    			}
    		}
    		
    		
    		
    	}
    
     
    	
    }// end Truck class
    Main
    Java Code:
    public class TruckTester 
    {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) 
    	{
    		Truck theTruck = new Truck("manifest.txt");
    		theTruck.load();
    		//theTruck.drive();
    		//theTruck.unload();
    
    	}// end main method
    
    }// end TruckTester class
    LinkedList
    Java Code:
    
    @SuppressWarnings({ "rawtypes" })
    
    public class LinkedList
    {
       protected static class Node
       {
          protected Comparable data;
          protected Node next;
    
          protected Node ( Comparable data )
          {
             this.data = data;
             this.next = null;
          }// end constructor
    
          protected Node ( Comparable data, Node next )
          {
             this.data = data;
             this.next = next;
          }// end constructor
    
          public void setData ( Comparable data )
          {  
    			this.data = data;
    		}// end setData
    
          public void setNext ( Node next )
          { 
    			this.next = next;
    		}// end setNext
    
          public Comparable getData ()
          {
    			return this.data;
    		}// end getData
    
          public Node getNext ()
          {
    			return this.next;
    		}// end getNext
          
    		
       }// end Node class
    
       protected Node head = null;
       protected int  size = 0;
    
       // Return the present size of the linked list
       
       public int size()
       {
    	   return size;
       }// end size
    
       public void addFirst(Comparable s)
       {
    	   Node newNode = new Node(s, head);
    	   head = newNode;
    	   size++;
       }
    
       public void addLast(Comparable node)
       {
    	   Node newNode = new Node(node);
    	   
    	   if(size == 0)
    	   {
    		   head = newNode;
    	   }
    	   else
    	   {
    		   Node curr = head;
    		   while(curr.next != null)
    		   {
    			   curr = curr.next;
    		   }
    		   curr.next = newNode;
    	   }
    	   size++;
       }
       
       public void add(Comparable string)
       {
    	   Node newNode = new Node(string);
    	   if (size == 0)
    	   {
    		   head = newNode;
    	   }
    	   else
    	   {
    		   Node curr = head;
    		   while (curr.next != null)
    		   {
    			   curr = curr.next;
    		   }
    		   curr.next = newNode;
    	   }
    	   size++;
       }
    
    /*
       public void clear()
       {
    	   head = null;
    	   size = 0;
       }
    	
       public String toString()
       {
    	   String s = "";
    	   if(head == null)
    	   {
    		   System.out.println("List is Empty!");
    	   }
    	   else
    	   {
    		   for(Node curr = head; curr != null; curr = curr.next)
    		   {
    			   s = s + curr.data + "\n";
    		   }
    	   }			
    	   return s;
       }
    	
       public void sort()
       {
    	   Node position;
    	   Node start;
    		
    	   if(size > 0)
    	   {
    		   for(position = head; position != null; position = position.next)
    		   {
    			   Node smallest = position;
    			   for(start = position.next; start != null; start = start.next)
    			   {
    				   if((Integer)start.data < (Integer)smallest.data)
    				   {
    					   smallest = start;
    				   }
    			   }
    			   Comparable temp = position.data;
    			   position.data = smallest.data;
    			   smallest.data = temp;
    		   }
    	   }
       }
    	
       public void reverse()
       {
    	   if(head == null)
    	   {
    		   System.out.println("The list is empty.");
    	   }
    	   reverse(head);
       }
    	
       public void reverse(Node node)
       {
    	   if(node != null)
    	   {
    		   reverse(node.next);
    		   System.out.println(node.data);
    	   }
    		
       }
    	
       public void listEven()
       {
    	   for(Node curr = head; curr != null; curr = curr.next)
    	   {
    		   if((Integer)curr.data % 2 == 0)
    		   {
    			   System.out.println(curr.data);
    		   }
    	   }
       }
    	
       public Node listEvery(int amt)
       {
    	   Node curr;
    		
    	   for(curr = head; curr.next != null; curr = curr.next)
    	   {
    			
    	   }
    	   return curr;
       }
    	
       public boolean removeN(int contains)
       {
    	   Node prev = null;
    	   Node curr = head;
    	   while(curr != null)
    	   {
    		   if(curr.data.equals(contains))
    		   {
    			   curr = curr.next;
    			   if(prev == null)
    				   prev = curr;
    			   else
    				   prev.next = curr;
    			   size--;
    			   return true;
    		   }
    		   else
    		   {
    			   prev = curr;
    			   curr = curr.next;
    		   }
    	   }
    	   return false;
       }
    	*/
    }// end LinkedList class
    Package
    Java Code:
    public class Package implements Comparable <Package>
    {
    	String tracking, weight;
    	
    	public Package(String tracking, String weight)
    	{
    		this.tracking = tracking;
    		this.weight = weight;
    	}
    	
    	public int getTracking() {
    		return Integer.parseInt(tracking);
    	}
    
    	public int getWeight() {
    		return Integer.parseInt(weight);
    	}
    
    	@Override
    	public int compareTo(Package arg0) 
    	{
    		// TODO Auto-generated method stub
    		return 0;
    	}
    	
    	public String getType()
    	{
    		return this.getType();
    	}
    
    }
    Box class
    Java Code:
    public class Box extends Package 
    {
    	private String length, width, height, type;
    
    	public Box(String tracking, String weight, String length, String width, String height) 
    	{
    		super(tracking, weight);
    		this.length = length;
    		this.width = width;
    		this.height = height;
    		this.type = "Box";
    	}
    
    }
    Letter
    Java Code:
    public class Letter extends Package
    {
    	private String length, width, type;
    	
    	public Letter(String tracking, String weight, String length, String width)
    	{
    		super(tracking, weight);
    		this.length = length;
    		this.width = width;
    		this.type = "Letter";
    	}
    
    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Need help accessing data in LinkedList

    Does this code compile for you? I get errors.

    Also will need the contents of the manifest.txt file.


    Can you explain what the problem is?
    Last edited by Norm; 08-02-2012 at 07:57 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default Re: Need help accessing data in LinkedList

    There is no problems with the code as far as I can tell, but I need to access the data in the linkedlist. I setup the linkedlist to contain objects of type package (some being box and some being letter). I just don't know how to access the data within the objects (tracking number, weight, length, width, height...)

    Thanks

    manifest.txt contents are:

    Chris "King of the Road" Peters
    3
    123450
    10
    12
    12
    5555551
    99
    36
    36
    36
    123890
    25
    10
    10
    656561
    50
    36
    36
    37
    3456541
    abc
    5
    5
    5
    879880
    1
    1
    1
    890980
    2
    2
    2

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Need help accessing data in LinkedList

    There is no problems with the code as far as I can tell
    How are you compiling the code? It will not compile for me.
    See line 58 in Truck:
    if(temp.head.)


    I just don't know how to access the data within the objects
    Use the classes's get... methods (or add some as needed)
    Last edited by Norm; 08-02-2012 at 08:56 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default Re: Need help accessing data in LinkedList

    Thats the load class that I was working on before I got stuck. As far as the getters, how would you write the get? Ive got the singly linkedlist that contains the package (box and letter) objects. Which class should I put the getters in? Box and Letter? or Package?

    Thanks again!

  10. #10
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default Re: Need help accessing data in LinkedList

    That sounded like I can't write the get methods. I can, but am just confused as to where to put them...

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Need help accessing data in LinkedList

    The Package class already has get methods for its data. Add the class specific getters to the other classes where you need to get their data.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default Re: Need help accessing data in LinkedList

    Seriously? Jeez, I feel like I should have known that. I guess I've been working on it too much. Fresh set of eyes helps! I'll try that and post my results.

    Thanks again for the help!

Similar Threads

  1. Accessing Data From Websites in Java
    By capitalistpig in forum New To Java
    Replies: 13
    Last Post: 06-21-2010, 10:11 PM
  2. JNI accessing non primitive data type
    By H_P in forum Advanced Java
    Replies: 1
    Last Post: 04-14-2010, 05:43 AM
  3. Accessing Data from a .txt file
    By Oasis13 in forum New To Java
    Replies: 5
    Last Post: 02-01-2008, 12:16 AM
  4. Packaging and accessing data files
    By todd in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •