• Post Reply 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

turning method to OO

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys

im a newbie to Java and in workshops what were doing is turning the below code to OO making it simipler. I was wondeirng if sumone if someone can turn the below code to OO soo i can get an idea of how code is turned into OO.

player: while (!false) {
try {
System.out.println(" human players?");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
h = Integer.parseInt(s);
if (nop - h < 0)
throw new UnsupportedFlavorException(null);
break player;
} catch (Exception e) {
System.out.println("number of humans ");
}
}

Thanks alot

Steve
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steve,

Individual methods aren't really "object-oriented" or not; object orientation is a property of entire programs, or at least of whole groups of classes. There are a number of problems with this code, but in no sense could you "make this OO" and improve it. At some level, all code, OO or not, has to execute a series of statements to get work done. I/O code like this, especially, more or less looks this way, and there isn't much you can do to change it.

While we're here, I could point out a few things really wrong with this code:

- "while (!false)" is needlessly complex; why not "while (true)" ?
- short, meaningless variable names make code hard to understand
- null message parameter to exception is asking for trouble; why not "", or no parameter?
- Misuse of DataFlavorException type, which is related to drag-and-drop; use IllegalArgumentException
- Terrible misuse of exception handling in general; throwing and catching an exception within a single method is silly. Just use "if/then!"
- unnecessary loop label just adds to the confusion.
 
steve low
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Im having problems using the if and then. please show me an example of how I can turn the below code into a better if statement so I can understand how its done. Thanks alot

 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand correctly what's happening here, you're just trying to prompt the user for a valid number which is less than or equal to "nop". I might write that like this (if I needed to write code that worked in JDK 1.4, as yours would; in JDK 1.5, you could use the "Scanner" class to simplify a little.)

 
steve low
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...this method is a flase basically less then 0 or numbers above 5 is a bad number.

heres what ive done its complaining about the the int h =-1 saying its duplicate. not sure what i have to do there and for some reason its complaining about the last syntax } why?

humanplayer: while (!flase) {
try {
System.out.println("How many human players?");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in), 1);

int h = -1; //dupliate variable

do {
try{
System.out.println("Bad number?");
System.out.flush();
String s = in.readLine();
if (s == null) System.exit(0);
h = Integer.parseInt(s);
}catch(NumberFormatException nfe){

}
} while (h < 0 && (nop - h) < 0);

}
} complains about this syntax

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should probably show the whole method to help us helps you.

Also please work on your formatting, your code is very hard to read without any indentations.
 
steve low
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


thats the full code i was going to attempt the true statement after i finsihed the false one.. but i kept on having the error on it. but ive listed the full code. in the above method which im doing it does not like int h = -1; also it complains about the last syntax for some reason? hope this helps
[ October 01, 2006: Message edited by: steve low ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can only declare a variable once; "int h = -1" could simply be "h = -1", since h already exists.

But now we are getting into another style issue: methods should never be this long, and you should never reuse variables for many different purposes this way. This should be broken up into multiple small er methods.

Given this issue -- the multiple-declarations problem -- it's clear that this belongs in "Java in General (Beginner)," and not here.
 
reply
    Bookmark Topic Watch Topic
  • New Topic