Question: In the following Java program, rewrite the getCoordinates method using factoring to eliminate redundancy. Your method must still produce the same output and result under

In the following Java program, rewrite the getCoordinates method using factoring to eliminate redundancy. Your method must still produce the same output and result under all cases.

import java.util.*;

public class Coordinates {

public static void main(String[] args) {

Scanner console = new Scanner(System.in);

double result = getCoordinates(console);

System.out.println(result);

}

public static double getCoordinates(Scanner console) {

System.out.print("What is the x coordinate? ");

double x = console.nextDouble();

int negatives;

if (x < 0.0) {

System.out.print("What is the y coordinate? ");

double y = console.nextDouble();

if (y < 0.0) {

negatives = 2;

System.out.println("negatives = " + negatives);

return x + y + negatives;

} else {

negatives = 1;

System.out.println("negatives = " + negatives);

return x + y + negatives;

}

} else {

System.out.print("What is the y coordinate? ");

double y = console.nextDouble();

if (y < 0.0) {

negatives = 1;

System.out.println("negatives = " + negatives);

return x + y + negatives;

} else {

negatives = 0;

System.out.println("negatives = " + negatives);

return x + y + negatives;

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!