1,105,416 Community Members

any suggestions to clean this up?

Member Avatar
javanewb
Newbie Poster
2 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello, this is my first post. I have this homework assignment, and it is completed and it works! my question is just if anyone has any suggestions to clean it up or make it more efficient? I have two seperate files GuessTheNumber.java which is:

// GuessTheNumber.java
//Written by Sean Kelley for IS109-8b
//Week 3 homework
import java.util.Random;//uses Random
import java.util.Scanner;// uses Scanner
        
public class GuessTheNumber {
    
    private int number;//random number to search for
    private int guess;// user input guess
    
    
    //method go get Random number
    public int pickNum(){
        
        Random randNum = new Random();
                
        number = 1 + randNum.nextInt(1000);//uses random to get a Number between
        //                                  1 and 1000 and saves as Number
        return number;//returns number 
               
    }//end pickNum
    
    //method to facilitate the guess process
    public void guess( int number ){
        
        int max = 1000;// max starts at 1000
        int min = 1;// min starts at 1
        Scanner input = new Scanner( System.in );
        
        System.out.println("Guess a number between 1 and 1000");//prompt for input
        guess = input.nextInt();//saves input as guess
        while( guess != number ){// display messages to help user zero in on number
            
            if ( guess < number ){
                min = guess;
                System.out.println("Too low. Try again.");
                guess = input.nextInt();
            }//end if
            else{
                max = guess;
                System.out.println("Too high. Try again.");
                guess = input.nextInt();
            }// end else   
        }//end While
        //display winner message
        System.out.println("Congratulations! You guessed the Number!");  
    }//end method guess
    //method to tie it all togeather
    public void play(){
        
        guess( pickNum());
    }//end play
}//end class GuessTheNumber

and then there is the GuessTheNumberTest.java which is:

// GuessTheNumberTest.java
//Written by Sean Kelley for IS109-8b
//Week 3 homework
//Test the game GuessTheNumber
import java.util.Scanner;//uses Scanner

public class GuessTheNumberTest {
    
    public static void main(String[] args){
        
        int again;//initializes again
        Scanner input = new Scanner(System.in);//initializes Scanner
        GuessTheNumber guessTheNumber = new GuessTheNumber();
        guessTheNumber.play();//Calls method play
        System.out.println("Play again? 1 for yes or 2 for no:");//prompt for input
        again = input.nextInt(); // save input as again
        while ( again == 1 ){//begin while to play again
            guessTheNumber.play();//call method play
            System.out.println("Play again? 1 for yes or 2 for no:");//prompt for input
            again = input.nextInt();//save input as again
        }//end While
        System.out.println("Thanks For Playing!");//output end message
    }//end main
}//end class GuessTheNumberTest

like I said it is complete just want to see if anyone has any comments or suggestions.
Thanks.
JavaNewb

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

How about this?

// GuessTheNumberTest.java
//Written by Sean Kelley for IS109-8b
//Week 3 homework
//Test the game GuessTheNumber
import java.util.Scanner;//uses Scanner

public class GuessTheNumberTest {

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);//initializes Scanner
        do {//begin while to play again
            (new GuessTheNumber()).play();//call method play
            System.out.println("Play again? 1 for yes or 2 for no:");//prompt for input
        } while (1 == input.nextInt());
        System.out.println("Thanks For Playing!");//output end message
    }//end main
}//end class GuessTheNumberTest
Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
1
 

...and this:

// GuessTheNumber.java
//Written by Sean Kelley for IS109-8b
//Week 3 homework
import java.util.Random;//uses Random
import java.util.Scanner;// uses Scanner

public class GuessTheNumber {

    //method to facilitate the guess process
    public void guess( int number )
    {
		int guessedNum;// user input guess
        Scanner input = new Scanner( System.in );
        System.out.println("Guess a number between 1 and 1000");//prompt for input

        while(number != (guessedNum = input.nextInt()))
        {// display messages to help user zero in on number
            if ( guessedNum < number )
            {
                System.out.println("Too low. Try again.");
                continue;
            }//end if

            System.out.println("Too high. Try again.");
        }//end While
        //display winner message
        System.out.println("Congratulations! You guessed the Number!");
    }//end method guess
    //method to tie it all togeather
    public void play(){

        guess(1 + (new Random().nextInt(1000)));
    }//end play
}//end class GuessTheNumber

Also: there are too many comments.
The comments should be removed from things that are obvious.

Member Avatar
javanewb
Newbie Poster
2 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hey thanks for the suggestions, I know the comments are a little overboard, but my instructor is all about them. Thanks again.

Member Avatar
WigglesMcMuffin
Light Poster
26 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

You could also move the "play again" segment inside the game, meaning you only need 1 scanner, and then just have the play method return an integer.

Member Avatar
hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 164 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
0
 

The ONLY comments which should be removed,
are the comments on lines 14 and 26.
The rest are fine.

Programmers should keep in mind that...
Ten years later, when YOUR code requires modification, and you've moved on to another job...
The next programmer will really appreciate those comments to find where they have to modify YOUR code.
I've gone back to MY OWN code, a year later, and needed those comments!

ps. I LOVE the fact that you comment your ending "}"s!!!
I've been doing that for years.
I've only seen ONE other person (until now) do that.
...AMOL... Is that you???

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

@hfx642
Personally, I think obvious lines that don't require comments are:

>> import java.util.Random;//uses Random
>> import java.util.Scanner;// uses Scanner

>> //display winner message

>> guess = input.nextInt();//saves input as guess

...and a lot of variables (from us all) can be renamed so that comments are not necessary, like:

int intNumUserGuessed = 0;

[From Uncle Bob Martin's "Clean Code"]
http://www.informit.com/articles/article.aspx?p=1327761

Member Avatar
hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 164 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
1
 

Well... I was just looking at the lines with code, so...
Yes! I would agree that there is no reason to comment the import lines.

As for the commenting of variables...
I would agree that there should be no reason to comment variables.
They should be named better.
People don't really realize how important a naming convention is.

Member Avatar
frank33
Light Poster
32 posts since Dec 2010
Reputation Points: 11 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

ps. I LOVE the fact that you comment your ending "}"s!!!

One more 'guilty' plea here.

But I go one step further and make sure the '}' is DIRECTLY under the matching '{'.
Which is NOT always done on the code shown here.

Elementary requirement in terms of readibility, imo.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

@frank33: Some instructors require K&R style for braces. :(
http://en.wikipedia.org/wiki/Indent_style

Member Avatar
frank33
Light Poster
32 posts since Dec 2010
Reputation Points: 11 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

That link is heartwarming, making me feel part of the 'curly bracket family'
( even if it did not mean people, specifically ).

Still, I prefer coder's own conventions rather than rules imposed by editors etc.
And obviously my own conventions are so obvious that everybody would/could/should
use them ( ! )

Question Answered as of 2 Years Ago by thines01, hfx642, frank33 and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: