DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4

Thread: Secret Letter

  1. #1
    Join Date
    Mar 2005
    Posts
    27

    Secret Letter

    Thanks for the help!
    Last edited by none_none; 04-20-2005 at 11:36 PM. Reason: Thanks!

  2. #2
    Join Date
    Feb 2005
    Posts
    55
    This sounds a bit like an assignment to me. We can help you, but we cant do it for you, we havent got the time, paste what you have and ill have a look at it

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Cool "Time... his trick is you and me" (D.Bowie)

    This one makes it in 4 guesses it seems.... (I still can' tfind my glasses though). This is a hybrid applet, it can run as an application and
    as an applet without any modification.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    
    /**
     * A Guess-A-Letter applet
     */
    
    public class GuessApplet extends Applet implements ActionListener {
      private boolean isStandalone = false;
      private static char [] chars={
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
        'o','p','q','r','s','t','u','v','w','x','y','z'
      };
      private int currHi=chars.length;
      private int currLow=0;
      private int guessCount=0;
      JLabel guessCountLbl = new JLabel();
      JTextField secretLetterTF = new JTextField();
      JLabel jLabel1 = new JLabel();
      JButton guessBtn = new JButton();
      JLabel guessLbl = new JLabel();
      JRadioButton lowRB = new JRadioButton();
      JRadioButton hiRB = new JRadioButton();
      ButtonGroup loHiRBGroup = new ButtonGroup();
    
      //Construct the applet
      public GuessApplet() {
      }
      //Initialize the applet
      public void init() {
        try {
          jbInit();
          guessBtn.addActionListener(this); // 'hook up' the guess btn
          guessCount=0;
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception {
        guessCountLbl.setText("Number of guesses: 0");
        secretLetterTF.setText("");
        secretLetterTF.setColumns(5);
        jLabel1.setText("Enter Secret Letter");
        guessBtn.setText("Guess");
        guessLbl.setBorder(BorderFactory.createLineBorder(Color.black));
        guessLbl.setPreferredSize(new Dimension(40, 22));
        guessLbl.setHorizontalAlignment(SwingConstants.CENTER);
        guessLbl.setText("");
        lowRB.setMinimumSize(new Dimension(49, 25));
        lowRB.setText("Low");
        hiRB.setText("Hi");
        this.add(jLabel1, null);
        this.add(secretLetterTF, null);
        this.add(guessBtn, null);
        this.add(guessLbl, null);
        this.add(guessCountLbl, null);
        this.add(lowRB, null);
        this.add(hiRB, null);
        loHiRBGroup.add(lowRB);
        loHiRBGroup.add(hiRB);
    
      }
      //Start the applet
      public void start() {}
      //Stop the applet
      public void stop() {}
      //Destroy the applet
      public void destroy() {}
      //Get Applet information
      public String getAppletInfo() {
        return "The guessALetter Applet";
      }
    
      /**
       * Handle callback from the guess btn
       * @param e
       */
      public void actionPerformed(ActionEvent e) {
        char guessChar;
        String s=secretLetterTF.getText().trim();
        if (s.length()==0) return; // bad entry....
        if (guessCount==0) {
          guessChar=getGuessChar();
          this.guessLbl.setText(Character.toString(guessChar));
        } else {
          char c = (char) s.getBytes()[0];
          guessChar = getGuessChar();
          this.guessLbl.setText(Character.toString(guessChar));
          if (c == guessChar) {
            guessCountLbl.setText("Correct, " + guessCount + " guesses");
            return;
          }
        }
        guessCount++;
        guessCountLbl.setText("Number of guesses: " + guessCount);
      }
      /**
       * Do a binary lookup
       * @return
       */
      private char getGuessChar() {
        int low=0;
        int hi=chars.length;
        if (lowRB.isSelected()) {
          low=currLow;
          hi=getMiddle(currLow,currHi);
        } else if (hiRB.isSelected()) {
          low=getMiddle(currLow,currHi);
          hi=currHi;
        }
        int guessPos=getMiddle(low,hi)-1;
        currHi=hi;
        currLow=low;
        System.out.println(currHi+" "+currLow);
        return chars[guessPos];
      }
      private int getMiddle(int a, int b) {
        return (int)(((double)(a+b))/2.0 +0.5);
      }
    
      //Main method
      public static void main(String[] args) {
        GuessApplet applet = new GuessApplet();
        applet.isStandalone = true;
        Frame frame;
        frame = new Frame();
        frame.setTitle("Applet Frame");
        frame.add(applet, BorderLayout.CENTER);
        applet.init();
        applet.start();
        frame.setSize(400,320);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
        frame.setVisible(true);
      }
    
    }

  4. #4
    Join Date
    Mar 2005
    Posts
    27

    Thanks!

    Thank you for the help. I really appreciate it much. I know that what you gave me is not complete but it's a way for me to understand the problem completely. Thanks once more.

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links