Menu

[Solved] Note Need Two Methods Void Add Polylist P Polylist Pgcd Polylist P Polylist Q Methods Wor Q37251426

NOTE: I only need two methods, which are void add (PolyList p),and PolyList pGCD(PolyList p, PolyList q). I have all other methodsworking, so if you need to use any previous methods for these two,free feel to do so. The add function can be interpreted as themerging of two sorted linked lists into a new single sortedlinkedlist. The pGCD function requires finding the greater commondivisor between the existing polynomial and the input polynomial(which is likely to be another polynomial), and return the gcdpolynomial.

In the assignment program bundle you will find a class PNode forrepresenting polynomials as described above. Each node has two datafields, one for storing the degree of a term and one for storingthe corresponding coefficient. The related class PolyList creates alist of PNodes arranged so that it matches a polynomial written innormal form. This is enforced by the class invariant, which saysthat (when the list is not empty) the remaining nodes are ordered(descending) according to their degree field, all nodes in the listhave different (distinct) degree fields and all nodes have non-zerocoefficient field.

PNode simplify(PNode p) — The output of this method should bethe result of simplifying the given list p so that all nodes havedistinct deg fields, all coeff fields are non-zero AND nodes areordered according to their deg field. As a precondition, it isassumed that the input list p already is sorted (descending)according to its deg field.

boolean simplified(PNode p) — This method should return trueexactly when the given list p represents a polynomial in itssimplest form, i.e. all nodes have distinct deg fields, all coefffields are non-zero AND nodes are ordered according to their degfield.

A final helper method in PNode is PNode priorityAdd(PNode p, intd, float c) — this creates a new PNode with the given data (d, c)and adds it to the given list p and returns the result that issimplified. Note to watch out for the case where the list alreadycontains a node with a deg field set to d (and of course there isnothing to do if c=0). As a precondition you may assume that theinput p corresponds to a list which is already simplified.

This Assignment In this assignment you are asked to implement anumber of methods in the classes PNode and PolyList.

Task 1 (60 marks total: implementation of basic pNode class andPolyList functionality) In PNode implement the following: booleansimplified(PNode p) , PNode simplify(PNode p), PNodepriorityAdd(PNode p, int d, float c)

In PolyList implement the following: int getDegree(), floatgetConstant(), boolean isConstant()

Task 2 (25 marks total: implementation of polynomial arithmetic)In PolyList implement the following: float evaluate(float a) , voidmultiplyByX(int d), void add(PolyList p) void differentiate() Task3 (15 marks total: implementation of greatest common divisor forpolynomials)

PolyList pGCD(PolyList p, PolyList q)

The greatest common divisor for polynomials is a generalisationof greatest common divisor for integers and involves “polynomialdivision and remainder”.

(I’m copy pasting the Eclipse program below:)

class PNode {

// Basic node

int deg; // The degree of a term

float coeff; // The coefficient of a term

PNode next;

PNode (int d, float c) { //Constructor: builds a node with given data

next= null;

deg= d;

coeff= c;

}

PNode (int d, float c, PNoden){ // Constructor: builds a node with given reference

next = n;

deg= d;

coeff= c;

}

// Basic node operations

void scale(float k) { //scales coeff by k

coeff= coeff*k;

}

void multiplyByX(int d) { //increases the degree by d

deg= deg + d;

}

boolean simplified(PNode p) {//TODO

// PRE: p is the first node of a list of PNodes.

// POST: Returns true iff the nodes are sorted (descending)according to their degree field

// AND all nodes have distinct deg fields (no repeats)

// AND all nodes have a non-zero coefficient field.

return false;

}

PNode simplify(PNode p) { //TODO

// PRE: p is the first node of a list of PNodes. All nodes inthe list are already

// Sorted descending according to their deg field

// POST: Rearranges the list so that it is simplified butequivalent to the original representation,

// i.e. ensures that each node has distinct deg field (by usingpolynomial arithmetic)

// AND removes any nodes with coeff field set to 0.

// The simplified representation must be mathematicallyequivalent (as a polynomial)

// to the original representation.

// Returns the first node of the now rearranged list

return null;

}

PNode priorityAdd(PNode p, int d,float c) { //TODO

// PRE: the given list p list is simplified (simplified returnsTRUE);

// POST: Creates a new PNode with the given data (deg = d,coeff=c) and adds it to the

// current list so that the result is also simplified.

// Returns the first node of the list with the new addition

return null;

}

}

class PolyList {

PNode first= null;

// Class invariant — an instance of PolyList must alwayssatisfy the condition:

// The list is empty OR

// (The list is not empty AND

// All nodes have distinct deg fields AND

// There are no nodes having coeff equal to 0 AND

// All nodes are ordered from first to last with decreasing degfield. )

PolyList(PNode p) { // Constructor that sets its first node tothe given PNode p.

first= p;

}

int getDegree() { //TODO

// Returns the largest degree in the list representation.

return 0;

}

float getConstant() {//TODO

// Returns the coefficient of the node with zero degree

return 0;

}

boolean isConstant() {//TODO

// Returns true iff the polynomial is constant (as afunction).

return false;

}

// For CR/D performance

float evaluate(float a) {//TODO

// returns the result of the polynomial evaluated at a.

return 0;

}

void multiplyByX(int d) {//TODO

// Multiplies the polynomial by x^d (x to the power of d).

}

void add(PolyList p) {//TODO

// Adds the given polynomial p to the current polynomial.

}

void differentiate() {//TODO

// Differentiates the current polynomial.

}

// For HD performance

PolyList pGCD(PolyList p, PolyList q) {//TODO

// PRE: p, q are polynomials with leading term having coefffield equal to 1

// Returns the GCD of p and q.

// Ensure that the coeff field of the leading term is 1.

return null;

}

}

Expert Answer


Answer to NOTE: I only need two methods, which are void add (PolyList p), and PolyList pGCD(PolyList p, PolyList q). I have all ot… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *