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

LAB EXERCISE - 5

Object Oriented Program (19 CSE 204)


STRINGS

Aim:

Write a program to count the number of words in string:

PROGRAM:

package String;

import java.util.Scanner;

public class noofword {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); // Create a Scanner object

System.out.println("Enter the sentance:");

String str = sc.nextLine();

int count = 1;

for (int i = 0; i < str.length() - 1; i++)

if ((str.charAt(i) == ' ') && (str.charAt(i + 1) != ' '))

count++;

}}

System.out.println("Number of words in a string : " + 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;

public class counthi {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the sentance:");

String check = sc.nextLine();

int count = countHi(check);

System.out.println("No of 'hi' is: " + count);

t countHi(String str) {

int len = str.length()-1;

int times = 0;

for(int i = 0; i < len; i++) {

if(str.charAt(i) == 'h' && str.charAt(i+1) == 'i')

times++;

return times;

}}
OUTPUT:

Aim:

Write a Java Program to compare a given string to the specified


string buffer

PROGRAM:
package String;

public class strbuffer {

public static void main(String[] args) {

String str1 = "String 1", str2 = "String 2";

StringBuffer strbuf = new StringBuffer(str1);

System.out.println("Comparing "+str1+" and "+strbuf+": " +


str1.contentEquals(strbuf));

System.out.println("Comparing "+str2+" and "+strbuf+": " +


str2.contentEquals(strbuf)); }

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;

public class rev_str {

public static void main(String[] args)

String str = "The quick brown fox jumps over the lazy dog.";

String new_str = str.replaceAll("fox", "cat");

System.out.println("Original string: " + str);

System.out.println("New String: " + new_str);}

output:
EXCEPTIONAL HANDLING
Code:
package generics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class exception {


public static void main(String[] args) {
BufferedReader a = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Input your name:");
StringBuffer b = new StringBuffer("Welcome ");
try {
b.append(a.readLine());
a.close();
System.out.println(b);
} catch (IOException e) {
System.out.println("Error Encountered getting user input:"+
e.getMessage());
e.printStackTrace();
}
}
}

output:
Hexadecimal to decimal

Code :
package generics;

import java.util.*;

public class HextoDecimal {

public static void main(String[] args) {

Scanner sc= new Scanner(System.in);

System.out.println("Enter the hexadecimal number: ");

String a=sc.nextLine();

try {

int i = Integer.parseInt(a,16);

System.out.println("int value = " + i);

catch (NumberFormatException e) {

System.out.print("invalid binary input");

}
String index
Code:
package generics;

import java.util.*;

public class stringIndex{


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the string: ");
String a=sc.nextLine();
System.out.println("Enter the index number: ");
int n=sc.nextInt();
try {
a.charAt(n);
System.out.println("String Index is valid");
}
catch (StringIndexOutOfBoundsException e) {
System.out.println("String Index is out of bounds");
}
}
}

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.*;

public class Collectionofanarray {

public static void main(String[] args)

ArrayList<Integer> ArrLis

= new ArrayList<Integer>();

ArrLis.add(14);

ArrLis.add(17);

ArrLis.add(32);

ArrLis.add(47);

System.out.println("ArrayList: " + ArrLis);

Object[] arr = ArrLis.toArray();

System.out.println("Elements of ArrayList"

+ " as Array: "

+ 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;

public class reverseofcollection

public static<T> List<T> reverseList(List<T> list)

List<T> reverse = new ArrayList<>(list);

Collections.reverse(reverse);

return reverse;

public static void main(String[] args)

List<Integer> list = Arrays.asList(5,4,3,2,1);

List<Integer> reverse = reverseList(list);

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;

public class comparecollections{

public static void main(String args[]) throws Exception

Integer arr[] = { 17, 14, 12, 7, 4, 5 };

System.out.println("Array: " + Arrays.toString(arr));

int minArray = Collections.min(Arrays.asList(arr));

int maxArray = Collections.max(Arrays.asList(arr));

System.out.println("Minimum value of Array is: "

+ minArray);

System.out.println("Maximum value of Array is: "

+ 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.*;

public class collection {

public static void main(String args[]) throws IOException {

BufferedReader in = new BufferedReader (new InputStreamReader(System.in));

System.out.println("How many elements you want to add to the array: ");

int n = Integer.parseInt(in.readLine());

String[] name = new String[n];

for(int i = 0; i < n; i++) {

name[i] = in.readLine();

List<String> list = Arrays.asList(name);

System.out.println();

for(String li: list) {

String str = li;

System.out.print(str + " ");

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;

class MyThread1 extends Thread {

public void run() {

int i;

try{

for(i=0;i<5;i++){

System.out.println("GOOD MORNING");

sleep(1000);

catch(Exception e){System.out.println(e);

class MyThread2 extends Thread{

public void run() {

int i;

try{for(i=0;i<5;i++){

System.out.println("HELLO");
sleep(2000);

}}

catch(Exception e){ System.out.println(e);

class MyThread3 extends Thread {

public void run() {

int i;

try

{for(i=0;i<5;i++){

System.out.println("WELCOME");

sleep(3000);

catch(Exception e) {

System.out.println(e);

}}

public class hello {

public static void main(String args[]){

MyThread1 ob1 = new MyThread1();


MyThread2 ob2 = new MyThread2();

MyThread3 ob3 = new MyThread3();

ob1.start();

ob2.start();

ob3.start();

Output:

To Write a program for generating 2 threads, one for printing even


numbers and the other for printing odd numbers
To Write a program for generating 2 threads, one for printing even
numbers and the other for printing odd numbers
Code:
public class thread2 {
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<11;i++){
if(i%2 == 1)
System.out.println(i);
}
}
}

class Runnable1 implements Runnable{


public void run(){
for(int i=0;i<11;i++){
if(i%2 == 0)
System.out.println(i);
}
}
}

Output:

You might also like