Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: question about my applet

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default question about my applet

    need to write write a Java applet that will let the user guess the gas price and compare it with the price predicted by my program. Guess gas price (100 - 150): that never changes. so far i have this but stuck with method, generate a random number from 100 to 150, inclusive. Set it to predictPrice. so far i have this

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
     
    public class Assign7 extends JApplet implements ActionListener
    {
    int predictPrice;
    int guessPrice;
     
    JLabel questionLabel;
    JTextField answerField;
     
    public void init ()
    {
     
    Container container = getContentPane();
    container.setLayout( new FlowLayout() );
     
    questionLabel = new JLabel();
    container.add ( questionLabel);
    answerField = new JTextField ( 3 );
    answerField.addActionListener ( this );
    container.add (answerField );
     
    predictPrice = generateQuestion();
    }
     
    public void actionPerformed ( ActionEvent actionEvent )
    {
     
    guessPrice = Integer.parseInt(answerField.getText());
     
    displayMessage();
    }
    public int generateQuestion()
    {
    int num1, num2;
     
    num1 = 1 + ( int ) (Math.random() * 100 );
    num2 = 1 + ( int ) (Math.random() * 150 );
     
    questionLabel.setText( "Guess oil price (100 - 150)" );
     
    return num1 * num2;
    }
     
    public void displayMessage()
    {
    if ( guessPrice == predictPrice)
    {
    showStatus ( "You are right." );
    predictPrice = generateQuestion();
    }
    else
    {
      if (guessPrice >= predictPrice);
      {
    showStatus ( "Too low." );
    predictPrice = generateQuestion(); 
      }
    }
    }
    }
    {
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: question about my applet

    Again I ask- where are you stuck? I'm really not sure what you don't understand. You have an Applet, you know what you have to do... I honestly don't know what the problem is.

    Recommended reading: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: question about my applet

    stuck at generating the number from 100 -150.

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: question about my applet

    ok i have this now
    public int generateQuestion()
    {
    Random rand = new Random();
    int num = rand.nextInt(51) + 101;
    questionLabel.setText( "Guess oil price (100 - 150)" );
     
    return num;
    }
     
    public void displayMessage()
    {
    if ( guessPrice == predictPrice)
    {
    showStatus ( "You are right." );
    predictPrice = generateQuestion();
    }
    else if (guessPrice >= predictPrice);
    showStatus ( "Too low." );
    predictPrice = generateQuestion(); 
    }
    }
     
    {
    }

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: question about my applet

    In your other thread, you said you found an algorithm for generating a random number. Where did you get stuck trying to implement that?

    Hint: Math.random() gives you a random decimal between 0 (inclusive) and 1 (exclusive). The API is your friend.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: question about my applet

    ...so now is your problem solved? You win the prize for most confusing poster of the day.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: question about my applet

    no cuz im not sure on the return if i leave num as the return does it stay the same number until user guess the right price and my showstatus says "your right"

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: question about my applet

    If I'm interpreting your question correctly, the answer is no. Every time you call the generateQuestion() function, you'll return a different random number. But then again, I might be misunderstanding what you're asking.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: question about my applet

    yes that what im asking. i need the number to stay the same so if i have an instance variable, called int randomNumber. and had this returned would it stay the same?

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: question about my applet

    What happened when you tried that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. java applet program question (on getting path fails)
    By hellocheese in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 30th, 2011, 04:34 PM
  2. when i run applet
    By chonch in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 22nd, 2011, 03:56 PM
  3. applet img
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: April 7th, 2010, 09:14 PM
  4. applet menubar
    By tabutcher in forum Java Applets
    Replies: 2
    Last Post: March 5th, 2010, 08:30 AM
  5. [SOLVED] applet vs. gui app
    By rptech in forum AWT / Java Swing
    Replies: 3
    Last Post: August 27th, 2009, 09:13 AM