logo Practice-It logo

coordinates

Author: Marty Stepp

Rewrite the coordinates method below by factoring to eliminate redundancy. Your code must produce the same results. (Practice-It won't be able to tell whether you have maximally factored the problem, but it can tell you whether your solution still produces equivalent output in all test cases.)

public static double getCoordinates(Scanner console) {
    System.out.print("x coordinate? ");
    double x = console.nextDouble();
    int neg;
    if (x < 0.0) {
        System.out.print("y coordinate? ");
        double y = console.nextDouble();
        if (y < 0.0) {
            neg = 2;
            System.out.println("negatives = " + neg);
            return x + y + neg;
        }  else {
            neg = 1;
            System.out.println("negatives = " + neg);
            return x + y + neg;
        }
    } else {
        System.out.print("y coordinate? ");
        double y = console.nextDouble();
        if (y < 0.0) {
            neg = 1;
            System.out.println("negatives = " + neg);
            return x + y + neg;
        }  else {
            neg = 0;
            System.out.println("negatives = " + neg);
            return x + y + neg;
        }
    }   
}

You do not need to put quotes around your answers.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.