Tuesday, April 28, 2015

Java Program for Cricket

I have written simple program for cricket in java using java swing. And same i have converted into javascript as well. You can play cricket here. Java source code is given below, you copy and play in your machine and you can enhance as much as possible.

Start playing cricket

              Computer               
Score: 0
Over: 0.0
 
 
              You              
Score: 0
Over: 0.0
 
 



import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

public class Cricket extends JFrame implements ActionListener{

JLabel lblComp = new JLabel("Computer");
JLabel lblYou = new JLabel("You");
JLabel lblCompScore = new JLabel("Score");
JLabel lblYouScore = new JLabel("Score");
JLabel dispCompScore = new JLabel("0/0");
JLabel dispYouScore = new JLabel("0/0");
JLabel lblCompOver = new JLabel("Over");
JLabel lblYouOver = new JLabel("Over");
JLabel dispCompOver = new JLabel("0");
JLabel dispYouOver = new JLabel("0");

JLabel compResult = new JLabel("");
JLabel youResult = new JLabel("");
JLabel result = new JLabel("");

JButton playCompBtn = new JButton("Play Computer");
JButton playYouBtn = new JButton("Play");
JButton newGame = new JButton("New Game");

int score = 0;
int wick = 0;
int over = 0;
int ball = 0;
int compOrYou = 0;
int compScore = 0;

Random cricketPlay = new Random();

public Cricket()  throws Throwable{
super("Cricket");
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.LIGHT_GRAY);
setBounds(10, 10, 600, 400);

newGame.setBounds(200, 10, 150, 25);
newGame.setFont(new Font("Courier New", Font.BOLD, 18));
newGame.setForeground(Color.GRAY);

lblComp.setBounds(80, 50, 100, 25);
lblComp.setFont(new Font("Courier New", Font.BOLD, 18));
lblComp.setForeground(Color.BLUE);

lblCompScore.setBounds(50, 90, 100, 25);
dispCompScore.setBounds(200, 90, 100, 25);
lblCompOver.setBounds(50, 130, 100, 25);
dispCompOver.setBounds(200, 130, 100, 25);

playCompBtn.setBounds(50,200,180,30);
playCompBtn.setFont(new Font("Courier New", Font.BOLD, 18));
playCompBtn.setForeground(Color.RED);


lblYou.setBounds(380, 50, 100, 25);
lblYou.setFont(new Font("Courier New", Font.BOLD, 18));
lblYou.setForeground(Color.BLUE);

lblYouScore.setBounds(350, 90, 100, 25);
dispYouScore.setBounds(500, 90, 100, 25);
lblYouOver.setBounds(350, 130, 100, 25);
dispYouOver.setBounds(500, 130, 100, 25);

playYouBtn.setBounds(350,200,180,30);
playYouBtn.setFont(new Font("Courier New", Font.BOLD, 18));
playYouBtn.setForeground(Color.RED);

compResult.setBounds(60,250,180,30);
compResult.setFont(new Font("Courier New", Font.BOLD, 18));
compResult.setForeground(Color.MAGENTA);

youResult.setBounds(370,250,180,30);
youResult.setFont(new Font("Courier New", Font.BOLD, 18));
youResult.setForeground(Color.MAGENTA);

result.setBounds(250,300,180,30);
result.setFont(new Font("Courier New", Font.BOLD, 18));
result.setForeground(Color.MAGENTA);



add(lblComp);
add(lblCompScore);
add(dispCompScore);
add(lblCompOver);
add(dispCompOver);
add(playCompBtn);

add(lblYou);
add(lblYouScore);
add(dispYouScore);
add(lblYouOver);
add(dispYouOver);
add(playYouBtn);

add(compResult);
add(youResult);
add(result);

add(newGame);
playYouBtn.setVisible(false);
playCompBtn.addActionListener(this);
playYouBtn.addActionListener(this);
newGame.addActionListener(this);
}
  public static void main(String args[])  throws Throwable {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new Cricket();
  }
  public void actionPerformed(ActionEvent e) {
if (e.getSource() == playCompBtn) {
compOrYou = 1;
for(int i=0; i<30; i++) {
playBall();
dispCompScore.setText(score+"/"+wick);
dispCompOver.setText(over+"."+ball);
if(over == 5 || wick ==10) {
compResult.setText("");
break;
}
}
playCompBtn.setVisible(false);
playYouBtn.setVisible(true);
compScore = score;
score = 0;
wick = 0;
over = 0;
ball = 0;
} else if(e.getSource() == playYouBtn) {
compOrYou = 2;
playBall();
dispYouScore.setText(score+"/"+wick);
dispYouOver.setText(over+"."+ball);
} else {
compScore = 0;
compOrYou = 0;
score = 0;
wick = 0;
over = 0;
ball = 0;
dispCompScore.setText(score+"/"+wick);
dispCompOver.setText(over+"."+ball);
dispYouScore.setText(score+"/"+wick);
dispYouOver.setText(over+"."+ball);
compResult.setText("");
youResult.setText("");
result.setText("");
playYouBtn.setVisible(false);
playCompBtn.setVisible(true);
}
  }
  private void playBall() {
int ballResult = cricketPlay.nextInt(8);
ball = ball + 1;
if(ball == 6) {
over = over + 1;
ball = 0;
}
if((ballResult >= 0 && ballResult <=4) || ballResult == 6) {
score = score + ballResult;
if(compOrYou == 1) {
compResult.setText(ballResult+" Run(s)");
} else {
youResult.setText(ballResult+" Run(s)");
}
} else {
wick = wick + 1;
if(compOrYou == 1) {
compResult.setText("Wicket");
} else {
youResult.setText("Wicket");
}
}


if(compOrYou == 2) {
if(over == 5 || wick == 10) {
if(compScore == score) {
result.setText("Match Draw");
} else if(compScore < score) {
result.setText("You Win");
} else {
result.setText("Computer Win");
}
playYouBtn.setVisible(false);
} else if(compScore < score) {
result.setText("You Win");
playYouBtn.setVisible(false);
}
}
  }
}

