Pages

Wednesday, April 08, 2015

Write a program that Roll Dice 6000000 times

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Random;

public class RollDie {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Random rnd = new Random(); // random number generator

        int frequency1 = 0; // maintains count of 1s rolled
        int frequency2 = 0; // count of 2s rolled
        int frequency3 = 0; // count of 3s rolled
        int frequency4 = 0; // count of 4s rolled
        int frequency5 = 0; // count of 5s rolled
        int frequency6 = 0; // count of 6s rolled


        int face; // most recently rolled value
        
        // tally counts for 6,000,000 rolls of a die
        for (int roll = 1; roll <= 6000000; roll++) {
            face = 1 + rnd.nextInt(6);
            // determine roll value 1-6 and increment appropriate counter
            switch (face) {
                case 1:
                    ++frequency1; // increment the 1s counter
                    break;
                case 2:
                    ++frequency2; // increment the 2s counter
                    break;
                case 3:
                    ++frequency3; // increment the 3s counter
                    break;
                case 4:
                    ++frequency4; // increment the 4s counter
                    break;
                case 5:
                    ++frequency5; // increment the 5s counter
                    break;
                case 6:
                    ++frequency6; // increment the 6s counter
                    break; // optional at end of switch
            } // end switch
        } // end for

        System.out.println("Face\tFrequency"); // output headers
        System.out.printf("1\t%d\n2\t%d\n3\t%d\n4\t%d\n5\t%d\n6\t%d\n",
                frequency1, frequency2, frequency3, frequency4,
                frequency5, frequency6);

    }
}

Output / Run:

Face Frequency
1 999981
2 1000458
3 1000487
4 999078
5 999474
6 1000522
BUILD SUCCESSFUL (total time: 1 second)

Write a program that print static Random Number

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import java.util.Random;

public class StaticRandomNo {

    public static void main(String[] args) {
            // always produced same nomber
        Random rand = new Random(20071969);
            for (int j = 0; j < 10; j++) {
                int pick = rand.nextInt(10);
                System.out.print(pick+ "   ");
            }
            System.out.println();
        }
    }

Output / Run:

3   0   3   0   7   9   8   2   2   5   
BUILD SUCCESSFUL (total time: 1 second)

Sunday, March 29, 2015

Basic Syntax & Program of Java Programing

Write a Program that Add Two Complex Number

Input

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package AddTwoComplexNumber;

public class complex {
// declare Instance Variable

    private int real;
    private int imaginary;

// constructor with parameter
    public complex(int realN, int imgN) {
        real = realN;
        imaginary = imgN;
    }

// constructor without parameter
    public complex() {
        real = 0;
        imaginary = 0;
    }

// setter method
    public void setReal(int realN) {
        real = realN;
    }

    public void setImgN(int imgN) {
        imaginary = imgN;
    }

// getter method
    public int getReal() {
        return real;
    }

    public int getImgN() {
        return imaginary;
    }

// Add complex Number
    public complex addCmplxNo(complex c) {
//create an object of complex
        complex temp = new complex();
        temp.setReal(this.getReal() + c.getReal());
        temp.setImgN(this.getImgN() + c.getImgN());
        return temp;
    }
}

Input (Driver File):


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package AddTwoComplexNumber;

public class ComplexTest{

    public static void main(String[] args) {

//create an object of complex
        complex a = new complex(8, 9);
        complex b = new complex(3, 4);
        complex c = a.addCmplxNo(b);

        System.out.println("(" + a.getReal() + "," + a.getImgN() + ")");
        System.out.println("(" + b.getReal() + "," + b.getImgN() + ")");


        System.out.print("The sum of  ");
        System.out.print("(" + a.getReal() + "," + a.getImgN() + ")");
        System.out.print("and (" + b.getReal() + "," + b.getImgN() + ")");

        System.out.println("is\n\t(" + c.getReal() + "," + c.getImgN() + ")");

    }
}

Output


(8,9)
(3,4)
The sum of  (8,9)and (3,4)is
 (11,13)

Basic Syntax of JAVA Language

Write a Program of GradeBook that include

