LAB#4
Linear array implementation through array list
Task#1
Implement algorithms of insertion deletion and traversing through static array.
Input
import java.util.Scanner;
public class Lab_4_Task_1 {
public static void main(String[] args) {
Scanner enter = new Scanner(System.in);
System.out.println(“Enter Size of Array : “);
int size = enter.nextInt();
int[] list = new int[size + 1];
System.out.print(“TRANSVERSE THE ARRAY\n”);
for(int t=0; t < size; t++){
list[t] = (int) (Math.random() * 100);
System.out.print(“At Position[“+t+”] = “+ list[t]+”, “);
}// end for loop
System.out.println(“\n\nEnter Position where you insert a value : “);
int p = enter.nextInt();
System.out.println(“Enter Value : “);
int v = enter.nextInt();
insert(p,v, list);
System.out.println(“\n\nEnter value which you want to delete : “);
int q = enter.nextInt();
delete(q,list);
enter.close();
}// end main method
static void insert(int position, int value_1, int[] la_1){
int j = la_1.length – 2;
while(j >= position ){
la_1[j + 1] = la_1[j];
j = j – 1;
}// end while loop
la_1[position] = value_1;
System.out.println(“INSERTED VALUE IN ARRAY AT POSITION “+position);
for(int i = 0; i < la_1.length; i++){
System.out.print(“At Position[“+i+”] = “+la_1[i]+”, “);
}
}// end insert method
static void delete(int value_2, int[] la_2){
int h = 0;
int y = la_2.length – 2;
for(int a=0; a < la_2.length-1; a++){
if(value_2 == la_2[a]){
h = a;
}// end if
}// end for loop
while(h <= y){
la_2[h] = la_2[h+1];
h++;
}// end while loop
System.out.println(“DELETED VALUE IN ARRAY”);
for(int p = 0; p < y; p++){
System.out.print(“At Position[“+p+”] = “+la_2[p]+”, “);
}// end for loop
}// end delete method
}// end class
Output:
Enter Size of Array :
3
TRANSVERSE THE ARRAY
At Position[0] = 23, At Position[1] = 63, At Position[2] = 69,
Enter Position where you insert a value :
2
Enter Value :
3
INSERTED VALUE IN ARRAY AT POSITION 2
At Position[0] = 23, At Position[1] = 63, At Position[2] = 3, At Position[3] = 69,
Enter value which you want to delete :
1
DELETED VALUE IN ARRAY
At Position[0] = 63, At Position[1] = 3,
Task#2
Implement all methods of arraylist.
Input:
import java.util.*;
public class l4_2 {
public static void main(String[] args) {
ArrayList<String> myList = new ArrayList<String>();
String a = new String(“umair”);
String b = new String(“hussain”);
String c = new String(“javaed”);
String d = new String(“khile”);
System.out.println(“Initial Size of Array List is : “+myList.size());
myList.add(a); // Add Object in Array List
myList.add(b); // Add Object in Array List
myList.add(2,c); // Add Object at index 2
System.out.println(“Contents of Array List : “+myList);
System.out.println(“Now Size of Array List is : “+myList.size());
System.out.println(“Index of Object \”b\” is : “+myList.indexOf(b));
System.out.println(“Last Index of Object \”c\” is : “+myList.lastIndexOf(c));
System.out.println(“Object at index 1 is : “+myList.get(1));
myList.set(2,d);
System.out.println(“Set Object \”d\” at index 2 : “+myList);
myList.remove(1);
System.out.println(“Remove Object at index 1 : “+myList);
}// end main method
}// end class
Output
Initial Size of Array List is : 0
Contents of Array List : [umair, hussain, javaed]
Now Size of Array List is : 3
Index of Object “b” is : 1
Last Index of Object “c” is : 2
Object at index 1 is : hussain
Set Object “d” at index 2 : [umair, hussain, khile]
Remove Object at index 1 : [umair, khile]
Task#3
Write a program to maintain the employ record by array list.Empoly information details are first name,date of joing,company ID,gender,date of birth addres,mobile,email.apply diffrint array list make employ class for date member main class for performing operation.
Input:
public class Employee {
String first_Name;
String last_Name;
String company_ID;
String gender;
String mobile;
String date_Of_Birth;
String date_Of_Joining;
String address;
Employee(String f,String l,String c, String g, String m, String b, String j, String a){
first_Name = f;
last_Name = l;
company_ID = c;
gender = g;
mobile = m;
date_Of_Birth = b;
date_Of_Joining = j;
address = a;
}
}// end class
import java.util.*;
public class Lab_4_Task_3 {
public static void main(String[] args) {
Scanner enter = new Scanner(System.in);
ArrayList<Employee> list = new ArrayList<Employee>();
char op = ‘n’;
do {
System.out.println(“—————————“);
System.out.println(“\tWELCOME”);
System.out.println(“—————————“);
System.out.println(“1. Search for Employees”);
System.out.println(“2. Enter Employee Details”);
System.out.println(“3. Delete Employee Details”);
System.out.print(“\nEnter your option : “);
int option = enter.nextInt();
switch(option){
case 1:
if(list.size() == 0){
System.out.println(“\nNo Employees Have Been Entered Yet”);
}// end if
else {
System.out.print(“Enter Employee Name : “);
String f_name = enter.next();
for(Employee obj : list){
if(f_name.equals(obj.first_Name)){
System.out.println(“——————–“);
System.out.println(“EMPLOYEE DETAILS”);
System.out.println(“——————–“);
System.out.println(“First Name : “+obj.first_Name);
System.out.println(“Last Name : “+obj.last_Name);
System.out.println(“Company ID : “+obj.company_ID);
System.out.println(“Gender : “+obj.gender);
System.out.println(“Mobile : “+obj.mobile);
System.out.println(“Date of Birth : “+obj.date_Of_Birth);
System.out.println(“Date of Joining : “+obj.date_Of_Joining);
System.out.println(“Address : “+obj.address);
}// end if
}// end for loop
}// end else
break;
case 2:
System.out.println(“————————–“);
System.out.println(“ENTER EMPLOYEE DETAILS”);
System.out.println(“————————–“);
System.out.print(“Enter First Name : “);
String n = enter.next();
System.out.print(“Enter Last Name : “);
String ln = enter.next();
System.out.print(“Enter Company ID : “);
String c = enter.next();
System.out.print(“Enter Gender : “);
String g = enter.next();
System.out.print(“Enter Mobile : “);
String m = enter.next();
System.out.print(“Enter Date of Birth : “);
String dob = enter.next();
System.out.print(“Enter Date of Joining : “);
String doj = enter.next();
System.out.print(“Enter Address : “);
String e = enter.next();
list.add(new Employee(n,ln,c,g,m,dob,doj,e));
break;
case 3:
System.out.print(“Enter Employee Name : “);
String f_n = enter.next();
for(Employee obj : list){
if(f_n.equals(obj.first_Name)){
list.remove(list.indexOf(obj));
}// end if
}// end for loop
break;
default :
System.out.println(“You Enter Wrong Option”);
}// end switch statement
System.out.println(“\n—————————————“);
System.out.println(“Do you want another operation y/n : “);
System.out.println(“—————————————“);
String operation = enter.next();
op = operation.charAt(0);
}// end do
while(op == ‘y’);
enter.close();
}// end main method
}// end class
Output:
—————————
WELCOME
—————————
- Search for Employees
- Enter Employee Details
- Delete Employee Details
Enter your option : 2
————————–
ENTER EMPLOYEE DETAILS
————————–
Enter First Name : irfan
Enter Last Name : chima
Enter Company ID : 0123
Enter Gender : mail
Enter Mobile : 03425128
Enter Date of Birth : 1990
Enter Date of Joining : 10-02-2017
Enter Address : nipa
—————————————
Do you want another operation y/n :
—————————————
y
—————————
WELCOME
—————————
- Search for Employees
- Enter Employee Details
- Delete Employee Details
Enter your option : 1
Enter Employee Name : irfan
——————–
EMPLOYEE DETAILS
——————–
First Name : irfan
Last Name : chima
Company ID : 0123
Gender : mail
Mobile : 03425128
Date of Birth : 1990
Date of Joining : 10-02-2017
Address : nipa
—————————————
Do you want another operation y/n :
—————————————
y
—————————
WELCOME
—————————
- Search for Employees
- Enter Employee Details
- Delete Employee Details
Enter your option : 3
Enter Employee Name : irfan
HOME TASK
Task #1
Input
public class LH4 {
String name;
String roll_No;
String age;
String section;
LH4(String n,String r, String a, String s){
name= n;
roll_No = r;
age = a;
section = s;
}
}// end student class
import java.util.*;
public class MainLH$ {
public static void main(String[] args) {
Scanner enter = new Scanner(System.in);
LH4 obj_1 = new LH4(“Khalid”, “106”, “19”, “C”);
LH4 obj_2 = new LH4(“Hamza”, “191”, “18”, “C”);
ArrayList<LH4> list_1 = new ArrayList<LH4>();
System.out.println(“———————–“);
System.out.println(“\tWELCOME”);
System.out.println(“———————–“);
System.out.println(“1. Add Object”);
System.out.println(“2. Remove Object”);
System.out.println(“3. Replace “);
System.out.println(“4. Add Object to Specified Index”);
System.out.println(“5. Remove Object from Specified Index”);
System.out.print(“\nEnter Option : “);
int op = enter.nextInt();
switch(op){
case 1:
list_1.add(obj_1);
System.out.println(“Object is added.”);
break;
case 2:
list_1.remove(list_1.indexOf(obj_1));
System.out.println(“Object is removed.”);
break;
case 3:
list_1.add(list_1.indexOf(obj_1),obj_2);
System.out.println(“Object is replaced.”);
break;
case 4:
System.out.println(“Enter index no. : “);
int i = enter.nextInt();
list_1.add(i, obj_1);
System.out.println(“Object is added at index “+i);
break;
case 5:
System.out.println(“Enter index no. : “);
int j = enter.nextInt();
list_1.remove(j);
System.out.println(“Object is removed from index “+j);
break;
default :
System.out.println(“You Entered Wrong Option”);
}// end switch
enter.close();
}// end method
}// end class
Output
———————–
WELCOME
———————–
- Add Object
- Remove Object
- Replace
- Add Object to Specified Index
- Remove Object from Specified Index
Enter Option : 1
Object is added.