Unit Testing Your Classes
You have created several classes in the previous two labs. Below are the names of these classes:
- Fraction
- Point
- Rectangle
main()
method in the class for the purpose of testing.
- Create a
main()
method in each of the classes you have developed. - Instantiate an object of each class in its respective
main()
method. - Use
System.out.println()
and print each object.
main()
method of the class in Eclipse, you will need to bring the class into focus in the editor and click Run. To run the class from the command-line, create the test
directory in the same directory as your src
directory if it doesn't exist. Then compile and run each class separately from the test
directory as shown below for the Fraction class.
javac -d . ../src/lab3/Fraction.java java lab3.FractionYou will notice that in the classes where you haven't implemented the
toString()
method, the objects are represented in a way you might not have expected. This is the default implementation of the toString()
method in Java. You may use the toString()
methods in the code below for the Rectangle and Point classes. Code for each of the classes follows for those that were not able to complete the previous labs.
- Fraction
package lab3; /** * Should include a description of the Fraction class and its invariant * * @author */ public class Fraction { /** * An int representing the numerator of the fraction. */ private int numerator; /** * An int representing the denominator of the fraction. */ private int denominator; /** * Constructs a new Fraction with a provided numerator and denominator * @param n the numerator of the new Fraction * @param d the denominator of the new Fraction */ public Fraction(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } /** * Retrieves the value of the numerator * @return An int representing the numerator */ public int getNumerator() { return numerator; } /** * Retrieves the value of the denominator * @return An int representing the denominator */ public int getDenominator() { return denominator; } /** * Sets the value of the numerator to the provided value * @param n the value to which the numerator should be changed. */ public void setNumerator(int n) { this.numerator = n; } /** * Sets the value of the denominator to the provided value * @param n the value to which the denominator should be changed. */ public void setDenominator(int d) { this.denominator = d; } /** * Returns a String representation of this Fraction. * @return A String representation of this Fraction */ public String toString() { String str = "Fraction:\n"; str += " "; return str; } /** * Returns the decimal value of the Fraction * @return A double representing the decimal value of this Fraction */ public double decimalValue() { return (double)numerator/(double)denominator; } /** * Multiples this Fraction by a given Fraction * @param otherFraction the Fraction to multiply by * @return A Fraction that is the result of multiplying this Fraction by the given Fraction */ public Fraction multiply( Fraction otherFraction ) { int n = this.numerator * otherFraction.numerator; int d = this.denominator * otherFraction.denominator; return new Fraction(n,d); } /** * Returns whether this Fraction is equal to a given Fraction. * @param other The Fraction to check equality against * @return A boolean signifying whether or not this Fraction and the given Fraction are equal */ public boolean equals( Fraction f ) { if( this.numerator == f.numerator && this.denominator == f.denominator ) { return true; } else return false; } } }
package lab4; /** * Should include a description of the Point class and its invariant * * @author */ public class Point { int x, y; public Point ( int x, int y ) { this.x = x; this.y = y; } public Point (Point obj){ this.x = obj.getX(); this.y = obj.getY(); } public int getX() { return x; } public int getY() { return y; } public static double distance ( Point point1, Point point2 ) { int deltaX = point1.x - point2.x; int deltaY = point1.y - point2.y; return Math.sqrt(Math.pow(deltaX,2) + Math.pow(deltaY, 2)); } public String toString() { return "(" + x + ", " + y + ")"; } }
package lab4; /** * Should include a description of the Rectangle class and its invariant * * * @author */ public class Rectangle { private Point upperLeft, lowerLeft, lowerRight, upperRight; public Rectangle (Point upperLeft, Point lowerLeft, Point lowerRight, Point upperRight) { this.upperLeft = upperLeft; this.lowerLeft = lowerLeft; this.lowerRight = lowerRight; this.upperRight = upperRight; } public Rectangle(Rectangle original) { upperLeft = new Point(original.upperLeft); lowerLeft = new Point(original.lowerLeft); lowerRight = new Point(original.lowerRight); upperRight = new Point(original.upperRight); } public double getLength () { return Point.distance(upperLeft,upperRight); } public double getWidth () { return Point.distance(upperLeft,lowerLeft); } public double getArea () { return getWidth() * getLength(); } public double getPerimeter () { return 2 * getWidth() + 2 * getLength(); } public String toString() { String str = "\nRectangle:"; str += "\n\tUpper Left:" + upperLeft.toString(); str += "\n\tLower Left:" + lowerLeft.toString(); str += "\n\tLower Right:" + lowerRight.toString(); str += "\n\tUpper Right: " + upperRight.toString(); return str; } }