File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Getting rid of redundancy? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

Getting rid of redundancy?

Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
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
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.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

Please do not post the same question more than once.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Getting rid of redundancy?
It's not a secret anymore!