Question

Q. Write a method add(PolyTerm): Two PolyTerm objects can be added if and only if they...

Q. Write a method add(PolyTerm): Two PolyTerm objects can be added if and only if they have the same exponent. If the calling object can be added to the parameter object, return their sum, otherwise (if they can't be added), return null. For example 2x^3 + -7.3x^3 = -5.3x^3 while 2x^3 + 8x^2 should return null.
(It's on JAVA)

The JUnit Test for this question is:

public class PolyTermTest {
   public static int score = 0;
   public static String result = "";
   public static String currentMethodName = null;
   ArrayList<String> methodsPassed = new ArrayList<String>();
  
   PolyTerm t1, t2, t3, t4;

   @BeforeEach
   public void setUp() throws Exception {
       currentMethodName = null;
       t1 = new PolyTerm(1, 1); //x
       t2 = new PolyTerm(2.4, 3); //2.4x^3
       t3 = new PolyTerm(-1.5, 0); //-1.5
       t4 = new PolyTerm(3.6, -2); //3.6x^-2
   }

@Test @Order(7) @Graded(marks=8, description="add(PolyTerm)")
   public void testAdd() {
       assertNull(t1.add(t2));
       assertNull(t1.add(t3));
       assertNull(t1.add(t4));
       assertNull(t2.add(t3));
       assertNull(t2.add(t4));
       assertNull(t3.add(t4));
      
       PolyTerm t5 = new PolyTerm(1.1, -2);
       assertNotNull(t4.add(t5));
       assertEquals(4.7, t4.add(t5).coefficient, 0.001);
       assertEquals(-2, t4.add(t5).exponent);

       currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
   }

Homework Answers

Answer #1

// Assuming Polyterm contains fields coefficient and exponent
public PolyTerm add(PolyTerm p)
{
   // exponents are same
   if(exponent == p.exponent)
       // create a new PolyTerm where coefficient = sum of this and p's coefficient and exponent = this's exponent
       return new PolyTerm(coefficient+p.coefficient, exponent); // Assuming PolyTerm contains a constructor that takes as inputs coefficient as first argument and exponent as secon argument

else // exponents are not same
       return null;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
***Write a method in Java to Evaluate: ***For exp: object t2 has coefficient = 2.4 and...
***Write a method in Java to Evaluate: ***For exp: object t2 has coefficient = 2.4 and exponent = 3. it represents 2.4 * x^3. Then t2.evaluate(-1.5) is 2.4 * -1.5^3 which is -8.1. @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class PolyTermTest{ public static int score = 0; public static String result = ""; public static String currentMethodName = null; ArrayList methodsPassed = new ArrayList(); PolyTerm t1, t2, t3, t4; @BeforeEach public void setUp() throws Exception { currentMethodName = null; t1 = new PolyTerm(1, 1);...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...