9. Write a Java Program to perform the following String Operations.
Read a String
Find out whether there is a given substring or not
Compare existing string by another string and display status
Replace existing string character with another character
Count number of works in a string
import java.util.Scanner;
class read
{
void input()
{
System.out.println("READING A STRING FROM CONSOLE\n");
System.out.println("Enter a string: ");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.println("String read from console is:\n "+s);
System.out.println("==================================\n\n");
}
}
class sub
{
Scanner s=new Scanner(System.in);
void check()
{
System.out.println("CHECKING SUBSTRING:\n");
System.out.println("Enter the string:");
String txt=s.nextLine();
System.out.println("Enter the sub string:");
String st=s.nextLine();
boolean result = txt.contains(st);
if(result)
{
System.out.println(st + " is present in the string.");
}
else
{
System.out.println(st + " is not present in the string.");
}
System.out.println("======================================\n\n");
}
}
class equal
{
Scanner s=new Scanner(System.in);
void compare()
{
System.out.println("COMPARING SUBSTRINGS:\n");
System.out.println("Enter a String:");
String txt=s.nextLine();
System.out.println("Enter 2nd String:");
String txt2=s.nextLine();
System.out.println("Enter 3rd String:");
String txt3=s.nextLine();
boolean result;
if(result=txt.equals(txt2))
{
System.out.println("\nString first and second are equal:"+result);
}
else if(result=txt.equals(txt3))
{
System.out.println("\nString first and third are equal:"+result);
}
else
{
System.out.println("Hence given Strings are not equal:");
}
System.out.println("=========================================\n\n");
}
}
class replace
{
String str="The quick brown fox jumps over the lazy dox";
String newstr;
void another()
{
System.out.println("REPLACING CHARACTERS:\n");
newstr=str.replace('d','f');
System.out.println("charcter Replacement :");
System.out.println("Original String :"+str);
System.out.println("New String is :"+newstr);
System.out.println("==================================\n\n");
}
}
class count
{
Scanner s=new Scanner(System.in);
void number()
{
System.out.println("COUNTING LENGTH OF A STRING:\n");
System.out.println("Enter the String to count the no. of words:");
String str=s.nextLine();
System.out.println("\nThe size of the String is:"+str.length());
System.out.println("==================================\n\n");
}
}
class strfun
{
public static void main(String args[])
{
read r=new read();
r.input();
sub s=new sub();
s.check();
equal e=new equal();
e.compare();
replace re=new replace();
re.another();
count c=new count();
c.number();
}
}
Output:
C:\Users\Student\Desktop\java prg>javac Strfun.java
C:\Users\Student\Desktop\java prg>java Strfun
READING A STRING FROM CONSOLE
Enter a string:
raj kumar
String read from console is:
raj kumar
==================================
CHECKING SUBSTRING:
Enter the string:
welcome to java
Enter the sub string:
to
to is present in the string.
======================================
COMPARING SUBSTRINGS:
Enter a String:
ram
Enter 2nd String:
sita
Enter 3rd String:
ram
String first and third are equal:true
=========================================
REPLACING CHARACTERS:
charcter Replacement :
Original String :The quick brown fox jumps over the lazy dox
New String is :The quick brown fox jumps over the lazy fox
==================================
COUNTING LENGTH OF A STRING:
Enter the String to count the no. of words:
ram is a boy
The size of the String is:12
==================================
10.Write a java program to implement arithmetic package and perform addition,subtraction, multiplication and division operations with command line agruments
//Program :file name: Rk.java
package pk;
public class Rk
{
public int add(int a,int b)
{
return a+b;
}
public int sub(int a,int b)
{
return a-b;
}
public int mul(int a,int b)
{
return a*b;
}
public int div(int a,int b)
{
return a/b;
}
}
Step 2: create a file with Kk.java
import Pk.Rk; // import <Package name>.<Class name>
class Kk
{
public static void main(String []args)
{
Rk c=new Rk();
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int z=c.mul(a,b);
int v=c.sub(a,b);
System.out.println("Addition is:"+c.add(a,b));
System.out.println("Division is:"+c.div(a,b));
System.out.println("Multiplication is:"+z);
System.out.println("Subtraction is:"+v);
}
}
Step 3: use the following command to run the package program
ubuntu@ubuntu:~/Desktop$ mkdir pk
ubuntu@ubuntu:~/Desktop$ cd pk
ubuntu@ubuntu:~/Desktop/pk$ gedit Rk.java
ubuntu@ubuntu:~/Desktop/pk$ javac -d . Rk.java
ubuntu@ubuntu:~/Desktop/pk$ ls
pk Rk.class Rk.java
ubuntu@ubuntu:~/Desktop/pk$ cd ..
ubuntu@ubuntu:~/Desktop$ gedit Kk.java
ubuntu@ubuntu:~/Desktop$ javac -d . Kk.java
ubuntu@ubuntu:~/Desktop$ ls K*
Kk.class Kk.java
ubuntu@ubuntu:~/Desktop$ java Kk 4 2
Addition is:6
Division is:2
Multiplication is:8
Subtraction is:2
11.Java program demonstrate the following exception handlings:
a.Dividesbyzero
b.ArrayIndexOutOfBound
c.Filenotfound
d.ArithmeticException
e.Userdefined Exception
import java.util.Scanner;
import java.io.*;
class dbz
{
void divide()
{
System.out.println("DIVIDED BY ZERO\n");
Scanner sc=new Scanner(System.in);
System.out.println("Enter P value: ");
int p=sc.nextInt();
System.out.println("enter Q value: ");
int q=sc.nextInt();
try
{
int result=p/q;
System.out.println("Result:"+p+"/"+q+"="+result);
}
catch(ArithmeticException e)
{
System.out.println("Divided by Zero");
}
System.out.println("====================");
}
}
class aiob
{
void array()
{
System.out.println("CHECKING ARRAY INDEX \n");
Scanner sc=new Scanner(System.in);
int a[]={1,2,3,4,5};
try
{
System.out.println("Array fixed size is 1 to 4");
System.out.println("Enter Array size: ");
int i=sc.nextInt();
if(a[i]<=5)
{
System.out.println(" Position of the given number is: "+a[i]);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index out of Bound");
}
System.out.println("====================");
}
}
class fnf
{
void file()
{
System.out.println("FILE CHECKING\n");
try
{
FileInputStream fin=new FileInputStream("Desktop/stock.txt");
System.out.println("File Found");
}
catch(FileNotFoundException e)
{
System.out.println("File not Found");
}
System.out.println("=======================");
}
}
class arex
{
void arth()
{
System.out.println("ARITHMETIC EXCEPTION\n");
Scanner sc=new Scanner(System.in);
System.out.println("Enter A value: ");
int a=sc.nextInt();
System.out.println("enter B value: ");
int b=sc.nextInt();
try
{
int result=a/b;
System.out.println("Result:"+a+"/"+b+"="+result);
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception");
}
System.out.println("====================");
}
}
class AgeException extends Exception
{
public AgeException(String str)
{
System.out.println(str);
}
}
class udex
{
void user()
{
System.out.println("USER DEFINED EXCEPTION\n");
Scanner sc=new Scanner(System.in);
try
{
System.out.println("Enter Age of Candidate: ");
int age=sc.nextInt();
if(age < 18)
throw new AgeException("Invalid age");
else
System.out.println("Valid age");
}
catch (AgeException a)
{
System.out.println(a);
}
}
}
class main
{
public static void main(String args[])
{
dbz d=new dbz();
d.divide();
aiob a=new aiob();
a.array();
fnf f=new fnf();
f.file();
arex ae=new arex();
ae.arth();
udex u=new udex();
u.user();
}
}