Help with Project


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Help with Project

  1. #1
    Join Date
    Nov 2003
    Posts
    5

    Question Help with Project

    Hi all, i hope someone here can help me. I've been given a project to do which needs completed by 2morrow, that Friday and am stuck. I've been asked to write some code to generate a random letter and then get the program to guess what that random letter is (no user input). I've manged to complete the 1st part which was to use another random generator to do the guessing but now the program must guess the letter in a more efficient way.

    Here is what i have so far. (This generates the random letter)

    Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    public class Part1{
    public static void main(String[] args){
    
    //declare the variables
    int number;
    char letter;
    String output = "";
    
    //create a random number generator
    Random numGenerator = new Random();
    
    //generate a number between 1-5 inclusive
    number = Math.abs(numGenerator.nextInt(4)) + 1;
    
    switch (number) {
    case 1:
    output= "The letter was a";
    break;
    case 2:
    output= "The letter was b";
    break;
    case 3:
    output= "The letter was c";
    break;
    case 4:
    output= "The letter was d";
    break;
    case 5:
    output= "The letter was e";
    break;
    //end switch
    }
    
    //print message
    JOptionPane.showMessageDialog(null, output, "Random Letter", JOptionPane.INFORMATION_MESSAGE);
    
    System.exit(0);
    } //end main
    } //end class
    I think ineed a FOR loop to test each of the random numbers (letters) but im a total newbie when it comes to java.

    Thanx for any help.

    [ArchAngel added CODE tags]

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    You haven't actually done the 1st part - you said that it had to generate a random letter, your code generates a random message. Also, your code is just a big static method - you need to introduce some objects here.

    Well, since I've managed to scare someone away from the forum today (and because you've posted some code) I'm in a good mood, so here's how I'd design the program (some things I wouldn't do quite like this, but since I'm assuming this is your first Java coursework I don't think it'll matter).

    Firstly, I'd create a class RandomLetter. This class represents a randomly-generated letter and will provide a single method for testing a guess:
    Code:
       public class RandomLetter {
    
         /** The randomly-generated letter held in the object. */
         private char generatedLetter;
    
         public RandomLetter() {
           // Insert code to generate a random letter between 'A' and 'Z'.
           // Assign the generated letter to instance variable 'generatedLetter'.
         }
    
          public boolean isLetter(char guess) {
            // Insert code to compare the guess against 'generatedLetter'.
          }
       }
    With me so far? The beauty of this code is that nothing outside this class will ever know what the letter is (meaning that you can never 'cheat'), you can only invoke the isLetter(...) method to see if your guess is correct.

    Once you've implemented the methods described above, THEN you've done the 1st part. You can THEN write the 2nd part where the computer tries to guess the letter. I'd do this by creating a 'Guesser' class:
    Code:
      public class Guesser {
        public int guess(RandomLetter rl, int attempts) {
          // Insert code to have attempt to guess the randomLetter a specified number of times.
          // This method returns the number of guesses it took to guess the random letter, or -1 if it didn't guess it correctly in the specified number of attempts.
        }
      }
    See what's happening? A Guesser object receives a RandomLetter object and an int specifiying how many attempts it should have. The return value being the number of attempts made before guessing correctly is completely made up by me - it depends upon what your lecturer wants.

    Well, that should give you plenty to get on with for the moment.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Nov 2003
    Posts
    5
    Thanx for that, although i've received some feedback on that 1st bit of code and i've been told its ok. We haven't yet been introduced to multiple obejcts yet.

    Is there anyway to get the program to test the value of "number" until it finds a match without using another random generator.

    Here is the other piece of code (feedback on this is OK)
    Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    public class Part1a{
    public static void main(String[] args){
    
    //declare the variables
    int number;
    int number2 =0;
    int counter;
    char letter;
    String output = "";
    String output2 = "";
    
    
    //create a random number generator
    long seed = (new Date()).getTime();
    Random numGenerator = new Random(seed);
    
    //generate a number between 1-5 inclusive
    number = Math.abs(numGenerator.nextInt(5)) + 1;
    
    //set the counter to 0
    counter = 0;
    
    switch (number) {
    case 1:
    output= "The letter is a.";
    break;
    case 2:
    output= "The letter is b.";
    break;
    case 3:
    output= "The letter is c.";
    break;
    case 4:
    output= "The letter is d.";
    break;
    case 5:
    output= "The letter is e.";
    break;
    //end switch
    }
    
    //a loop to get the 2nd letter to match the first letter
    while (number != number2) {
    //generate a 2nd number between 1-5 inclusive
    number2 = Math.abs(numGenerator.nextInt(5)) + 1;
    
    counter ++;
    } 
    
    //construct the output
    output2 = output + " It took " + counter + " guess(es) to match the letters.";
    
    //print message
    JOptionPane.showMessageDialog(null, output2, "Random Letter", JOptionPane.INFORMATION_MESSAGE);
    
    System.exit(0);
    } //end main
    } //end class
    Thanx again.

    Looks like im not going to get much sleep tonight.

    [ArchAngel added CODE tags]

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    You mean that you haven't been taught how to define classes and create objects?!!! From what people have been posting on the forums this academic year, I'm becoming increasingly more concerned about how Java is being taught....sigh...ok...rant over...

    OK, if you've been told this way of doing things is fine, then I'll go with that.

    With regards to the guessing of the random letter, it depends upon how you want to do it. For example, you could just loop through the range of possible letters and compare each one with the letter which was originally generated. This is probably how I'd do it.

    Your layout and indenting leaves something to be desired - this is how I'd have done it:
    Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    
    public class Part1a{
    	public static void main(String[] args){
    
    		// Declare the variables.
    		int number;
    		int number2 = 0;
    		int counter;
    		char letter;
    		String output = "";
    		String output2 = "";
    
    
    		// Create a random number generator.
    		long seed = (new Date()).getTime();
    		Random numGenerator = new Random(seed);
    
    		// Generate a number between 1-5 inclusive.
    		number = Math.abs(numGenerator.nextInt(5)) + 1;
    
    		// Set the counter to 0.
    		counter = 0;
    
    		switch (number) {
    			case 1:
    				output = "The letter is a.";
    				break;
    			case 2:
    				output = "The letter is b.";
    				break;
    			case 3:
    				output = "The letter is c.";
    				break;
    			case 4:
    				output = "The letter is d.";
    				break;
    			case 5:
    				output = "The letter is e.";
    				break;
    		} // End switch
    
    		// A loop to get the 2nd letter to match the first letter.
    		while (number != number2) {
    			// Generate a 2nd number between 1-5 inclusive.
    			number2 = Math.abs(numGenerator.nextInt(5)) + 1;
    			counter ++;
    		} 
    
    		// Construct the output.
    		output2 = output + " It took " + counter + " guess(es) to match the letters.";
    
    		// Print message.
    		JOptionPane.showMessageDialog(null, output2, "Random Letter", JOptionPane.INFORMATION_MESSAGE);
    
    		System.exit(0);
    	} // End main.
    } // End class.
    See what I've changed.

    Also your variable names are not self-explanitory. How about using names like 'generatedNumber', 'guesses' etc.
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Nov 2003
    Posts
    5
    Okalydokaly, i dont suppose you can give me any hints on the neccessary code needed to loop through the range of letters instead of just randomly generating another set then comparing.

    Sorry about this, i tried my hand at C++ about 7 years ago and i don't think ive ever really recovered from the trauma.

    Thanx

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    Hints?!

    In your code thus far you generated a random number. Now what you need to do is loop through 1 to 5 comparing the current number with the one originally generated.
    ArchAngel.
    O:-)

  7. #7
    Join Date
    Nov 2003
    Posts
    5

    Smile

    FANTASTICO, just cracked it. Starting to make a bit of sense now. Think i might postpone the suicide attempt now.

    I've altered the code to create a random string of 1 number between 0 - 9 followed by 1 letter from a - e followed by another number between 0 - 9. i need to compare this string with another string hidden inside another class file called SecretCode.class.

    Any ideas???

    Thanx for all this.

  8. #8
    Join Date
    Mar 2003
    Posts
    834
    Glad to hear it's starting to make sense. But what's this sudden mention of SecretCode class? I thought you didn't know how to create them, or is this something your lecturer has given you?

    When comparing two Strings, use the .equals(...) method.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center