Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Needs help in logic (http://www.velocityreviews.com/forums/t751213-needs-help-in-logic.html)

Eric 07-10-2011 02:05 AM

Needs help in logic
 
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);
}
}

markspace 07-10-2011 02:56 AM

Re: Needs help in logic
 
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.


Roedy Green 07-10-2011 09:02 AM

Re: Needs help in logic
 
On Sat, 9 Jul 2011 19:05:42 -0700 (PDT), Eric <ehtisham@gmail.com>
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.

lewbloch 07-11-2011 06:18 PM

Re: Needs help in logic
 
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



All times are GMT. The time now is 05:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.