Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Random Output
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hemin92




PostPosted: Wed Jan 14, 2009 6:21 pm   Post subject: Random Output

i trying to make a program that gives a list of tv manufacturers and their price range. I have another box that says in stock and out of stock and gives the stores to get it from. I want to be able to click on a tv type and the stock box will randomly say in stock or out of stock how do i do this? Here is my code so far:

Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;       
import java.util.*;

public class TvManufacturer implements ActionListener {
 JFrame frame;
 JPanel contentPane;
 JComboBox tvNames;
 JComboBox Stock;
 JLabel tvListPrompt, Price,Store;
 
    public TvManufacturer(){
        /* Create and set up the frame */
        frame = new JFrame("TV Manufacturer (EDITION 10.0)");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* Create a content pane with a BoxLayout and empty borders */
        contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
        contentPane.setBorder(BorderFactory.createEmptyBorder(9, 0, 100, 100));
       
     /* Create a combo box and a descriptive label */
     tvListPrompt = new JLabel("Select a Manufacturer:");
    tvListPrompt.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
     contentPane.add(tvListPrompt);
     
     /* Create a combo box and a descriptive label */
     tvListPrompt = new JLabel("Stock Check: ");
     tvListPrompt.setAlignmentX(JLabel.LEFT_ALIGNMENT);
     contentPane.add(tvListPrompt);
     
    String[] names = {"Sony", "Sharp", "Panasonic", "Samsung", "LG", "Toshiba"};
     tvNames = new JComboBox(names);
    tvNames.setAlignmentX(JComboBox.RIGHT_ALIGNMENT);
     tvNames.setSelectedIndex(0);
    tvNames.addActionListener(this);
     contentPane.add(tvNames);
     
     String[] stock = {"IN STOCK AT:","OUT OF STOCK AT:", };
     Stock = new JComboBox(stock);
    Stock.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
     Stock.setSelectedIndex(0);
    Stock.addActionListener(this);
     contentPane.add(Stock);

     /* Create and add a label that will display the Tv price range */
    Price = new JLabel("$499- $19,999");
     Price.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 10));
    Price.setAlignmentX(JComboBox.RIGHT_ALIGNMENT);
     contentPane.add(Price);
   
    ---->      /* Create and add a label that will display the Tv price range */
    Store = new JLabel("Walmart, Best Buy, Future Shop");
     Store.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
   Store.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
     contentPane.add(Store); <----------
     
   
   
   
       
     /* Add content pane to frame */
        frame.setContentPane(contentPane);

        /* Size and then display the frame. */
        frame.pack();
        frame.setVisible(true);
    }
   
   
 /**
  * Handle a selection from the combo box
  * post: The TV price range for the selected TV Manufacturer has been displayed.
  */

    public void actionPerformed(ActionEvent event) {
  JComboBox comboBox = (JComboBox)event.getSource();
  String tvName = (String)comboBox.getSelectedItem();
  String Stock = (String)comboBox.getSelectedItem();
  if (tvName == "Sony") {
   Price.setText("$499 - $19,999");
  } else if (tvName == "Sharp") {
   Price.setText("$349.99 - $5,999.98");
  } else if (tvName == "Panasonic") {
   Price.setText("$699.99 - $6,999.98");
  } else if (tvName == "Samsung") {
   Price.setText("$379.99 - $5,299.98");
  } else if (tvName == "LG") {
  Price.setText("$399.99 - $3,799.98");
  } else if (tvName == "Toshiba") {
   Price.setText("$289.99 - $2,599.99");
  }
   -------> if (Stock == "IN STOCK AT:") {
       Store.setText("Walmart, Best Buy, Future Shop");
     }else if (Stock == "OUT OF STOCK AT:"); {
       Store.setText("Walmart, Best Buy, Future Shop");{
       }}} <--------
       
       
     
 
   
 /**
 * Main method
 * Create and show the GUI.
 */

    public static void runGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);

      TvManufacturer prices  = new TvManufacturer();
    }
     

    public static void main(String[] args) {
   
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                runGUI();
            }
        });
    }
}


The code with the arrow is what i want to randomly show when a tv brand is selected



Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="java"]Code Here[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
hemin92




PostPosted: Thu Jan 15, 2009 8:15 pm   Post subject: Re: Random Output

I need help!! Can someone please tell me how to do random out?
Insectoid




PostPosted: Thu Jan 15, 2009 8:56 pm   Post subject: RE:Random Output

Math.random()
HeavenAgain




PostPosted: Thu Jan 15, 2009 9:11 pm   Post subject: RE:Random Output

basically have a look at the following 2 methods.
Java:
import java.util.Random;

public class RandomNumber
{
        public static void main(String[] args)
        {
                //method number 1
                System.out.println(Math.random());

                //method number 2
                int n = 2; // any number
                Random rand = new Random();
                System.out.println(rand.nextInt(n));
        }
}

take a look at the API on these 2 approaches
  • http://java.sun.com/javase/6/docs/api/java/lang/Math.html#random()
  • http://java.sun.com/javase/6/docs/api/java/util/Random.html
hemin92




PostPosted: Fri Jan 16, 2009 10:23 am   Post subject: Re: Random Output

How do i incorporate random into my program i don't know how to
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: