Thursday 4 July 2013

Dies Game Java Program

Dies Game Java Program

Java Code:
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTextArea;


public class DiceCup
{

    public int frequency;
    public int [] faceValue= new int[10];
    int sum0fValues=0;
   
    //int r=1;

    private Die d= new Die();
   
    DiceCup(){
         
    }
     public void rollDice()

      {
        // System.out.print("i");
         for(int r=0; r<faceValue.length; r++){
         faceValue[r]= d.roll();
         sum0fValues+=faceValue[r];
         }
       
      }
     public String toString()
     {
         String [] a=new String[10];
         for(int i=0 ; i<faceValue.length;i++){
             a[i]=Integer.toString(faceValue[i]);
             //System.out.println(a[i]);
         }
       
     String f = "total time of dice rolled is :"+Integer.toString(frequency) + " \n"+ "total score : "+ Integer.toString(sum0fValues);
   
    // String v= Integer.toString(sum0fValues);
     return f;
     }
   
     public void display(){
         for(int i=0; i<faceValue.length;i++){
             frequency+=1;
           
             System.out.println("Die " + frequency + " Value "+faceValue[i]);
             }
         System.out.print("Sum of Values is  "+sum0fValues);
     }
   
}
----------------------------------------------
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTextArea;


public class DiceExperiment {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame fram=new JFrame();
        fram.setSize(400,200);
        fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fram.setVisible(true);
        DiceCup d= new DiceCup();
        d.rollDice();
        d.display();
        JTextArea text;
        text = new JTextArea(20, 50);
          fram.add(text);
          text.setFont(new Font( "Courier", Font.PLAIN, 10 ));
          text.setText(d.toString());
         
        //  text.setText(d.display());
    }

}
----------------------------------------------------
public class Die
{
private final int MAX = 6; // maximum face value
private int faceValue; // current value showing on the die

public Die()
{
faceValue = 1;
}

public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}

public void setFaceValue (int value)
{
faceValue = value;
}

public int getFaceValue()
{
return faceValue;
}

public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
}

Snapshots:


No comments:

Post a Comment