CENTRE FOR DISTANCE AND ONLINE EDUCATION
CENTRE FOR DISTANCE AND ONLINE EDUCATION
LIVE SESSION – SESSION NO. 1
O02LA502– JAVA PROGRAMMING LAB
MASTER OF COMPUTER APPLICATION
JOYASHRI BASAK
ASSISTANT PROFESSOR
At the end of this session, you should be able to:
Learning
• get hands on experience on core concepts of
Objectives Object-Oriented Programming (OOP).
• code in Java for various applications.
• utilize Virtual lab. in Lumen Portal.
INTRODUCTION
•
•
•
Exercises in Java
1 Write a Java program to display employee details (employee name, salary) using class and method invocation
2 Write a java program to demonstrate the use of (i) increment (++) and decrement operator (--) (ii) IF-ELSE
Statement (iii) While loop.
3 Write a java program to perform matrix multiplication using 2-dimenstional array
4 Write a Java Program to check whether given String is Palindrome or not
5 Write a java program to demonstrate the use of super class and sub class concept.
6 Write a Java program using exception handling methods
7 Write a Java program to create a class and a subclass and demonstrate that a method can override.
8 Write a Java program to demonstrate the use of byte stream.
9. Write a Java program to create and start multiple threads that increment a shared counter variable
concurrently.
Assessments
• Student will have to complete Practical subjects in two parts.
1. attend Guided sessions for Guided exercises - 70 Marks
2. perform the Unguided exercises - 30 Marks
• The end semester examination (Unguided) will be conducted by the
Controller of Examination (CoE) for three hours for 30 marks, which
forms the Unguided Exercise(s) (UGE), where one or more problem(s)
will be assigned to you.
Scheme of Examination – Practical course/subject
Credits Duration of Internal University Total
University Assessment Exam Marks
Exam in hour(s) Marks Marks
2 3 70 30 100
To complete the lab course successfully, you have to score a combined
average of 40% (i.e., totally 40 marks) in both Guided and Unguided parts
together and a minimum of 40% in each part (i.e., 28 marks in Guided part
and 12 marks in Unguided part).
Exercises
• Before starting exercises, get used to with online tools of virtual labs,
• Practice as much as you can
Exercise 1
Write a Java program to display employee details (employee name, salary)
using class and method invocation
Algorithm:
Step 1; declare class Employee
Step 2: declare variables String employeeName;
int employeeSal;
Step 3: create constructer of class Public Employee(String name, int Salary)
Step 4: create a method Display to display variable values
Step 5: call main () method and create object for class
Employee emp=new Employee (“Bibhuti”, 50000);
Step 6: call display method by using object,
[Link]();
Step 7: End
(Note : You can apply your own Algorithm to program a code for a given Java Programming Exercises)
Exercise 1
“this” keyword in Java
In Java, “this” is a reference variable that refers to the current object.
this - can also be used in following ways:
• refer to current class instance variables.
• this() to invoke the current class constructor
• return the current class instance
• as the method parameter
• invoke the current class method
• as an argument in the constructor call
Exercise 1 (Example code)
class Main {
String employeeName;
int employeeSal;
//Contructor Main
public Main(String name, int salary){
employeeName = name;
employeeSal = salary;
}
void display(){
[Link]("Name of employee is: " + [Link] );
[Link]("Salary of employee is: " + [Link]);
}
public static void main(String[] args){
Main emp = new Main("Bibhuti", 50000);
[Link]();
}
}
Exercise 2
Write a java program to demonstrate the use of
(i) increment (++) and decrement operator (--)
(ii) IF-ELSE Statement
(iii) While loop.
Exercise 2 : increment (++) operator
Write a java program to demonstrate the use of (i) increment (++) and decrement operator (--)
Increment operators are used to increase the value of a variable by one. It adds 1 to the current value of a
variable.
There are two ways to use the increment operator:
pre increment operator (++x) : When the increment operator is placed before the variable (++x), it increments
the value of the variable before the value is used in the expression.
For example:
int x = 5;
int y = ++x; // Now, value of x is 6 and value of y is also 6
post increment operator (x++): When the increment operator is placed after the variable (x++), it increments
the value of the variable after its current value is used in the expression.
For example:
int x = 5;
int y = x++; // Now, value of y is 6, but value of x is 5
Exercise 2 : decrement operator (--)
Write a java program to demonstrate the use of (i) increment (++) and decrement operator (--)
Decrement operators are used to decrease the value of a variable by one. It subtracts 1 from the current
value of a variable.
There are two ways to use the decrement operator:
Pre Decrement Operator (--x): The prefix decrement operator decreases the value of the variable by 1
before the value is used in the expression.
Example:
int x = 7;
int y = --x; // Now, y is 6 and x is also 6
Post Decrement Operator (x--): The postfix decrement operator decreases the value of the variable by 1
after the value is used in the expression.
Example:
int x = 7;
int y = x--; // Now, y is 6, but x is 7
Exercise 2 (i) increment (++) and decrement operator (--)
Write a java program to demonstrate the use of (i) increment (++) and decrement operator (--)
class Main {
public static void main(String[] args) {
int x = 10;
//Preincrement operator
int y = ++x;
[Link]("y value is: " + y + " and x value is: " + x);
//Postincrement operator
y = x++;
[Link]("y value is: " + y + " and x value is: " + x);
//Pre decrement operator
int i = 6;
int j = --i;
[Link]("j value is: " + j + " and i value is: " + i);
//Post decrement operator
j = i--;
[Link]("j value is: " + j + " and i value is: " + i);
}
}
Exercise 2 (i) increment (++) and decrement operator (--)
Limitations of Increment and Decrement Operators:
•
•
Exercise 2: IF-ELSE Statement
Write a java program to demonstrate the use of (ii) If-else
statement
The syntax of the if...else statement is:
if (condition) {
// codes in if block
}
else {
// codes in else block
}
Exercise 2: IF-ELSE Statement
Write a java program to demonstrate the use of (ii) If-else statement
// Program to check whether number is greater than zero.
class Main {
public static void main(String[] args) {
int number = 5;
Also Change the value of the number to a
// check if number is greater than 0 negative integer (say number = -1) and see
if (number > 0) { the result.
[Link]("The number is greater than zero.");
}
// execute this block
// if number is not greater than 0
else {
[Link]("The number is not greater than zero.");
}
[Link](“This statement is outside the if...else block");
}
}
Exercise 2: while loop
Write a java program to demonstrate the use of (iii) while loop
The syntax of the while loop is:
while (condition){
//code to be executed
Increment / decrement statement
}
Exercise 2: while loop
Write a java program to demonstrate the use of (iii) while loop
// Program to print the number from 1 to 10 using while loop
public class Main {
public static void main(String[] args) {
int i=1;
//Using while loop to print the number from 1 to 10
while(i<=10){
[Link](i);
i++; // using Increment operator
}
}
}
Exercise 3:
Write a Java program to perform matrix multiplication using 2-
dimensional array
Let’s consider a simple 2 × 2 matrix multiplication
𝐴= [3 7
4 9]
𝑎𝑛𝑑 𝐵= [6 2
5 8]
Now each of the elements of product matrix AB can be calculated as follows:
• AB11 = 3 × 6 + 7 ×5 = 53
• AB12 = 3 × 2 + 7 × 8 = 62
• AB21 = 4 × 6 + 9 × 5 = 69
• AB22 = 4 × 2 + 9 × 8 = 80
Therefore,
AB = [53 62
69 80]
Exercise 3:
Write a java program to perform matrix multiplication using 2-
dimensional array
Key Learnings
▪
▪
▪
STUDENT SUPPORT
Thank You !!