Wednesday, 10 July 2013

Rolling a dice by java Program(Random number Generator)

Generating a Random Number in java is pretty easy by using Random() method in java. by using it we can create a simple dice game
Program:

import java.util.*;

class apple

{

 public static void main(String[] args)

{

  Random dice = new Random();

  int roll;

  for (int j = 1; j <= 1; j++)

{

   roll = 1 + dice.nextInt(6);

   System.out.println(roll + "");

  }

 }

}

Output: it will randomly changed when ever you run this program from 1 to 6 

Java Program Using Methods to display name

Program:


import java.util.*;
class cls1{
public void show(String name)
{
System.out.println("hello  "+name);
}
}
class apple
{
public static void main(String[] args)
{
 Scanner input=new Scanner(System.in);
cls1 c=new cls1();
System.out.println("enter your name:");
String name=input.nextLine();
c.show(name);
}
}

 Output:
enter your name: vegeta!

hello vegeta!

Note:
In this program when you enter your name
by using scanner class
then it will stores the string in "name"
we had created class cls1 having method
show(name)

so,your entered name is stored in that method
So,as a result it will displayed along with hello

Thursday, 4 July 2013

Basic addition java program

 Program:

import java.util.*;
public class apple
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double i,j,k;
System.out.println("enter first number");
i=sc.nextDouble();
System.out.println("enter second number");
j=sc.nextDouble();
k=i+j;
System.out.println(k);
}
}

Output:
 enter first number
5
enter second number
6
11.0

note:
you can build multiplication,subtraction,division,etc by just chaning the operator (+,-,*,/)
In this program we are using scanner class for dynamically entering the values of different numbers that's why importing java.util package

command line arguments program

/*command line argument*/


class cmd

{

 public static void main(String[] args)

 {

  for(int i=0;i<args.length;i++)

  {

  System.out.println("value="+args[i]);

 }

 }

}

note:
for compiling command line arguments 
you have to pass arguments next to class file name
i.e, here my class name is cmd
for creating class file
-->javac cmd.java
for running class file(your java program)
-->java cmd eminem is the best rapper

Output -->
1stvalue=eminem
2ndvalue=is
3rdvalue=the
4thvalue=best
5thvalue=rapper

In this program the first string is given 0 value 
In output we are giving 5 strings so it assigns 0-4 values

Basic Hello World Java Program

Program :


class Hello
{
 public static void main(String[] args)
 {
  System.out.println("Hello World!");
 }
}
Output :
    Hello World!

About Us

This blog is all about Java Programming, mainly aimed to provide easy and point to point learning of java.
you will get a lot of useful tutorials with simple examples.

Our Contributors: Techno2know