Core Java Examples
Core Java Examples
1) What is Java?
Simple
Object Oriented
Platform independent
Secured
Robust,
Architecture neutral
Portable
Interpreted
Multithreaded
Distributed
6) What is JVM?
7) What is JRE?
8) What is JDK?
i) Data Types
ii) Modifiers
iii) Variables
iv) Operators
v) Control Flow Statements
vi) Methods Etc...
i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation
Navigation
i) Documentation Section
ii) Package declaration statement
iii) Import Statement/s
iv) Class declaration
v) main Method (Java Program execution starts
from main method)
vi) Declarations (We declare Variables and
Constants.)
vii) General Statements (Ex: d = a + b + c;)
viii) Code blocks (Condition blocks, Loop Blocks
etc...)
Purpose of Comments
i) To make the code Readable
ii) To make the code disable from Execution
b) Use /*..........
------------
----------
----------
-------------*/ for multiple line comment
Or
In Eclipse IDE,
Select Statements/Steps
Source menu -> Add Block Comment
Uncomment
Select Comment block
Source menu -> Remove Block Comment
------------------------------------------------
Section III: Java Modifiers
i) Access Modifiers
ii) Non Access Modifiers
i) public
public access modifiers is accessible everywhere.
Ex:
public class Sample{
}
ii) private
The private access modifier is accessible only
within class.
Ex:
private int a=100;
iii) default
Ex:
class Sample {
}
iv) protected
The protected modifier is accessible within
package, outside of the package but through
Inheritance.
Ex:
protected class Sample {
}
i) static
static modifier is used to create classes, methods
and variables.
ii) final
final modifier for finalizing classes, methods and
variables.
iii) abstract
abstract modifier is used to create abstract classes
and abstract methods.
Etc...
------------------------------------------------
Section IV: Java Data Types
Examples in Java:
int a;
int b=100;
int c, d, e;
int f=10, g=20, h=30;
iii) Characters
g) Character, Ex: char z = "A";
iv) Conditional
Example:
1) What is Variable?
Syntax:
dataType variableName;
Example:
int a;
char x;
i) Initialization
int a =100;
int b;
b=a;//Reading
4) What are Variable Naming restrictions in Java?
i) Local Variables
Local variables are declared in methods or blocks.
i) Arithmetic Operators
ii) Relational Operators
iii) Assignment Operators
iv) Logical Operators
Etc...
i) ==
ii) !=
iii) >
iv) >=
v) <
vi) <=
Note: Relational Operators return Boolean/logical
result (true/false)
System.out.println(a>b);//false
System.out.println(a>=b);//false
System.out.println(a<b);//true
System.out.println(a<=b);//true
System.out.println(a==b);//false
System.out.println(a!=b);//true
}
}
i) Assignment =
ii) Add and Assign +=
iii) Subtract and Assign -=
iv) Multiply and Assign *=
int a = 10;
System.out.println(a);//10
a += 10;
System.out.println(a);//20
a -= 10;
System.out.println(a);//10
a *= 10;
System.out.println(a);//100
}
}
Java Modifiers
Modifiers are keywords that we add to those
definitions to change their meanings.
There are Two types of modifiers in Java.
a) Access Modifiers
b) Non-access Modifiers
---------------------
a) Access Modifiers
There are 4 types of modifiers in Java.
we use access modifiers to define access control
for classes, methods and variables etc...
1) private
2) default
3) protected
4) public
---------------------
1) private:
The private access modifier is accessible only
within class.
Ex:
class A {
Ex:
package abc;
class Sample {
int a;
.
.
---------------------
3) protected
The protected access modifier is accessible within
package, and out side of the package but through
Inheritance only.
Ex:
---------------------
4) public
public access modifier is accessible everywhere. It
has widest scope among all other modifiers.
Ex:
public Class A{
b) Non-access Modifiers
Java provides no of non access modifiers to
achieve many other functionality.
Ex:
class Abc {
etc...
Syntax:data_type variable_name;
int a = 100;
----------------------
Two types of Data types in java:
Ex:
byte a =100;
short a = 10000;
int a = 100000;
float a = 1.2;
Ex:
char a ='Z'
------------------------
Conditional8) boolean (undefined, depends on
JVM)
Ex:
boolean a = true
---------------------------------
b) Non-primitive / Reference data types
Ex:
Java Variables
1) What is Variable?
A named memory location to store or hold the
data.
Syntax:
dataType VariableName;
Ex:
int a;
---------------
dataType variable1, variable2, variable3;
Ex:
int a, b, c;
------------------------
dataType VariableName = value;
Ex:
int a =100;
--------------------------
3) Assigning values to variables
a) Initialization
int a;
a=10; // Initialization
--------------
int a=10; // Initialization
----------------
b) Reading
using Input devices
from files(text/excel)
from databases
from Application objects
-------------------------
4) Types of Variables
a) Instance Variable
A variable that is declared inside the class but
outside the method
b) Local variable
Ex:
myvar
MYVAR
$myvar
_myvar
myvar1
myvar_1
----------
1myvar - invalid
----------------------
> Variable names cannot be equal to java reserved
words
Ex:
int
for
import
package JavaExamples;
System.out.println (a);
System.out.println (b);
System.out.println (c);
System.out.println (d);
System.out.println (e);
System.out.println (f);
System.out.println (g);
System.out.println (h);
System.out.println (x);
System.out.println (y);
System.out.println (z);
System.out.println (j);
}
}
Operators in Java
a) Assignment Operators
b) Arithmetic operators
c) Relational Operators
d) Logical operators
etc...
-----------------------
a) Assignment Operators
1) Assignment operator =
a = 10;
2) Add and +=
a = 10;
a += 20;
a = 10;
a -= 5;
a = 10;
a *= 5;
------------------------
Example:
public static void main (String [] args){
int a = 10;
System.out.println (a); // 10
a += 10;
System.out.println (a); // 20
a -= 10;
System.out.println (a); // 10
a *= 5;
System.out.println (a); //50
}
--------------------------------------
b) Arithmetic operators
3) Multiplication *
4) Division /
5) Modules %
6) Increment ++
7) Decrement --
-----------------------------
Example:
public static void main (String [] args){
int a= 10, b = 5;
String c ="Selenium", d = "Testing";
}
--------------------------------------
c) Relational operators
Types of Result in Computer Programming
3+5=8
2 * 7 = 14
True or false
1) ==
2) !=
3) >
4) >=
5) <
6) <=
----------------------
Example:
3) Logical Or Operator ||
------------
Result Criteria
1) Not operator
----------------------------
Operand 1 Operand 2 Result
-------------------------------------
true true false
true false true
false true true
false false true
-----------------------------------
2) And operator
----------------------------
Operand 1 Operand 2 Result
-------------------------------------
true true true
true false false
false true false
false false false
---------------------------------------
3) Or operator
----------------------------
Operand 1 Operand 2 Result
-------------------------------------
true true true
true false true
false true true
false false false
------------------------------------
Example:
2) switch statement
------------------------------
Types of Conditions
1) Single condition
Ex:
if (a > b) {
----
----
2) Compound condition
Ex:
3) Nested Condition
if (a > b){}
if (a < c) {}
if (a < d) {
----
----
}
------------------------------
Usage of Conditional Statements
1) Executing a block of statements when condition
is true.
Syntax:
if (Condition) {
Statements
---------
---------
}
if (a > b) {
System.out.println("A is a Big Nuber");
}
------------------------------------
2) Executing a block of statements when
compound condition is true
Syntax:
if ((condition) && (condition2)) {
Statements
----------
-----------
}
Example:int a, b, c = 2;
a =10;
b =5;
Example:
public static void main (String [] args){
int a, b;
a =10;
b =50;
if (a > b) {
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}
-----------------------------------------
4) Decide among several Alternates (else if
structure)
Syntax:
if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else
{
statements
----------
--------
}
Example:
public static void main (String [] args){
int a = -100;
Example:
public static void main (String [] args){
int a =10, b =7, c = 5, d = 13;
if (a > b){}
if (a > c) {}
if (a > d){
System.out.println("A is a Big Number");
}
else {
System.out.println("A is Not a Big
Number");
}
---------------------------------------
6) Decide among several alternates (using Switch
case structure).
Syntax:
switch (expression) {
case value:
Statements
-------
break;
case value:
Statements
-------
break;
case value:
Statements
-------
break;
default
Statements
-------
------
}
switch (grade){
case 'A':
System.out.println ("Excellent");
break;
case 'B':
System.out.println ("Well Done");
break;
case 'C':
System.out.println ("Better");
break;
default:
System.out.println("Invalid Grade");
}
---------------------------------------------------------
Java Loop statements
Whenever we want to execute a block of
statements several times then we use Loop
structures.
2) while Loop
3) do...while Loop
Syntax:
for(stratvalue; endValue; increment/decrement) {
Statements
---------
--------
}
Example:
for (int i=1; i<=10; i++)
{
System.out.println(i);
}
Example 2:
// print 10 to 1 Numbers using For Loop.
Syntax:
Initialization
while (condition) {
statements
------
-------
increment/decrement
}
int i = 1;
while (i <=10){
System.out.println(i);
i++;
}
Example 2:// print 10 to 1 Numbers using While
Loop.
int i = 10;
while (i >= 1){
System.out.println(i);
i--;
}
Example 3:// print every 10th number up to 100
using While Loop.
int i = 10;
while (i <= 100){
System.out.println(i);
i= i+10;
}
--------------------------------------
3) do ...while Loop----------------------------------
-----
it repeats a block of statements while condition is
true and Irrespective of the condition, it executes a
block of statements at least once.
Syntax:
do
{
Statements
----------
----------
Increment/Decrement
} while(Condition);
------------
Example:int i = 20;
do {
System.out.println("i value is : " + i);
i++;
} while(i <= 10);
---------------------------------------
4) Enhanced for Loop------------------------------
---------
It executes all elements in an Array
Syntax:
for (declaration: expression) {
statements
----------
-----------
}
class StringExample {
public ststic void main (String [] args) {
System.out.println(myTool);// Selenium
Example:
package javaExamples;
System.out.println(str1.equals(str2));// false
System.out.println(str1.equals(str3));// true
package javaExamples;
System.out.println(str1.contains("len"));//
true (finding sub string)
System.out.println(str1.contains("lem"));//
false (finding sub string)
Arrays in Java
1) Introduction:
> Java Array is an object that holds a fixed
number of values of a single data type.
abc[0] =10;
abc[1] = 20;
.
.
--------------
int [] abc = new int[5];
abc[0] =2;
abc[1] =3;
System.out.println(abc[0]+abc[1]);
------------------------------
int [] abc = {10, 20, 30, 40, 50} //Creating Array
and Initializing
System.out.println(abc.length);
System.out.println(xyz[1]+xyz[2]);
//Priting Array
for (int i=0; i < xyz.length; i++) {
System.out.println(xyz[i]);
}
-------------------
Creating Arrays (Different data types)
int [] array1 ={1, 2, 3, 4, 5}; // Array of Integers
char [] array2 ={'A', 'B', 'C'}; // Array of
Characters
boolean [] array3 = {true, false, false, true,false};
// Array of Boolean values
String [] array4 = {"Selenium", "UFT", "Java",
"LoadRunner"}; // Array of Strings
System.out.println(array1[1]); // 2
System.out.println(array2[1]); // B
System.out.println(array3[1]); // false
System.out.println(array4[0]); // Selenium
--------------------------------
3) Copy of values an Array into another Array
System.out.println(array2[2]);
Example 2:
int [] array1 ={1, 2, 3, 4, 5};
int [] array2 = new int [array1.length];
4) Advantages of Arrays:
5) Disadvantages of Arrays
6) Types of Arrays
b) Multidimensional Array
1) compareTo() Method
Example:
Integer a = 5;
System.out.println(a.compareTo(8)); //-1
System.out.println(a.compareTo(5));//0
System.out.println(a.compareTo(2));//1
Result Criteria:
if the integer is equal to the argument then 0
if the integer is less than the argument then -1
if the integer is greater than the argument then 1
-----------------------------------
2) equals() Method
Integer a = 5;
Integer b = 10;
Integer c = 5;
Short d = 5;
System.out.println(a.equals(b));//false
System.out.println(a.equals(c)); //true
System.out.println(a.equals(d)); //false
--------------------------------------
3) abs Method (Returns Absolute value)
Example:
Integer a = -5;
double b = -10.234;
System.out.println(Math.abs(a));// 5
System.out.println(Math.abs(b));// 10.234
----------------------------------------------
4) round Method (Rounds the value nearest
Integer)
Example:
double a = 10.575;
double b = 10.498;
System.out.println(Math.round(a));// 11
System.out.println(Math.round(b));// 10
-----------------------------------------
5) min Method (Returns minimum value between
two numbers)
Example:
Example:
int a =10, b =20;
double c = 1.234, d = 3.567;
System.out.println(Math.max(a, b)); // 20
System.out.println(Math.max(c, d)); // 3.567
System.out.println(Math.max(123, 124)); // 124
System.out.println(Math.max(10.345, 10.3451));
// 10.3451
System.out.println(Math.max(1, 1)); // 1
-------------------------------
7) random Method (Generates Random Number)
Example:
System.out.println(Math.random()); //
System.out.println(Math.random()); //
---------------------------------------------
II) Character Methods
Example:
char a = '1';
System.out.println(Character.isLetter(a)); //false
System.out.println(Character.isLetter('A'));//true
System.out.println(Character.isLetter('a'));//true
System.out.println(Character.isLetter('*'));//false
--------------------------------
2) isDigit Method (It returns weather the value is
Number or not?)
char a = '1';
System.out.println(Character.isDigit(a)); //true
System.out.println(Character.isDigit('A'));//false
System.out.println(Character.isDigit('a'));//false
System.out.println(Character.isDigit('*'));//false
System.out.println(Character.isDigit('7')); //true
-----------------------------------------------
3) isUppercase Method (Checks weather the
value is Upper case or not?)
Example:
System.out.println(Character.isUpperCase('C'));//t
rue
System.out.println(Character.isDigit('z')); //false
-------------------------------------
4) isLowercase Method (Checks weather the
value is Lower case or not?)
Example:
System.out.println(Character.isLowerCase('C'));//f
alse
System.out.println(Character.isLowerCase('z'));
//true
------------------------------
5) toUppercase Method (Converts the value to
Upper case)
Example:
System.out.println(Character.toUpperCase('a'));//
A
System.out.println(Character.toUpperCase('A'));
//A
-----------------------------------
6) toLowercase Method (Converts the value to
Lower case)
Example:
System.out.println(Character.toLowerCase('a'));//a
System.out.println(Character.toLowerCase('A'));
//a
-------------------------------------------------------
III) String Methods
Example:
String str1 ="SELENIUM";
String str2 ="selenium";
String str3 ="seleniuma";
String str4 ="selenium";
int result;
result = str1.compareTo(str2);
System.out.println(result); //
result = str3.compareTo(str2);
System.out.println(result); //
result = str2.compareTo(str4);
System.out.println(result); //
-------------------------------------
2) charAt Method (character by position)
Example:
String str1 ="Selenium";
String str2 = "UFT";
String str3 ="Selenium";
System.out.println(str1.equals(str2)); //false
System.out.println(str1.equals(str3)); //true
-----------------------------
5) equalsIgnorecase Method
Examples:
String str1 ="selenium";
String str2 = "UFT";
String str3 ="SELENIUM";
String str4 ="SELENIUM";
System.out.println(str3.equalsIgnoreCase(str4));
//true
System.out.println(str1.equalsIgnoreCase(str3));
//true
System.out.println(str1.equalsIgnoreCase(str2));
//false
-----------------------------------------------
6) toUppercase Method (Converts values To
Upper case)
Example:
String str1 ="selenium";
String str2 ="SELEnium";
String str3 ="SELENIUM";
System.out.println(str1.toUpperCase());
System.out.println(str2.toUpperCase());
System.out.println(str3.toUpperCase());
-------------------------------------
7) toLowercase Method (Converts values To
Lower case)
System.out.println(str1.toLowerCase());
//selenium
System.out.println(str2.toLowerCase());
//selenium
System.out.println(str3.toLowerCase());
//selenium
---------------------------------------------
8) trim Method (removes spaces from both sides
of a String)
Example:
System.out.println(str1);
System.out.println(str1.trim());
System.out.println(str2.trim());
System.out.println(str3.trim());
---------------------------------------------
9) substring Method (Returns sub string)
Example:
String str1 ="Welcome to Selenium Testing";
System.out.println(str1.substring(10)); //
Selenium Testing
System.out.println(str1.substring(19)); //Testing
System.out.println(str1.substring(10, 18));
//Selenium
------------------------------------------------
10) endsWith Method (ends with specified
suffix)
Example:
-----------
String str1 = "Selenium Testing";
System.out.println(str1.endsWith("Testing"));//tru
e
System.out.println(str1.endsWith("ing"));//true
System.out.println(str1.endsWith("Selenium"));//f
alse
--------------------------------------------------
11) length() Method (Returns length of a String)
String s = "Selenium";
System.out.println(s.length()); //8
--------------------------------------------------
IV) Array Methods
1) length() Method
int [] array1 = {10, 20, 30, 40, 50};
System.out.println(array1.length);//5
-------------------------------------
2) toString() Method (print Array)
System.out.println(a);// true
System.out.println(b);// false
Java Methods
I) What is Method?
Method is a set of statements to perform an
operation, methods are also known as procedures
or functions.
i) Built in methods
a) String methods
b) Array methods
c) Character methods
d) Number methods
etc...
Writing method:
Syntax:
modifier returnType methodName(Parameters) {
// Method body
}
Method calling:
modifier methodName(Parameters) {
//Method body
}
Examples:
Internal method:
----------------
public static void main(String [] args){
studentRank(499);
}
public static void studentRank(int marks) {
if (marks >= 600) {
System.out.println("Rank:A1");
}
else if (marks >= 500) {
System.out.println("Rank:A2");
}
else {
System.out.println("Rank:A3");
}
}
------------------------------------
Example 3: External Method (Calling from
external or another class).
import xyza.CopyArray;
public class Apple extends CopyArray {
public static void main (String [] args){
studentRank(678);
}
}
------------------------------------------
VI) Method Overloading in Java
If a class have multiple methods with same name,
but different parameters, It is known as Method
overloading.
Ex:
int a = 10/0;
----------------------------------------------
2) Scenario where NullPointerException
occurs
if we have null value in any variable, performing
any operation by the variable,
ex:
String s = null;
System.out.println(s.length());//NullPointerExcepti
on
---------------------------------------------------
3) Scenario where NumberFormatException
occurs
The wrong formatting of any value, may
occur NumberFormatException
Ex:
String s = "abc";
int y = Integer.parseInt(s);
System.out.println(y);//NumberFormatExcept
ion
--------------------------------------
4) Scenario where ArrayIndexOutOfBounds
exception occurs
If we are inserting any value in the wrong index.
Ex:
int [] a = new int [5];
a[10] = 100;
System.out.println(a[10]);//ArrayIndexOutOf
Bounds
---------------------------------------------------------
-----
Java Program Example:------------
int a =10;
int b = 0;
int result;
result = a/b;
System.out.println(result);
System.out.println("Hello Java");
System.out.println("Hello Selenium");
----------------------------------
Use try block for Exception Handling
Syntax:
--------
try {
Statements
------
-------
------
}
catch (exception e) {
Exception handling code
}
-----------------------------------
Java Program With Exception handling
int a =10;
int b = 0;
int result;
try{
result = a/b;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Divided by Zero Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
--------------------------------------
Multiple try blocks for handling multiple exceptions
Example:
int a =10;
int b = 0;
int result;
int [] array1 = new int [4];
try{
result = a/b;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Divided by Zero Error");
}
try{
array1[10]= 100;
System.out.println(array1[10]);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Array Out of Bound Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
ii) DataInputStream
iii) BufferedReader
---------------
Using java.util.Scanner is the easier and includes
many methods to check input data is valid to read.
Example:---------------
package javaiooperations;
import java.util.Scanner;
public class ReadInput {
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
Create a folder
delete a folder
Read data
import java.io.File;
ii) Polymorphism
iii) Abstraction
iv) Encapsulation
------------------------
i) Inheritance:
> It is a process of inheriting (reusing) the class
members 9variables and methods) from one class
to another class is called Inheritance.
> Non static (object level) class members only can
be inherited.
Syntax:
i) Single Inheritance
Ex:
Ex:
In Class C
add method
-----------------------------
Class C extends Class D
-------------------------------
Inheritance example:
----------------------
public class B {
int a = 10;
int b = 20;
public void addition(){
System.out.println("Addition of a, b is: " +
(a+b));
}
public static void main (String [] args){
B myObject = new B();
myObject.addition();
}
}
---------------------------------
package javaiooperations;
i) No of Arguments
Ex:
add (int a, int b){
}
Ex:
Ex:
public class Y {
int a = 10, b=20;
public void add () {
System.out.println(a+b);
}
}
-----------------
public class Z extends Y{
int a = 1, b=2;
public void add () {
System.out.println(a+b);
}
public static void main (String [] args){
Z obj = new Z();
obj.add(); // 3
Ex:
Ex:
Inheritance:
-------------
A sub class extends Super class is known as
Inheritance.
System.out.println("");
// Access Non static class members using
Object /Instance
AbstractionExample obj = new
AbstractionExample();
System.out.println(obj.c); // 30
System.out.println(obj.d); // 40
obj.add2(); //70
}
}
---------------------------------------
Abstract Class
> java Class contains 100% concrete methods
Ex:
5 methods concrete
5 methods Abstract
* Abstract class
----------------------
Class3 (having 10 methods)
10 methods Abstract
* Abstract class
------------------------------
Example for Abstract Class:
--------------------------
Syntax:
}
-----------------------------
Super Class
package JavaOOPS;
@Override
public void handle() {
System.out.println("Bikes have Handle");
}
@Override
public void seat() {
System.out.println("Bikes have Seats");
}
public static void main (String [] args){
HeroHonda obj = new HeroHonda();
obj.engine();
obj.handle();
obj.seat();
}
}
------------------------------------------------
iv) Encapsulation:
It is a process of wrapping code and data together
into a single unit.
Advantages:
ex:
package JavaOOPS;
@Override
public void add() {
System.out.println("Addition");
}
@Override
public void sub() {
System.out.println("Subtraction");
}
public static void main (String []args){
Exa4 obj= new Exa4();
obj.add();
obj.sub();
}
oops:
Ex:C++,JAVA,.NET ETC...
1.Encapsulations
2.Abstraction
3.Inheritance
4.polymorphism
1.Encapsulations:
System.out.print("Name : " +
encap.getName()+
" Age : "+ encap.getAge());
}
}
This would produce the following result:
Benefits of Encapsulation:
2.Abstraction
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary
3600.0
Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.
Abstract Methods:
3.Inheritance:
true
true
true
true
true
true
HAS-A relationship:
4.Polymorphism
Any Java object that can pass more than one IS-A
test is considered to be polymorphic. In Java, all
Java objects are polymorphic since any object will
pass the IS-A test for their own type and for the
class Object.
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary
3600.0
Methods in Java
Method:
2. Method definition/Body/Implementation:
Types of Methods:
1.Method
without returntype and without parameters:
Eg: class Addition {
Void add(){
int a=11;
int b=22;
int c=a+b;
System.out.println(sum of
+a+and+a+is+c);
}
Public static void main(String[]ar){
Addition abc=new Addition();
Abc.add;
}
}
return result;
}
}
This would produce the following result:
The maximum between 5 and 2 is 5
This program contains the main method and the
max method. The main method is just like any
other method except that it is invoked by the JVM.
The main method's header is always the same, like
the one in this example, with the modifiers public
and static, return value type void, method name
main, and a parameter of the String[] type.
String[] indicates that the parameter is an array of
String.
The void Keyword:
This section shows how to declare and invoke a
void method. Following example gives a program
that declares a method named printGrade and
invokes it to print the grade for a given score.
Example:
public class TestVoidMethod {