Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Point
- // Spec: In this program we will again be utilizing an Object Oriented Design to model points and perform simple linear calculations.
- public class Point
- {
- private double x, y;
- // default constructor
- public Point()
- {
- x = 0;
- y = 0;
- }
- // parameter constructor
- public Point(double x, double y)
- {
- this.x = x;
- this.y = y;
- }
- // returns the x
- public double getX()
- {
- return x;
- }
- // returns the y
- public double getY()
- {
- return y;
- }
- // sets the x
- public void setX(double x)
- {
- this.x = x;
- }
- // sets the y
- public void setY(double y)
- {
- this.y = y;
- }
- // assigns x and y new points
- public void assignPoint(double newX, double newY)
- {
- x = newX;
- y = newY;
- System.out.println("(" + newX + ", " + newY + ")");
- }
- // moves x how many units you choose
- public void moveX(double units)
- {
- x += units;
- }
- // moves y how many units you choose
- public void moveY(double units)
- {
- y += units;
- }
- // gets the slope: y = mx + b
- // changed the method type so we can get the slope in a double instead of an integer
- public double getSlope(Point b)
- {
- double m = 0;
- m = (b.getY() - y) / (b.getX() - x);
- return m;
- }
- // gets the distance between the two points
- public double getDistance(Point b)
- {
- double d = Math.sqrt(Math.pow(x - b.getX(), 2) + Math.pow(y - b.getY(), 2));
- return d;
- }
- // gets the midpoint between two points
- public Point getMidpoint(Point b)
- {
- double x1 = (x + b.getX()) / 2.0;
- double y1 = (y + b.getY()) / 2.0;
- Point midpoint = new Point(x1, y1);
- return midpoint;
- }
- // gets the equation of two pairs of points using y = mx + b form
- public String getEquation(Point b)
- {
- String s;
- double m = (b.getY() - y) / (b.getX() - x);
- // point-slope formula
- double d1 = y - b.getY();
- double d2 = m * (x - b.getX());
- s = "y = " + m + "x + " + b.getY();
- return s;
- }
- // writes the output of the program
- public String toString()
- {
- String s;
- s = "(" + x + ", " + y + ")";
- return s;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Point
- // Spec: In this program we will again be utilizing an Object Oriented Design to model points and perform simple linear calculations.
- import java.util.Scanner;
- public class PointClient
- {
- public static void main(String args[])
- {
- Point p1 = new Point();
- Point p2 = new Point();
- Scanner sc = new Scanner(System.in);
- boolean running = true;
- int choice;
- double d1, d2;
- // welcoming message
- System.out.println("Welcome to the point class! Please choose a number from 1-7! \n");
- while(running)
- {
- System.out.println("1) Assign Points");
- System.out.println("2) Print Points");
- System.out.println("3) Get Slope");
- System.out.println("4) Get Distance");
- System.out.println("5) Get Midpoint");
- System.out.println("6) Print Equation");
- System.out.println("7) Quit");
- choice = sc.nextInt();
- switch(choice)
- {
- case 1:
- System.out.println("Point 1: ");
- System.out.println("What would you like to assign to the x variable?");
- d1 = sc.nextDouble();
- System.out.println("What would you like to assign to the y variable?");
- d2 = sc.nextDouble();
- p1.assignPoint(d1, d2);
- System.out.println("Point 2: ");
- System.out.println("What would you like to assign to the x variable?");
- d1 = sc.nextDouble();
- System.out.println("What would you like to assign to the y variable?");
- d2 = sc.nextDouble();
- p2.assignPoint(d1, d2);
- System.out.println("\nWhat would you like to do next? \n");
- break;
- case 2:
- System.out.println("Point 1: (" + p1.getX() + ", " + p1.getY() + ")");
- System.out.println("Point 2: (" + p2.getX() + ", " + p2.getY() + ")");
- System.out.println("\nWhat would you like to do next? \n");
- break;
- case 3:
- System.out.println("Slope Between Point 1 & Point 2: " + p1.getSlope(p2));
- System.out.println("\nWhat would you like to do next? \n");
- break;
- case 4:
- System.out.println("Distance Between Point 1 & Point 2: " + p1.getDistance(p2));
- System.out.println("\nWhat would you like to do next? \n");
- break;
- case 5:
- System.out.println("Midpoint Between Point 1 & Point 2: " + p1.getMidpoint(p2));
- System.out.println("\nWhat would you like to do next? \n");
- break;
- case 6:
- System.out.println("The Equation: " + p1.getEquation(p2));
- System.out.println("\nWhat would you like to do next? \n");
- break;
- case 7:
- System.out.println("Bye-bye!");
- System.exit(0);
- break;
- default:
- System.out.println("Sorry, you put in a wrong charatcer. Please input a number of 1-7. \n");
- }
- }
- }
- }
- /*
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 4
- Distance Between Point 1 & Point 2: 8.54400374531753
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 5
- Midpoint Between Point 1 & Point 2: (-4.0, 5.5)
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 6
- The Equation: y = -0.375x + 4.0
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 1
- Point 1:
- What would you like to assign to the x variable?
- 2
- What would you like to assign to the y variable?
- 5
- (2.0, 5.0)
- Point 2:
- What would you like to assign to the x variable?
- 2
- What would you like to assign to the y variable?
- 9
- (2.0, 9.0)
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 2
- Point 1: (2.0, 5.0)
- Point 2: (2.0, 9.0)
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 3
- Slope Between Point 1 & Point 2: Infinity
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 4
- Distance Between Point 1 & Point 2: 4.0
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 5
- Midpoint Between Point 1 & Point 2: (2.0, 7.0)
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 6
- The Equation: y = Infinityx + 9.0
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 1
- Point 1:
- What would you like to assign to the x variable?
- 4
- What would you like to assign to the y variable?
- 6
- (4.0, 6.0)
- Point 2:
- What would you like to assign to the x variable?
- -2
- What would you like to assign to the y variable?
- 6
- (-2.0, 6.0)
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 2
- Point 1: (4.0, 6.0)
- Point 2: (-2.0, 6.0)
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 3
- Slope Between Point 1 & Point 2: -0.0
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 4
- Distance Between Point 1 & Point 2: 6.0
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 5
- Midpoint Between Point 1 & Point 2: (1.0, 6.0)
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 6
- The Equation: y = -0.0x + 6.0
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 1
- Point 1:
- What would you like to assign to the x variable?
- -4
- What would you like to assign to the y variable?
- 0
- (-4.0, 0.0)
- Point 2:
- What would you like to assign to the x variable?
- 0
- What would you like to assign to the y variable?
- 4
- (0.0, 4.0)
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 2
- Point 1: (-4.0, 0.0)
- Point 2: (0.0, 4.0)
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 3
- Slope Between Point 1 & Point 2: 1.0
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 4
- Distance Between Point 1 & Point 2: 5.656854249492381
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 5
- Midpoint Between Point 1 & Point 2: (-2.0, 2.0)
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 6
- The Equation: y = 1.0x + 4.0
- What would you like to do next?
- 1) Assign Points
- 2) Print Points
- 3) Get Slope
- 4) Get Distance
- 5) Get Midpoint
- 6) Print Equation
- 7) Quit
- 7
- Bye-bye!
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement