Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- Point p = new Point(5, 6);
- Point q = new Point(-1, -8);
- LineSegment l = new LineSegment(p, q);
- System.out.println(l);
- System.out.println("The midpoint of ths line segment is: " + l.midpoint());
- System.out.println("The length of this lin segment is: " + l.lengthOfSegment());
- System.out.println("The slope of this line segment is: " + l.slope());
- }
- }
- class Point {
- double x;
- double y;
- public Point(double x, double y) {
- this.x = x;
- this.y = y;
- }
- double distanceFromOrigin() {
- return Math.sqrt(x * x + y * y);
- }
- public String toString() {
- return "(" + x + ", " + y + ")";
- }
- }
- class LineSegment {
- Point a;
- Point b;
- public LineSegment(Point a, Point b) {
- this.a = a;
- this.b = b;
- }
- public String toString() {
- return "LIne segment from " + a + " to " + b;
- }
- double lengthOfSegment() {
- return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
- }
- Point midpoint() {
- return new Point((a.x + b.x) / 2, (a.y + b.y) / 2);
- }
- double slope() {
- return (a.y - b.y) / (a.x - b.x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement