File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes turning method to OO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "turning method to OO" Watch "turning method to OO" New topic
Author

turning method to OO

steve low
Greenhorn

Joined: Sep 27, 2006
Posts: 27
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
steve low
Greenhorn

Joined: Sep 27, 2006
Posts: 27
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
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: Sep 27, 2006
Posts: 27
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
steve low
Greenhorn

Joined: Sep 27, 2006
Posts: 27


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
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: turning method to OO