1

OUTPUT should look like:

Enter the x and y coordinates of the first point: 3 -2
Enter the x and y coordinates of the second point: 9 2
Distance: 7.211
Positive Slope

I have updated the OP with what the code should look like if you're trying to find the distance.

Here is what my code looks like right now:

import java.util.Scanner;

public class LineEvaluator
{

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the x and y coordinates of the first point: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();

        System.out.print("Enter the x and y coordinates of the second point: ");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();

        double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        System.out.printf("Distance: %.3f", distance);

        double slope = ((y2 - y1) / (x2 - x1));

        if(slope > 0)
        System.out.println("Positive Slope");
        else if(slope < 0)
        System.out.println("Negative Slope");
        else if(slope == 0)
        System.out.println("Horizontal");
    }
   }
5
  • 1
    Remove javascript tag. Sep 18, 2015 at 0:25
  • 1
    Update your question a little: what exactly is wrong with what you have? Does it fail to compile, produce incorrect output...? Sep 18, 2015 at 0:26
  • @ben I'm having trouble getting the output 7.211 when i enter those coordinates, instead i get 7.000. I also have no idea how to go about writing a code to tell me if the slope is either horizontal, vertical, negative or positive. Sep 18, 2015 at 0:31
  • Replace x1 with d, x2 with f and so forth. Youve fallen victim to integer division, and it prints 7.000 because you cast the integer to a double. Sep 18, 2015 at 0:34
  • 1
    For your other question, think about what it would mean to have each of those for words. Positive and negative are easy. Horizontal is too. Vertical is a little trickier but not too hard. Sep 18, 2015 at 0:35

1 Answer 1

4

So... First of all don't say int to double like that - go in the documentations and you'll find that input.nextDouble() exists and you can use that instead directly.

e.x.:

int y2 = input.nextInt();
double g = y2;
to...
double g = input.nextDouble()

Also, in your distance formula, you are using:

double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)*(y1 - y2));

If you're using that, please name your doubles to the proper names(x1, x2, etc...)

When you use int, it truncates. Read up on the differences: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Then, you can calculate the slope, say you store it in a variable slope.

When you're trying to find a slope that has infinite slope, you will get this error: ArithmeticException.

You can fix this by a try catch, surrounding the code you might divide by 0, and then System.out.println("Vertical Line") at that situation, or you could evaluate later and leave the try catch blank. Use this: Double.isInfinite(double) to evaluate later on.

Try catch example:

try{
    slope = (y2-y1)/(x2-x1)//if it's divide by zero then we will get this error
}catch(ArithmeticException e){
//Take care of the error as we discussed.
}

Use if-elses or switch statements to evaluate the slope:

if(slope > 0)
System.out.println("Positive slope")
else if(slope == 0)
System.out.println("Flat slope")...

Hope you got the idea.

6
  • 1
    Aww no you did it for him. Your answer is right but i was hoping to encourage OP to get there on his own with hints. Oh well. Upvote anyways :P Sep 18, 2015 at 0:36
  • 1
    @BenKnoble My bad! I tried to point to some useful resources for him to read, but I know that people never read the sources I point to so I just tell him what's going on. I'll stray away from that in the future. Sep 18, 2015 at 0:38
  • 3
    Its ok. Its a matter of opinion and style. As long as you arent helping the "hey heres this problem and i have nothing write all my code for me kthxbye" posters youre fine. It is an excellent answer. Sep 18, 2015 at 0:40
  • Hey thanks man, really appreciate it. I decided to change it to double x1 = input.nextDouble(); like you suggested. You lost me at the slope part, but i'll keep trying to figure it out. Thank you! Sep 18, 2015 at 0:47
  • @StevenOrozco What do you need help with on that part? Sep 18, 2015 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.