Results 1 to 4 of 4
  1. #1
    thisisIT is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default Polynomials Arithmetic

    import java.util.*;

    class Term {
    private int coefficient;
    private int degree;

    public Term(int coefficient, int degree) {
    this.coefficient = coefficient;
    this.degree = degree;
    }

    public int getCoefficient() {
    return coefficient;
    }

    public int getDegree() {
    return degree;
    }
    }

    public class Polynomial {
    private char polyVar;
    private ArrayList<Term> terms;

    public Polynomial(char polyVar, int ... coefsAndDegs) {
    // accept only 'a' through 'z' as the polynomial variable..
    if (polyVar < 'a' || polyVar > 'z') {
    throw new IllegalArgumentException("Invalid polynomial variable.");
    } else {
    this.polyVar = polyVar;
    }

    // check that coefsAndDegs are provided in pairs..
    if (coefsAndDegs.length % 2 != 0) {
    throw new IllegalArgumentException("Coefficients and Degrees must be provided in pairs.");
    } else {
    terms = new ArrayList<Term>();

    for (int i = 0; i < coefsAndDegs.length / 2; i++) {
    Term term = new Term(coefsAndDegs[i * 2], coefsAndDegs[i * 2 + 1]);
    terms.add(term);
    }
    }
    }

    -----------------------------------------------------------------------
    // **OTHER CLASS** my main method where user will input two polynomial

    public static Polynomial enterPolynomial(String entPoly){
    Scanner kb = new Scanner(System.in);
    System.out.print(entPoly + "\n\tEnter Polynomial Variable(a-z): ");
    char variable = kb.nextLine().charAt(0);
    System.out.print("\tEnter number of terms of Polynomial: ");
    String[] temp = kb.nextLine().split(" ");

    int[] coefsDegs = new int[temp.length];
    for(int i = 0; i<coefsDegs.length; i++){
    coefsDegs[i] = Integer.parseInt(temp[i]);
    }
    Polynomial poly = new Polynomial(variable, coefsDegs);
    return poly;
    }


    hard lol. i cant make a method on how to add, subtract, and multiply and even evaluate . can someone help me?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by thisisIT View Post
    hard lol. i cant make a method on how to add, subtract, and multiply and even evaluate . can someone help me?
    Better use a Map<Integer, Integer> where the key is the exponent and the value is the coefficient. That way you can easily find the terms you need.

    kind regards,

    Jos

  3. #3
    thisisIT is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    but, im just new to java.
    can give me some example?
    do you mind chatting wit me? just in short time. just wanna ask something.
    *RUSHING*

  4. #4
    thisisIT is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Char as an arithmetic operator
    By gatzke in forum New To Java
    Replies: 2
    Last Post: 01-27-2009, 04:21 PM
  2. Arithmetic Stacks
    By unc123w in forum New To Java
    Replies: 22
    Last Post: 10-21-2008, 08:24 PM
  3. Illegal Arithmetic Operations?
    By Cruor in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 04:46 PM
  4. How to parse postfix arithmetic expressions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:36 PM
  5. Root Finder for polynomials
    By gibsonrocker800 in forum Advanced Java
    Replies: 11
    Last Post: 01-03-2008, 11:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •