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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started