Professional Documents
Culture Documents
Java Strings, Eh, Generics, Threads
Java Strings, Eh, Generics, Threads
Aim:
PROGRAM:
package String;
import java.util.Scanner;
int count = 1;
count++;
}}
}}
Aim:
Return the number of times that the string “hi” appears anywhere in the string.
PROGRAM:
public static inpackage String;
import java.util.Scanner;
t countHi(String str) {
int times = 0;
times++;
return times;
}}
OUTPUT:
Aim:
PROGRAM:
package String;
output:
Aim:
Write a Java program to replace each substring of a given string that matches
the given regular expression with the given replacement.
Sample string : "The quick brown fox jumps over the lazy dog." In the above
string replace all the fox with cat.
Write a Java program to find first non-repeating character in a string
Write a Java program to divide a string in equal parts.
PROGRAM:
package String;
String str = "The quick brown fox jumps over the lazy dog.";
output:
EXCEPTIONAL HANDLING
Code:
package generics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
output:
Hexadecimal to decimal
Code :
package generics;
import java.util.*;
String a=sc.nextLine();
try {
int i = Integer.parseInt(a,16);
catch (NumberFormatException e) {
}
String index
Code:
package generics;
import java.util.*;
output:
GENERICS AND COLLECTIONS
Write a Java program to show how to convert a collection to an array by using
list.toArray () method of Java
Code:
package generics;
import java.util.*;
ArrayList<Integer> ArrLis
= new ArrayList<Integer>();
ArrLis.add(14);
ArrLis.add(17);
ArrLis.add(32);
ArrLis.add(47);
System.out.println("Elements of ArrayList"
+ Arrays.toString(arr));
Output:
Reverse of a list
Code:
package generics;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Collections.reverse(reverse);
return reverse;
System.out.println(reverse);
Output:
Compare the element of a collection by converting a string into a treeset using A.
Collection.min() and B. Collection.max() methods of Collection
Code:
package generics;
import java.util.Arrays;
import java.util.Collections;
+ minArray);
+ maxArray);
output:
Write a program to convert an array into a collection using Arrays.asList(name)
method of Java Util class
package generics;
import java.util.*;
import java.io.*;
int n = Integer.parseInt(in.readLine());
name[i] = in.readLine();
System.out.println();
Output:
THREADS
To Write a JAVA program that creates threads by extending Thread class. First thread display
“Good Morning “every 1 sec, the second thread displays “Hello “every 2 seconds and the
third display “Welcome” every 3 seconds .
Code:
package threads;
int i;
try{
for(i=0;i<5;i++){
System.out.println("GOOD MORNING");
sleep(1000);
catch(Exception e){System.out.println(e);
int i;
try{for(i=0;i<5;i++){
System.out.println("HELLO");
sleep(2000);
}}
int i;
try
{for(i=0;i<5;i++){
System.out.println("WELCOME");
sleep(3000);
catch(Exception e) {
System.out.println(e);
}}
ob1.start();
ob2.start();
ob3.start();
Output:
Output: