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 8 of 8

Thread: Linked List question

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Linked List question

    Hey Guys I'm really stuck on a question here what I'm supposed to do is

    Write a class that represents a person. A person should have a name and an age.
    Write a Java program that implements a linked list using the LinkedList class:

    The program should create five people objects with different names and ages and
    add them to the LinkedList using the add(e) method.
    The program should then print out the names and ages of each person object in the
    LinkedList, using a method or methods from the LinkedList class.

       public class Person
    	{
    		private int age;
    		private String firstName;
     
     
     
    		public Person(String firstName,int age)
    		{
    		this.age = age;
    		this.firstName = firstName;
     
    		}
     
     
    	public void printName() 
    		{
    		System.out.println(firstName);
    		}
     
    	public void printAge()
    		{
    		System.out.println(age);
    		}
     
     
     
    	}

    import java.util.*;
     
    public class Linkedlist{
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		String[] firstName = {"John", "Paul", "George", "Ringo", "Pete"};
    		List<String> list1 = new LinkedList<String>();
     
    		for (String x : firstName)
    			list1.add(x);
     
     
     
     
    		printMe(list1);
     
     
    	}
    		//printMe Method
     
    		private static void printMe(List<String> l){
    			for(String b :l)
    				System.out.printf("%s ", b);
    			System.out.println();
     
    	}
     
     
    }
    My problem is I have create five people objects with different names and ages and
    add them to the LinkedList using the add(e) method. The program should then print out the names and ages of each person object in the
    LinkedList, using a method or methods from the LinkedList class.
    I'm not sure exactly how I'm able to do this. Does this explain my problem?

    can anyone help me?

    GROT


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Linked List question

    Can you explain what your problem is by asking some specific questions?

    Change the java tags to code tags: Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Linked List question

    My problem is I have create five people objects with different names and ages and
    add them to the LinkedList using the add(e) method. The program should then print out the names and ages of each person object in the
    LinkedList, using a method or methods from the LinkedList class.
    I'm not sure exactly how I'm able to do this. Does this explain my problem?

    can anyone help me?

    GROT

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Linked List question

    There are a lot of items in the list. Which ones are the problem?
    1)create five people objects with different names and ages
    2)add them to the LinkedList using the add(e) method.
    3) print out the names and ages of each person object in the list.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Linked List question

    What I have done is it correct?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Linked List question

    How do you tell if a program is correct?

    Does it compile without any errors?

    Does it execute without errors and give the correct result?

    If the answers to those questions is yes and yes, then the program could be correct.


    What steps of the program have you done?
    Does the code compile OK?
    Does it execute OK?
    Are the results what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Linked List question

    The question is

    -Write a class that represents a person. A person should have a name and an age.
    -Write a Java program that implements a linked list using the LinkedList class:

    -The program should create five people objects with different names and ages and add them to the LinkedList using the add(e) method.
    -The program should then print out the names and ages of each person object in the LinkedList, using a method or methods from the LinkedList class.

    I can add names and ages to the linked list but why should I need to have a Person Class?
    Sorry for being difficult :/

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Linked List question

    Lets start with the first item in your list. What problems are you having creating the Person class?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  3. Replies: 1
    Last Post: October 25th, 2012, 02:03 PM
  4. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  5. Linked List Help
    By BuhRock in forum Collections and Generics
    Replies: 3
    Last Post: September 26th, 2011, 08:43 AM