Advertisement
Guest User

Untitled

a guest
May 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3.  
  4. public class EquationTest{
  5. public static void main(String[] args){
  6. File file = new File ("slope.txt");
  7. try{
  8. Scanner reader = new Scanner(file);
  9. int x1 = 0;
  10. int x2 = 0;
  11. int y1 = 0;
  12. int y2 = 0;
  13. while(reader.hasNext()){
  14. for(int x = 0; x <=3; x++){
  15. int num = reader.nextInt();
  16. if (x == 0)
  17. x1 = num;
  18. else if (x == 1)
  19. y1 = num;
  20. else if (x == 2)
  21. x2 = num;
  22. else
  23. y2 = num;
  24. }
  25. Line line = new Line(x1, x2, y1, y2);
  26.  
  27. System.out.println(line);
  28. }
  29. }
  30. catch (Exception e){
  31.  
  32. }
  33. }
  34. }
  35.  
  36. ____________________________________________________________________________________________________
  37.  
  38.  
  39. public class Line{
  40. double x1, x2, y1, y2;
  41.  
  42. public Line(int x1, int x2, int y1, int y2){
  43. this.x1 = x1;
  44. this.x2 = x2;
  45. this.y1 = y1;
  46. this.y2 = y2;
  47. }
  48.  
  49. public boolean hasSlope(){
  50. if (x1 == x2)
  51. return false;
  52. else
  53. return true;
  54. }
  55.  
  56. public double getSlope(){
  57. if (hasSlope())
  58. return ((this.y2 - this.y1) / (this.x2 - this.x1));
  59. else
  60. return 0;
  61. }
  62.  
  63. public String toString(){
  64. String points = "points (" + x1 + ", "+ y1 + ") and (" + x2 + ", " + y2 + ") \n";
  65. String theLine;
  66. if(hasSlope()){
  67. String slope = "slope: " + getSlope() + "\n";
  68. String equation = "equation: y = " + getSlope() + " * x + " + (y1 - (getSlope() * x1)) + "\n";
  69.  
  70. theLine = points + slope + equation;
  71. }
  72. else{
  73. String undefined = "slope is undefined";
  74. theLine = points + undefined;
  75. }
  76. return theLine;
  77. }
  78. }
  79.  
  80. _____________________________________________________________________________________
  81.  
  82. 78 86 92 91
  83. 66 60 72 72
  84. 77 73 73 85
  85. 82 88 81 77
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement