Java Lecture Note COM 124
Java Lecture Note COM 124
COMPILED BY DR ARAOYE O. I.
1
1.
Programming Techniques
Programming is meant to solve real problems. Approach to solve real world problems is
referred here as programming techniques
1.6.1 Procedure Oriented Programming (POP) Technique
In the procedure oriented technique exhibits a top-down approach in solving problem in which the
problem is viewed as the sequence of things to be done such as input data, process data and output
result in this order as seen in programming language such as BASIC, pascal and c.
Example in BASIC is shown below
INPUT A, B, C
LET A = B+ C
PRINT A
This program is organized in sequential order and follow a particular logic
The primary focus is on functions. Program are broken down into subprograms known as
functions or procedures
In a multi-function program, many important data items are placed as global so that they may be
accessed by all the functions. Each function may have its own local data. Global data are more
vulnerable to an inadvertent change by a function. In a large program it is very difficult to identify
what data is used by which function. In case we need to revise an external data structure, we also
need to revise all functions that access the data. This provides an opportunity for bugs to creep in.
Another serious drawback with the procedural approach is that we do not model real world
problems very well. This is because functions are action-oriented and do not really corresponding
to the element of the problem.
Some Characteristics exhibited by POP are:
• Emphasis is on doing things (algorithms).
2
1.
Let the object name be Obj1. Obj1 has the following properties: name, password,
Account number and balance. Obj1 methods are: displayBalance(), displayName(),
displayAccount()
3
1.
In OOP, data of an object is private to the object and cannot be accessed by another
object.
Objects communicate in OOP by sending message to each other through method.
Some of the features of OOP are:
• Emphasis is on data rather than procedure.
• Programs are divided into what are known as objects.
• Data structures are designed such that they characterize the objects.
• Functions that operate on the data of an object are ties together in the data structure.
• Data is hidden and cannot be accessed by external function.
• Objects may communicate with each other through function.
• New data and functions can be easily added whenever necessary.
• Follows bottom up approach in program design.
Exercise 1.1
1. What is programming?
2. Discuss types of Programming Language
3. State the advantages of high-level programming languages over the other
4. Compare the speed of the three major Computer Programming Language
5. Discuss Translator
6. Compare Compiler and Interpreter in term of speed, interpretation method and
execution method.
7. Discuss OOP and POP
8. Describe an object
9. State the advantages of OOP over POP
4
1.
A class gives birth to as many objects as desired by assigning new values and using the
same method as shown in Figure 2.1. A class is like a Mother giving birth to many children
of the same resemblance. By the time you assign new values to the properties of a Class it
becomes an object.
The wrapping up of data and function into a single unit (called class) is known as
encapsulation. Data and encapsulation is the most striking feature of a class. The data is
not accessible to the outside world, and only those functions which are wrapped in the
class can access it. These functions provide the interface between the object’s data and the
program. This insulation of the data from direct access by the program is called data hiding
or information hiding.
5
1.
Abstraction refers to the act of representing essential features without including the
background details or explanation. Classes use the concept of abstraction and are defined
as a list of abstract attributes such as height, name, and weight, and function operate on
these attributes. They encapsulate all the essential properties of the object that are to be
created.
2.3 Inheritance
Inheritance is a key concept in OOP where a class can inherit both properties and method
of another class usually such a class is referred to as a sub class to the original class or
mother class. For examples an account class can be sub divided into current account class
and saving account class Student class can be sub divided into undergraduate student class
and postgraduate student class. When a class is derived from another class, the subclass
has access to both the method and properties of the root class which is a concept of
inheritance. Figure 2.2 shows the diagram for inheritance
In OOP, the concept of inheritance provides the idea of reusability. This means that we
can add additional features to an existing class without modifying it. This is possible by
deriving a new class from the existing one. The new class will have the combined feature
of both the classes
The real appeal and power of the inheritance mechanism is that it allows the programmer
to reuse a class i.e almost, but not exactly, what he wants, and to tailor the class in such a
way that it does not introduced any undesirable side-effects into the rest of classes.
6
1.
7
1.
2.4 Polymorphism
When a particular operation is being shared by many objects of different classes such
operation exhibits a polymorphism. This is possible in inheritance where all subclasses
shared similar operation. For example a draw operation can be applied on different shape
like triangle, circle, rectangle and square. Each of this shape shared the same interface
irrespective of their instances
1. Through inheritance, we can eliminate redundant code extend the use of existing
• Classes.
8
1.
2. We can build programs from the standard working modules that communicate
with one another, rather than having to start writing the code from scratch. This
leads to saving of development time and higher productivity.
3. The principle of data hiding helps the programmer to build secure program that
can not be invaded by code in other parts of a programs.
Exercise 2.1
Features of Java
Unlike many other programming languages including C and C++, when Java is compiled,
it is not compiled into platform specific machine, rather into platform-independent byte
code. This byte code is distributed over the web and interpreted by the Virtual Machine
(JVM) on whichever platform it is being run on.
3. Simple
Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it
would be easy to master.
4. Secure
With Java's secure feature it enables to develop virus-free, tamper-free systems.
Authentication techniques are based on public-key encryption.
5. Architecture-neutral
Java compiler generates an architecture-neutral object file format, which makes the
compiled code executable on many processors, with the presence of Java runtime
system.
6. Portable
Being architecture-neutral and having no implementation dependent aspects of the
specification makes Java portable. The compiler in Java is written in ANSI C with a clean
portability boundary, which is a POSIX subset.
7. Robust
Java makes an effort to eliminate error-prone situations by emphasizing mainly on
compile time error checking and runtime checking.
8. Multithreaded
With Java's multithreaded feature it is possible to write programs that can perform many
tasks simultaneously. This design feature allows the developers to construct interactive
applications that can run smoothly.
9. Interpreted
Java byte code is translated on the fly to native machine instructions and is not stored
anywhere. The development process is more rapid and analytical since the linking is an
incremental and light-weight process.
11. High Performance
With the use of Just-In-Time compilers, Java enables high performance.
10
1.
12. Distributed
Java is designed for the distributed environment of the internet.
13. Dynamic
Java is considered to be more dynamic than C or C++ since it is designed to adapt to an
evolving environment. Java programs can carry an extensive amount of run-time
information that can be used to verify and resolve accesses to objects at run-time.
Compiling and running a java program is very easy after JDK installation. Following are
the steps −
1. Open a command prompt window and go to the directory where you saved the java
program say your java filename is MyjavaProgram.java. Note your filename must
be the same as your main class name. We shall learnt about this in the later chapter
2. Assume it's C:\. Type 'javac MyJavaProgram.java' and press enter to compile your
code. If there are no errors in your code, the command prompt will take you to the
next line (Assumption: The path variable is set).
3. Now, type ' java MyJavaProgram ' to run your program. You will be able to see
the result printed on the window.
Note the program in this book is compiled using NetBeans IDE 8.0.2. We shall learnt about
NetBeansIDE later in the book
3.5 Java Program Structure
Java generally has the program structure as shown below
Import statements;
Classes declaration;
11
1.
package draraoyebook;
/**
@author Dr Araoye
*/
public class DRARAOYEBOOK {
Public class <classname> where class name is user defined name. The class name is
followed by an open curly brace {. After the brace bracket then the main method name
is declared which is compulsory for every java program The main method is written
thus public static void main(String[] args). This is equally followed by an open brace
bracket as seen in Figure 3.7. Then the body of instructions are written in the main
method and then closed by curly brace brackets which is equivalent to number of open
brace bracket as seen in Figure 3.7. The number of open curly brace is two and the
number of close curly brace is two. Writing of the body of the method is a major we
shall discover in later chapters. Note: The curly braces {} marks the beginning and the
end of a block of code.
12
1.
All Java components require names. Names used for classes, objects, variables, and
methods are called identifiers. In Java, there are several points to remember about
identifiers. They are as follows -
1. All identifiers should begin with a letter (A to Z or a to z), currency character ($)
or an underscore (_).
2. After the first character, identifiers can have any combination of characters.
3. A keyword cannot be used as an identifier.
4. Most importantly, identifiers are case sensitive. Examples of legal identifiers: age,
$salary, _value, __1_value. Examples of illegal identifiers: 123abc, -salary.
5. Class: Names should be in CamelCase. Use nouns because a class is normally
representing something in the real world. CamelCase (also known as Upper
13
1.
CamelCase) is where each new word begins with a capital letter e.g.,
AccountNumber, StudentName, StudentAge
6. Packages: Names should be in lowercase e.g draraoye book as seen in figure 3.7
7. Method : Name is written in mixed case. Mixed case (also known as Lower
CamelCase) is the same as CamelCase except the first letter of the name is in
lowercase (e.g. displayArea(), calculateArea(). showName e.tc
8. Variables: Names should be mixed case
9. Objects: Names should be in mixed case
10. Interface: names should be upper Camelcase
11. Constant: names should be in upper case
Exercise 3. 1
14
1.
4.1 Introduction
Variables are memory location where data are stored. Every data has a unique memory
address which is normally referred to as a variable. A simple illustration of variable and
data relationship is shown in Figure 4.1
The relationship between variable and data is that when you get variable of a data, you can
access the data. When you get A then you can access 5 from Figure 4.1. You need to know
the right variable for your data for easy access.
A variable is an identifier as discussed in Section 3.6 and it must follow the naming
rules. Your variable’s name must be meaningful to avoid confusion.
Variable stores data of different types and are of different sizes.
A data can be numeric or non-numeric. Examples of data that are numeric are 123, 3.142,
3x108 e.t.c. A non- numeric data type consists of characters or combination of characters
e.g “A”, “Total” true, e,tc. You need to know the various data types in java so as to
associate correct data type to a variable.
In Java a variable must specify the type of data it will stores. Variables in java are written
in mixed case as discussed in Section 3.6
Data types are divided into two groups: Primitive data types and Primitive data types
Data types are divided into two groups:
15
1.
String
Arrays
Classes
Exercise 4.1
Identify the correct data type for the following data items
1. Serial-number
2. Average score
3. Age
4. Are you a student? (Y/N)
5. Number of items sold in a Super-Market
16
1.
In your java program, you must specify that your variable is of data type before you are
allowed to store data into it.
The rule for specifying a variable as a data type:
<data-type><space> <variable-name>;
From class Exercise 4.1, we can specify data type for the data items as
follows: int seriaNumber; double averageScore ; byte age; char
response ; short noItemSold ; boolean reply ;
Note the semi-colon at the end of each line.
Exercise 4.2
Give variable’s names for the following values and specify the most appropriate data
type
1. 3.142
2. 8X1015
3. 45678999
4. B
5. True
6. 20
7. 2x10-6
8. 1234456789456777
9. false
10. 123456
4.4 Writing of Comments in Java
Comments are used to document and describe what a particular part of program is doing
without following the rules of the program. Program compiler ignores comment
statements. A comment s can be single line or multiple lines.
17
1.
For a single line comment java uses double forward slashes (\\) to write the comment.
Example, refer to Figure 3.5 The rule is
\\<Comment>
\\ This is a single line comment
\\ This is a demonstration of my program
Consider the program in Section 3.5 and these statements in the program
System.out.println("You are welcome to Dr Araoye Class");
System.out.println("This is a demonstration of simple Java Program");
These statements print the statements in double quote to the computer monitor. This is an
example of output statement.
18
1.
System is a built-in Java class that contains method, such as out , which is short for
"output". The println() method, short for "print line", is used to print a value to the screen
or a file.
print() and println() method do the same function but the difference is println() method
directs the output to one line per instruction while print() does not insert a new line after a
previous output
Example
int a =5;
System.out.println(a);
Here a is an integer variable and not a string and its value is 5.
The rule is
System.out.println(variable’s name);
19
1.
System.out.println(“The value of a
is”+a); Here string and variable are
joined together.
The output will be
The value of a is 5
Consider Program example 5.1 below and the output
Exercise 5.1
20
1.
The difference between a variable and a constant is that variable’s value can be changed
during program execution while constant’s value cannot be changed during program
execution.
21
1.
The keyword final is used in conjunction with the variable declaration in order to make
the variable a constant. According to java’s rule, a constant must be written in capital
letter. To convert the variables in Section 5.3.1 to constant the keyword final is added to
them thus:
final int seriaNumber =1;
noItemSold = 50 ;
public class
ProgramExample5_2{
22
1.
These are the operators that are used to evaluate arithmetic expression. The operator, its
meaning and example are described in Table 5. 1
Table 5.1 List of Arithmetic Operators
Operator Meaning Example
+ Addition c = a+b;
- Subtraction c = a-b;
/ Division c = a/b
* Multiplication c = a*b;
public class
ProgramExample5_3 { public
static void main(String[] args)
23
1.
{ // Written by Dr Araoye O.
I.
Increment operator is used to increase the value of an object by 1 and it is denoted as ++.
Increment operator can be prefix or postfix on an object. Example of prefix is ++a while
24
1.
example of postfix is a++. The difference in prefix and postfix is that prefix increase the
value of object by 1 before execution while postfix increase the value of an object by 1
after execution. The decrement operator decreases the value of an object by 1 and it is
denoted as --. It can be used as prefix and postfix in the same way as increment operator.
Study Program example 5.4 and
Program Example 5.5 to see the effect of prefix and postfix increment and decrement
operators
25
1.
public class
ProgramExample5_5 { public
static void main(String[] args) {
// Written by Dr Araoye O. I.
Scanner class is the class that has a method called System.in which can be used to get
input from the computer keyboard. Unlike System.out , you have to create object of the
class before you can use the methods of the class. Scanner class is under a package called
util. A package consists of related classes. Under a package, you can import a single class
or all the classes at a time. Java has many in-built packages.
26
1.
Assignment 5.1
Before you can use an in-built class you have to import it. Import statement is used to
import a class or set of classes. The rules is
Rule 1: To import a class: import java.<package_name.class_name>;
Example: import java.util.Scanner;
Rule 2 : To import all classes in a package: import java.<package_name.*>;
Example import java.util.*;
Remember that package’s name is written in small letters, class’s name begins with
capital letter.
27
1.
Once an object of a scanner is created, the methods of the class be accessed by the
objects as discussed in Section 2.1
There are different Scanner methods for obtaining data from keyboard. The method to be
used depends on the nature of the data. The table below shows data types and methods to
be used for each data type.
Data Type Method Example
Double nextDouble( ) double a = myInput.nextDouble( );
Integer nextInt( ) int b = myInput.nextInt( );
Float nextFloat( ) float c = myInput.nextFloat( );
Byte nextByte( ) byte d = myInput.nextByte( );
Short nextShort( ) short s = myInput.nextShort( );
Long nextLong ( ) long l = myInput.nextLong( );
String nextLine( ) String s =myInput.nextLine( );
public class
ProgramExample5_6 { public
static void main(String[] args) {
// Written by Dr Araoye O. I.
double p,r, t, i;
Scanner myInput = new Scanner
(System.in); System.out.println("Enter
Value for Principal"); p=
myInput.nextDouble();
System.out.println("Enter Value for
Rate"); r= myInput.nextDouble();
28
1.
29
1.
}
}
30
1.
The Java Math class has many methods for performing basic numerical operation. Table
5.1 contains various Java Math methods, descriptions and Examples. Math class is in
lang package.
x and y
Math.min(x,y) It returns the minimum value Math.min(10,20)
of x andy
31
1.
}
}
32
1.
Exercise 5.2
1. State the difference between a variable and a constant
2. Write java statement to declare a constant
3. What is assignment statement? Give examples of assignment statements
4. Discuss increment and decrement operators
5. Write the output of the program below public static void main(String[] args) {
// Written by Dr Araoye O. I.
int a=44; int b = 45 ; int c =
++a; // prefix increment operator
int e= --b; // prefix decrement
operator
System.out.println("The value of a for prefix increment operator is "+c);
System.out.println("The value of a after prefix increment operator is
"+a);
System.out.println("The value of b for prefix decrement operator is
"+e); System.out.println("The value of b after prefix decrement operator
is "+b);
}
}
6. Write java rules to the following
i. Declaring object of
a class ii. Creating
object of a class
7. In a tabular form, write various methods of scanner object to capture various data
types
8. Write a program to capture your biodata and display them on the computer
monitor
i. y = a2+2ab+b2
33
1.
iii.
v. v = 𝜋𝑟2ℎ
vi.
vii.
SELECTION STATEMENTS
6.1 Introduction
Selection statements are also known as IF statements. They are used to select a statement
out of parallel alternative statements based on its truth condition. It is like an objective
question where only one answer is correct. An application of IF stamen can be found where
a grade is assigned to a score based on the range of a score. Below is an example of range
of scores and grade
0-39 F
40-44 E
45-49 D
50-59 C
60-69 B
70-100 A
34
1.
Conditional expression uses relational operators and logical operators to evaluate its
condition.
Table 6.1 shows relational operators, their meanings and examples
35
1.
This is a statement with only one path. It evaluates its expression only if the condition is
true otherwise ignores it. The rule is if (conditional_ Statement) {
36
1.
37
1.
Block of Statements 1
} else if
(conditional_Statement
2)
{
Block of Statements 2
}
else if (conditional_Statement 3)
{
Block of Statements 3
} else if
(conditional_Statement
4)
{
Block of Statements 4
} else if
(conditional_Statement n-
1)
{
Block of Statements n-1
}
else
{
Block of Statements n
}
Consider Figure 6.1, The java statements to print grade is given in program example 6.3
ProgramExample6_3 { public
static void main(String[] args)
{ // Written by Dr Araoye O.
I.
Scanner myScore = new Scanner (System.in);
System.out.println("Enter your Score"); double score =
myScore.nextDouble(); if ((score>=70)&&(score<100))
System.out.println(" GRADE A"); else if
((score>=60)&&(score<70)) System.out.println(" GRADE
B"); else if ((score>=50)&&(score<60)) System.out.println("
GRADE C"); else if ((score>=45)&&(score<50))
System.out.println(" GRADE D"); else if
((score>=40)&&(score<45)) System.out.println(" GRADE E");
else
System.out.println(" GRADE F");
}
}
39
1.
i.
import java.util.Scanner;
public class
40
1.
ProgramExample6_4 { public
static void main(String[] args)
{ //Written by Dr Araoye O.
I.
double a,b,c,r1,r2,d;
Scanner input = new Scanner
(System.in);
System.out.println("Enter value for
a"); a= input.nextDouble();
System.out.println("Enter value
for b"); b= input.nextDouble();
System.out.println("Enter
value for c"); c=
input.nextDouble(); d=b*b-
4.0*a*c; if (d==0) {
r1=-
b/(2*a);
r2=r1;
System.out.println("X1="+r1);
System.out.println("X2="+r2);
}
else if (d>0.0){
r1 =(-b+Math.sqrt(d))/(2.0*a);
r2=(-b-Math.sqrt(d))/(2.0*a);
System.out.println("X1=
"+r1);
System.out.println("X2= "+r2);
} else
System.out.println("COMPLEX ROOT");
}
41
1.
public class
ProgramExample6_5 { public
static void main(String[] args) {
// Written by Dr Araoye O. I.
int month =
2; switch
(month) {
case 1:
42
1.
System.out.println("Ja
nuary"); break; case
2:
System.out.println("Fe
bruary"); break; case 3:
System.out.println("Ma
rch"); break; case 4:
System.out.println("Ap
ril"); break; case 5:
System.out.println("Ma
y"); break; case 6:
System.out.println("Ju
ne"); break; case 7:
System.out.println("Jul
y"); break; }
}
}
The conditional operator is also known as the ternary operator. This operator consists of
three operands and is used to evaluate Boolean expressions. The goal of the operator is to
decide; which value should be assigned to the variable. The operator is written as: variable
y = (expression)? value if true: value if false
43
1.
Exercise 6.2
44
1.
Below 40 F 0.0
2. Write the output for the following conditional operators given that a = 40 and b =
60
i. c= (b>a)? 50:100 ii. x= (a>b)? 150:200 iii. y= (a==b)? 250:400
LOOP STATEMENTS
Loop statements are also known as iterative statements. They are used for repeated
execution of a statement or block of statements as long as condition binding the loop is
true. Every loop statement has three major components
7 .1 while Statement
The rule for while statement:
Initialization of control variable;
While (Boolean Condition) {
Block of statements;
Update of control variable;
}
Program Example 7.1
public class
ProgramExample7_1 { public
static void main(String[] args)
{ // Written by Dr Araoye O.
I.
45
1.
int i =
0;
while
(i
<10) {
System.out.println("Value of
i = " +i); i++;
}}}
46
1.
sum + i;
i++;
}
System.out.println ("The sum of the first fifty integer number is "+sum);
}
}
The do..while loop is a variant of the while loop. This loop will execute the code block
once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true. The rule is
Control variable
initialization; do {
Block of Statements ;
Update of control variable;
}
while (Boolean Conditions);
The do…while loop version of Program example 7.1 and example 7.2 are written as
example 7.3 and example 7.4 respectively.
47
1.
public class
ProgramExample7_3 { public
static void main(String[] args)
{ // Written by Dr Araoye O.
I. int i = 0; do {
System.out.println("The value of i
is = "+i); i++; } while (i <10);
}
}
48
1.
sum =
sum +
i; i++;
}
while
(i
<=50);
System.out.println ("The sum of the first fifty integer number is "+sum);
}
}
The rule is
for (control-variable initialization; Boolean-condition; update of control-
variable) {
Block of statements to be executed;
}// End of for..Statement
Example
for ( i=0; i<10; i++) {
System.out.println (“ The value of I = “ +i);
}
Program Example 7.5 public
class ProgramExample7_5 {
public static void
main(String[] args) { //
49
1.
public class
ProgramExample7_6 { public
static void main(String[] args)
{ // Written by Dr Araoye O.
I. int sum = 0; for (int i =
1; i<=50; i++) { sum = sum +
i;
}
System.out.println ("The sum of first fifty integers is = " +sum);
}
}
50
1.
A for statement can be nested in another for statement. The outer statement controls the
inner statement. The inner statement will stop executing when the condition binding the
outer statement expires. Two integer control variables are required for nested for
statement. The rule is
for (control-variable1 initialization; Boolean-condition; update of control-
variable1) { for (control-variable2 initialization; Boolean-condition; update of
control-variable2) { Block of statements to be executed;
}// End of loop2
}// End of loop1
Example
//loop of i for(int
i=1;i<=4; i++){
//loop of j for(int
j=1;j<=3;j++){
System.out.println(
i+" "+j);
}//end of i
}//end of j
public class
ProgramExample7_7 { public
static void main(String[] args)
{ // Written by Dr Araoye O.
51
1.
52
1.
1. Define a Loop
2. Discuss various form of Loop statements in Java
3. Discuss with program example, the difference between while..do and do..while
4. Write a program to generate the series 1+22+32+42…n2
5. Write a program to print numbers from 1 to 50.
6. Write a program to calculate the sum of first N natural number, where N is
entered through the keyboard
METHODS
Methods are subprograms in a main program which can be called t as an entity by its name.
Methods also known as functions are written to perform a particular task. A method can
return a value to a variable or may not return a value which is usually refers to as void
method. A method can be static or public. When it is static, there is no need to create object
of such method and usually written under main class but when it is public there is need to
53
1.
create object of such method and it is written outside main class. We shall look at static
method in this chapter and public method under classes and objects chapter
Static void methods are written before main method and are called within the body of main
method. Static method can have a passing parameters or may not have. Subsequent
examples in this section shows non.passing parameters static void method and passing
parameter static void method. The general syntax structure of void method is
<static><void><method_name>( Parameters) {
Body of the code
}
Example
Void static areaCylinder ( ) {
Body of code
}
To call the method from the main method body, the rule is:
The methods’name followed by
semi colon method_name();
Example areaCylinder ( ); See
program example 8.1 Program
example 8.1 shows a method used
to calculate the area of a cylinder
and the area called in the body of
the main method.
54
1.
When parameters are passed to method, they are called arguments. The arguments passed
in the body of the method are called former parameters while the argument passed in the
body of main method are called actual parameters Program Example 8.2
55
1.
You may pass direct value as argument as seen in Program Example 8.3
56
1.
This is a method that returns the value of its expression to a variable of its type. The
syntax structure of return method is:
<static><return_datatype><method_name>(
Parameters) { Body of the code; return expression; }
Example:
static int areacylinder (double radius, double
height) { double area =
2*pi*radius*(radius+height); return (area);
}
57
1.
Let us look at the implementation of this example with passing parameters and without
passing parameters in Program examples 8.4 and 8.5 respectively
myInput.nextDouble();
System.out.println("Enter Height" );
double height =
myInput.nextDouble(); final double
pi =3.142; double area =
2*pi*(radius+height); return (area);
}
// End of method and begining of main
method public static void
main(String[] args) { // @author Dr
Araoye O. I.
// e-mail: [email protected]
double areal = areaCylinder( ); // Calling of return method without passing parameters
System.out.println(" Area of Cylinder is " + areal);
}
}
This is a program that swap two integer numbers. IF A = 4 and B =7, at the end of
execution, A will be 7 and B will be 4.
public class ProgramExample8_6 { static void
swap ( int a, int b ) { // declaration of method
59
1.
60
1.
P= , n= 8 and r =2
61