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

Thread: I'm having trouble adding polynomials using a singly linked list

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question I'm having trouble adding polynomials using a singly linked list

    I've looked everywhere online and can't find the answer i'm looking for. I don't know how to import the scanner and loop it properly to take the user's inputs and turn them into the polynomials and i have a lot of trouble with the syntax of doing so... I don't know much Java at all, but this is the assignment and what i have so far... god bless anyone willing to help!
    My assignment is:

    You are asked to write a Java program that helps the user add and print polynomials of any
    degree. If possible, students are encouraged to make the program help the user multiply two
    polynomials of any degree.

    1. Polynomial Class

    This class stores polynomials as a linked list of terms. Each term contains a coefficient and a
    power of an unknown variable x. The implementations of linked lists in chapter 3 of the
    textbook or the LinkedList class in the Java collections package can be in this assignment.
    For example, the polynomial P(x) = 5x
    10 + 9x
    7
    – x – 10 can be stored as list of terms (5, 10), (9,
    7), (-1, 1) and (-10, 0) in the linked list. This class should have one or more constructors and
    methods to add, multiply, and print polynomials. For example, a polynomial P above can be
    constructed as:

    Polynomial p = new Polynomial();
    p.addTerm(-10, 0);
    p.addTerm(-1, 1);
    p.addTerm(9, 7);
    p.addTerm(5, 10);

    For example, to multiply the polynomial P(x) by another polynomial Q(x), the following method
    can be used:

    Polynomial r = p.multiply(q);

    Of course, this multiply method can be after both polynomials were instantiated. To add the
    polynomial P(x) to another polynomial Q(x), the following method can be used:

    Polynomial r = p.add(q);

    To print the polynomial P(x) on the output console, the following method can be used:

    p.print();

    In this assignment, the program should ask the user for the polynomial(s) and the operation to
    apply. You can parse the line input by the user or use a menu-driven input. Students are free to
    choose whichever is easier to implement. For simplicity, the main method must be implemented
    inside the Polynomial class to make the program consist of only a single file.

    What i have so far is:
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class PolynomialMath<E> {
     
    	public static class Node <E> {
    		private E element;
    		private Node<E> next;
    		public Node(E e, Node<E> n){
    			element = e;
    			next = n;
    		}
     
    		public E getElement() { return element; }
    		public Node<E> getNext() { return next; }
    		public void setNext(Node<E> n) { next = n; }
    	}
     
     
    private Node<E>	head = null;
    private Node<E> tail = null;
    private int size = 0;
    public PolynomialMath() { }
     
     
    public int size() { return size; }
    public boolean isEmpty() {return size == 0;}
    public E first() {
    	if (isEmpty()) return null;
    	return head.getElement();
    }
     
     
    public E last() {
    	if (isEmpty()) return null;
    	return tail.getElement();
    }
     
     
    public void addFirst(E e) {
    	head = new Node<>(e, head);
    	if (size == 0)
    		tail = head;
    	size++;
    }
     
     
    public void addLast(E e) {
    	Node<E> newest = new Node<>(e, null);
    	if (isEmpty())
    		head = newest;
    	else
    		tail.setNext(newest);
    	tail = newest;
    	size++;
    }
     
     
    public E removeFirst() {
    	if (isEmpty()) return null;
    	E answer = head.getElement();
    	head = head.getNext();
    	size--;
    	if (size == 0)
    		tail = null;
    	return answer;
    }
     
     
     
        public static void main(String args[]){
            ArrayList l = new ArrayList();
            System.out.println("Enter the input");
            Scanner input=new Scanner(System.in);
     
             String a =input.nextLine();
             l.add(a);
     
    for (int i = 0; i < l.size(); i++) {
      System.out.println(l.get(i));
          } 
                System.out.println(l);
     
     
     
     
     
        }
    }
    Last edited by onestepahead; October 5th, 2014 at 08:24 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm having trouble adding polynomials using a singly linked list

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES 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
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm having trouble adding polynomials using a singly linked list

    thank you for the tip, i'm very new to java and programming forums...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm having trouble adding polynomials using a singly linked list

    The code has lost its formatting in places. There are many statements starting in the first column that should be indented.
    scanner and loop it properly to take the user's inputs
    Work on that part first. Later add code to put the data into the list.
    When getting user input there needs to be a way for the user to tell the program he's done entering data.
    What technique has your instructor talked about?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm having trouble adding polynomials using a singly linked list

    Thank you for your input, i appreciate your response! I am really struggling with this assignment because i cannot get past the part you just mentioned: user input and data gathering.

    To answer your question: My professor has not gone over methods this complex for taking user inputs. This was supposed to be a competency test for linked lists and turned into something else in my case.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm having trouble adding polynomials using a singly linked list

    Two common techniques for ending user input:
    the user tells the program the number of entries and the program loops that number of times
    the user enters a special/non-data value (sentinel) that the program looks for to end the loop

    Chose one of those and write a loop to get user input. Put the input into an arraylist for now. When finished getting user input, print out the contents of the arraylist to show what was read and saved.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm having trouble adding polynomials using a singly linked list

    Alright! That's good advice, thank you!

Similar Threads

  1. Help with singly linked list.
    By jackzorz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 29th, 2014, 09:02 PM
  2. Adding Student Objects into singly linked list alphabetically [NullPointerException]
    By Restivethought in forum Object Oriented Programming
    Replies: 2
    Last Post: March 29th, 2013, 07:37 AM
  3. Singly Linked List
    By koala711 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 02:15 PM
  4. Singly-Linked list structure
    By blaster in forum Algorithms & Recursion
    Replies: 24
    Last Post: March 11th, 2012, 03:42 PM

Tags for this Thread