bay naa ko modified versions sa imong mga codes.. ako lang i post..
i compare nalang ang mga modified versions with the ones you have..
then, if naa ka questions.. ask nalang nya for clarifications..
Point.java, located in C:\Java\Packages\Geometry
Code:
package Geometry;
public class Point {
public Point() {
this.x = 0;
this.y = 0;
}
public Point(double xVal, double yVal){
x = xVal;
y = yVal;
}
public Point(final Point oldPoint){
x = oldPoint.x;
y = oldPoint.y;
}
public void move(double xDelta, double yDelta){
x += xDelta;
y += yDelta;
}
public double distance(final Point aPoint){
return (Math.sqrt((x-aPoint.x)*(x-aPoint.x) + (y-aPoint.y)*(y-aPoint.y)));
}
public String toString(){
return Double.toString(x) + ", " + y;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public void setX(double inputX){
x = inputX;
}
public void setY(double inputY){
y = inputY;
}
public void setPoints(double x, double y) {
this.x = x;
this.y = y;
}
private double x;
private double y;
}
Rectangle.java, located in C:\Java\Packages\Geometry
Code:
package Geometry;
public class Rectangle{
Point[] point = null;
public Rectangle(){
this.initialize();
point[0].setPoints(0,0);
point[1].setPoints(1,0);
point[2].setPoints(0,1);
point[3].setPoints(1,1);
}
public Rectangle(double point1_x,double point1_y,double point2_x, double point2_y){
this.initialize();
point[0].setPoints(point1_x, point1_y);
point[3].setPoints(point2_x, point2_y);
point[1].setPoints(point2_x, point1_y);
point[2].setPoints(point1_x, point2_y);
}
public Rectangle(final Rectangle oldRect){
point[0] = oldRect.point[0];
point[3] = oldRect.point[3];
point[1] = oldRect.point[1];
point[2] = oldRect.point[2];
}
public double getWidth(){
return point[0].distance(point[1]);
}
public static void printRectangle(final Rectangle rect){
for(int i= 0;i<4;i++){
System.out.println("Corner"+(i+1)+" X: "+rect.point[i].getX()+" Corner"+(i+1)+" Y: "+rect.point[i].getY());
}
System.out.println();
}
private void initialize() {
this.point = new Point[4];
for (int i = 0; i < this.point.length; i++) {
this.point[i] = new Point();
}
}
}
MyRect.java, located in My Documents
Code:
import Geometry.*;
public class MyRect{
public static void main(String[] args){
Rectangle myRect = new Rectangle(0.0,0.0,2.0,1.0);
Rectangle.printRectangle(myRect);
}
}
compiled with the following command:
Code:
C:\Documents and Settings\Administrator\My Documents>javac -cp ".;C:\Java\Packages" MyRect.java
run with the following command:
Code:
C:\Documents and Settings\Administrator\My Documents>java -cp ".;C:\Java\Packages" MyRect
and lastly, ang output:
Code:
Corner1 X: 0.0 Corner1 Y: 0.0
Corner2 X: 2.0 Corner2 Y: 0.0
Corner3 X: 0.0 Corner3 Y: 1.0
Corner4 X: 2.0 Corner4 Y: 1.0
main reasons for modifications sa code is NullPointerException thrown due to access of uninitialized objects and access to non-existent method, namely setPoints, inside the Rectangle class..
if naa questions, or in need of more explainations or clarifications.. post lang.. ill do my best to help..
good luck with java! :mrgreen: