CENTRE FOR DISTANCE AND ONLINE EDUCATION
CENTRE FOR DISTANCE AND ONLINE EDUCATION
LIVE SESSION – SESSION NO. 2
O02LA502– JAVA PROGRAMMING LAB.
MASTER OF COMPUTER APPLICATION
JOYASHRI BASAK
ASSISTANT PROFESSOR
At the end of this session, you should be able to:
Learning
• Perform Java coding using class, method and
Objectives constructor invocation
• Demonstrate the use of :
I. Array
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.
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 1 (Another Example code)
class Main {
String employeeName;
int employeeSal;
//Contructor
public Main() {
employeeName = "Bibhuti";
employeeSal = 50000;
};
void display(){
[Link]("Name of employee is: " + employeeName );
[Link]("Salary of employee is: " + employeeSal);
}
public static void main(String[] args){
Main emp = new Main();
[Link]();
}
}
Perquisites for Exercise 3:
Java Arrays
•
•
data-type[ ] variablename;
data-type variablename[ ]; // both are valid declarations
Exercise 3:
Array Example code
public class Main {
public static void main(String[] args) {
// Declaring and initializing an array
int[] numbers = {10, 20, 30, 40, 50};
// Accessing array elements
[Link]("First element: " + numbers[0]); // prints 10
[Link]("Third element: " + numbers[2]); // prints 30
// Printing all elements using a loop
[Link]("All elements:");
for (int i = 0; i < [Link]; i++) {
[Link](numbers[i]);
}
}
}
Key Learnings
▪
▪
STUDENT SUPPORT
Thank You !!