Saturday, April 11, 2015

Java final access modifier

Purpose of final access modifier knows everyone, that is if any variable is declared as final, value of that variable can not be changed during run time. Normally all the constant variables will be declared in a constant class and that class will be referred throughout the project.
In this case, if any constant value need to be changed which was declared as final, we have to compile all the class files which all are referring that constant class. Because final variables will be referred at the time of compilation only. During run time, final variables will not be referred.

We will see below example,

Constant java file:
     public class JavaConstant {
           public static final int MIN_VAL = 100;
     }

Java file which is using above constant:
     public class JavaPrint {
           public static void main(String args[]) {
                System.out.println(JavaConstant.MIN_VAL);
           }
     }

We will compile both java files and execute the JavaPrint class file.
Output will be 100

Now , we are modifying JavaConstant java file as below
     public class JavaConstant {
           public static final int MIN_VAL = 150;// value modified from 100 to 150
     }

Now, we are compiling JavaConstant java file alone and execute JavaPrint class file.
Output will be 100 again.
Because modified value of MIN_VAL will not be referred during run time since it is declared as final

Now, we will compile JavaPrint java file and execute JavaPrint class file.
Output will be 150

Thursday, April 2, 2015

Interesting java codes

Most of the software engineers are living with java programs daily for their big projects. But the real fact is, we all are doing Copy Paste job, because mostly your working projects will be well designed. As we are very busy with Copy Paste job, some times we failed to understand the some basic interesting things in java. Some of us might be experienced with interesting things in java programs. I am sharing few things here which i have learned.

Some of us might though that, below code will not compile. But below code will compile and execute

     public class Curiosity {
           public static void main (String[ ] args) {
                System.out.println ("A curiosity");
                http://google.com
                System.out.println ("compile Ok!");
          }
     }
Output:
A curiosity
compile Ok!

Reason is simple, many of us aware of label statement in java. So here "http:" is label and // is comment line.

The next one is strange. Is is possible to call any method or field by null reference?. Most of us will give answer as NO. We have seen NullPointerException many times, so we thought that, below code will give run time exception. But below code will compile and execute successfully.

     public class Foo {
           static int fubar = 30;
           public static void main(String args[]) {
                Foo foo = null;
                System.out.println(foo.fubar);
           }
     }
Output:
30

Reason: Here, we have to notice variable declaration of "fubar". "fubar"is declared as static, so this will be stored in Method Area. Static variables are accessed via class and though the object is null, the type Foo is what is used to determine reference to the variable.