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

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

...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.

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

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 for hfx642

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???

@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 for hfx642

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.

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.

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 ( ! )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.