Results 1 to 4 of 4
  1. #1
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default shortening a java program?

    I want to shorten the code I have for this game (i.e. get rid of the printstream & currentTimeMillis) in order to make it a little bit shorter and more straightforward, to-the-point kind of a code. Any suggestions?
    The point to this code is to
    "Your program chooses the number to be guessed by selecting a random integer in the
    range 1 to 100. The application displays the prompt: Guess a number between 1 and
    100 . After the user enters a number, your program should output something similar to
    one of the following :
    o Too High...Try again! (if number guessed is higher than selected number)
    o Too Low...Try again! (if number guessed is lower than selected number)
    o You got it! (if number was guessed)
    o Invalid entry...must be between 1 and 100! (if number guessed was out
    of range)

    The program should also keep track of the number of valid guesses, and report those .
    After the user successfully guesses the correct answer, the user should be given the
    option to play again!

    You have to name your class as Guess, and the file as Guess.java.
    You have to implement the Guess class follow the given documentation. "

    Java Code:
    import java.io.PrintStream;
    import java.util.Random;
    import java.util.Scanner;
    
    public class Guess {
    
        int compare(int i, int j) {
            if(i == j)
                return 0;
            return i <= j ? 2 : 1;
        }
    
        void printMsg(int i) {
            if(i == 0) {
                System.out.printf("\t%20s", new Object[] {
                    message[i]
                });
                System.out.printf("\t No. of tries : %2d\n", new Object[] {
                    Integer.valueOf(trial)
                });
            } else {
                System.out.printf("\t%20s\n", new Object[] {
                    message[i]
                });
            }
        }
    
        public Guess() {
            sc = new Scanner(System.in);
            time = System.currentTimeMillis();
            rand = new Random(time);
            Target = 1 + rand.nextInt(100);
            trial = 0;
        }
    
        public Guess(int i) {
            sc = new Scanner(System.in);
            time = System.currentTimeMillis();
            rand = new Random(time);
            Target = i;
            trial = 0;
        }
    
        public void play() {
            boolean flag = false;
            do {
                System.out.printf("\n\n\tPlease enter your guess (between 1 and 100) : ", new Object[0]);
                guess = sc.nextInt();
                if(guess > 100 || guess < 1) {
                    System.out.printf("\t%20s", new Object[] {
                        message[3]
                    });
                } else {
                    trial++;
                    int i = compare(guess, Target);
                    printMsg(i);
                    if(i == 0) {
                        System.out.printf("\n\n\t%20s (Y/N) :", new Object[] {
                            message[4]
                        });
                        String s = sc.next();
                        if(s.charAt(0) == 'y' || s.charAt(0) == 'Y') {
                            Target = 1 + rand.nextInt(100);
                            trial = 0;
                        } else {
                            return;
                        }
                    }
                }
            } while(true);
        }
    
        public static void main(String args[]) {
            System.out.printf("\n\n\tWelcome to CSC211 Guessing Game .", new Object[0]);
            Guess guess1 = new Guess();
            guess1.play();
            System.out.printf("\n\n\tThank you for playing ......\n\n", new Object[0]);
        }
    
        Scanner sc;
        long time;
        public Random rand;
        String message[] = {
            "You got it!", "Too High ... Try again!", "Too Low...Try again!", "Invalid entry ... must be between 1 and 100!", "Do you want to play again?"
        };
        int guess;
        public int Target;
        int trial;
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: shortening a java program?

    Why change it?

  3. #3
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default Re: shortening a java program?

    Because I would like for it to be more straight forward and a bit shorter. And I don't believe I actually need the currentTimeMillis() stuff.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: shortening a java program?

    IMHO its a waste of time worrying about making code shorter without an operational requirement.

    Adding comments would do a lot toward making the code more understandable.

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 04:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 12:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •