Você está na página 1de 13

Student: Băilă Romina-Ioana

Group: 30423

Programming Techniques
Assignment 1
-Polynomial Calculator-
Contents:
1. Problem Specification
2. Analyzing the problem, assumptions, modelling, scenarios, use cases
3. Design (design decisions, UML diagrams, class design, interfaces,
packages, algorithms)
4. Implementation and testing
5. Results
6. Conclusion and future developments
7. Bibliography

1. Problem Specification

This first assignments consists of proposing, designing and implementing a


system for polynomial processing. We should take into consideration also the
fact that these polynomials are of one variable and have integer coefficients.

There are also some constraints to which the program should comply so that
it can be accepted:
-respecting the OOP paradigms
-the application should be organized into packages
-all classes can have a maximum size of 300 lines (except for the User
Interface Classes)
-all methods can have a maximum size of 30 lines (there can be exceptions,
such as switches with multiple cases)
-respecting the Java naming conventions

For passing with the minimum mark, the assignment must include:
-GUI (without using any drag and drop tools)
-add and subtract operations
-project specification (the UML diagrams should not be generated
automatically)

Then, to get an even higher grade, the application should also have the
following:
-multiplication operation
-divide operation
-derivative operation
-integrate operation
-OOP design (Monomial and Polynomial classes, using List<> instead of
array[], using foreach etc.)
-Junit Tests

The graphical interface will allow the entering of data, the 2 polynomials, the
selection of the desired operation and then it will output the result of the
operation in the same form as the input.
2. Analyzing the problem, modelling, scenarios, use
cases
2.1 Problem analysis
A system that can process polynomials is useful for a wide range of
users, from students who need help with their exercises to experts that need
a system that can calculate complex operations. The graphic interface is
easier to use than the console by users that do not have an interest in
computer science.

Before talking about polynomials, we must first clarify what a


monomial is, as it will help us later on. A monomial has 3 main parts: a
coefficient, an unknown (usually denoted by “x”) and a degree. We can take “-
2x^3” as a monomial example. Then, -2 is the coefficient and 3 is the degree
of the monomial.

A polynomial is like a structure holding 2 or more monomials.


Conventionally, the monomials in a polynomial are arranged in the
descending order of their degrees. It is also worth knowing that the term
degree of a polynomial refers to the highest degree among all monomials
forming that polynomial (conventionally, it is the degree of the first
polynomial). An example of a polynomial is: “2x^4+3x^3-6x^2”. Then this
polynomial is comprised of 3 monomials and has the degree 4.

For each mathematical operation, we must implement a specific


algorithm in order to get the correct result. For adding and subtracting 2
polynomials you can only add or subtract the monomials that have the same
degree (the operation is done on the coefficients) and the elements that
don’t have the same degree in both polynomials are added with the
appropriate sign. For multiplication, each monomial from the first monomial
is multiplied with each of the monomials from the second one, then the
monomials of the result that have the same degree will be added (as
presented in the simplify method). The division of 2 polynomials consists of
multiple steps: you must divide the first term of the dividend by the highest
term of the divisor then you must multiply the divisor by the result just
obtained after that subtract the product just obtained from the appropriate
terms of the original dividend and repeat the previous three steps, except
this time use the n terms that have been written as the dividend. The
derivation and integration are both done by deriving or integrating each
monomial of the polynomial. Though it may seem unlikely, the derivation and
integration operations are the easiest to implement.

2.2 Assumptions
Assumptions may be used to make the task of implementing the
polynomial calculator easier. But too many assumptions could be made for
this assignment. The only assumption that I made when I implemented the
calculator was that the user has some basic mathematical knowledge and
knows how a polynomial should look like. But in case he/she does not know
or does not remember, an error window is popped describing the only
accepted format for the input.

2.3 Modelling
Because we know that the system has one variable and integer
coefficients, we can build the polynomials using 2 classes: monomial and
polynomial.
The monomial and polynomial classes have been modelled to represent
their mathematical equivalents. Just as in mathematics, a polynomial is built
from one or more parts, each having a degree and a coefficient, called a
monomial that is separated from the other monomials by a plus or minus
sign. For both classes monomial and polynomial there will be created
operations, that represent the operations you can do with the monomial or
polynomial. The addition of monomials will be different from the one of
polynomials, but the polynomial one will use the monomial addition.

2.4 Scenarios
The application allows a user to enter 2 polynomials in a valid format
(which is displayed
the user did not get it right with how the polynomial should look like the first
time. Then the user clicks a button which has the symbolic representation of
each function on it and then the result is displayed (for division, the quotient
is displayed in the result field and the remainder in the remainder field).

Examples of possible scenarios:


Input data Operation Displayed
One/ two wrongly Any operation Error window
written polynomials describing how the
input polynomials
should look like
P1: 2x^2+3x^1 Addition Result:
P2: 3x^3+5x^2 P1+P2 3x^3+7x^2+3x^1
P1: 2x^2+3x^1 Subtraction Result: -3x^3-
P2: 3x^3+5x^2 P1-P2 2x^2+3x^1
P1: 2x^2+3x^1 Multiplication Result:
P2: 3x^3+5x^2 P1*P2 6x^5+19x^4+15x^3
P1: 1x^3-2x^2+3x^1- Division Result: 1x^2-
1x^0 P1/P2 4x^1+11x^0
P2: 1x^1+2x^0 Remainder: -23x^0
P1: 2x^2+3x^1 Derivate Result: 4x^1+3x^0
(P1)’
P1: 3x^2+2x^2 Integrate Result: 1x^3+2x^2
∫P1

2.5 Use cases


A use case diagram represents the user interacting with the application.
3. Design (design decisions, UML diagrams, data structures, class
design, interfaces, relationships, packages, algorithms, user
interface)
3.1 Design decisions

I choose an object oriented approach for this problem, separating the


monomials from
the polynomials and also the computations from the data. As a monomial is
defined only by its coefficient and degree, these are the only 2 instance
variables assigned to the class Monom. Class Polynom extends
ArrayList<Monom>, as I find this representation practical and very easy to be
understood by anyone who has basic knowledge in Java.

Also, every method in Calculus class is static, so it can be referenced


from another class, in this case the GUI (Interfata) class.
3.2 UML Diagrams
3.3 Class design
For this assignment, I used 2 main classes for storing the data and
formatting it, namely
Monom and Polynom. I chose to separate the data from the computations, as
I think it increases readability and complies more to the OOP Paradigms.

 In class Monom:
- Attributes:
 float coeff = the coefficient of the monomial. Though the
problem says that the polynomial will have only integer
coefficients, in case of division we could get real numbers as
resulted coefficients
 int degree = the degree of the monomial
- Constructors:
o There is only one constructor, taking as parameters a float
representing the coefficient and an int representing the
degree
- Methods:
o getCoeff(): float = returns the coefficient of the monom
o getDegree(): int = returns the degree of the monom
o setCoeff(float): = sets coefficient of monom to desired value
o setDegree(int): = sets degree of monom to desired value
o printMonom(): = this method is used only for testing and it
says which is the coefficient and which is the degree for the
current monomial
o equals(Object): boolean = overridden method to see if 2
monomials are equal
 In class Polynom:
- Attributes: doesn’t have any, as it is enough to say that this class
implements an ArrayList of monomials
- Constructors:
o Only the implicit one, which is called by new Polynom()
- Methods:
o extractMonomDetails(String): Polynom = as the name
suggests, this method is used to extract the monomials from
the polynomials written by the user and put each element in
its place (coefficient and degree, which are associated
accordingly)
o printPol(): String = this is a sort of overridden toString method,
as it displays a polynomial in the desired form
o findMonom(int): Monom = this method says if a monomial
having a specific degree (but doesn’t care about the
coefficient) exists in the polynomial. If so, the monomial is
returned, otherwise null is returned. It is used for example in
the simplify method from the Calculus class
o sortPolynomDes(): = this is a method which sorts the
monomials inside a polynomial in the descending order of
their degrees (again, it doesn’t care about the coefficients). It
is used for printing the polynomial in a conventional way and
also, it is very very useful in the div method from Calculus
class (without this method, when implementing div we would
have to implement another method for finding the monomial
with the highest degree inside a polynomial; but with this
method, we know that the monomial having the highest
degree is always the first monomial in any polynomial)
o equals(Object): boolean = overridden method used only in
JUnit Tests

Class Calculus is easy to understand, as it only implements basic


mathematical
operations on monomials or polynomials.

Class Interfata is the GUI class which creates the user interface. It
also links every
button with the appropriate action, so, of course, it implements Application. I
used JavaFX instead of the classical Swing, as I think it creates more
appealing applications.

3.4 Algorithms
There were many algorithms used while developing this application. But surprisingly (for me at
least), the algorithm used for division was the most difficult to implement. This is why I think it is worth
mentioning at least the pseudocode for polynomial division in this section:
quotient ← 0
remainder ← n //At each step n = divisor × quotient + remainder; n is
the first polynomial
while remainder ≠ 0 AND degree(remainder) ≥ degree(divisor):
mr ←remainder(0) / divisor(0) //Divide the leading terms
quotient ← quotient + temp
remainder ← remainder − temp * divisor
return (quotient, remainder)

3.5 Packages
For this application, 3 packages were used for the main program and
one package for
the JUnit Tests. I think that it keeps the whole program even more organized
than having just separate classes in the same package.

3.6 Interfaces
For the implementation of this project, there were no interfaces used
as there wasn’t any need to implement them. All classes have different
methods and all methods have different names.

4. Implementation and testing


4.1 Implementation
The polynomials are introduced as a sum of monomials. Both of the
input polynomials
should have the following form:
coefficient(positive with no sign, negative with - sign; then + is
omitted) + “x^” + degree

Examples of valid input:


- 1x^2+3x^1-6x^0
- 2x^1+4x^4+5x^7-3x^2
Examples of invalid input:
- x^2+3x^3 – first monomial doesn’t have a coefficient (coefficient 1
is not implicit)
- 3x^2+2 – last monomial doesn’t have a degree (degree 0 and 1 are
not implicit)

The monomials do not have to be entered in any order; ordering the


monomials inside a
polynomial is done by the program. Coefficient 1 and degrees 0 and 1 should
be explicitly written. The only allowed letter for the unknown is x and the
only allowed symbols are “+”, “-“ and “^”.

4.2 Testing
The testing was done using JUnit Tests, which appear in the package
test, class
UnitTest. In this class, every method from the Calculus class (Computations
package) was tested. I will insert here one example here for the most
difficult operation (division). The rest of the tests are similar.

@Test
public void div()
{
Polynom p1 = new Polynom();
Polynom p2 = new Polynom();
ArrayList<Polynom> p3 = new ArrayList<>( );
Polynom q = new Polynom();
Polynom r = new Polynom();

p1.extractMonomDetails( "1x^3-2x^2+3x^1-1x^0" );
p2.extractMonomDetails( "1x^1+2x^0" );
q.extractMonomDetails( "1x^2-4x^1+11x^0" );
r.extractMonomDetails( "-23x^0" );
p3 = Calculus.div( p1, p2 );
boolean test1 = q.equals( p3.get( 0 ) );
boolean test2 = r.equals( p3.get( 1 ) );
boolean testfin = test1 && test2;
assertEquals( true, testfin );
}

5. Results
I managed to implement and develop a fully function polynomial
calculator used for
polynomial processing. Some representative screenshots can be seen below.

Result of addition:

Invalid input:

6. Conclusions and future developments


6.1 Conclusions
Implementing a program like this can be hard when deciding what to use for
the implementation and how to implement it (ex. To have 2 polynomials and
update one with the result or have 3 polynomials and put the result of the
operation in the third). Implementing the multiplication and the division was
especially difficult even after managing to implement the others. The number
of hours was more than expected in solving this project. The number of hours
may be because of the lack of exercise with programming techniques and
because of the fact that I had to rethink the algorithms 5-10 times until I got
them right. Deciding for an efficient way to have float coefficients for the
division and integration operations was quite difficult, but the
implementation of it was easier than expected.

6.2 Future developments


There are many additions that can be made to this application, like improving
the range of functionality.
Examples of improvements:
 Coefficients of float type for real coefficients
 Doing operations with polynomials that have more than one variable
 There can be more operations, such as: finding the root of a
polynomial, calculating the polynomial for a certain value, verify
equality which could be used in case of long unordered polynomials
 The input has a strict form; it could be loosened to make it easier for
users to introduce data
 There could be implemented a button that can switch between the
inputs instead of having a button for deriving the first polynomial and
another for deriving the second one
 There could be implemented a button that can put the output in one of
the inputs of our choice, for n-grade derivation or just implement the n-
grade derivation
 Draw the graphic for the polynomial functions

7. Bibliography
o www.stackoverflow.com
o www.wikipedia.org
o www.gliffy.com
o www.docs.oracle.com/javase/7

Você também pode gostar