Needs help in logic

Discussion in 'Java' started by Eric, Jul 10, 2011.

  1. Eric

    Eric Guest

    Hi guys: Can any one please help me in that program. I try it but
    stuck in it.

    ask user to enter random number between 1 to 1000 using JTextfield.
    Input the user's guess in the code for a previously-registered event-
    handler method or the actionPerformed method of class based on the
    ActionListener
    interface. For the first guess color the entire background red. If
    this is the second or later guess, and they are further from the
    correct number than the last guess, then color the entire background
    blue. If they get the correct number then
    color the background some other color than red or blue. If the user
    guessed the number correctly, respond with their number, post a
    congratulatory message, get a new random number, and display a JButton
    to start a new game. Otherwise, to help the user close in on the
    correct number, post a message, with their guessed number, whether
    they are "TOO HIGH" or "TOO LOW" from the correct number, and whether
    they are "WARMER" or "COLDER" (this should match the background
    color). Also report the guess number of the next guess (e.g. "Enter
    guess number nnn"). use a concatenated string in JLabel for these
    incorrect guess messages. The process is repeated each game until the
    user guesses the correct number. erase obsolete status messages.



    import java.awt.*;
    import javax.swing.*;
    public class guessprogram {
    public static void main(String args[]){
    String title = (args.length == 0 ? "Guess Program"
    : args[0]);
    JFrame frame = new JFrame(title);
    Container content = frame.getContentPane();
    JPanel namePanel = new JPanel(new BorderLayout());
    JLabel nameLabel = new JLabel("Enter Number: ");
    JTextField nameTextField = new JTextField();
    nameLabel.setLabelFor(nameTextField);
    namePanel.add(nameLabel, BorderLayout.WEST);
    namePanel.add(nameTextField, BorderLayout.CENTER);
    content.add(namePanel, BorderLayout.NORTH);
    frame.setSize(250, 150);
    frame.setVisible(true);
    }
    }
     
    Eric, Jul 10, 2011
    #1
    1. Advertising

  2. Eric

    markspace Guest

    Well, I don't do homework for people, but I'll give you some hints.

    1. Don't use BorderLayout, it kinda sucks for what you're trying to do.
    The default FlowLayout of a JPanel is proabably better. Put all the
    components in the JPanel and add that to the main content pane of the
    JFrame. Then pack() the frame (frame.pack()) instead of setting its size.

    2. This is the next bit to add to your program:

    > event-
    > handler method or the actionPerformed method of class based on the
    > ActionListener


    Read this:

    <http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html>

    Then add a method to your main class like this:

    public class guessprogram implements .... name here... {

    public static void main(String args[]) {
    // this is what you have already...
    }

    public void actionPerformed(ActionEvent e) {
    // logic to test input here
    }

    }

    And add the ActionListener as that link shows you. That should get you
    started. Your program has threading problems, but we'll leave that
    alone for a beginner problem.
     
    markspace, Jul 10, 2011
    #2
    1. Advertising

  3. Eric

    Roedy Green Guest

    On Sat, 9 Jul 2011 19:05:42 -0700 (PDT), Eric <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >ask user to enter random number between 1 to 1000 using JTextfield.


    see http://mindprod.com/jgloss/homework.html

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    One thing I love about having a website, is that when I complain about
    something, I only have to do it once. It saves me endless hours of grumbling.
     
    Roedy Green, Jul 10, 2011
    #3
  4. Eric

    lewbloch Guest

    markspace wrote:
    > ... Your program has threading problems, but we'll leave that
    > alone for a beginner problem.
    >


    But not for very long, just long enough to get you through the
    immediate questions.

    --
    Lew
     
    lewbloch, Jul 11, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. logia
    Replies:
    6
    Views:
    651
  2. Replies:
    43
    Views:
    1,326
    Oliver Wong
    Dec 9, 2005
  3. spike
    Replies:
    8
    Views:
    1,474
    Steve Holden
    Feb 9, 2010
  4. Zayd Connor
    Replies:
    8
    Views:
    116
    Zayd Connor
    Apr 6, 2009
  5. Kishore

    Needs help with Matching Logic

    Kishore, Jul 20, 2004, in forum: Perl Misc
    Replies:
    8
    Views:
    113
    Kishore
    Jul 22, 2004
Loading...

Share This Page