Upload files to "OOP/Java/Lab/Week9"

This commit is contained in:
Aadit Agrawal 2024-10-14 02:40:08 +05:30
parent cd43a87e9b
commit 2b9a21ac62
4 changed files with 226 additions and 0 deletions

View File

@ -0,0 +1,26 @@
import java.util.Scanner;
class counts{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the Lines...");
String str=in.nextLine();
str.trim();
int no=str.length();
System.out.println("Number of characters="+no);
String arr[]=str.split(" ");
System.out.println("Number of words="+arr.length);
String lines[]=str.split("\\.",0); // as \. to put a full stop and \\. to recognize \.
System.out.println("Number of Lines="+lines.length);
char characters[]=new char[no];
String lowstr=str.toLowerCase();
lowstr.getChars(0, no, characters, 0);
int i,count=0;
for(i=0;i<no;i++)
{
char cc=characters[i];
if(cc=='a'||cc=='e'||cc=='i'||cc=='o'||cc=='u')
count++;
}
System.out.println("Number of vowels="+count);
}
}

View File

@ -0,0 +1,68 @@
import java.util.Scanner;
public class menu_main2 {
public int compare(String st1,String st2)
{
return st1.compareTo(st2);
}
public String caseConvert(String st)
{
String converted="";
for(int i=0;i<st.length();i++){
char ch = st.charAt(i);
if(Character.isUpperCase(ch)){
converted+=Character.toLowerCase(ch);
}
else{
converted+=Character.toUpperCase(ch);
}
}
return converted;
}
public boolean substring(String st,String sub)
{
return st.contains(sub);
}
public StringBuffer hello(String st,String sub)
{
int index = st.indexOf(sub);
StringBuffer sb = new StringBuffer(st);
sb.replace(index,index+sub.length(),"Hello");
return sb;
}
public static void main(String[] args) {
menu_main2 obj = new menu_main2();
Scanner sc = new Scanner(System.in);
System.out.println("Enter:\na. To compare two strings\nb. To convert the uppercase character to lower and vice-versa\nc. To display whether an entered string is a substring of the other or not\nd. If the entered string is a substring of the other, replace it with Hello");
char choice = sc.nextLine().charAt(0);
System.out.println("Enter string");
String str1 = sc.nextLine();
String str2;
switch(choice)
{
case 'a':
System.out.println("Enter string to be checked");
str2 = sc.nextLine();
System.out.println("Comparison:"+obj.compare(str1,str2));
break;
case 'b':
System.out.println("String in converted case:"+obj.caseConvert(str1));
break;
case 'c':
System.out.println("Enter string to be checked");
str2 = sc.nextLine();
System.out.println("Is it a substring:"+obj.substring(str1,str2));
break;
case 'd':
System.out.println("Enter string to be checked");
str2 = sc.nextLine();
System.out.println("Substituting hello string is:"+obj.hello(str1,str2));
break;
}
}
}

View File

@ -0,0 +1,82 @@
import java.util.Scanner;
class palin_alpha_rev_concat{
static boolean palindrome(String str)
{
String rev=reverse(str);
return str.contentEquals(rev);
}
static String[] alphabetic(String str)
{
String arr[]=str.split(" ");
for(int i=0;i<arr.length-1;i++)
{
for(int j=0;j<arr.length-i-1;j++)
{
int a=arr[j].compareTo(arr[j+1]);
if(a>0)
{
String temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
return arr;
}
static String reverse(String str)
{
StringBuffer rev=new StringBuffer(str);
rev.reverse();
String revstr=rev.toString();
return revstr;
}
static String concating(String str)
{
String rev=reverse(str);
return str+rev;
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the String");
String str=in.nextLine();
char contin='y';
while(contin=='y'){
System.out.println("What do you want to do?1 for checking palindrome,2 for alphabetic,3 for reversing,4 for concatinatig with reverse");
int choice=in.nextInt();
switch(choice)
{
case 1:
{
System.out.println("String is a palindrome:"+palindrome(str));
break;
}
case 2:
{
String arr[]=alphabetic(str);
System.out.println("String in alphabetic order:");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
break;
}
case 3:
{
System.out.println("Reversed string="+reverse(str));
break;
}
case 4:
{
System.out.println("Concatenated String="+concating(str));
break;
}
}
System.out.println("Continue?(y/n)");
contin=in.next().charAt(0);
}
}
}

View File

@ -0,0 +1,50 @@
import java.util.Scanner;
class Student {
int regNumber;
String firstName;
String lastName;
String degree;
Student(int regNumber, String firstName, String lastName, String degree) {
this.regNumber = regNumber;
this.firstName = firstName;
this.lastName = lastName;
this.degree = degree;
}
}
class search_name {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student[] students = new Student[5];
// Sample data: 5 students as an array of Student objects
students[0] = new Student(101, "John", "Doe", "Computer Science");
students[1] = new Student(102, "Jane", "Smith", "Engineering");
students[2] = new Student(103, "Bob", "Johnson", "Mathematics");
students[3] = new Student(104, "Alice", "Brown", "Physics");
students[4] = new Student(105, "Charlie", "Wilson", "History");
System.out.print("Search by (first_name/last_name): ");
String searchType = sc.nextLine().toLowerCase();
System.out.print("Enter the name to search: ");
String searchName = sc.nextLine().toLowerCase();
boolean found = false;
for (Student student : students) {
if ((searchType.equals("first_name") && student.firstName.toLowerCase().contains(searchName))
|| (searchType.equals("last_name") && student.lastName.toLowerCase().contains(searchName))) {
System.out.println("Found student:");
System.out.println("Registration Number: " + student.regNumber);
System.out.println("First Name: " + student.firstName);
System.out.println("Last Name: " + student.lastName);
System.out.println("Degree: " + student.degree);
System.out.println();
found = true;
}
}
if (!found) {
System.out.println("No student found with the provided name.");
}
}
}