Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Operations with lists

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Operations with lists

    Hello.

    I'm new here and to programming in Java. I'm starting to have classes now and I need help with a project. What I'm asked to do is the following:

    - The user is asked to write a sentence;
    - From that sentence, each word needs to be placed in a node of a chained list (i don't know if this is the correct term);
    - Then, I need a new list with the sentence from the list before but with the words in a reverse order (ex: I am noob -> noob am I);
    - Next, i need to create an ordered bidirectional list that must contain just the words that are not repeated on the initial phrase;
    - Eliminate all the occurrences of a given word from the 1st list;
    - Eliminate form the bidirectional all the words between x and y, being x and y words inputted by the user;
    - Move a set of nodes from the 1st list (like cut and paste);
    - Copy a set of nodes from the 1st list (like copy and paste);
    - And finally print the lists.

    I realize that maybe this is too much but I really need help because I really don't know even where to start. I think this exercise is really too much for my level... I only have the menu.

    Hope someone can help me.

    Thank you in advance.

    Regards.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Operations with lists

    Please post what you have so far along with a more detailed question. Simply posting the homework assignment with no indication of where you are, what your questions are, the effort made to solve this problem, etc...does not provide any information for anyone to help aside from posting links to general tutorials, such as these:
    How To Ask Questions The Smart Way
    The Java™ Tutorials

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Operations with lists

    Hello and thank you for your answer.

    That's just it: I don't know where to start. To be sure, I'm not looking for someone to put here the code, I just wanted someone to point me in the right direction.

    So far, I have only one class: the menu one with only that, the menu. But I can post it here if that's what you want:

    PHP Code:

    import javax
    .swing.JOptionPane;

    public class 
    menu {
        public static 
    int options() {
            
    int op;
            do {
                
    String a "Menu"
                        
    "\n1 - write a sentence"
                        
    "\n2 - words in a reverse order"
                        
    "\n3 - create an ordered bidirectional list with words that are not repeated"
                        
    "\n4 - delete word"
                        
    "\n5 - delete word between x and y"
                        
    "\n6 - Move nodes"
                        
    "\n7 - Copy nodes"
                        
    "\n8 - print lists"
                        
    "\n0 - exit";
                
    op Integer.parseInt(JOptionPane.showInputDialog("\nOption? (0 to 8)"));
            }
                while ((
    op 0) || (op 8));
                return 
    op;
        } 
    That's it. What do I need to do next? Create a class for the 3 lists or a separate for each?

    Once again, thank you for your answer.

    Regards.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Operations with lists

    Sorry, but where in your requirements does it say to create a GUI? Are you sure you're not just supposed to do all of this on the command line?

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Operations with lists

    Actually, it doesn't. I just assumed it would be easier to do it like that. Am I wrong?

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Operations with lists

    Ok, after some documentation checking, I've come up with this. Can someone take a look if I'm on the right track? How can I see if both lists are ok?

    PHP Code:

    import javax
    .swing.JOptionPane;

    public class 
    menu {
        public static 
    int options() {
            
    int op;
            do {
                
    String a "Menu"
                        
    "\n1 - write a sentence"
                        
    "\n2 - words in a reverse order"
                        
    "\n3 - create an ordered bidirectional list with words that are not repeated"
                        
    "\n4 - delete word"
                        
    "\n5 - delete word between x and y"
                        
    "\n6 - Move nodes"
                        
    "\n7 - Copy nodes"
                        
    "\n8 - print lists"
                        
    "\n0 - exit";
                
    op Integer.parseInt(JOptionPane.showInputDialog("\nOption? (0 to 8)"));
            }
                while ((
    op 0) || (op 8));
                return 
    op;
        }  

       public static 
    void main(String[] args) {
            list 
    = new list();
            list 
    = new list();
            
    int option opcoes();
            while (
    option != 0) {
                
    String sentence;
                switch (
    option) {
                case 
    1: {
                    do
                        
    sentence JOptionPane.showInputDialog("Input sentence: ");
                    while (
    sentence.compareTo("") == 0);
                    
    A.receivesentence(sentence);
                    break;
                }
                
                case 
    2: {
                    do
                        
    sentence JOptionPane.showInputDialog("Input sentence: ");
                    while (
    sentence.compareTo("") == 0);
                    
    B.receivesentence2(sentence);
                    break;
                }
                
                case 
    3: {
                   
                    break;
                }
    }
            }
                
        }

    PHP Code:
    import java.util.*;

    public class list {
        
            private 
    node head;
            
            
            public 
    void receivesentence(String sentence){
                
                
    String [] words sentence.split(" ");
                for (
    int i 0words.length-1i++){
                    
    this.insertordered(words[i]);
                }
                    
            }
            
            
            public 
    void receivesentence2(String sentence){
                
                
    String [] words sentence.split(" ");
                for (
    int i 0words.length-1i++){
                    
    this.insertinverted(words[i]);
                }
                    
            }
            
            public 
    void insertordered(String word){
                
                
    node new=new node(word);
                
                
    node temp=head;
                
                    while((
    temp.getnext()!=null)&&(temp.getnext().getword().compareTo(new.getword())<0)){
                        
    temp=temp.getnext();            
                    }
                    
                    new.
    setnext(temp.getnext());
                    new.
    setprev(temp);
                    
                    
    temp.setnext(new);
                        
            }
        
            public 
    void insertinverted(String word){
                
                
    node new=new node(word);
                
                
    node temp=head;
                
                    while((
    temp.getnext()!=null)&&(temp.getnext().getword().compareTo(new.getword())>0)){
                        
    temp=temp.getnext();            
                    }
                    
                    new.
    setnext(temp.getnext());
                    new.
    setprev(temp);
                    
                    
    temp.setnext(new);
                        
            }    
            
        
            public 
    String print()
            {
                
    String st "";
                if (
    head.getnext() != head)
                {
                    
    node temp head.getnext();
                    while(
    temp != head)
                    {
                        
    st += temp.getword() + " ";
                        
    temp temp.getnext();
                    }
                }
                else
                    
    st += "The list is empty!";
                
                return 
    st;
            }

    PHP Code:
    public class node {
        
        private 
    String word;
        private 
    node next;
        private 
    node prev;
        
        public 
    node(String word){
            
    this.word=word;
        }
            
        public 
    String getword() {
            return 
    word;
        }
        public 
    void setword(String word) {
            
    this.word word;
        }
        public 
    node getnext() {
            return 
    next;
        }
        public 
    void setnext(node next) {
            
    this.next next;
        }
        public 
    node getprev() {
            return 
    prev;
        }
        public 
    void setprev(node prev) {
            
    this.prev prev;
        }
        
        


  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Operations with lists

    Good morning.

    Can someone help me with this one? It's due today...

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Operations with lists

    If your requirements don't say to create a GUI, I would think that a GUI is overkill. Not sure whether you'd get points off or not.

    Anyway, I'd be happy to help if you posted an SSCCE and described what's happening vs what you think should be happening.

  9. #9
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Operations with lists

    It doesn't say anywhere but it's pretty much common practice. Ok, thank you for your answer.
    So, now it's placing the sentence inside the A list but not on the B list. And, for some reason, it's not placing on the correct order on both of them.

    I have the list class like this, now:

    PHP Code:
    package pacote;

    public class list {

        private 
    node cab;

        public list()

        {
            
    cab = new node("");
            
    cab.setprev(cab);
            
    cab.setnext(cab);
        }


        public 
    void receiveinsert(String insert){

            
    String [] words insert.split(" ");
            for (
    int i 0words.lengthi++){
                
    this.insertordered(words[i]);
            }

        }

        public 
    void receiveinsert2(String insert){

            
    String [] words insert.split(" ");
            for (
    int i 0words.lengthi++){
            
    this.insertinverted(words[i]);

            }
        }

        public 
    void insertordered(String word){

            
    node newnode=new node(word);
            
    node temp=cab.getnext();

            while(
    temp.getnext()!=cab &&(temp.getnext().getword().compareTo(newnode.getword())<0)){
                
    temp=temp.getnext(); 
            }

            
    newnode.setnext(temp.getnext());
            
    newnode.setprev(temp);
            
    temp.setnext(newnode);

        }



        public 
    void insertinverted(String word){

            
    node newnode=new node(word);
            
    node temp=cab.getnext();

            while(
    temp.getnext()!=cab &&(temp.getnext().getword().compareTo(newnode.getword())>0)){
                
    temp=temp.getnext(); 
            }


            
    newnode.setnext(temp.getnext());
            
    newnode.setprev(temp);
            
    temp.setnext(newnode);

        } 


        public 
    String printordered()

        {
            
    String st "";
            
    node temp cab.getnext();

            if (
    temp!= cab)

            {
                while(
    temp != cab)
                {
                    
    st += temp.getword() + " ";
                    
    temp temp.getnext();
                }

            }

            else
                
    st += "The list is empty!";

            return 
    st;

        }
        
        public 
    String printinverted()

        {
            
    String st "";
            
    node temp cab.getprev();

            if (
    temp!= cab)

            {
                while(
    temp != cab)
                {
                    
    st += temp.getword() + " ";
                    
    temp temp.getprev();
                }

            }

            else
                
    st += "The list is empty!";

            return 
    st;

        }    


    And I also had to change the menu class:

    PHP Code:
    public static void main(String[] args) {

            list 
    = new list();
            list 
    = new list();
            
    int option options();

            while (
    option != 0) {
                
    String sentence;

                switch (
    option) {

                case 
    1
                {
                    
    sentence JOptionPane.showInputDialog("Input sentence: ");
                    
    A.receivesentence(sentence);
                    
    B.receivesentence2(sentence);
                    break;
                }

                case 
    2
                {
     
    //               sentence = JOptionPane.showInputDialog("Input sentence to reverse: ");
     //               B.receivesentence2(sentence);
                    
    break;
                }

                case 
    3: {
                    
                    break;
                }

                case 
    8:

                {
                    
    JOptionPane.showMessageDialog(null,A.printordered()); 
                    
    JOptionPane.showMessageDialog(null,B.printinverted()); 
                    break;
                }


               }

                
    option options();
            }

        }

    Last edited by datreta; October 29th, 2010 at 09:36 AM. Reason: Translation errors...

Similar Threads

  1. Linked Lists
    By Jnoobs in forum Java Theory & Questions
    Replies: 1
    Last Post: October 23rd, 2010, 04:09 PM
  2. Problems in linked lists
    By Hotzero in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2010, 09:25 AM
  3. unsafe operations note??
    By bookface in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 22nd, 2010, 09:58 AM
  4. Bulk operations on sets/ map problem
    By kyuss in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 14th, 2010, 12:48 PM
  5. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 08:13 PM