I'm trying to get rid of redundancy in my program, I know I can take out the System.out.println("What is the y coordinate?) but not sure what else to combine.
Any help is appreciated, thanks!
I posted this in the Beginners forum, but no one was answering, so I thought I'd try it out here!
Ralph Cook
Ranch Hand
Joined: May 29, 2005
Posts: 479
posted
0
To get rid of redundancy, look for the things that are redundant and fold them into single structures.
Your code repeats several things: for instance, you prompt for a coordinate, then get a coordinate from the console in three places. You can have a method that takes a parameter so that it can name the coordinate (x or y) that is to be gotten, prompts for it, gets the input, and returns the input value.
One of your repeats -- getting y from the console in one place if x is negative and another if it is not -- is entirely there because you prompt for the input separately based on the value of x. But you don't need to do that; you are going to prompt for y regardless of x's value, so you can prompt for and get x, prompt for and get y (in one place), and then print out and return the appropriate values after you have gotten the input.
You always return "x + y + negatives" -- so your testing code can test x and y to figure out what the value of "negatives" is to be, and then just have the return and the calculation done once.
There are one or two other things you can do to the code, but this removes redundancies.