Results 1 to 5 of 5
  1. #1
    jgcmetal86 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Hi I'm new to the forum and need some help.

    I'm taking a java class this semester and the teacher is not that helpful with student's questions. I've done pretty good in the class but I'm having some trouble with the last few programs. I use Netbeans to program. Here is the problem we are working on:

    Create a DiceInterfaceGui class
    • Add the ability to change the number of faces on a Die to the Die class. (i.e. create a constructor which sets sides using a parameter).
    • Create a GUI with the following
    • Number of sides on the die text field
    • Number of die to roll text field
    • Button for Roll
    • Button for Clear
    • Button for Exit
    • Text Area to display all of the die and the sum of all of the die. You set the TextArea output using setText(“Text to set”) exactly like a text field. TextAreas can have new lines (“\n”) exactly like a message dialog.

    This is my code for the gui.

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /*
    * DiceRoller.java
    *
    * Created on Jul 18, 2012, 1:27:14 PM
    */
    package chapmanjg_lab5b;

    /**
    *
    * @author chapmanjg
    */
    public class DiceRoller extends javax.swing.JFrame {

    /** Creates new form DiceRoller */
    public DiceRoller() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jtfSides = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    jtfdice = new javax.swing.JTextField();
    jtfResult = new javax.swing.JTextField();
    jbRoll = new javax.swing.JButton();
    jbClear = new javax.swing.JButton();
    jbExit = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setTitle("Dice Roller");

    jLabel1.setText("Enter the number of sides on the die:");

    jtfSides.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jtfSidesActionPerformed(evt);
    }
    });

    jLabel2.setText("Enter the number of dies to roll:");

    jtfdice.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jtfdiceActionPerformed(evt);
    }
    });

    jtfResult.setEditable(false);
    jtfResult.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jtfResultActionPerformed(evt);
    }
    });

    jbRoll.setText("Roll");
    jbRoll.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jbRollActionPerformed(evt);
    }
    });

    jbClear.setText("Clear");

    jbExit.setText("Exit");
    jbExit.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jbExitActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(8, 8, 8)
    .addComponent(jbRoll, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addComponent(jbClear)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jbExit))
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
    .addComponent(jtfdice, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
    .addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createParallelGroup(javax.swing.GroupLayout .Alignment.TRAILING, false)
    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(jtfSides, javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 18, Short.MAX_VALUE)
    .addComponent(jtfResult, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(34, 34, 34))
    );

    layout.linkSize(javax.swing.SwingConstants.HORIZON TAL, new java.awt.Component[] {jbClear, jbExit, jbRoll});

    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel1)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jtfSides, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(35, 35, 35)
    .addComponent(jLabel2)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jtfdice, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jbRoll)
    .addComponent(jbClear)
    .addComponent(jbExit)))
    .addComponent(jtfResult, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(77, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void jtfSidesActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handiling code here:
    }

    private void jtfdiceActionPerformed(java.awt.event.ActionEvent evt) {

    }

    private void jtfResultActionPerformed(java.awt.event.ActionEven t evt) {
    // TODO add your handling code here:
    }

    private void jbExitActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }

    private void jbRollActionPerformed(java.awt.event.ActionEvent evt) {
    int numDice, numSide;
    numDice = Integer.parseInt(jtfSides.getText());
    numSide = Integer.parseInt(jtfdice.getText());
    double sides = 0;
    for (int i = 0; i < numDice; i++){
    int value= (int)(Math.random()*sides+1);

    }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
    * For details see How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(DiceRoller.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(DiceRoller.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(DiceRoller.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(DiceRoller.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new DiceRoller().setVisible(true);
    }
    });
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton jbClear;
    private javax.swing.JButton jbExit;
    private javax.swing.JButton jbRoll;
    private javax.swing.JTextField jtfResult;
    private javax.swing.JTextField jtfSides;
    private javax.swing.JTextField jtfdice;
    // End of variables declaration
    }

  2. #2
    jgcmetal86 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Hi I'm new to the forum and need some help.

    I've also got this from the previous project. It's pretty much the same thing except not in a GUI. I'm trying to figure out where to put what.

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package chapmanjg_lab5a;

    import javax.swing.JOptionPane;

    /**
    *
    * @author chapmanjg
    */
    public class Chapmanjg_lab5a {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    String input = JOptionPane.showInputDialog("How many dice do you want to roll");
    int numberOfDice = Integer.parseInt(input);
    Dice dice = new Dice (numberOfDice);
    dice.rollDice();
    Die [] tempDice = dice.getDice();

    String output = "";
    for (int i = 0 ; i < tempDice.length; i++)
    {
    if (i<tempDice.length-1)
    {
    output+=tempDice[i].getValue()+ " + ";

    }else {
    output+=tempDice[i].getValue()+ " = " ;
    }
    }
    output+=dice.getDiceSum();

    JOptionPane.showMessageDialog(null, output);

    }
    }


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package chapmanjg_lab5a;

    /**
    *
    * @author chapmanjg
    */
    public class Dice {

    private Die [] dice;

    public Die[] getDice()
    {
    return dice;
    }
    Dice(int numberOfDice)
    {
    dice = new Die[numberOfDice];
    for (int i=0; i < dice.length; i++)
    {
    dice[i] = new Die();
    }
    }

    Dice(int numberOfDice, int numberOfSides)
    {
    dice = new Die[numberOfSides];
    for (int i=0; i < dice.length; i++)
    {
    dice[i] = new Die();
    }

    dice = new Die [5];
    for (int i=0; i < dice.length; i++)
    {
    dice[i] = new Die();
    }
    }

    public void rollDice ()

    {
    for (int i=0;i<dice.length;i++) {
    dice[i].roll();

    }
    }




    public int getDiceSum ()

    {

    int sum=0;
    for (int i=0;i<dice.length;i++)
    {
    sum+=dice[i].getValue();
    }

    return sum;

    }
    }


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package chapmanjg_lab5a;

    /**
    *
    * @author chapmanjg
    */
    public class Die {


    int value, sides;
    Die() {
    sides=6;
    }
    Die (int numberOfSides)
    {
    sides = numberOfSides;
    }
    public void roll(){
    value=(int)(Math.random()*sides+1);
    }
    public int getValue() {
    return value;
    }
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Hi I'm new to the forum and need some help.

    Please go through the Forum Rules -- particularly the third paragraph. Also go through these two pages:
    Guide For New Members
    BB Code List - Java Programming Forum

    This one might also help you to get better help on a forum:
    How to ask questions the smart way

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Hi I'm new to the forum and need some help.

    Moved from Advanced Java. Homework questions belong here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jgcmetal86 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Hi I'm new to the forum and need some help.

    Thanks I will read over that stuff. I just figured out most of this problem. Java is quiet the challenge.

Similar Threads

  1. New to this forum
    By thestranger in forum Introductions
    Replies: 6
    Last Post: 07-27-2008, 09:53 AM
  2. hello forum....
    By Vijay Swaminatha in forum Introductions
    Replies: 3
    Last Post: 06-29-2008, 05:58 AM
  3. New sub forum
    By tim in forum Suggestions & Feedback
    Replies: 3
    Last Post: 03-17-2008, 08:12 PM
  4. Welcome to our new forum: Forum Lobby
    By JavaForums in forum Forum Lobby
    Replies: 18
    Last Post: 02-07-2008, 05:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •