Sunteți pe pagina 1din 12

EXPERIMENT NO.

: 8

TITLE: Strings Handling

Q1. Write a program for searching strings for the first occurrence of a character or sub
string and for the last occurrence of a character or sub string.

import java.util.Scanner;
public class FirstOccurence {
public static void main(String args[]) {
System.out.println("Enter a string");
Scanner temp = new Scanner(System.in);
String str = temp.next();
System.out.println("Enter keyword to search");
Scanner temp2 = new Scanner(System.in);
String str2 = temp2.next();
for (int i = -1; (i = str.indexOf(str2, i + 1)) != -1;) {
System.out.println("Position of entered text from forward is: "+i);
}
for (int j =-1; (j = str.lastIndexOf(str2, j - 1)) != -1; ) {
System.out.println("Position of entered text from backward is: "+j);
}
}
}

Output
Q2. Write a program that converts all characters of a string in capital letters. (Use
String Buffer to store a string). Dont use inbuilt function.

import java.util.Scanner;
public class CapitalLetter {
public static void main(String[] input)
{
char ch;
int temp;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Character in Lowercase : ");
ch = scan.next().charAt(0);
temp = (int) ch;
temp = temp - 32;
ch = (char) temp;
System.out.print("Equivalent Character in Uppercase = " +ch);
}
}
Output:

Q3. Write a program in Java to read a statement from console, convert it into
uppercase and again print on console. (Dont use inbuilt function)

public class Console {


public static void main(String args[])
{
char ch;
int temp;
ch = (args[0]).charAt(0);
temp = (int) ch;
temp = temp - 32;
ch = (char) temp;
System.out.print("Equivalent Character in Uppercase = " +ch);
}
}
Q4.Write a program in Java to create a String object. Initialize this object with your
name. Find the length of your name using the appropriate String method. Find
whether the character a is in your name or not; if yes find the number of times a
appears in your name. Print locations of occurrences of a .Try the same for different
String objects.
import java.util.*;
public class Data {
String name;
Data(String n){ name=n; }
void disp()
{
System.out.println("Name :"+name);
int c=0;
int len=name.length();
for(int i=0;i<len;i++)
if(name.charAt(i)=='a'||name.charAt(i)=='a')
{
c++;
System.out.println("number of occurance :"+c);
System.out.println("Possition :"+(i+1));
}
if(c==0)
System.out.println("there is no 'a' available in the string");
}
}
class Name
{
public static void main(String ar[])
{
Data d1=new Data("ujjawal");
d1.disp();
Data d2=new Data("upes");
d2.disp();
}
}

EXPERIMENT NO.: 9
Q1.Write a Java code that converts int to Integer converts Integer to String converts
String to int converts int to String converts String to Integer converts Integer to int.

import java.util.Scanner;
public class Wraper
{
public static void main(String[] args)
{
System.out.println("Enter a number");
Scanner temp=new Scanner(System.in);
int a=temp.nextInt();
System.out.println("Entered int is:"+a);
Integer b=Integer.valueOf(a);
System.out.println("int to Integer value is: "+b);
String c=String.valueOf(b);
System.out.println("Integer to String value is: "+c);
int d=Integer.parseInt(c);
System.out.println("String to int value is: "+d);
String e=String.valueOf(d);
System.out.println("int to String value is: "+e);
Integer f= Integer.valueOf(e);
System.out.println("String to Integer value is: "+f);
int g=f.intValue();
System.out.println("Integer to int value is: "+g);
}
}

Output:

Q2. Write a Java code that converts float to Float converts Float to String converts
String to float converts float to String converts String to Float converts Float to float.
import java.util.Scanner;
public class Wrapper {
public static void main(String args[])
{
System.out.println("Eneter a float value");
Scanner temp=new Scanner(System.in);
float a=temp.nextFloat();
System.out.println("Entered float value is: "+a);
Float b=new Float(a);
System.out.println("float to Float value is: "+b);
String c=Float.toString(b);
System.out.println("Float to String value is: "+c);
float d=Float.parseFloat(c);
System.out.println("String to float value is: "+d);
String e=String.valueOf(d);
System.out.println("float to String value is: "+e);
Float f= Float.valueOf(e);
System.out.println("String to Float value is: "+f);
float g=f.floatValue();
System.out.println("Float to float value is: "+g);
}
}

Output
EXPERIMENT-10
TITLE: Threads

Q1.Write a program to implement the concept of threading by extending Thread Class


and Runnable interface.
public class Multithreading extends Thread{
public void run(){
System.out.println("My thread is in running state.");
}
public static void main(String args[]){
Multithreading obj=new Multithreading();
obj.start();
}
}

public class Multithreading implements Runnable {


public void run(){
System.out.println("My thread is in running state.");
}
public static void main(String args[]){
Multithreading obj=new Multithreading();
Thread tobj =new Thread(obj);
tobj.start();
}
}

Q2. Write a program for generating 2 threads, one for printing even numbers and the
other for printing odd numbers.
public class Mythread {
public static void main(String[] args) {
Runnable r = new Runnable1();
Thread t = new Thread(r);
t.start();
Runnable r2 = new Runnable2();
Thread t2 = new Thread(r2);
t2.start() }}
class Runnable2 implements Runnable{
public void run(){
for(int i=0;i<10;i++){
if(i%2 == 1)
System.out.println("odd "+i); }}}
class Runnable1 implements Runnable{
public void run(){
for(int i=0;i<10;i++){
if(i%2 == 0)
System.out.println("even "+i);
}
}
}

Q3. Write a program to launch 10 threads. Each thread increments a counter variable.
Run the program with synchronization.
public class CounterVariable {
public static void main(String arg[])throws Exception
{
variabe d1=new variabe();
variabe d2=new variabe();
variabe d3=new variabe();
variabe d4=new variabe();
variabe d5=new variabe();
variabe d6=new variabe();
variabe d7=new variabe();
variabe d8=new variabe();
variabe d9=new variabe();
variabe d10=new variabe();
System.out.println(d10.count);
}
}
class item { static int count=0; }
class variabe extends item implements Runnable
{
item d=this;
Thread t;
variabe()
{
t=new Thread(this);
t.start();
}
public void run()
{ d=syn.increment(d); }
}
class syn
{
synchronized static item increment(item i)
{
i.count++;
return(i);
}
}

Q4. Write a Java program to create five threads with different priorities. Send two
threads of the highest priority to sleep state. Check the aliveness of the threads and
mark which thread is long lasting.
public class FiveThreads {
public static void main(String args[]) throws InterruptedException
{
Thread T1=new Thread();
Thread T2=new Thread();
Thread T3=new Thread();
Thread T4=new Thread();
Thread T5=new Thread();
T1.setPriority(7);
T2.setPriority(2);
T3.setPriority(10);
T4.setPriority(5);
T5.setPriority(8);
T1.sleep(1500);
if (T1.isAlive())
System.out.println("Thread 1 is alive");
else
System.out.println("Thread 1 is not alive");
T2.start();
if (T2.isAlive())
System.out.println("Thread 2 is alive");
else
System.out.println("Thread 2 is not alive");
T3.sleep(1000);
if (T3.isAlive())
System.out.println("Thread 3 is alive");
else
System.out.println("Thread 3 is not alive");
T4.start();
if (T4.isAlive())
System.out.println("Thread 4 is alive");
else
System.out.println("Thread 4 is not alive");
T5.start();
if (T5.isAlive())
System.out.println("Thread 5 is alive");
else
System.out.println("Thread 5 is not alive");
}
}

S-ar putea să vă placă și