Homework help: should be simple for you guys


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Homework help: should be simple for you guys

  1. #1
    Join Date
    May 2006
    Posts
    2

    Unhappy Homework help: should be simple for you guys

    I go to SDSU and am stuck on my homework for intro to Java (due tonight) and I missed a couple classes so problem solving and the book will make it take past my deadline to solve ..... what am I doing wrong? here is the code I am playing with and I am not sure if I am even on the right track ...... the homework part is noted with HW

    <a href="http://www-rohan.sdsu.edu/~masc0500/LECS108/s06hw5.txt"> here is the description of what I am trying to do</a>

    class Node
    {

    public Object data;
    public Node next;

    public Node()
    {
    this.data = null;
    this.next = null;
    }

    public Node(Object a)
    {
    this.data = a;
    this.next = null;
    }

    public Node(Object a, Node b)
    {
    this.data = a;
    this.next = b;
    }

    }



    public class List
    {
    private Node head;
    private Node tail;
    public List()
    {
    this.head = null;
    this.tail = null;
    }
    //-------------------------
    public void insertAtFront(Object obj)
    {
    if (this.size()==0)
    {
    Node n = new Node(obj);
    this.head = n;
    this.tail = n;
    }
    else
    {
    Node n = new Node(obj);
    n.next = this.head;
    this.head = n;
    }
    }
    //----------------------------
    public void insertAtBack(Object obj)
    {
    if (this.size()==0)
    {
    Node n = new Node(obj);
    this.head = n;
    this.tail = n;
    }
    else
    {
    Node n = new Node(obj);
    this.tail.next = n;
    this.tail = n;
    }
    }
    //------------------------------
    public Object removeFromFront()
    {
    if (this.size()==0)
    {
    System.out.println("No data to remove.");
    return null;
    }
    else if (this.size()==1)
    {
    Object a = this.head.data;
    this.head = null;
    this.tail = null;
    return a;
    }
    else
    {
    Object a = this.head.data;
    this.head = this.head.next;
    return a;
    }
    }



    //-----------------------------start HW ----------
    public void insertAtSorted(String s)
    {
    if (this.size()==0)
    {
    insertAtFront(s);
    return;
    }
    else if (s<=this.head)
    {
    insertAtFront(s);
    return;
    }
    else if (s>=this.tail) // how to make so this works?
    {
    insertAtBack(s);
    return;
    }
    else
    {
    Node ntlnode = this.head;
    while (ntlnode.next <= s)
    {
    ntlnode = ntlnode.next;
    Node blah = new Node(s);
    blah.next = ntlnode;
    this.next = blah;
    }
    }
    }
    //----------------------------- end HW -----------



    public Object removeFromBack()
    {
    if (this.size()==0)
    {
    System.out.println("No data to remove.");
    return null;
    }
    else if (this.size()==1)
    {
    Object a = this.tail.data;
    this.head = null;
    this.tail = null;
    return a;
    }
    else
    {
    Object a = this.tail.data;
    Node ntlnode = this.head;
    while (ntlnode.next != this.tail)
    {
    ntlnode = ntlnode.next;
    }
    this.tail = ntlnode;
    this.tail.next = null;
    return a;
    }
    }
    //-------------------------
    public boolean isEmpty()
    {
    return this.head == null;
    }
    //-------------------------
    public void print()
    {
    if (this.size()==0)
    {
    System.out.println("Empty list!");
    return;
    }
    Node curr = this.head;
    while (curr != null)
    {
    System.out.println(curr.data.toString());
    curr = curr.next; //move forward to the next data item.
    }
    System.out.println();
    }
    //-------------------------
    public int size()
    {
    int count = 0;
    Node curr = this.head;
    while (curr != null)
    {
    count++;
    curr = curr.next;
    }
    return count;
    }
    //-------------------------
    } //end List class

  2. #2
    Join Date
    May 2006
    Posts
    2
    this is what my ultimate goal is in the HW section

    public void insertAtSorted(String s)
    {

    if list is currently empty, then issue an insertAtFront(s),
    and then exit this routine immediately.
    (CS107 reminder: Java's "return;" command will end a subroutine.)

    if s's value <= the head node's value,
    then just do an insertAtFront(s),
    and then exit this routine immediately.

    (IMPORTANT HINT: use the data-type conversion functions
    we discussed in detail, earlier in the semester!
    Also, you may need to "cast" to a String, when you retrieve
    the head node's data value, because the Node class's .data property
    is declared as data type Object.)


    if s's value is >= the tail node's value,
    then just do an insertAtBack(s),
    and then exit this routine immediately.


    Observation: code execution can only get to this location, here,
    if s needs to be inserted somewhere in the middle of the list.

    So, therefore, you should now "roller coaster" through the list
    (using the roller coaster techniques used in the List class's "print" routine
    or "size" routine or "removeFromBack" routine). However, you should halt
    the roller-coastering as soon as (or right before) you hit a node whose value
    is >= s's value. Then, using Java references techniques (discussed earlier
    in the semester), you should create a new node having s as its "data",
    and should manually insert this new node right before the node that halted you.

    }

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    to do your comparison, you must use the String class's compareTo method. So if
    Code:
    {
         if( s.compareTo(head.data) <= 0 ) // returns neg val if s is "less than", 0 if equal
         {
             this.insertFront( s );
         }
         else if ( s.compareTo(tail.data) >= 0 ) // returns pos val if s is "greater than"
         {
             this.insertTail( s );
         }
         else 
         {
             this.insertSearch( s );
         }
    }

Similar Threads

  1. Simple tic tac toe applet
    By Ant_Magma in forum Java
    Replies: 5
    Last Post: 09-26-2005, 04:41 AM
  2. Guys can you help me out with the VB coding
    By BAW264 in forum VB Classic
    Replies: 3
    Last Post: 05-09-2005, 10:20 PM
  3. Creating a Simple Combo Box in a Web App
    By Ranchoz71 in forum ASP.NET
    Replies: 3
    Last Post: 04-06-2005, 01:40 PM
  4. Simple VB 6.0 XML parsing
    By slimasian in forum VB Classic
    Replies: 1
    Last Post: 03-02-2005, 02:38 PM
  5. Simple Java DOM question
    By Lenin in forum XML
    Replies: 0
    Last Post: 03-21-2001, 05:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center