Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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.
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).
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 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.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:
7. Bibliography
o www.stackoverflow.com
o www.wikipedia.org
o www.gliffy.com
o www.docs.oracle.com/javase/7