Two Caveats
- all instance variables must be explicitly written as “private”
- Declare ArrayList’s as type “list”:
The classes below represent different ways of representing mathematical concepts like MathExpressions, Fractions etc. and they all implement the Mathable interface.
public abstract class GraphicalMathObject
methods
- public abstract void displayGraphicalRepresentation() ;
public class Fraction extends GraphicalMathObject
This class is used to represent Fractions
Instance Variables
- private int numerator
- private int denominator
Constructor : The constructor should initialize both instance variables
- public Fraction(int num, int denom)
Methods
- public void displayGraphicalRepresentation() ; (just print out ” I’m a fraction -displayGraphicalRepresentation()”. This is the method that renders a graphical representation of the fraction .
- private int getGCF() //returns the GCF of numerator and denominator
- public void simplify()//simplifies the fraction. Note: you must call getGCF()
- public Fraction clone() //return a clone of the current object (** Extra credit…research this) ( link )
- plus all of the methods in Mathable (Described at bottom)
public class MathExpression extends GraphicalMathObject
This is a class that is used to represent mathematical expressions like 3x2or 5x2y2 . It contains the methods and variables described below and it must implement Mathable
Instance Variables
- private int coefficient
- private List<String> variables
- private List<Integer> powers
NOTE: you should use the following sytnax
Declare ArrayList’s as type “list”:
Constructor : You can decide how to write the constructor.
- Just be sure to initialize the instance variables
methods implement all methods of the Mathable Interface in ways that make sense for this class. The idea with Mathable is that you should be able to tell if the objects are either equivalent, exactly the same and that you should be able to return a String representation (data) that uniquely identifies the given mathematical expression.
-
public void addTerm( String term , int power) ) // This is used to represent multiplication, actually and this method adds a term with a power to the object’s list of
variables
and power to the list ofpowers
. You can not assume that the term does not yet exist in the expression .
For instance, if the original term were 3x5 and the object called addTerm("y", 6) the object would then be 3x5y6. Or for another example, if the original term is 2x3z6 , and addTerm("z", 5) the object would be 2x3z11 . - public void displayGraphicalRepresentation() ; (just print out “I”m a MathExpression.: displayGraphicalRepresentation()”). This is the method that draws the MathExpression on the screen like:
- plus all of the methods in Mathable
Examples
3x2y5 |
coefficient: 3 variables : [ “x”, “y”] powers : [ 2, 5] |
x1y11 |
coefficient: 1 variables : [ “x”, “y”] powers : [ 1, 11] |
3y2x5 |
coefficient: 3 variables : [ “y”, “x”] powers : [ 5 , 2] |
Interface : Mathable
- public String getData( ) ; represents core mathematical information that defines current object.
- public boolean exactlyEquals(Mathable other) ;
- public boolean isEquivalent(Mathable other) ;