Author Topic: Any one program Java?  (Read 855 times)

Offline SilverZ06

  • Silver Member
  • ****
  • Posts: 1727
Any one program Java?
« on: October 20, 2014, 08:23:41 PM »
I am taking a Java course and I have a project due tomorrow. I am done with the project but I would like someone with Java experience to review the code. My professor does not return the projects once he has graded them so I have no idea where I can improve. 

Here are the requirements of the assignment:
Quote
Description and Requirements:

For this project you are to implement a stand-alone Java program to play a guessing game.  Your program should pick a random number between one and ten (inclusive) and prompt the user for their guess.  For each guess made, your program should tell the user if the guess was too high, too low, or correct.  The user should only have four (4) tries to get it right before they lose the game.

When a game is over, a dialog should announce if they won or lost and ask the user if they want to play again.  Your dialog should have yes and no buttons.  If they lost this dialog box must show them what the correct answer was.

All user input and output should be done using javax.swing.JOptionPane class, which display dialog boxes.  Other than this, the program is non-GUI.

Your project must be a stand-alone program, not an applet.
Sample Game Log:

Below is the captured output of a sample run of this game.  Your game should work the same way.  To make this easier to follow, the model solution has had the GUI input and output replaced with non-GUI I/O, but is otherwise identical.  User input is shown using boldface.

C:\Temp>java GuessingGameNonGUI
(4 Guesses left) What is your guess (1-10)? 5
(3 Guesses left) Too high!  What is your guess (1-10)? 3
(Game Over) Correct!  Well done!  Play again (y/n)? y
(4 Guesses left) What is your guess (1-10)? 10
(3 Guesses left) Too high!  What is your guess (1-10)? 9
(2 Guesses left) Too high!  What is your guess (1-10)? 8
(1 Guesses left) Too high!  What is your guess (1-10)? 7
(Game Over) Sorry, you lose!  (number was 2)  Play again (y/n)? n
C:\Temp>

You should download the model solution GuessingGame.class and run it a few times, using:

   C:\Temp>java GuessingGame

Other Requirements:

The program should use the class java.util.Random to generate the numbers (which is easier than using java.lang.Math random number functions), and use the swing JOptionPane input dialog to read in the user's guess, and a message dialog for the "game over" dialog.  The user is told if the guess is too low or too high, or if they got the right answer.  If the user doesn't guess correctly with four tries, they lose.

As this is your first programming assignment where I don't provide sample code to use, I provide the design of the program for you.  Your code must have the following methods at least:

    public static void main ( String [] args )
    This method is responsible for the "Play again?" logic, including the "you won/you lost" dialog.  This means a loop that runs at least once, and continues until the player quits.
    static boolean playGame ( ??? )
    This method is responsible for playing one complete game each time it is called, including the "what is your guess?" input dialog.  Note the message displayed in the input dialog changes each time through the loop, to show if the user's last guess was too high or too low.  The method should return true if the user won.  If they don't guess correctly after four tries, the user has lost and the method should return false.
    static int compareTo ( ???, ??? )
    This method compares the user input (a single guess) with the correct answer.  It returns a negative integer if the guess is too low, a positive integer if the guess is too high, and 0 (zero) if the guess is correct.

No other methods are needed, but if you can make a good case for it you may have additional methods.  (You still need these three methods.)  You must decide what arguments, if any, to pass to these methods.  Please note you must use the design implied by the methods I have required.  (Even if you would have designed the game program differently!)

You must meet all the requirements from the description above.  If you include any creative extras, be sure your program still performs the guessing game as described above.  Creative extras are "extras" and you are not free to modify the project requirements.

A non-working project can score quite well (so don't be afraid to turn one in).  Also a fully working project may not score 100%.

You must work alone on your project, however you can ask your instructor for help anytime.  Do not wait until "the last minute" to begin work on your projects!
« Last Edit: October 20, 2014, 08:45:56 PM by SilverZ06 »

Offline SilverZ06

  • Silver Member
  • ****
  • Posts: 1727
Re: Any one program Java?
« Reply #1 on: October 20, 2014, 08:26:47 PM »
Here is my code (I realize the input is not validated and runtime errors are possible if the user inputs anything other than an int but at this point in time I don't know the correct way to validate the data):
Quote
import javax.swing.*;  //for JOptionPane
import java.util.*;    //for Random numbers

class Project3{
   public static void main( String[] args ) {
      //This method is responsible for the "play again?"
     //logic as well as the "you won/you lost" dialog.
     boolean winner;
     int message=1;
     do{
        //create a random number from 1-10
       Random random = new Random();
       int randomNum = random.nextInt(( 10 - 1 ) + 1 ) + 1;
      
       winner = playGame( randomNum );
       //display message depending on win or lose
       if (winner == true) {
          message = JOptionPane.showConfirmDialog( null,
             "You guessed correctly!\nWould you like to play again?",
             "Congrats!", JOptionPane.YES_NO_OPTION,
             JOptionPane.INFORMATION_MESSA GE);
       }
       else if (winner == false) {
          message = JOptionPane.showConfirmDialog( null,
             "Correct answer was " + randomNum +"\nWould you like to play again?",
             "You lose!", JOptionPane.YES_NO_OPTION,
             JOptionPane.INFORMATION_MESSA GE);
       }
       } while (message==JOptionPane.YES_OPTION);
      
      
   }
  
   static boolean playGame( int randomNum ){
      //This method is responsible for playing one complete game
    boolean result=false;
    int comparison;
    String userGuess;
    String message = " ";
    for (int i=4; i>0; i--){
       int userNum;
       userGuess = JOptionPane.showInputDialog ( null,
           message +"Enter your guess 1-10:", i + " guesses remaining",
                   JOptionPane.QUESTION_MESSAGE);
      userNum = Integer.parseInt( userGuess );
      
      //compare user number with random number
      comparison = (compareTo( userNum, randomNum ));
      if (comparison == 0){
         result=true;
         break;
      }
      else if (comparison == -1){
         message = "Too Low!  ";
         continue;
      }
      else if (comparison == 1){
         message = "Too High!  ";
         continue;
      }
    }
    return result;  
   }
  
   static int compareTo ( int userNum , int randomNum ){
      //This method compares user input with the correct answer
     if ( userNum == randomNum ){
        return 0;
     }
     else if ( userNum> randomNum ){
        return 1;
     }
     else {
        return -1;
     }
     
   }
}

Offline olds442

  • Persona Non Grata
  • Gold Member
  • *****
  • Posts: 2239
Re: Any one program Java?
« Reply #2 on: October 21, 2014, 05:57:30 AM »
Here is my code (I realize the input is not validated and runtime errors are possible if the user inputs anything other than an int but at this point in time I don't know the correct way to validate the data):

You can read up on the try catch blocks to handle exceptions, it still amazes me how java loves making basic I/O more complex than it needs to be.

http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html
only a moron would use Dolby positioning in a game.
IGN: cutlass "shovels and rakes and implements of destruction"

Offline Mickey1992

  • Gold Member
  • *****
  • Posts: 3362
Re: Any one program Java?
« Reply #3 on: October 22, 2014, 09:23:11 AM »
I don't have any Swing experience but your Java looks good.   :rock

Offline Tumor

  • Platinum Member
  • ******
  • Posts: 4272
      • Wait For It
Re: Any one program Java?
« Reply #4 on: October 22, 2014, 06:54:22 PM »
My professor does not return the projects once he has graded them so I have no idea where I can improve. 

Start by not selecting that professor again.
"Dogfighting is useless"  :Erich Hartmann

Offline SilverZ06

  • Silver Member
  • ****
  • Posts: 1727
Re: Any one program Java?
« Reply #5 on: October 22, 2014, 07:06:36 PM »
Start by not selecting that professor again.

I wish it were that easy. As far as I know he is the only professor that teaches Java. I will have him again next semester for advanced Java. He is a very smart man but feedback is crucial to me.

Offline rogwar

  • Silver Member
  • ****
  • Posts: 1913
Re: Any one program Java?
« Reply #6 on: October 23, 2014, 07:38:18 AM »
My professor does not return the projects once he has graded them so I have no idea where I can improve. 


That is unacceptable. Talk to him and take it up the chain of command if no resolution.

Offline FESS67

  • Silver Member
  • ****
  • Posts: 1051
Re: Any one program Java?
« Reply #7 on: October 23, 2014, 08:07:31 AM »
I agree with rogwar.

I learned programming many years ago and I have since moved away from the business.  A professor teaching us C+ was delivering a lecture where none of us could understand what he was saying.  I challenged him ( I was 30 at the time and considered the old man of the uni class) and asked for clarification.  He said, "turn in your project and you will understand"

I complained to the faculty and we got some additional tuition, we also backed that up with student led workshops (which I have to say were the best learning I did in uni) where we thrashed it out until we understood it.

Basic premise - YOU are paying for your education - challenge the establishment if required but make sure you can prove you are putting enough effort in to stand your ground.

Offline Traveler

  • Gold Member
  • *****
  • Posts: 3147
      • 113th Lucky Strikes
Re: Any one program Java?
« Reply #8 on: October 23, 2014, 08:32:12 AM »
I don't do java, but I did do CICS cobol and BAL for a very long time on the big iron.  So did you test your code?  What happens if I have my key lock set to uppercase and I press the 4 Key, but because it's uppercase your program receives a $, what happens? Just wondering?
Traveler
Executive Officer
113th LUcky Strikes
http://www.hitechcreations.com/wiki/index.php/113th_Lucky_Strikes