Advertisement
Crenox

Point Java Program

Oct 20th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1. // Sammy Samkough
  2. // Point
  3. // Spec: In this program we will again be utilizing an Object Oriented Design to model points and perform simple linear calculations.
  4.  
  5. public class Point
  6. {
  7. private double x, y;
  8.  
  9. // default constructor
  10. public Point()
  11. {
  12. x = 0;
  13. y = 0;
  14. }
  15.  
  16. // parameter constructor
  17. public Point(double x, double y)
  18. {
  19. this.x = x;
  20. this.y = y;
  21. }
  22.  
  23. // returns the x
  24. public double getX()
  25. {
  26. return x;
  27. }
  28.  
  29. // returns the y
  30. public double getY()
  31. {
  32. return y;
  33. }
  34.  
  35. // sets the x
  36. public void setX(double x)
  37. {
  38. this.x = x;
  39. }
  40.  
  41. // sets the y
  42. public void setY(double y)
  43. {
  44. this.y = y;
  45. }
  46.  
  47. // assigns x and y new points
  48. public void assignPoint(double newX, double newY)
  49. {
  50. x = newX;
  51. y = newY;
  52.  
  53. System.out.println("(" + newX + ", " + newY + ")");
  54. }
  55.  
  56. // moves x how many units you choose
  57. public void moveX(double units)
  58. {
  59. x += units;
  60. }
  61.  
  62. // moves y how many units you choose
  63. public void moveY(double units)
  64. {
  65. y += units;
  66. }
  67.  
  68. // gets the slope: y = mx + b
  69. // changed the method type so we can get the slope in a double instead of an integer
  70. public double getSlope(Point b)
  71. {
  72. double m = 0;
  73.  
  74. m = (b.getY() - y) / (b.getX() - x);
  75.  
  76. return m;
  77. }
  78.  
  79. // gets the distance between the two points
  80. public double getDistance(Point b)
  81. {
  82. double d = Math.sqrt(Math.pow(x - b.getX(), 2) + Math.pow(y - b.getY(), 2));
  83.  
  84. return d;
  85. }
  86.  
  87. // gets the midpoint between two points
  88. public Point getMidpoint(Point b)
  89. {
  90. double x1 = (x + b.getX()) / 2.0;
  91. double y1 = (y + b.getY()) / 2.0;
  92. Point midpoint = new Point(x1, y1);
  93.  
  94. return midpoint;
  95. }
  96.  
  97. // gets the equation of two pairs of points using y = mx + b form
  98. public String getEquation(Point b)
  99. {
  100. String s;
  101. double m = (b.getY() - y) / (b.getX() - x);
  102.  
  103. // point-slope formula
  104. double d1 = y - b.getY();
  105. double d2 = m * (x - b.getX());
  106.  
  107. s = "y = " + m + "x + " + b.getY();
  108.  
  109. return s;
  110. }
  111.  
  112. // writes the output of the program
  113. public String toString()
  114. {
  115. String s;
  116.  
  117. s = "(" + x + ", " + y + ")";
  118.  
  119. return s;
  120. }
  121. }
  122. -------------------------------------------------------------------------------------------------------------------------------
  123. // Sammy Samkough
  124. // Point
  125. // Spec: In this program we will again be utilizing an Object Oriented Design to model points and perform simple linear calculations.
  126.  
  127. import java.util.Scanner;
  128.  
  129. public class PointClient
  130. {
  131. public static void main(String args[])
  132. {
  133. Point p1 = new Point();
  134. Point p2 = new Point();
  135. Scanner sc = new Scanner(System.in);
  136.  
  137. boolean running = true;
  138. int choice;
  139. double d1, d2;
  140.  
  141. // welcoming message
  142. System.out.println("Welcome to the point class! Please choose a number from 1-7! \n");
  143.  
  144. while(running)
  145. {
  146. System.out.println("1) Assign Points");
  147. System.out.println("2) Print Points");
  148. System.out.println("3) Get Slope");
  149. System.out.println("4) Get Distance");
  150. System.out.println("5) Get Midpoint");
  151. System.out.println("6) Print Equation");
  152. System.out.println("7) Quit");
  153.  
  154. choice = sc.nextInt();
  155.  
  156. switch(choice)
  157. {
  158. case 1:
  159. System.out.println("Point 1: ");
  160. System.out.println("What would you like to assign to the x variable?");
  161. d1 = sc.nextDouble();
  162. System.out.println("What would you like to assign to the y variable?");
  163. d2 = sc.nextDouble();
  164. p1.assignPoint(d1, d2);
  165. System.out.println("Point 2: ");
  166. System.out.println("What would you like to assign to the x variable?");
  167. d1 = sc.nextDouble();
  168. System.out.println("What would you like to assign to the y variable?");
  169. d2 = sc.nextDouble();
  170. p2.assignPoint(d1, d2);
  171.  
  172. System.out.println("\nWhat would you like to do next? \n");
  173. break;
  174.  
  175. case 2:
  176. System.out.println("Point 1: (" + p1.getX() + ", " + p1.getY() + ")");
  177. System.out.println("Point 2: (" + p2.getX() + ", " + p2.getY() + ")");
  178.  
  179. System.out.println("\nWhat would you like to do next? \n");
  180. break;
  181.  
  182. case 3:
  183. System.out.println("Slope Between Point 1 & Point 2: " + p1.getSlope(p2));
  184.  
  185. System.out.println("\nWhat would you like to do next? \n");
  186. break;
  187.  
  188. case 4:
  189. System.out.println("Distance Between Point 1 & Point 2: " + p1.getDistance(p2));
  190.  
  191. System.out.println("\nWhat would you like to do next? \n");
  192. break;
  193.  
  194. case 5:
  195. System.out.println("Midpoint Between Point 1 & Point 2: " + p1.getMidpoint(p2));
  196.  
  197. System.out.println("\nWhat would you like to do next? \n");
  198. break;
  199.  
  200. case 6:
  201. System.out.println("The Equation: " + p1.getEquation(p2));
  202.  
  203. System.out.println("\nWhat would you like to do next? \n");
  204. break;
  205.  
  206. case 7:
  207. System.out.println("Bye-bye!");
  208. System.exit(0);
  209. break;
  210.  
  211. default:
  212. System.out.println("Sorry, you put in a wrong charatcer. Please input a number of 1-7. \n");
  213. }
  214. }
  215. }
  216. }
  217. /*
  218.  
  219. 1) Assign Points
  220. 2) Print Points
  221. 3) Get Slope
  222. 4) Get Distance
  223. 5) Get Midpoint
  224. 6) Print Equation
  225. 7) Quit
  226. 4
  227. Distance Between Point 1 & Point 2: 8.54400374531753
  228.  
  229. What would you like to do next?
  230.  
  231. 1) Assign Points
  232. 2) Print Points
  233. 3) Get Slope
  234. 4) Get Distance
  235. 5) Get Midpoint
  236. 6) Print Equation
  237. 7) Quit
  238. 5
  239. Midpoint Between Point 1 & Point 2: (-4.0, 5.5)
  240.  
  241. What would you like to do next?
  242.  
  243. 1) Assign Points
  244. 2) Print Points
  245. 3) Get Slope
  246. 4) Get Distance
  247. 5) Get Midpoint
  248. 6) Print Equation
  249. 7) Quit
  250. 6
  251. The Equation: y = -0.375x + 4.0
  252.  
  253. What would you like to do next?
  254.  
  255. 1) Assign Points
  256. 2) Print Points
  257. 3) Get Slope
  258. 4) Get Distance
  259. 5) Get Midpoint
  260. 6) Print Equation
  261. 7) Quit
  262. 1
  263. Point 1:
  264. What would you like to assign to the x variable?
  265. 2
  266. What would you like to assign to the y variable?
  267. 5
  268. (2.0, 5.0)
  269. Point 2:
  270. What would you like to assign to the x variable?
  271. 2
  272. What would you like to assign to the y variable?
  273. 9
  274. (2.0, 9.0)
  275.  
  276. What would you like to do next?
  277.  
  278. 1) Assign Points
  279. 2) Print Points
  280. 3) Get Slope
  281. 4) Get Distance
  282. 5) Get Midpoint
  283. 6) Print Equation
  284. 7) Quit
  285. 2
  286. Point 1: (2.0, 5.0)
  287. Point 2: (2.0, 9.0)
  288.  
  289. What would you like to do next?
  290.  
  291. 1) Assign Points
  292. 2) Print Points
  293. 3) Get Slope
  294. 4) Get Distance
  295. 5) Get Midpoint
  296. 6) Print Equation
  297. 7) Quit
  298. 3
  299. Slope Between Point 1 & Point 2: Infinity
  300.  
  301. What would you like to do next?
  302.  
  303. 1) Assign Points
  304. 2) Print Points
  305. 3) Get Slope
  306. 4) Get Distance
  307. 5) Get Midpoint
  308. 6) Print Equation
  309. 7) Quit
  310. 4
  311. Distance Between Point 1 & Point 2: 4.0
  312.  
  313. What would you like to do next?
  314.  
  315. 1) Assign Points
  316. 2) Print Points
  317. 3) Get Slope
  318. 4) Get Distance
  319. 5) Get Midpoint
  320. 6) Print Equation
  321. 7) Quit
  322. 5
  323. Midpoint Between Point 1 & Point 2: (2.0, 7.0)
  324.  
  325. What would you like to do next?
  326.  
  327. 1) Assign Points
  328. 2) Print Points
  329. 3) Get Slope
  330. 4) Get Distance
  331. 5) Get Midpoint
  332. 6) Print Equation
  333. 7) Quit
  334. 6
  335. The Equation: y = Infinityx + 9.0
  336.  
  337. What would you like to do next?
  338.  
  339. 1) Assign Points
  340. 2) Print Points
  341. 3) Get Slope
  342. 4) Get Distance
  343. 5) Get Midpoint
  344. 6) Print Equation
  345. 7) Quit
  346. 1
  347. Point 1:
  348. What would you like to assign to the x variable?
  349. 4
  350. What would you like to assign to the y variable?
  351. 6
  352. (4.0, 6.0)
  353. Point 2:
  354. What would you like to assign to the x variable?
  355. -2
  356. What would you like to assign to the y variable?
  357. 6
  358. (-2.0, 6.0)
  359.  
  360. What would you like to do next?
  361.  
  362. 1) Assign Points
  363. 2) Print Points
  364. 3) Get Slope
  365. 4) Get Distance
  366. 5) Get Midpoint
  367. 6) Print Equation
  368. 7) Quit
  369. 2
  370. Point 1: (4.0, 6.0)
  371. Point 2: (-2.0, 6.0)
  372.  
  373. What would you like to do next?
  374.  
  375. 1) Assign Points
  376. 2) Print Points
  377. 3) Get Slope
  378. 4) Get Distance
  379. 5) Get Midpoint
  380. 6) Print Equation
  381. 7) Quit
  382. 3
  383. Slope Between Point 1 & Point 2: -0.0
  384.  
  385. What would you like to do next?
  386.  
  387. 1) Assign Points
  388. 2) Print Points
  389. 3) Get Slope
  390. 4) Get Distance
  391. 5) Get Midpoint
  392. 6) Print Equation
  393. 7) Quit
  394. 4
  395. Distance Between Point 1 & Point 2: 6.0
  396.  
  397. What would you like to do next?
  398.  
  399. 1) Assign Points
  400. 2) Print Points
  401. 3) Get Slope
  402. 4) Get Distance
  403. 5) Get Midpoint
  404. 6) Print Equation
  405. 7) Quit
  406. 5
  407. Midpoint Between Point 1 & Point 2: (1.0, 6.0)
  408.  
  409. What would you like to do next?
  410.  
  411. 1) Assign Points
  412. 2) Print Points
  413. 3) Get Slope
  414. 4) Get Distance
  415. 5) Get Midpoint
  416. 6) Print Equation
  417. 7) Quit
  418. 6
  419. The Equation: y = -0.0x + 6.0
  420.  
  421. What would you like to do next?
  422.  
  423. 1) Assign Points
  424. 2) Print Points
  425. 3) Get Slope
  426. 4) Get Distance
  427. 5) Get Midpoint
  428. 6) Print Equation
  429. 7) Quit
  430. 1
  431. Point 1:
  432. What would you like to assign to the x variable?
  433. -4
  434. What would you like to assign to the y variable?
  435. 0
  436. (-4.0, 0.0)
  437. Point 2:
  438. What would you like to assign to the x variable?
  439.  
  440. 0
  441. What would you like to assign to the y variable?
  442. 4
  443. (0.0, 4.0)
  444.  
  445. What would you like to do next?
  446.  
  447. 1) Assign Points
  448. 2) Print Points
  449. 3) Get Slope
  450. 4) Get Distance
  451. 5) Get Midpoint
  452. 6) Print Equation
  453. 7) Quit
  454. 2
  455. Point 1: (-4.0, 0.0)
  456. Point 2: (0.0, 4.0)
  457.  
  458. What would you like to do next?
  459.  
  460. 1) Assign Points
  461. 2) Print Points
  462. 3) Get Slope
  463. 4) Get Distance
  464. 5) Get Midpoint
  465. 6) Print Equation
  466. 7) Quit
  467. 3
  468. Slope Between Point 1 & Point 2: 1.0
  469.  
  470. What would you like to do next?
  471.  
  472. 1) Assign Points
  473. 2) Print Points
  474. 3) Get Slope
  475. 4) Get Distance
  476. 5) Get Midpoint
  477. 6) Print Equation
  478. 7) Quit
  479. 4
  480. Distance Between Point 1 & Point 2: 5.656854249492381
  481.  
  482. What would you like to do next?
  483.  
  484. 1) Assign Points
  485. 2) Print Points
  486. 3) Get Slope
  487. 4) Get Distance
  488. 5) Get Midpoint
  489. 6) Print Equation
  490. 7) Quit
  491. 5
  492. Midpoint Between Point 1 & Point 2: (-2.0, 2.0)
  493.  
  494. What would you like to do next?
  495.  
  496. 1) Assign Points
  497. 2) Print Points
  498. 3) Get Slope
  499. 4) Get Distance
  500. 5) Get Midpoint
  501. 6) Print Equation
  502. 7) Quit
  503. 6
  504. The Equation: y = 1.0x + 4.0
  505.  
  506. What would you like to do next?
  507.  
  508. 1) Assign Points
  509. 2) Print Points
  510. 3) Get Slope
  511. 4) Get Distance
  512. 5) Get Midpoint
  513. 6) Print Equation
  514. 7) Quit
  515. 7
  516. Bye-bye!
  517. Press any key to continue . . .
  518. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement