Attached Files: Polynomial.java HW3.java Given two polynomials in form of strings. Write a Java program that adding, subtracting, and multiplying two polynomials using maps. Input: The input will be two polynomials in the following form of strings, for example, “(-4.5)X^1 + (-2.5)X^0 + 1X^3” “1X^2 + 1X^0” Output: Polynomial p: X^3 -4.5X -2.5 Polynomial q: X^2 + 1.0 p+q: X^3 + X^2 -4.5X -1.5 p-q: X^3 -X^2 -4.5X -3.5 p*q: X^5 -3.5X^3 -2.5X^2 -4.5X -2.5 Requirements: Using a Java HashMap or TreeMap to represent a polynomial. Each term will be saved as a key-value entry in the map. The size of the map will be the number of terms in the given polynomial or less. After you parse the input strings, you should insert the terms into your HashMap or TreeMap immediately. To parse a string, you may refer to the Java StringTokenizer class or String class. You may add more test cases in the main method but remember to change the main method back to the original state before you submit the project. Classes required for the project: Polynomial class holds a HashMap or TreeMap associated with a polynomial. Polynomial arithmetic must be done in the Polynomial class. You should also implement a toString() method to print out polynomials. The main method must be in the HW3 class. Polynomial.java import java.util.StringTokenizer; public class Polynomial { // use either HashMap or TreeMap to represent a polynomial //private HashMap p; //private TreeMap p; public Polynomial(String st) { //add code } //add auxiliary methods and/or constructors public Polynomial add(Polynomial q) { //add code } public Polynomial subtract(Polynomial q) { //add code } public Polynomial multiply(Polynomial q) { //add code } public String toString() { //add code } } ************************************************************************************************ HW3.java public class HW3 { /* * Do not modify the main method. * You may add more test cases but change the main method back to the original state before you submit the project. */ public static void main(String[] args) { String s = "(-4.5)X^1+(-2.5)X^0+1X^3"; String t = "1X^2+1X^0"; Polynomial p = new Polynomial(s); Polynomial q = new Polynomial(t); System.out.println("Polynomial p: " + p); System.out.println("Polynomial q: " + q); System.out.println("p+q: " + p.add(q)); System.out.println("p-q: " + p.subtract(q)); System.out.println("p*q: " + p.multiply(q)); System.out.println(); s = "1X^0+(-1)X^1+2X^2+(-2)X^0"; t = "(-1)X^0+1X^5"; p = new Polynomial(s); q = new Polynomial(t); System.out.println("Polynomial p: " + p); System.out.println("Polynomial q: " + q); System.out.println("p+q: " + p.add(q)); System.out.println("p-q: " + p.subtract(q)); System.out.println("p*q: " + p.multiply(q)); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Attached Files:

  • Polynomial.java
  • HW3.java

Given two polynomials in form of strings. Write a Java program that adding, subtracting, and multiplying two polynomials using maps.

Input:

The input will be two polynomials in the following form of strings, for example,

“(-4.5)X^1 + (-2.5)X^0 + 1X^3”

“1X^2 + 1X^0”

Output:

Polynomial p: X^3 -4.5X -2.5

Polynomial q: X^2 + 1.0

p+q: X^3 + X^2 -4.5X -1.5

p-q: X^3 -X^2 -4.5X -3.5

p*q: X^5 -3.5X^3 -2.5X^2 -4.5X -2.5

Requirements:

Using a Java HashMap or TreeMap to represent a polynomial. Each term will be saved as a key-value entry in the map. The size of the map will be the number of terms in the given polynomial or less.

After you parse the input strings, you should insert the terms into your HashMap or TreeMap immediately. To parse a string, you may refer to the Java StringTokenizer class or String class.

You may add more test cases in the main method but remember to change the main method back to the original state before you submit the project.

Classes required for the project:

  1. Polynomial class holds a HashMap or TreeMap associated with a polynomial. Polynomial arithmetic must be done in the Polynomial class. You should also implement a toString() method to print out polynomials.
  2. The main method must be in the HW3 class.

Polynomial.java

import java.util.StringTokenizer;

public class Polynomial {

// use either HashMap or TreeMap to represent a polynomial

//private HashMap<Integer, Double> p;

//private TreeMap<Integer, Double> p;

public Polynomial(String st) {

//add code }

//add auxiliary methods and/or constructors

public Polynomial add(Polynomial q)

{

//add code

}

public Polynomial subtract(Polynomial q)

{

//add code

} public Polynomial multiply(Polynomial q) {

//add code

}

public String toString() {

//add code

} }

************************************************************************************************

HW3.java

public class HW3 {

/* * Do not modify the main method. * You may add more test cases but change the main method back to the original state before you submit the project. */

public static void main(String[] args)

{ String s = "(-4.5)X^1+(-2.5)X^0+1X^3";

String t = "1X^2+1X^0";

Polynomial p = new Polynomial(s);

Polynomial q = new Polynomial(t);

System.out.println("Polynomial p: " + p);

System.out.println("Polynomial q: " + q);

System.out.println("p+q: " + p.add(q));

System.out.println("p-q: " + p.subtract(q));

System.out.println("p*q: " + p.multiply(q));

System.out.println();

s = "1X^0+(-1)X^1+2X^2+(-2)X^0";

t = "(-1)X^0+1X^5";

p = new Polynomial(s);

q = new Polynomial(t);

System.out.println("Polynomial p: " + p);

System.out.println("Polynomial q: " + q);

System.out.println("p+q: " + p.add(q));

System.out.println("p-q: " + p.subtract(q));

System.out.println("p*q: " + p.multiply(q)); } }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 9 steps with 9 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education