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 6 of 6

Thread: Help please

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help please

    Write a class named Coin. The Coin class should have the following field:
    • A String named sideUp. The sideUp field will hold either “heads” or “tails” indicating the side of the coin that is facing up.
    The Coin class should have the following methods:
    • A no-arg constructor that randomly determines the side of the coin that is facing up (“heads” or “tails”) and initializes the sideUp field accordingly.
    • A void method named toss that simulates the tossing of the coin. When the toss method is called, it randomly determines the side of the coin that is facing up (“heads” or “tails”) and sets the sideUp field accordingly.
    • A method named getSideUp that returns the value of the sideUp field.
    Write a program that demonstrates the Coin class. The program should create an instance of the class and display the side that is initially facing up. Then, use a loop to toss the coin 20 times. Each time the coin is tossed, display the side that is facing up. The program should keep count of the number of times heads is facing up and the number of times tails is facing up, and display those values after the loop finishes.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help please

    Do you have any specific questions about your assignment?
    Please post your code and any questions about problems you are having.

    Be sure to wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help please

    sry I wrote wrong one (this one is done )
    but help me with this
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    /**
    * The MenuWindow class demonstrates a menu system.
    */

    public class MenuWindow extends JFrame
    {
    private JLabel messageLabel; // To display a message
    private JLabel results;// to display result
    private final int LABEL_WIDTH = 400; // The label's width
    private final int LABEL_HEIGHT = 200; // The label's height

    // The following variables will reference menu components.
    private JMenuBar menuBar; // The menu bar
    private JMenu fileMenu; // The File menu
    private JMenu textMenu; // The Text menu
    private JMenu cellMenu; // The phone menu
    private JMenu addMenu; // The add on menu
    private JMenuItem exitItem; // An item to exit the application
    private JRadioButtonMenuItem blackItem; // To make the text black
    private JRadioButtonMenuItem redItem; // To make the text red
    private JRadioButtonMenuItem blueItem; // To make the text blue
    private JRadioButtonMenuItem blackItem1; // To make the text black
    private JRadioButtonMenuItem redItem1; // To make the text red
    private JRadioButtonMenuItem blueItem1; // To make the text blue

    private JCheckBoxMenuItem voiceItem; // To toggle visibility
    private JCheckBoxMenuItem textItem; // To toggle visibility

    /**
    * Constructor
    */

    public MenuWindow()
    {
    // Call the JFrame constructor.
    super("Cell Phone Package");

    // Specify an action for the close button.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create the message label and set its size and color.
    messageLabel = new JLabel("Use the menu to " +
    "select the best plan for you.",
    SwingConstants.CENTER);
    messageLabel.setPreferredSize(
    new Dimension(LABEL_WIDTH, LABEL_HEIGHT));
    messageLabel.setForeground(Color.BLACK);

    // Add the label to the content pane.
    add(messageLabel);



    // Build the menu bar.
    buildMenuBar();

    // Pack and display the window.
    pack();
    setVisible(true);


    }

    /**
    * The buildMenuBar method builds the menu bar.
    */

    private void buildMenuBar()
    {
    // Create the menu bar.
    menuBar = new JMenuBar();

    // Create the file and text menus.
    buildFileMenu();
    buildTextMenu();
    buildAddMenu();
    buildCellMenu();

    // Add the file and text menus to the menu bar.
    menuBar.add(fileMenu);
    menuBar.add(textMenu);
    menuBar.add(addMenu);
    menuBar.add(cellMenu);


    // Set the window's menu bar.
    setJMenuBar(menuBar);
    }

    /**
    * The buildFileMenu method builds the File menu
    * and returns a reference to its JMenu object.
    */

    private void buildFileMenu()
    {
    // Create an Exit menu item.
    exitItem = new JMenuItem("Exit");
    exitItem.setMnemonic(KeyEvent.VK_X);
    exitItem.addActionListener(new ExitListener());

    // Create a JMenu object for the File menu.
    fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);

    // Add the Exit menu item to the File menu.
    fileMenu.add(exitItem);
    }

    /**
    * The buildTextMenu method builds the Text menu
    * and returns a reference to its JMenu object.
    */

    private void buildTextMenu()
    {
    // Create the radio button menu items to change the color
    // of the text. Add an action listener to each one.
    blackItem = new JRadioButtonMenuItem("300 minutes:$45 per month", true);
    blackItem.setMnemonic(KeyEvent.VK_B);
    blackItem.addActionListener(new PlanListener());

    redItem = new JRadioButtonMenuItem("800 minutes:$65 per month");
    redItem.setMnemonic(KeyEvent.VK_R);
    redItem.addActionListener(new PlanListener());

    blueItem = new JRadioButtonMenuItem("1500 minutes:$99 per month");
    blueItem.setMnemonic(KeyEvent.VK_U);
    blueItem.addActionListener(new PlanListener());

    // Create a button group for the radio button items.
    ButtonGroup group = new ButtonGroup();
    group.add(blackItem);
    group.add(redItem);
    group.add(blueItem);


    // Create a JMenu object for the Text menu.
    textMenu = new JMenu("Minutes");
    textMenu.setMnemonic(KeyEvent.VK_T);

    // Add the menu items to the Text menu.
    textMenu.add(blackItem);
    textMenu.add(redItem);
    textMenu.add(blueItem);

    }

    private void buildAddMenu()
    {
    // Create the radio button menu items to change the color
    // of the text. Add an action listener to each one.
    blackItem1 = new JRadioButtonMenuItem("Model 100:$29.95", true);
    blackItem1.setMnemonic(KeyEvent.VK_S);
    blackItem1.addActionListener(new PlanListener());

    redItem1 = new JRadioButtonMenuItem("Model 110:$49.95");
    redItem1.setMnemonic(KeyEvent.VK_K);
    redItem1.addActionListener(new PlanListener());

    blueItem1 = new JRadioButtonMenuItem("Model 200:$99.95");
    blueItem1.setMnemonic(KeyEvent.VK_P);
    blueItem1.addActionListener(new PlanListener());

    // Create a button group for the radio button items.
    ButtonGroup group = new ButtonGroup();
    group.add(blackItem1);
    group.add(redItem1);
    group.add(blueItem1);


    // Create a JMenu object for the Text menu.
    addMenu = new JMenu("Cell-Phone model");
    textMenu.setMnemonic(KeyEvent.VK_Z);

    // Add the menu items to the Text menu.
    addMenu.add(blackItem1);
    addMenu.add(redItem1);
    addMenu.add(blueItem1);

    }

    private void buildCellMenu()
    {
    // Create a check box menu item to make the text
    // visible or invisible.
    voiceItem = new JCheckBoxMenuItem("Voice mail option", true);
    voiceItem.setMnemonic(KeyEvent.VK_E);
    voiceItem.addActionListener(new PlanListener());

    // Create a check box menu item to make the text
    // visible or invisible.
    textItem = new JCheckBoxMenuItem("Text message option", true);
    textItem.setMnemonic(KeyEvent.VK_F);
    textItem.addActionListener(new PlanListener());

    // Create a JMenu object for the Text menu.
    cellMenu = new JMenu("Add ons");
    cellMenu.setMnemonic(KeyEvent.VK_D);

    // Add the menu items to the Text menu.
    cellMenu.add(voiceItem);
    cellMenu.add(textItem);

    }


    /**
    * Private inner class that handles the event that
    * is generated when the user selects Exit from
    * the File menu.
    */

    private class ExitListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    }

    /**
    * Private inner class that handles the event that
    * is generated when the user selects a color from
    * the Text menu.
    */

    private class PlanListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    double plan1;
    double package1;
    double text;
    double message;
    double addOn;
    double total;

    // Determine which color was selected and
    // act accordingly.
    if (blackItem.isSelected()){
    plan1=45;
    else if (redItem.isSelected())
    plan1=65;
    else if (blueItem.isSelected())
    plan1=99;
    if (blackItem1.isSelected())
    package1=29.95;
    else if (redItem1.isSelected())
    package1=49.95;
    else if (blueItem1.isSelected())
    package1=99.95;
    if (voiceItem.isSelected())
    message =5;
    if(textItem.isSelected())
    text=10;

    addOn=message + text;
    total= package1+(6/package1)*100+ plan1+ addOn;



    }
    }


    /**
    * The main method creates an instance of the MenuWindow
    * class, which causes it to display its window.
    */

    public static void main(String[] args)
    {
    new MenuWindow();
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help please

    You forgot to ask any questions.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Be sure the code is properly formatted.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help please

    <import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    /**
    * The MenuWindow class demonstrates a menu system.
    */
     
    public class MenuWindow extends JFrame
    {
    private JLabel messageLabel; // To display a message
    private JLabel results;// to display result
    private final int LABEL_WIDTH = 400; // The label's width
    private final int LABEL_HEIGHT = 200; // The label's height
     
    // The following variables will reference menu components.
    private JMenuBar menuBar; // The menu bar
    private JMenu fileMenu; // The File menu
    private JMenu textMenu; // The Text menu
    private JMenu cellMenu; // The phone menu
    private JMenu addMenu; // The add on menu
    private JMenuItem exitItem; // An item to exit the application
    private JRadioButtonMenuItem blackItem; // To make the text black
    private JRadioButtonMenuItem redItem; // To make the text red
    private JRadioButtonMenuItem blueItem; // To make the text blue
    private JRadioButtonMenuItem blackItem1; // To make the text black
    private JRadioButtonMenuItem redItem1; // To make the text red
    private JRadioButtonMenuItem blueItem1; // To make the text blue
     
    private JCheckBoxMenuItem voiceItem; // To toggle visibility
    private JCheckBoxMenuItem textItem; // To toggle visibility
     
    /**
    * Constructor
    */
     
    public MenuWindow()
    {
    // Call the JFrame constructor.
    super("Cell Phone Package");
     
    // Specify an action for the close button.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    // Create the message label and set its size and color.
    messageLabel = new JLabel("Use the menu to " +
    "select the best plan for you.",
    SwingConstants.CENTER);
    messageLabel.setPreferredSize(
    new Dimension(LABEL_WIDTH, LABEL_HEIGHT));
    messageLabel.setForeground(Color.BLACK);
     
    // Add the label to the content pane.
    add(messageLabel);
     
     
     
    // Build the menu bar.
    buildMenuBar();
     
    // Pack and display the window.
    pack();
    setVisible(true);
     
     
    }
     
    /**
    * The buildMenuBar method builds the menu bar.
    */
     
    private void buildMenuBar()
    {
    // Create the menu bar.
    menuBar = new JMenuBar();
     
    // Create the file and text menus.
    buildFileMenu();
    buildTextMenu();
    buildAddMenu();
    buildCellMenu();
     
    // Add the file and text menus to the menu bar.
    menuBar.add(fileMenu);
    menuBar.add(textMenu);
    menuBar.add(addMenu);
    menuBar.add(cellMenu);
     
     
    // Set the window's menu bar.
    setJMenuBar(menuBar);
    }
     
    /**
    * The buildFileMenu method builds the File menu
    * and returns a reference to its JMenu object.
    */
     
    private void buildFileMenu()
    {
    // Create an Exit menu item.
    exitItem = new JMenuItem("Exit");
    exitItem.setMnemonic(KeyEvent.VK_X);
    exitItem.addActionListener(new ExitListener());
     
    // Create a JMenu object for the File menu.
    fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
     
    // Add the Exit menu item to the File menu.
    fileMenu.add(exitItem);
    }
     
    /**
    * The buildTextMenu method builds the Text menu
    * and returns a reference to its JMenu object.
    */
     
    private void buildTextMenu()
    {
    // Create the radio button menu items to change the color
    // of the text. Add an action listener to each one.
    blackItem = new JRadioButtonMenuItem("300 minutes:$45 per month", true);
    blackItem.setMnemonic(KeyEvent.VK_B);
    blackItem.addActionListener(new PlanListener());
     
    redItem = new JRadioButtonMenuItem("800 minutes:$65 per month");
    redItem.setMnemonic(KeyEvent.VK_R);
    redItem.addActionListener(new PlanListener());
     
    blueItem = new JRadioButtonMenuItem("1500 minutes:$99 per month");
    blueItem.setMnemonic(KeyEvent.VK_U);
    blueItem.addActionListener(new PlanListener());
     
    // Create a button group for the radio button items.
    ButtonGroup group = new ButtonGroup();
    group.add(blackItem);
    group.add(redItem);
    group.add(blueItem);
     
     
    // Create a JMenu object for the Text menu.
    textMenu = new JMenu("Minutes");
    textMenu.setMnemonic(KeyEvent.VK_T);
     
    // Add the menu items to the Text menu.
    textMenu.add(blackItem);
    textMenu.add(redItem);
    textMenu.add(blueItem);
     
    }
     
    private void buildAddMenu()
    {
    // Create the radio button menu items to change the color
    // of the text. Add an action listener to each one.
    blackItem1 = new JRadioButtonMenuItem("Model 100:$29.95", true);
    blackItem1.setMnemonic(KeyEvent.VK_S);
    blackItem1.addActionListener(new PlanListener());
     
    redItem1 = new JRadioButtonMenuItem("Model 110:$49.95");
    redItem1.setMnemonic(KeyEvent.VK_K);
    redItem1.addActionListener(new PlanListener());
     
    blueItem1 = new JRadioButtonMenuItem("Model 200:$99.95");
    blueItem1.setMnemonic(KeyEvent.VK_P);
    blueItem1.addActionListener(new PlanListener());
     
    // Create a button group for the radio button items.
    ButtonGroup group = new ButtonGroup();
    group.add(blackItem1);
    group.add(redItem1);
    group.add(blueItem1);
     
     
    // Create a JMenu object for the Text menu.
    addMenu = new JMenu("Cell-Phone model");
    textMenu.setMnemonic(KeyEvent.VK_Z);
     
    // Add the menu items to the Text menu.
    addMenu.add(blackItem1);
    addMenu.add(redItem1);
    addMenu.add(blueItem1);
     
    }
     
    private void buildCellMenu()
    {
    // Create a check box menu item to make the text
    // visible or invisible.
    voiceItem = new JCheckBoxMenuItem("Voice mail option", true);
    voiceItem.setMnemonic(KeyEvent.VK_E);
    voiceItem.addActionListener(new PlanListener());
     
    // Create a check box menu item to make the text
    // visible or invisible.
    textItem = new JCheckBoxMenuItem("Text message option", true);
    textItem.setMnemonic(KeyEvent.VK_F);
    textItem.addActionListener(new PlanListener());
     
    // Create a JMenu object for the Text menu.
    cellMenu = new JMenu("Add ons");
    cellMenu.setMnemonic(KeyEvent.VK_D);
     
    // Add the menu items to the Text menu.
    cellMenu.add(voiceItem);
    cellMenu.add(textItem);
     
    }
     
     
    /**
    * Private inner class that handles the event that
    * is generated when the user selects Exit from
    * the File menu.
    */
     
    private class ExitListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    }
     
    /**
    * Private inner class that handles the event that
    * is generated when the user selects a color from
    * the Text menu.
    */
     
    private class PlanListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    double plan1;
    double package1;
    double text;
    double message;
    double addOn;
    double total;
     
    // Determine which color was selected and
    // act accordingly.
    if (blackItem.isSelected()){
    plan1=45;
    else if (redItem.isSelected())
    plan1=65;
    else if (blueItem.isSelected())
    plan1=99;
    if (blackItem1.isSelected())
    package1=29.95;
    else if (redItem1.isSelected())
    package1=49.95;
    else if (blueItem1.isSelected())
    package1=99.95;
    if (voiceItem.isSelected())
    message =5;
    if(textItem.isSelected())
    text=10;
     
    addOn=message + text;
    total= package1+(6/package1)*100+ plan1+ addOn;
     
     
     
    }
    }
     
     
    /**
    * The main method creates an instance of the MenuWindow
    * class, which causes it to display its window.
    */
     
    public static void main(String[] args)
    {
    new MenuWindow();
    }
    }>


    --- Update ---

    it does not run well

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help please

    Please edit the code in your last post and format it properly. Nested statements should be indented 3-4 spaces. All statements should not start in the first column.

    You have forgotten to post your questions about any problems you are having.

    --- Update ---

    it does not run well
    Please explain what the code does and what is wrong with its execution and say what you want the program to do.

    Please edit the code in your last post and format it properly. Nested statements should be indented 3-4 spaces. All statements should not start in the first column.
    Its hard to read and understand unformatted code. If you want any help testing the code, you need to format it.
    If you don't understand my answer, don't ignore it, ask a question.