Very Simple OOP Example
parent
9a1fa9479d
commit
3bf3458c82
@ -0,0 +1,25 @@
|
||||
public class Coords {
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Coords(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
public class Line {
|
||||
private double gradient;
|
||||
private double intercept;
|
||||
|
||||
private int x1;
|
||||
private int x2;
|
||||
private int y1;
|
||||
private int y2;
|
||||
|
||||
public Line(Coords coords1, Coords coords2) {
|
||||
this.x1 = coords1.getX();
|
||||
this.y1 = coords1.getY();
|
||||
this.x2 = coords2.getX();
|
||||
this.y2 = coords2.getY();
|
||||
}
|
||||
|
||||
public void calculateGradient() {
|
||||
gradient = (y2 - y1) / (x2 - x1);
|
||||
}
|
||||
|
||||
public void calculateIntercept() {
|
||||
intercept = y1 - (gradient * x1);
|
||||
}
|
||||
|
||||
public void displayEquation() {
|
||||
System.out.println("y = " + gradient + "x + " + intercept);
|
||||
}
|
||||
|
||||
public double getGradient() {
|
||||
return gradient;
|
||||
}
|
||||
|
||||
public void setGradient(int gradient) {
|
||||
this.gradient = gradient;
|
||||
}
|
||||
|
||||
public double getIntercept() {
|
||||
return intercept;
|
||||
}
|
||||
|
||||
public void setIntercept(int intercept) {
|
||||
this.intercept = intercept;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Coords p1 = new Coords(4, 16);
|
||||
Coords p2 = new Coords(2, 8);
|
||||
|
||||
Line myLine = new Line(p1, p2);
|
||||
|
||||
myLine.calculateGradient();
|
||||
myLine.calculateIntercept();
|
||||
myLine.displayEquation();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue