Results 1 to 4 of 4
Thread: shortening a java program?
- 02-22-2012, 01:01 AM #1
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; }
- 02-22-2012, 02:26 AM #2
Re: shortening a java program?
Why change it?
- 02-22-2012, 03:16 AM #3
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.
- 02-22-2012, 03:29 AM #4
Similar Threads
-
Call one Java Program from another Java Program
By rajpalparyani in forum New To JavaReplies: 3Last Post: 02-14-2011, 04:13 AM -
Is There A Way To Call Another Java Program Within A Java Program
By SwissR in forum New To JavaReplies: 4Last Post: 07-30-2010, 12:25 PM -
execute java program within java program
By popey in forum New To JavaReplies: 2Last Post: 10-22-2009, 05:32 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks