Solved

Learning Java PolynomialTester

Posted on 2008-11-02
5
212 Views
Last Modified: 2010-03-30
I am add method of getDescription to the Polynomial class  Method should return one of the 4 strings from the chart.  This method should take no input parameters.   Then add code to the main class that will prompt the user to enter the number of terms in a polynomial.

Chart is
             Number of Terms            Description
                    1                                  monomial
                    2                                  binomail
                    3                                  trinomial
                    4 or more                    Polynomial(no special name)

Output should read:
How many ters are in the polynomial function?
2
The fucntion is a binomial

Any help would greatly be appreciated.

***********************************
package polynomialtester;
import javax.swing.JOptionPane;
public class PolynomialTester {
   
    public static void main(String[] args)
    {
    String input;
    int numTerms;
   
    input = JOptionPane.showInputDialog("How many terms are in the polyomial funcions?");
   numTerms = int.parseInt(input);      
    }

}
*********************************
package polynomialtester;

public class Polynomial
{
    public Polynomial(int initTerms)
    {
        numTerms = initTerms;
    }
    public int getTerms()
    {
    return numTerms;
    }
  public int getDescription(int numTerms)
    {
      String monomial;
      String binomail;
      String trinomial;
      String Polynomial;
         
        switch(numTerms)
        {
              case 1 : return "monomial";
              case 2 : return  "binomail";
              case 3 : return "trinomial";
              default :
                    if(numTerms >= 4)
                    {
                          return "polynomial";
                    }
         }
         // HERE BELONGS A return FOR ALL OTHER CASES.
     }        
               
    private int numTerms;  
   
}

0
Comment
Question by:ca1358
  • 4
5 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 22865233
The following lines have no purpose and should be deleted:

    public Polynomial(int initTerms)
    {
        numTerms = initTerms;
    }
    public int getTerms()
    {
    return numTerms;
    }
and:
      String monomial;
      String binomail;
      String trinomial;
      String Polynomial;

;JOOP!
0
 

Author Comment

by:ca1358
ID: 22865244
public Polynomial(int initTerms)
    {
        numTerms = initTerms;
    }
    public int getTerms()
    {
    return numTerms;
    }
The exercise has this start and must include the above in the class.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22865255
Some lines added:

    public static void main(String[] args)
    {
    String input;
    int numTerms;
   
    input = JOptionPane.showInputDialog("How many terms are in the polyomial funcions?");

    try // Checking input, prepare for errors:
    {
          numTerms = int.parseInt(input);
          System.out.println("That function is " + Polynomial.getDescription(numTerms);
    }
    catch(ParseException e)
    {
           System.err.println("Bad input:  " + input);
    }

the other class changes too:

  public static int getDescription(int numTerms)
    {
      String monomial;
      String binomail;
      String trinomial;
      String Polynomial;
         
        switch(numTerms)
        {
              case 1 : return "monomial";
              case 2 : return  "binomail";
              case 3 : return "trinomial";
              default :
                    if(numTerms >= 4)
                    {
                          return "polynomial";
                    }
         }
         return("Illegal number.");
     }        

The private int at the bottom can be deleted too.
Unless you insist in creating an object for Polynomial ......................

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22865263
I was typing while you answered.
You should have listed the exercise first!

Now the code changes a bit.

;JOOP!
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 500 total points
ID: 22865286
***********************************
package polynomialtester;
import javax.swing.JOptionPane;
public class PolynomialTester
{
    public static void main(String[] args)
    {
    String input;
    int numTerms;
   
    input = JOptionPane.showInputDialog("How many terms are there in the polynomial function?");
    try // Checking input, prepare for errors:
    {
          numTerms = int.parseInt(input);
          Polynomial p = new Polynomial(numTerms);
          System.out.println("That function is " + p.getDescription();
    }
    catch(ParseException e)
    {
           System.err.println("Bad input:  " + input);
    }
}
*********************************
package polynomialtester;

public class Polynomial
{
               
    private int numTerms;  // Declaration on top!!!

   
    public Polynomial(int initTerms)  // Constructor
    {
        numTerms = initTerms;
    }
    public int getTerms()     // Unused now.
    {
    return numTerms;
    }

     public int getDescription()  // No argument, it was already given!
     {
        switch(numTerms)
        {
              case 1 : return "monomial";
              case 2 : return  "binomail";
              case 3 : return "trinomial";
              default :
                    if(numTerms >= 4)
                    {
                          return "polynomial";
                    }
         }
         return("Illegal number.");
     }        
}

;JOOP!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Not needed 13 94
Why does the Android Studio logcat show this error when the user presses the button? 2 42
jar file executable 12 42
eclipse shortcuts 9 45
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now