Pages

Sunday, March 29, 2015

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

No comments:

Post a Comment