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);...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
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...
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...
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 -...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
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:...
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...
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;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT