Sunday, August 30, 2015

hello every body, welcome to today's class.

we're going to discuss declaring a method wih a paramater in java programming language.

we're going to also use scanner class to get an input from user.

Small hints about the source code.

Line 12 creates a Scanner named input for reading the course name from the user. Line 15 creates the GradeBook object myGradeBook. Line 18 prompts the user to enter a course name. Line 19 reads the name from the user and assigns it to the nameOfCourse variable, using Scanner method nextLine to perform the input. The user types the course name and presses Enter to submit the course name to the program. Pressing Enter inserts a newline character at the end of the characters typed by the user. Method nextLine reads characters typed by the user until it encounters the newline character, then returns a String containing the characters up to, but not including, the newline. The newline character is discarded.

1 // GradeBook.java

2 // Class declaration with one method that has a parameter.

3

4 public class GradeBook

5 {

6 // display a welcome message to the GradeBook user

7 public void displayMessage(String coursename );

8 {

9 System.out.printf ("welcome to the grade book for %s", coursename);

10

11 } // end method displayMessage

12 } // end class GradeBook

Class declaration with one method that has a parameter.

1 // GradeBookTest.java

2 // Create GradeBook object and pass a String to

3 // its displayMessage method.

4 import java.util.Scanner; // program uses Scanner

5

6 public class GradeBookTest

7 {

8 // main method begins program execution

9 public static void main( String[] args )

10 {

11 // create Scanner to obtain input from command window

12 Scanner input = new Scanner( System.in );

13

14 // create a GradeBook object and assign it to myGradeBook

15 GradeBook myGradeBook = new GradeBook();

16

17 // prompt for and input course name

18 System.out.println( "Please enter the course name:" );

19 String nameOfCourse = input.nextLine(); // read a line of text

20 System.out.println(); // outputs a blank line

21

22 // call myGradeBook's displayMessage method

23 // and pass nameOfCourse as an argument

24 myGradeBook.displayMessage (nameOfCourse );

25 } // end main

26 } // end class GradeBookTest

that's all , thanks :D

































next we're gonna discuss set and get methods.

1 comment: