Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Cycle 2: Program To Sort Strings

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Object Oriented Programming Lab – 20MCA132

Lab Cycle 2

1. Program to Sort strings

import java.util.Scanner;
import java.util.Arrays;
public class sort {
public static void main(String[] args) { int
i,j;
Scanner sc = new Scanner(System.in);
System.out.println("Name: Athul Ajay");
System.out.println("Reg No: SJC22MCA-017");
System.out.println("Date: 28/03/2023");
System.out.println("Course code: 20MCA132");
System.out.println("Enter the number of
words"); int num=sc.nextInt(); String word[]=new
String[num]; sc.nextLine(); for( i=0;i<num;i++){
System.out.println("\nEnter a Word\n");
word[i]=sc.nextLine();
}
for( i=0;i<num-1;i++){
for( j=i+1;j<num;j++){
if(word[i].compareTo(word[j])>0)
{ String temp = word[i];
word[i]=word[j]; word[j]=temp;
}
}
}
System.out.println("Sorted Strings using CompareTo function
="+Arrays.toString(word));
System.out.println(word);

}}

SJCET Palai Department of MCA


Object Oriented Programming Lab – 20MCA132

2. Search an element in an array.

import java.util.Scanner;

public class Search { public static void


main(String[] args) { int i,j,x=0; boolean state
= false;
Scanner sc = new Scanner(System.in);
System.out.println("Name: Athul Ajay");
System.out.println("Reg No: SJC22MCA-017"); System.out.println("Date:
28/03/2023");
System.out.println("Course code: 20MCA132");
System.out.println("Enter the number of elemets in array"); int
num=sc.nextInt(); String word[]=new String[num]; sc.nextLine(); for(

SJCET Palai Department of MCA


Object Oriented Programming Lab – 20MCA132

i=0;i<num;i++){ System.out.println("\nEnter a Word\n");


word[i]=sc.nextLine();
}
System.out.println("Enter the element to Search");
String search = sc.nextLine(); for( i=0;i<num;i++){ if(word[i].equals(search)){
x = i; state = true;
} } if(state)
{
System.out.println("Element found at position = "+x);
}
else{
System.out.println("Element found not found");

}
}

SJCET Palai Department of MCA


Object Oriented Programming Lab – 20MCA132

3. Perform string manipulations

import java.util.Scanner;
public class String_man{ public static
void main(String[] args) {
System.out.println("Name: Athul Ajay");
System.out.println("Reg No: SJC22MCA-017");
System.out.println("Date: 28/03/2023");
System.out.println("Course code: 20MCA132");
System.out.println();
System.out.println("Enter The String");

SJCET Palai Department of MCA


Object Oriented Programming Lab – 20MCA132

Scanner sc = new Scanner(System.in);


String str1 = sc.nextLine();
System.out.println("Length of String = "+str1.length());
System.out.println("Character at First position = "+str1.charAt(1));
System.out.println("String Contains 'Col' sequence :"+str1.contains("Col"));
System.out.println("String ends with e : "+str1.endsWith("e"));
System.out.println("Replace'col' with 'kol' : "+str1.replaceAll("col","kol"));
System.out.println("LOWERCASE : "+str1.toLowerCase());
System.out.println("UPPERCASE : "+str1.toUpperCase());
}
}

4. Program to create a class for Employee having attributes eNo, eName


eSalary. Read n employ information and Search for an employee given
eNo, using the concept of Array of Objects.

import java.util.Scanner; public

class Employee { int eNo; String

eName; double

SJCET Palai Department of MCA


Object Oriented Programming Lab – 20MCA132

eSalary; public Employee(int

eNo, String

eName, double
eSalary) {
this.eNo = eNo; this.eName = eName;
this.eSalary =
eSalary;
}

public static void main(String[] args) {

System.out.println("Name: Athul Ajay");


System.out.println("Reg No: SJC22MCA-017");
System.out.println("Date: 28/03/2023");
System.out.println("Course code: 20MCA132");
System.out.println();

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the number of employees: "); int n
= scanner.nextInt();

Employee[] employees = new Employee[n]; for


(int i = 0; i < n; i++) {
System.out.println("Enter details for employee " + (i+1) +
":"); System.out.print("eNo: "); int eNo = scanner.nextInt();
System.out.print("eName: "); String eName =
scanner.next(); System.out.print("eSalary: "); double eSalary
= scanner.nextDouble(); employees[i] = new
Employee(eNo, eName, eSalary);
}

System.out.print("Enter the employee number to search: ");


int searchNo = scanner.nextInt(); boolean found = false; for
(Employee employee : employees) { if (employee.eNo
== searchNo) {

SJCET Palai Department of MCA


Object Oriented Programming Lab – 20MCA132

System.out.println("Employee found:
eNo=" + employee.eNo + ",
eName="
+ employee.eName + ", eSalary=" + employee.eSalary); found =
true; break;
} } if
(!found) {
System.out.println("Employee not found.");
}

scanner.close();
}
}

SJCET Palai Department of MCA

You might also like