|
The ClayGraphics clay.math package consists of 1 Java Interface and 1 Java Class which implement the interfaces. Additionally, 1 Java Main Classes are crafted to test the Java Classes.
|
init:
deps-jar:
compile-single:
run-single:
>>> creating four points (testing the constructors)
>>> displaying four points (testing toString())
a = (0,0)
b = (30,40)
c = (80,50)
d = (30,40)
>>> referencing coordinates (testing x() and y())
x of a = 0; y of a = 0
x of b = 30; y of b = 40
x of c = 80; y of c = 50
x of d = 30; y of d = 40
>>> compute distances (testing distance())
distance from a to b = 50.0
distance from a to c = 94.33981132056604
distance from b to c = 50.99019513592785
distance from b to d = 0.0
>>> check equality (testing equals())
a equals b ? false
b equals b ? true
c equals b ? false
d equals b ? true
>>> check point movement (testing moveTo())
>>> move a to (1.0,1.0)
>>> take 1.0 from each coordinate of b
>>> exchange the coordinate of c
>>> leave d alone
a = (1,1)
b = (29,39)
c = (50,80)
d = (30,40)
>>> create 3 random points (testing random())
>>> first coordinate: [0,100]
>>> second coordinate: [0,1000]
(642,19)
(59,65)
(325,10)
>>> create 3 random points and copy them (testing copy)
(569,1)
(890,33)
(385,62)
(569,1)
(890,33)
(385,62)
BUILD SUCCESSFUL (total time: 2 seconds)
|
/*
* ClayPointADT.java
*
* Created on June 6, 2006, 1:49 PM
*/
package clay.math;
/**
*
* @author blue
*/
public interface ClayPointADT {
public int x();
public int y();
public ClayPoint copy();
public double distance(ClayPoint p);
public boolean equals(ClayPoint p);
public String toString();
public void moveTo(int newx,int newy);
public void random(int h, int w);
}
/*
* ClayPoint.java
*
* Created on June 5, 2006, 7:59 PM
*/
package clay.math;
/**
*
* @author blue
*/
import java.awt.*;
import java.awt.image.*;
public class ClayPoint extends Point implements ClayPointADT {
public ClayPoint()
{
x = 0;
y = 0;
}
public ClayPoint(int x, int y)
{
this.x = x;
this.y = y;
}
public int x()
{
return x;
}
public int y()
{
return y;
}
public ClayPoint copy()
{
return new ClayPoint(this.x,this.y);
}
public double distance(ClayPoint p)
{
double deltax = x - p.x();
double deltay = y - p.y();
double deltaXsquared = deltax * deltax;
double deltaYsquared = deltay * deltay;
double sum = deltaXsquared + deltaYsquared;
return (double)Math.sqrt(sum);
}
public boolean equals(ClayPoint p)
{
boolean result = ( this.x() == p.x() ) && ( this.y() == p.y() );
return result;
}
public String toString()
{
return "(" + x + "," + y + ")";
}
public void moveTo(int newx, int newy)
{
x = newx;
y = newy;
}
public void random(int h, int w)
{
int x = rn(0,w);
int y = rn(0,h);
this.x = x;
this.y = y;
}
static private int rn(int low, int high)
{
double rn = (double)( Math.random() * ( high - low + 1 ) );
rn = low + rn;
int result = (int)Math.floor(rn);
return result;
}
}
/*
* ClayPointTest.java
*
* Created on June 6, 2006, 2:12 PM
*/
package testers;
import clay.math.ClayPoint;
/**
*
* @author blue
*/
public class ClayPointTest {
static private ClayPoint a;
static private ClayPoint b;
static private ClayPoint c;
static private ClayPoint d;
public ClayPointTest() {
}
public static void main(String[] args) {
createPoints();
displayPoints();
referenceCoordinates();
computeDistances();
checkEquality();
movePoints();
randomPoints();
copyPoints();
}
static private void createPoints()
{
report("creating four points (testing the constructors)");
a = new ClayPoint();
b = new ClayPoint(30,40);
c = new ClayPoint(80,50);
d = new ClayPoint(30,40);
}
static private void displayPoints()
{
report("displaying four points (testing toString())");
System.out.print("a = "); display(a);
System.out.print("b = "); display(b);
System.out.print("c = "); display(c);
System.out.print("d = "); display(d);
}
static private void referenceCoordinates()
{
report("referencing coordinates (testing x() and y())");
System.out.print("x of a = " + a.x());
System.out.println("; y of a = " + a.y());
System.out.print("x of b = " + b.x());
System.out.println("; y of b = " + b.y());
System.out.print("x of c = " + c.x());
System.out.println("; y of c = " + c.y());
System.out.print("x of d = " + d.x());
System.out.println("; y of d = " + d.y());
}
static private void computeDistances()
{
report("compute distances (testing distance())");
System.out.println("distance from a to b = " + a.distance(b));
System.out.println("distance from a to c = " + a.distance(c));
System.out.println("distance from b to c = " + b.distance(c));
System.out.println("distance from b to d = " + b.distance(d));
}
static private void checkEquality()
{
report("check equality (testing equals())");
System.out.println("a equals b ? " + b.equals(a));
System.out.println("b equals b ? " + b.equals(b));
System.out.println("c equals b ? " + b.equals(c));
System.out.println("d equals b ? " + b.equals(d));
}
static private void movePoints()
{
report("check point movement (testing moveTo())");
report("move a to (1.0,1.0)");
report("take 1.0 from each coordinate of b");
report("exchange the coordinate of c");
report("leave d alone");
a.moveTo(1,1);
b.moveTo(b.x()-1,b.y()-1);
c.moveTo(c.y(),c.x());
d.moveTo(d.x(),d.y());
System.out.print("a = "); display(a);
System.out.print("b = "); display(b);
System.out.print("c = "); display(c);
System.out.print("d = "); display(d);
}
static private void randomPoints()
{
report("create 3 random points (testing random())");
report("first coordinate: [0,100]");
report("second coordinate: [0,1000]");
ClayPoint p = new ClayPoint();
p.random(100,1000);
display(p);
p.random(100,1000);
display(p);
p.random(100,1000);
display(p);
}
static private void copyPoints()
{
report("create 3 random points and copy them (testing copy)");
ClayPoint p = new ClayPoint();
p.random(100,1000);
display(p);
ClayPoint c1 = p.copy();
p.random(100,1000);
display(p);
ClayPoint c2 = p.copy();
p.random(100,1000);
display(p);
ClayPoint c3 = p.copy();
display(c1);
display(c2);
display(c3);
}
static private void report(String s)
{
System.out.println(">>> " + s);
}
static private void display(ClayPoint p)
{
System.out.println(p.toString());
}
}
|