Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.File;
- public class EquationTest{
- public static void main(String[] args){
- File file = new File ("slope.txt");
- try{
- Scanner reader = new Scanner(file);
- int x1 = 0;
- int x2 = 0;
- int y1 = 0;
- int y2 = 0;
- while(reader.hasNext()){
- for(int x = 0; x <=3; x++){
- int num = reader.nextInt();
- if (x == 0)
- x1 = num;
- else if (x == 1)
- y1 = num;
- else if (x == 2)
- x2 = num;
- else
- y2 = num;
- }
- Line line = new Line(x1, x2, y1, y2);
- System.out.println(line);
- }
- }
- catch (Exception e){
- }
- }
- }
- ____________________________________________________________________________________________________
- public class Line{
- double x1, x2, y1, y2;
- public Line(int x1, int x2, int y1, int y2){
- this.x1 = x1;
- this.x2 = x2;
- this.y1 = y1;
- this.y2 = y2;
- }
- public boolean hasSlope(){
- if (x1 == x2)
- return false;
- else
- return true;
- }
- public double getSlope(){
- if (hasSlope())
- return ((this.y2 - this.y1) / (this.x2 - this.x1));
- else
- return 0;
- }
- public String toString(){
- String points = "points (" + x1 + ", "+ y1 + ") and (" + x2 + ", " + y2 + ") \n";
- String theLine;
- if(hasSlope()){
- String slope = "slope: " + getSlope() + "\n";
- String equation = "equation: y = " + getSlope() + " * x + " + (y1 - (getSlope() * x1)) + "\n";
- theLine = points + slope + equation;
- }
- else{
- String undefined = "slope is undefined";
- theLine = points + undefined;
- }
- return theLine;
- }
- }
- _____________________________________________________________________________________
- 78 86 92 91
- 66 60 72 72
- 77 73 73 85
- 82 88 81 77
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement