Pages

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