Please help me

/**Die.java*/

import java.util.*;

public class Die

{

private int sides;

private final int s = 6; //maximum face value

private int valueOnDie; // current value showing on the die

// Constructor which sets the initial face value

public Die()

{

valueOnDie = 1;

}

public Die(int s)

{

sides = s;

}

public int roll()

{

valueOnDie = (int)(Math.random() * s) + 1;

return valueOnDie;

}

public void setValue (int value)

{

valueOnDie = value;

}

public int getValue()

{

return valueOnDie;

}

public String toString()

{

String result = Integer.toString(valueOnDie);

return result;

}

}

/**PairOfDice.java*/

import java.util.*;

import java.text.*;

import java.io.*;

import java.lang.*;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

public class PairOfDice //extends JFrame

{

private Die die1, die2;

int value1,value2,total;

public PairOfDice ()

{

die1 = new Die();

die2 = new Die();

}

public int roll ()

{

value1 = die1.roll();

value2 = die2.roll();

total = value1 + value2;

return total;

}

public int getDie1()

{

return die1.getValue();

}

public int getDie2()

{

return die2.getValue();

}

public int getSum ()

{

return (die1.getValue()+die2.getValue());

}

}

/**DiceRoller.java*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class DiceRoller extends JFrame{

private JTextField firstDie = new JTextField(3);

private JTextField secondDie = new JTextField(3);

private JTextField totalRoll = new JTextField(3);

public DiceRoller()

{

JButton rollBtn= new JButton("Roll");

rollBtn.addActionListener(new RollBtnListener());

JButton exitBtn = new JButton("Exit");

exitBtn.addActionListener(new ExitBtnListener());

JPanel content = new JPanel();

content.setLayout(new FlowLayout());

content.add(new JLabel("First Die:"));

content.add(firstDie);

content.add(new JLabel("Second Die:"));

content.add(secondDie);

content.add(new JLabel("Total roll:"));

content.add(totalRoll);

content.add(rollBtn);

content.add(exitBtn);

setContentPane(content);

pack();

setTitle("Dice Roller");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

class RollBtnListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

int t,d1,d2;

PairOfDice frm = new PairOfDice();

t=frm.roll();

d1 = frm.getDie1();

d2 = frm.getDie2();

System.out.println("Sum of Dice:"+t);

System.out.println("Die1:"+d1);

System.out.println("Die2:"+d2);

/*if(t == 7)

System.out.println("Craps!");

if(d1==d2)

{

if(d1==1)

System.out.println("Snake Eyes!");

else if(d1==6)

System.out.println("Box Cars!");

}

*/

totalRoll.setText(""+t);

firstDie.setText(""+d1);

secondDie.setText(""+d2);

}

}

class ExitBtnListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

}

public static void main(String args[]){

DiceRoller window = new DiceRoller();

window.setVisible(true);

}

}

there is no problem with the code that i had given above it is working perfectly

i wanted to add some statements in the DiceRoller.java that if the dice is rolled and if both the die1 and die2 are 1 it should show the message "snake Eyes"

and if die1=die2=6 it should show Box cars

and if the total of the dice is 7 then it should show Craps!

so how can i add these features into my original program

my original program output is as shown

Dice Roller(title)

First Die: 6Second Die: 1Total roll: 7Roll(button)Exit(button)

since the total is 7 it should display the message Craps! in the window

please help me i will be thankful to you

[4393 byte] By [surkuma] at [2007-11-27 8:20:42]
# 1
At first glance (which wasn't that close since there's no formatting) your commented code looks like it should work. What happened when you ran it?
hunter9000a at 2007-7-12 20:09:03 > top of Java-index,Java Essentials,New To Java...
# 2

actually the code is running perfectly

what i want to get from you is

/*if(t == 7)

System.out.println("Craps!");

if(d1==d2)

{

if(d1==1)

System.out.println("Snake Eyes!");

else if(d1==6)

System.out.println("Box Cars!");

}

*/

i wanted to print an extra message in the frame using the above code

how can i print or dispaly a message in the frame

i use JLabel for naming die1 and die2 and its total

and JText field for displaying the result when the roll button is pressed

now how can i display the message craps or snake or box cars in the frame that is what i want to know

thank you

surkuma at 2007-7-12 20:09:03 > top of Java-index,Java Essentials,New To Java...
# 3
You mean like a popup dialog? http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
hunter9000a at 2007-7-12 20:09:03 > top of Java-index,Java Essentials,New To Java...
# 4

can you do me one favor can you please copy the code and then just compile and run it

the output will be in a frame with as

die 1: 6die2: 1total: 7rollexit

i wanted to add a message in the same frame by using the code that i commented, is that possible (not pop up it should be in the same frame)

surkuma at 2007-7-12 20:09:03 > top of Java-index,Java Essentials,New To Java...
# 5
dude you are using swing JFrame then why are you using System.out.println("")?. create a jlabel and use those if/else statements or JTextField
schumachera at 2007-7-12 20:09:03 > top of Java-index,Java Essentials,New To Java...
# 6
Next time please use a more meaningful subject line. Yours contains no information. We know you need help. The point is to give an idea of your problem so people can decide whether they're interested in trying to help.
jverda at 2007-7-12 20:09:03 > top of Java-index,Java Essentials,New To Java...
# 7
you can use &&exampleif ( die1 == 1 && die2 == 1) {//print something}
deAppela at 2007-7-12 20:09:03 > top of Java-index,Java Essentials,New To Java...
# 8

Since there is only one way to get a double six or a double 1:

if(total == 2) {

} else if(total == 12) {

}

floundera at 2007-7-12 20:09:03 > top of Java-index,Java Essentials,New To Java...
# 9

> actually the code is running perfectly

>

> what i want to get from you is

>

> /*if(t == 7)

> System.out.println("Craps!");

>

> if(d1==d2)

> {

> if(d1==1)

> System.out.println("Snake Eyes!");

> else if(d1==6)

> System.out.println("Box Cars!");

>

> */

>

> i wanted to print an extra message in the frame using

> the above code

> how can i print or dispaly a message in the frame

>

> i use JLabel for naming die1 and die2 and its total

>

> and JText field for displaying the result when the

> roll button is pressed

>

> now how can i display the message craps or snake or

> box cars in the frame that is what i want to know

>

> thank you

print text in JTextField =

JTextField txt;

txt = new JTextField(" ");

txt.setText("Result: " + result);

or simply create the JTextField and in your if:

if (die1 == 1 && die2 == 1){

txt.setText("someText");

}

if (die1 == 2 && die2 == 2){

txt.setText("someText");

}

etc

Message was edited by:

deAppel

-grammatical error

deAppela at 2007-7-12 20:09:04 > top of Java-index,Java Essentials,New To Java...
# 10
thanks for your helpi have done it using JText but i was told not to use JText for displaying the message so is there any other waythanks in advance
surkuma at 2007-7-12 20:09:04 > top of Java-index,Java Essentials,New To Java...
# 11

Sure

You could send the output to a file then open and view the file.

You could send the output directly to the printer, collect and read printout.

You could email the output, read and or print email message.

You could possibly even manage to connect to a telegraph service and send morse code.

But of course all this is pointless becuase we did not set the assignment and as such have no fucking idea what your teacher wants. Try asking them.

floundera at 2007-7-12 20:09:04 > top of Java-index,Java Essentials,New To Java...