How do I add 2 Polynomials

I have a school project that I have almost completed, but I am totally stuck on one part.
The overall project is to write a program that reads in 2 polynomials from the user, sorts them in to 2 linked lists and then traverses those lists and adds the 2 polynomials, saving the sum in a 3rd polynomial. I have completed the code for the reading and list sorting, but I am stuck on traversing the list and adding the polynomials.
These are my guidelines:
Set up traversing references to termNodes for both polynomials p and q that point to the first
terms of each and advance through each. Set up reference to the sum r that always points to the
last added term.
If the exponent of p?s term is equal to the exponent of q?s term, add the coefficients and append
this new term to polynomial r (insert it ?after? the traversing reference of r and update this traversing
reference to this latest term.) Advance both p?s and q?s traversing references.
If the exponent of p?s term is less than the exponent of q?s term, add a copy of q?s term to r. Advance q?s traversing reference.
If the exponent of p?s term is greater than the exponent of q?s term, add a copy of p?s term to r.
Advance p?s traversing reference.
When one (or both) of p?s and q?s traversing pointers is null, write two loops that add a copy of the
remaining terms from p or q (you do not know which traversal is complete, so two loops are
required) to r.
I am just totally lost on this. Any code, pointers, anything that can point me in the right direction would be much appreciated.
If it helps, my other code is below
package linkedList;
public class termNode implements Comparable
private int coefficient;
private int exponent;
private termNode next;
//CONSTRUCTOR WITH PARAMETERS
public termNode(int c, int e)
coefficient = c;
exponent = e;
next = null;
//COPY CONSTRUCTOR
public termNode(termNode otherNode)
coefficient = otherNode.coefficient;
exponent = otherNode.exponent;
//OVERRIDE toString AND RETURN VALUES
public String toString()
     if (exponent == 0)
          return coefficient + " ";
     else if (exponent == 1)
          return coefficient + "x ";
     else
          return coefficient + "x^" + exponent + " ";
//COMPARE EXPONENTS:     RETURN 0 IF EQUAL, NEGATIVE IF THIS OBJECT IS LESS THAN
//OTHER OBJECT AND POSITIVE IF THIS OBJECT IS GREATER THAN OTHER OBJECT
public int compareTo (Object other)
if (exponent==((termNode) other).exponent)
          return 0;
else if (exponent < ((termNode) other).exponent)
          return -1;
else
          return 1;
//MUTATOR METHOD FOR COEFFICIENT
public void setCoefficient(int c)
coefficient = c;
public int getCoefficient()
return coefficient;
//MUTATOR METHOD FOR EXPONENT
public void setExponent(int e)
exponent = e;
public int getExponent()
return exponent;
//MUTATOR METHOD FOR NEXT
public void setNext(termNode n)
next = n;
public termNode getNext()
return next;
package linkedList;
import java.util.Scanner;
public class polynomial
private termNode head;
//DEFAULT CONSTRUCTOR CREATES EMPTY LIST
public polynomial()
head = null;
//READ INPUT FROM USER
public void readPolynomial()
int a = 0;
int b = 1;
Scanner kbInput = new Scanner(System.in);
System.out.println("Enter 0 and 0 to end.");
System.out.print("coefficient: ");
kbInput.skip("\\s*");//SKIPS LEADING WHITE SPACE
a = kbInput.nextInt();//READS FIRST VALUE IN TO a
kbInput.nextLine(); //SKIPS REMAINDER OF INPUT LINE
System.out.print("exponent: ");
kbInput.skip("\\s*");//SKIPS LEADING WHITE SPACE
b = kbInput.nextInt();//READS SECOND VALUE IN TO b
kbInput.nextLine(); //SKIPS REMAINDER OF INPUT LINE
while (a!=0)
termNode newTerm = new termNode(a,b);
insert(newTerm);
System.out.println("Enter 0 and 0 to end.");
System.out.print("coefficient: ");
kbInput.skip("\\s*");//SKIPS LEADING WHITE SPACE
a = kbInput.nextInt();//READS FIRST VALUE IN TO COEFFICIENT
kbInput.nextLine(); //SKIPS REMAINDER OF INPUT LINE
System.out.print("exponent: ");
kbInput.skip("\\s*");//SKIPS LEADING WHITE SPACE
b = kbInput.nextInt();//READS SECOND VALUE IN TO EXPONENT
kbInput.nextLine(); //SKIPS REMAINDER OF INPUT LINE
public void insert(termNode newNode)
termNode prev = null;
termNode cur = head;//CUR POINTS TO FIRST ITEM IN LIST (HEAD)
while (cur!=null && (newNode.compareTo(cur)>0))//TRAVERSE LIST WHILE CUR IS NOT NULL AND
{                                              //newNode EXPONENT IS GREATER THAN cur EXPONENT
          prev = cur;
          cur = cur.getNext();
if (prev==null)//INSERT AT BEGINNING OF LIST
          newNode.setNext(head);
          head = newNode;
else//INSERT IN LIST BASED ON PREVIOUS TRAVERSING
          newNode.setNext(cur);
          prev.setNext(newNode);
//System.out.println(cur.getNext());
//DISPLAY "0" FOR EMPTY LIST, OTHERWISE TRAVERSE AND DISPLAY POLYNOMIAL
public void display()
if (head==null)
          System.out.println("0");
else
          for (termNode cur=head; cur!=null; cur = cur.getNext())
                    System.out.print(cur);
}

My first bit of advice is to lose those awful star-boxed comments. Atrocious.
Just saw the second class. You have, by a wide margin, the very worst commenting style I've ever seen.
Wait for it - "My teacher tells me we have to comment our code or s/he'll take off points, so I have to keep them." Oy.
You aren't following the Sun standards for Java coding, either. I'd recommend learning them.
The rest ain't so bad.
So your two problems - list traversal and addition. We'll start with the second one. Do you know how to add two polynomials? Do you know the rules for adding two monomials? If I gave you these, what would the answer be?
p1(x) = 4x^2
p2(x) = 5x^3
p1(x) + p2(x) = ?
How 'bout
p3(x) = 5x^2 + 4
p4(x) = 10x^100 - x^2
p3(x) + p4(x) = ?
If you can figure these out, the coding should be clearer.
As for the list, your "next" and "head" data members lead me to believe that you intend to do your own linked list. Do you know how to do that? Why aren't you considering the Java collections?
%

Similar Messages

  • How do I add URI web link with custom tooltip like "CLICK HERE TO UPDATE" instead of URI web link in tooltip.

    How do I add URI web link with custom tooltip like "CLICK HERE TO UPDATE" instead of URI web link in tooltip.

    You've probably found an answer to this by now, but I think this has been addressed in another forum -- The link below suggested using a button and adding the tooltip to the button. 
    https://forums.adobe.com/thread/304974?start=0&tstart=0
    Sounds like it would work but I haven't actually tried it. 
    Good luck~!

  • How do i add type kit fonts to muse web site

    how do I +add type kit fonts to muse website

    Hi.
    Check this video, might be helpful
    Let me know if you have any questions

  • How can I add more than one same spry menu (eg. collapsible menu)  with in different styles (font size, color, background, etc) on current page?

    How can I add more than one same spry menu (eg. collapsible menu)  with in different styles (font size, color, background, etc) on current page?

    Hi Nancy,
    This screenshot was only for imagination. A part of the code (not all) is below.  In the code there are some background images but they are not seem in live mode.
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="css/my_site.css" rel="stylesheet" type="text/css" />
    <link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css"/>
    <link href="SpryAssets/SpryCollapsiblePanel.css" rel="stylesheet" type="text/css" />
    <script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
    <script src="SpryAssets/SpryCollapsiblePanel.js" type="text/javascript"></script>
    <style>
    #CollapsiblePanel1 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-color: #003366;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel1 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        line-height: 52px;
    #CollapsiblePanel1 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/international.jpg);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel2 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelContent {
        background-color: blue;
    #CollapsiblePanel3 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel3 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel3 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel4 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel4 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel4 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel5 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel5 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel5 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    </style>

  • How do I add multiple apple id's to one account

    How do I add multiple Apple id's to one account for billing and Icloud use?

    You don't. You can't combine Apple IDs.

  • HT201342 How can I add an e-mail address to my existing icloud addresses?  I currently only use one and I would like to ad two more?

    How can I add an e-mail address to my existing icloud account?  I curently only use one of the three.

    Each iCloud account only has a single iCloud account.  If you want, you can add up to 3 alias addresses that will receive email in the same iCloud inbox, as explained here: http://support.apple.com/kb/PH2622.

  • How can I add amazon to Apple TV?

    I am a new Apple TV user. So far I am impressed and like it a lot. However I would like to use the Amazon Prime and I have yet to find it. How can I add it to my Apple TV?

    You cannot add apps to Apple TV. If you have an IOS device you can AirPlay to stream using the app from there

  • HT201269 How do I add another contact / recipient to an SMS. Easy in Android, but I can't see how on my 5s.  Have to create a group on my Macbook Pro (OS 9.1), but it's not getting synced via usb. I'd prefer not to use cloud.

    How do I add a contact / recipient to an SMS on iPhone 5s?  It's easy on Android, but I've not sussed it on the iPhone - can't create a group etc.  Tried to create a group on Mac, but it either doesn't exist or won't sync to the phone with usb, nor will other contact details created on Mac.  I'd rather not use the cloud.  I'm sure contacts used to sync to my old iPod touch in Snow Leopard.
    Is it not more logical to have the phone as the main source of this sort of data?  We're more likely to update on a device we carry all the time surely?
    Please advise. Thank you

    Hey Cornish wrinkly,
    It sounds like you want to create a group message. You can read about group messaging on the iPhone here:
    iOS: Understanding group messaging
    http://support.apple.com/kb/HT5760
    Welcome to Apple Support Communities!
    Regards,
    Delgadoh

  • How do I add multiple tickets under the same confirmation number from the American Airlines app to Passbook?

    I have a confirmation number from American Airlines for a flight with my family all under my name and for some reason I cannot add all of our tickets to Passbook. To clarify, I went to the American Airlines app and put in the confirmation number along with my name which pulled up my boarding pass only. Although I was able to add this boarding pass to Passbook, I was not able to add any of the other boarding passes for my children to Passbook that should have also been under the flight reservation. In fact I couldn't even see them in the American Airlines app. I tried to use their names and the same confirmation number but nothing happened, and nothing else seems to be working. How can I add all of these boarding passes to my Passbook?

    Fair enough.
    I started doing this before unlimited data and emails on phones was a common commodity, on my Samsung Blackjack.
    I then continued it due to its convenience. My only counter argument is this:
    Even when emails are set to immediately push, I am always notified earlier by this forwarding message (sometimes by several minutes/hours due to the fact that it is not dependent on my being connected to 4G or wifi to receive it.
    Also, to contradict snozdop's point that both methods use data and battery, I say this - when I use the forwarding method, the information comes in a pure text format and therefore uses considerably less information than an HTML and CSS rich email, with embedded images and such -also, unlimited text messages aids curve costs.
    I will, however, give this method a go. In any case, a solution to my original question would still be greatly appreciated.
    Thanks guys.

  • How do I add multiple contacts to a group?

    How do I add multiple contacts to a group on my Ipad?

    See if this thread helps.
    https://discussions.apple.com/thread/4114588
    Matt

  • How do i add a company logo to my email signature on my mac book pro

    how do i add a company logo to my email signature on my mac book pro

    I would like to know how to do this as well. All I'm able to do is add the link, but not the button. My colleague has done this on his Outlook on his HP. I'm not sure why this is so difficult!

  • How do I add a printer connected to another iMac on the network to my printer list?

    How do I add an Epson printer connected to an iMac on the network to the printer list on my MacBook Pro? I cannot get it to show up in the list for printers to add.

    That only works if the printer is itself a network printer. You can't do it if it is directly connected to another computer on the network unless it is configured to be a Shared Printer on the other computer.

  • How do i add multiple email addresses to my calendar

    How do I add multiple email addresses to my calendar on my iphone

    You can create an email alias at the mail feature at iCloud.com

  • How do I add my new lapttop to my family iTunes? I downloaded music, but cannot play it

    How do I add my new lap top to my family iTunes? I downloaded music, but cannot play it on my laptop

    There's a couple of ways to get through to the authorisation controls in the 11.0.x versions.
    The control is still in the Store menu, but first (if you're using iTunes versions 11.0.x) you might need to bring up the menu bar to see the Store menu.
    If you're using 11.0.x, click on the wee boxy icon up in the top-left corner of your iTunes to see the "Show Menu Bar" control, as per the following screenshot:
    Then you'll find the control in the Store menu:
    Alternatively, if you don't want to bring up the menu bar, it's still possible to get into the authorise controls via nested menus accessible from the wee boxy icon. Here's a screenshot of where to find them:

  • How do I add my Macbook Pro to the Icloud devices?

    2 things...my I cloud account will NOT show Keychains on this MacBook Pro 
    upon further review the Pro is NOT a device I added on Icloud....only my cell phone is added, how do I add this pro?

    http://www.apple.com/iphone/icloud/

  • How do I add tap to tweet to my notification center on my macbook pro?

    My friend has the same MacBook Pro as me and she has the 'tap to tweet' option in her notification center while I don't. How do I add it?

    Welcome to Apple Support Communities
    Open System Preferences > Mail, Contacts and Calendars, press the + button and add the Twitter account. Then, Tap to Tweet will appear on the Notification Center. This only works with Mountain Lion and later

Maybe you are looking for

  • Text display slanted in the middle nokia 5800

    nokia 5800 displays text or menus in a bit tilted manner in the middle part of the screen whereas it is straight at the ends.movies play fine without the tilt.tried formatting the phone but same problem persists help please!

  • Want to change filename

    Recently switched to Mac & iPhoto and discovered that the new photos I've been importing and renaming (or so I thought) in iPhoto are not really changing the filename. I wish to change the filename (not just the title via iPhoto) so that my photo nam

  • JBO-35007 Row currency... another solution

    Hi all, I found another solution for handling of JBO-35007. If you implement extension of FacesPageLifecycle like in SRDemoSampleADFBC, in your class (eg. MyFacesPageLifecycle), right after line "JboException jboex = (JboException) ex;" in method "pr

  • Solaris 2.5.1 and DHCP?

    Does Solaris 2.5.1 support DHCP and how do you configure if it does? thanks

  • Adobe reader 8 / acrobat

    I have windows xp. I am trying to open a pdf file that was sent to me. I have adobe reader 8. I was told by my instructor I need acrobat to open it. Doesn't adober reader 8 have acrobat or do I need something else? I am Computer DUMB!! Please help me