1. Instance Variable 2. Constructor (with Parameter & without Parameter) 3 setter method 4. getter method 5. Display Massage method(with Parameter & without Parameter)

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package gradebooktest;
/*  Write a Program that include 
 * Instance Variable
 * constructor (with Parameter & without Parameter)
 * setter method
 * getter method
 * Display Massage method(with Parameter & without Parameter)
 */
public class GradeBook {
//Instance Variable
    private String CrsName;

    // Constructor without Parameter Initialize
    public GradeBook() {
        // initialize courseName
        CrsName = "NULL";
    } // end constructor setCrsName

    // Constructor without Parameter Initialize
    public GradeBook(String name) {
        // initialize courseName
        CrsName = name;
    } // end constructor setCrsName

    // method set the course name (Instanse variable)
    public void setCrsName(String name) {
        CrsName = name;
    } // end method setCrsName

    // method get the course name (Instanse variable)
    public String getCrsName() {
        return CrsName;
    } // end method setCrsName

// display a welcome message to the GradeBook user
    public void displayMessage() {
        System.out.println("Welcome 2 BookName: " + getCrsName());
    } // end method displayMessage

    public void displayMessage(String CrseName1) {
        System.out.println("Welcome to Display Message:\t" + this.getCrsName());
    }    // end method displayMessage
} // end class GradeBook
 

Write a Program of DriverFile or TestFile of GradeBook that include  
1. Create two object of GradeBook 
2.  Take Input from user and pass to GradeBook method setter
3. Display Books Name

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package gradebooktest;

import java.util.Scanner;

public class GradeBookTest {

    // main method begins program execution
    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);

        // create a GradeBook object and assign it to myGradeBook
        GradeBook myGradeBook1 = new GradeBook(" ");
        GradeBook myGradeBook2 = new GradeBook(" ");

        System.out.println("Enter BOOK Name: ");
        String Name1 = Input.nextLine();
        System.out.println();

        System.out.println("Enter BOOK Name: ");
        String Name2 = Input.nextLine();
        System.out.println();

        // call myGradeBook's displayMessage method
        myGradeBook1.setCrsName(Name1);
        myGradeBook1.displayMessage();

        // call myGradeBook's displayMessage method
        myGradeBook2.setCrsName(Name2);
        myGradeBook2.displayMessage();

    } // end main
} // end class GradeBookTest

Output:


Enter BOOK Name:
Islamic Studies

Enter BOOK Name:
Physiology

Welcome 2 BookName: Islamic Studies
Welcome 2 BookName: Physiology

Saturday, March 21, 2015

Write a Program to Generate Random Number within Specific Range

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Write a Program to Generate Random Number within Range
 */
package random12;

import java.util.Random;

public class Random12 {

    public static void main(String[] args) {
        Random rnd = new Random();

        System.out.println("Generate Number Range 0 to 29");
        for (int j = 0; j < 5; j++) {
            // Generate number 0 to 39
            System.out.printf("%2d \t", rnd.nextInt(30));
        }// end of for loop

        System.out.println("\nGenerate Number Range 1 to 40");
        for (int j = 0; j < 5; j++) {
            // Generate number 1 to 40
            int range = rnd.nextInt(40) + 1;
            System.out.printf("%2d \t", range);
        }// end of for loop
        
        /* Range start from other then zero number
         * for example,to pick a number from from 50 to 75 inclusively, 
         * the upper limit number will be 75-50+1 = 26 and 
         * 50 needs to be added to the result: 
        */
        System.out.println("\nGenerate Number Range 50 to 75");
        for (int j = 0; j < 5; j++) {
            int range = rnd.nextInt(26) + 50;
            System.out.printf("%2d \t", range);
        }// end of for loop
        System.out.println();
    }// end of main
}// end of class

Output:

Generate Number Range 0 to 29
17  19  11  26   2  
Generate Number Range 1 to 40
16  19  12  26  27  
Generate Number Range 50 to 75
53  68  50  50  57  

Write a Program to Generate Random Number

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/*
 * Write a Program to generate random number
 */
package random12;

import java.util.Random;
public class Random12 {

    public static void main(String[] args) {
        // TODO code application logic here
           Random rnd = new Random();
        for (int j = 0; j < 5; j++) {
            System.out.printf("%2d \t", rnd.nextInt() );
        }// end of for loop
    }// end of main
}// end of class

Output:


-1603606673  439348859  -1927187981  -245239965  -1529655924 

Random Variable