Answer to Question #297646 in Java | JSP | JSF for Bruhh

Question #297646

3. Term

by CodeChum Admin

Construct a class called Term. It is going to represent a term in polynomial expression. It has an integer coefficient and an exponent. In this case, there is only 1 independent variable that is 'x'.


more details here. Important!

https://pastebin.com/wMX2BhFk


1
Expert's answer
2022-02-14T19:21:21-0500
public class Term {
    private int coefficient;
    private int exponent;

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

    public Term times(Term t) {
        return new Term(coefficient * t.coefficient, exponent + t.exponent);
    }

    @Override
    public String toString() {
        if (coefficient == 1) {
            if (exponent == 0) {
                return "1";
            } else if (exponent == 1) {
                return "x";
            } else {
                return "x^" + exponent;
            }
        } else {
            if (exponent == 0) {
                return Integer.toString(coefficient);
            } else if (exponent == 1) {
                return coefficient + "x";
            } else {
                return coefficient + "x^" + exponent;
            }
        }
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Term term = new Term(in.nextInt(), in.nextInt());
        System.out.println(term.times(new Term(in.nextInt(), in.nextInt())));

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS