המחלקה Point
public class Point {
private double x;
private double y;
public Point (){
this.x = 0;
this.y = 0;
}
public Point (double x,double y){
this.x = x;
this.y = y;
}
public void setX(double x){
this.x = x;
}
public void setY(double y){
this.y = y;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public double dis(double x1,double y1){
double dx,dy,c;
dx = Math.abs(x–x1);
dy = Math.abs(y–y1);
c = Math.sqrt(dx*dx+dy*dy);
return c;
}
public String toString(){
return "("+x+","+y+")";
}
}
שימוש ב-Point
double jadek;
Point p1 = new Point(1,7);
Point p2 = new Point(6,3);
System.out.println("p1 –> x = "+p1.getX());
System.out.println("p1 –> y = "+p1.getY());
System.out.println("p2 –> x = "+p2.getX());
System.out.println("p2 –> y = "+p2.getY());
jadek = p2.dis(p1.getX(), p1.getY());
System.out.println("c = "+jadek);
המחלקה Pen
public class Pen {
private char color;
private int thickness;
private double ml;
public Pen(char color,int thickness,double ml){
this.color = color;
this.thickness = thickness;
this.ml = ml;
}
}
בניית שני עצמים מטיפוס Pen
Pen pen1 = new Pen('b',5,20);
Pen pen2 = new Pen('c',5,40);