Professional Documents
Culture Documents
20I3040 ParasJain ITR3C2 Assignment 4
20I3040 ParasJain ITR3C2 Assignment 4
/*Write a program to differentiate between compareTo() and equals method of String class
*/
public class Q1 {
public static void main(String[] args) {
String s1 = "This is Question 1";
String s2 = "tHiS is qUesTion 1";
String s3 = "This is Question 1";
// here s1 and s3 are identical strings but s2 has some variations in its case.
/*
* Lets try equals method, the equal method returns a boolean value and takes
* and object as argument to which the string will be compared.
*/
System.out.println("Equal to Method:");
// Here we are comparing s1 with s3 which are identical, hence it returns true
System.out.println( s1.equals(s3));
// Here we are comparing s1 with s2 which are same in text but different in
// there case , hence it returns false.
System.out.println( s1.equals(s2));
// Here we are using the equalsIgnoreCase() method which ignores the case while
// checking two strings hence it returns true.
System.out.println( s1.equalsIgnoreCase(s2));
/*
* Now let's try the compare to method, the compare to method will return an
* integer value and takes a object as argument
*
* The compare method will return: 1. Positive value: If the first string is
* greater than second. 2. Negetive value: If the first string is smaller than
* second. 3. Zero: If the first string is equal to second.
*
* The compareTo() function compare each charecter of the strings by there ASC
II
* values, hence it return the difference between the ascii value of the char
of
* firs string and the other string.
*/
System.out.println("\n Compare to Method:");
/*
* The first charecter of s1 is Q and s2 is q the ascii difference between q
and
* Q is of 32 hence it return -32 and stop checking ahead
*/
System.out.println( s1.compareTo(s2));
/*
* The first charecter of s1 is Q and s3 is Q the ascii difference between Q
and
* Q is of 0 hence it return 0 and does the same for all char
*/
System.out.println( s1.compareTo(s3));
// We also have the ignoreCase in compareTo() method
System.out.println( s1.compareToIgnoreCase(s2));
/*
* here the difference of ascii values will become 0 as the cases are ignored
* hence it will return 0.
*/
}
}
Output:
Q2. Write a java program to reverse each word of a given string.
/*Write a program to differentiate between compareTo() and equals method of String class
*/
public class Q2 {
// Method to reverse the string
public static String reverce(String input) {
char[] temp = input.toCharArray();
char[] output = new char[input.length()];
for (int i = 0; i < input.length(); i++) {
output[i] = temp[input.length() - 1 - i];
}
String result = String.valueOf(output);
return result;
}
// Method to reverce the words
public static String revWords(String input) {
String result = "";
String revWord = "";
String[] words = input.split("\\s");
/*
* "\\s" here stands for white spaces, this method will split the words whneve
r
* the white space is found. and will store allow words in a string array
*/
// we can now reverce each word
for (String word : words) {
revWord = reverce(word);
result += revWord + " ";
}
return result;
}
public static void main(String[] args) {
String test = "This is the test Stirng";
System.out.println("Original String: " + test);
System.out.println("Reverce String: " + revWords(test));
}
}
Output:
Q3. Write a program to perform binary search on integer array using Array class of java.
import java.lang.reflect.Array;
import java.util.Arrays;
// Write a program to perform binary search on integer array using Array class of java.
public class Q3 {
public static void main(String[] args) {
int [] intArray = {1,2,5,9,14,23,54,101};
int key = 3;
int index = Arrays.binarySearch(intArray, key);
if (index < 0) {
System.out.println("Element not found");
}
else{
System.out.println("Found at Index: " + index);
}
key = 9;
index = Arrays.binarySearch(intArray, key);
if (index < 0) {
System.out.println("Element not found");
}
else{
System.out.println("Found at Index: " + index);
}
}
}
Output:
Q4. Write a java program to count the total number of occurrences of a given character in
a string without using any loop?
import java.util.Scanner;
/*Write a java program to count the total number of occurrences of a given character in
a string
without using any loop?*/
public class Q4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Test case string
String before = sc.nextLine();
char key = sc.next().charAt(0);
// Lenth before removing the key
int befLen = before.length();
// Removing all i from string
String[] splited = before.split(String.valueOf(key));
String after = String.join("", splited);
int aftLen = after.length();
// Now number of occurence = befLen - aftLen
int numberOfOccurence = befLen - aftLen;
System.out.println("Number of Occurence = " + numberOfOccurence);
}
}
Output:
Q5. Write a program to copy and add all the elements of a Vector to an array.
import java.util.Vector;
/*Write a program to copy and add all the elements of a Vector to an array.*/
public class Q5 {
public static void main(String[] args) {
// Creating vector
Vector<Integer> v = new Vector<Integer>();
// Adding elements to vector
v.add(1);
v.add(2);
v.add(3);
v.add(5);
v.add(32);
// Copying elements to array
Integer[] arr = new Integer[v.size()];
v.copyInto(arr);
int sum = 0;
System.out.println("The elements of vector are: ");
for (Integer i : arr) {
System.out.print(i + " ");
sum += i;
}
System.out.println("\nThe sum of elements of vector is: " + sum);
}
}
Output:
Q6. Write a program to get substring of a string.
/*Write a program to get substring of a string.*/
public class Q6 {
public static void main(String[] args) {
String testCase = "This is a String";
// Creating sub-strings
String subString = testCase.substring(3);
String subString2 = testCase.substring(5, 12);
// Printing sub-strings
System.out.println("Substring with no end bound: " + subString);
System.out.println("Substring with end bound: " + subString2);
}
}
Output:
Q7. Write a program to Check whether an element exists in Vector or not.
import java.util.Scanner;
import java.util.Vector;
/*Write a program to Check whether an element exists in Vector or not.*/
public class Q7 {
public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
v.add(4);
v.add(2);
v.add(1);
v.add(9);
v.add(11);
v.add(6);
v.add(14);
v.add(10);
// Inputs the key
Scanner sc = new Scanner(System.in);
Integer key = sc.nextInt();
/*
* vector.contains() method is used to check if the given key is present in
* vector or not
*/
System.out.println(v.contains(key));
}
}
Output:
Q8.Write a program to search element in Vector using index.
/*Write a program to search element in Vector using index.*/
import java.util.Vector;
import java.util.Scanner;
public class Q8 {
public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
v.add(4); // Index = 0
v.add(2); // Index = 1
v.add(1); // Index = 2
v.add(9); // Index = 3
v.add(11); // Index = 4
v.add(6); // Index = 5
v.add(14); // Index = 6
v.add(10); // Index = 7
// Inputs the key
Scanner sc = new Scanner(System.in);
Integer index = sc.nextInt();
/*
* vector.elementAt method is used to access the element at the given index an
d
* v.size() to get the size of the vector
*/
if (index <= v.size() - 1 && index >= 0) {
System.out.println("The element at given index is " + v.elementAt(index));
} else {
System.out.println("No element found at given index");
}
}
}
Output:
Q9.Write a java program to copy Elements of one Vector to another and display elements
of both the vectors.
/*Write a java program to copy Elements of one Vector to another and display
elements of both the vectors.*/
import java.util.Vector;
public class Q9 {
public static void main(String[] args) {
// Creating the vectors
Vector<Integer> v1 = new Vector<Integer>();
Vector<Integer> v2 = new Vector<Integer>();
// Adding elements to vector
v1.add(1);
v1.add(5);
v1.add(45);
v1.add(21);
v1.add(23);
v1.add(4);
v1.add(15);
v1.add(71);
// Copying elements of vector to another vector
v2 = v1;
// Elements of vectror 1
System.out.println("\nElements of first vector");
for (int i = 0; i < v1.size(); i++) {
System.out.print(v1.elementAt(i) + " ");
}
// Elements of vectror 2
System.out.println("\n\nElements of second vector");
for (int i = 0; i < v2.size(); i++) {
System.out.print(v2.elementAt(i) + " ");
}
}
}
Output:
import java.util.Vector;
import java.util.Scanner;
/*Write a program to remove element from specified index in Vector.*/
public class Q10 {
public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
v.add(4); // Index = 0
v.add(2); // Index = 1
v.add(1); // Index = 2
v.add(9); // Index = 3
v.add(11); // Index = 4
v.add(6); // Index = 5
v.add(14); // Index = 6
v.add(10); // Index = 7
// Before deleting element
System.out.println("\nBefore Deletion:");
for (int i = 0; i < v.size(); i++) {
System.out.print(v.elementAt(i) + " | ");
}
// Inputs the key
System.out.print("\n\nEnter the index to delete: ");
Scanner sc = new Scanner(System.in);
Integer index = sc.nextInt();
// deleting the element
v.removeElementAt(index);
// After deleting element
System.out.println("\nAfter Deletion:");
for (int i = 0; i < v.size(); i++) {
System.out.print(v.elementAt(i) + " | ");
}
System.out.println();
}
}
Output:
/*Write a program to convert Vector to ArrayList*/
import java.util.ArrayList;
import java.util.Vector;
public class Q11 {
public static void main(String[] args) {
// Creating the vector
Vector<Integer> v = new Vector<Integer>();
// Adding elements to vector
v.add(1);
v.add(5);
v.add(45);
v.add(21);
v.add(23);
v.add(4);
v.add(15);
v.add(71);
// Creating a Array List
ArrayList<Integer> al = new ArrayList<Integer>();
// copying vector to array list
for (int i = 0; i < v.size(); i++) {
al.add(v.elementAt(i));
}
// printing the ArrayList
System.out.println("\nThe elements of array list are:");
for (int i = 0; i < al.size(); i++) {
System.out.print(al.get(i) + " ");
}
System.out.println();
}
}
Output:
Q12. Write a program to sort Vector using Collections.sort().
//Write a program to sort Vector using Collections.sort().
import java.util.Collections;
import java.util.Vector;
public class Q12 {
public static void main(String[] args) {
// Creating the vector
Vector<Integer> v = new Vector<Integer>();
// Adding elements to vector
v.add(1);
v.add(5);
v.add(45);
v.add(21);
v.add(23);
v.add(4);
v.add(15);
v.add(71);
// Print Before Sorting
System.out.print("\nBefore Sorting: ");
for (int i = 0; i < v.size(); i++) {
System.out.print(v.elementAt(i) + " | ");
}
// Sorting the vector
Collections.sort(v);
// Print After Sorting
System.out.print("\nAfter Sorting: ");
for (int i = 0; i < v.size(); i++) {
System.out.print(v.elementAt(i) + " | ");
}
System.out.println();
}
}
Output:
/*Write a program to replace an element in Vector.*/
import java.util.Vector;
import java.util.Scanner;
public class Q13 {
public static void main(String[] args) {
// Creating the vector
Vector<Integer> v = new Vector<Integer>();
// Adding elements to vector
v.add(1);
v.add(5);
v.add(45);
v.add(21);
v.add(23);
v.add(4);
v.add(15);
v.add(71);
// Print Before Sorting
System.out.print("\nBefore replacement: ");
for (int i = 0; i < v.size(); i++) {
System.out.print(v.elementAt(i) + " | ");
}
/*
* taking element to replace and the number to replace with
*/
Scanner sc = new Scanner(System.in);
Integer key;
System.out.println();
do {
System.out.print("Element element to be replaced: ");
key = sc.nextInt();
} while (!v.contains(key));
System.out.print("Element to replace with: ");
Integer replacement = sc.nextInt();
Integer index = v.indexOf(key);
// Replacing element in vector
v.add(index, replacement);
v.remove(index + 1);
// Print After Sorting
System.out.print("After replacement: ");
for (int i = 0; i < v.size(); i++) {
System.out.print(v.elementAt(i) + " | ");
}
System.out.println();
}
}
Output:
Q14. Write a program to print Fibonacci series up to given terms using recursion.
Eg. Enter term: 5
Fibonacci series is 0 1 1 2 3.
/*Write a program to print Fibonacci series up to given terms using recursion.
Eg. Enter term: 5
Fibonacci series is 0 1 1 2 3.*/
import java.util.Scanner;
public class Q14 {
// Calculate the term at given index
public static int fab(int n) {
// Recurtion stop condition
if (n == 0 || n == 1) {
return n;
}
// Recursion
return fab(n - 1) + fab(n - 2);
}
public static void main(String[] args) {
// takes the number of terms
Scanner sc = new Scanner(System.in);
System.out.print("Enter terms: ");
int n = sc.nextInt();
// prints fibonaci series
for (int i = 0; i < n; i++) {
System.out.print(fab(i) + " ");
}
}
}
Output:
/*Write a program to convert decimal to binary equivalent using recursion.*/
import java.util.Scanner;
public class Q15 {
public static long dtob(int d) {
// Recursion stop condition
if (d == 0) {
return 0;
}
// Recursion
return ((d % 2) + 10 * dtob(d / 2));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter decimal number: ");
int decimal = sc.nextInt();
System.out.print("The binary equivelent is: " + dtob(decimal));
}
}
Output: