Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting rid of redundancy?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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!


 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.

rc
 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please do not post the same question more than once.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic