You are on page 1of 21

Name :-Shrutika N.

Ninawe
Sub:-java
Roll no:-22.
Assignment no :-4
1. Write a program to differentiate between compareTo() and equals
method of String class.
Ans:-
class GFG {
public static void main(String[] args)
{

// Get some Strings to compare


String s1 = "A";
String s2 = "A";
String s3 = "a";
String s4 = new String("A");

//equals method of string class

// Compare s1 and s2
// It should return true as they both have the same content
System.out.println(s1 + " .equals " + s2+ ": " + s1.equals(s2));

// Compare s1 and s3 It should return false as they both


// have the different content

System.out.println(s1 + " .equals " + s3 + ": " + s1.equals(s3));


// Compare s2 and s3
// It should return false as they both have the different content

System.out.println(s2 + " .equals " + s3+ ": " + s2.equals(s3));

// Compare s1 and s4
// It should return true as they both have the same content
System.out.println(s1 + " .equals " + s4+ ": " + s1.equals(s4));

//compareTo method of String

// Compare s1 and s2
// It should return 0 as they both have the same ASCII value

System.out.println(s1 + " .compareTo " + s2+ ": " + s1.compareTo(s2));

// Compare s1 and s3
// It should return -32 as they both have the different ASCII value

System.out.println(s1 + " .compareTo " + s3 + ": " + s1.compareTo(s3));

// Compare s3 and s2
// It should return 32 as they both have the different ASCII value

System.out.println(s3 + " .compareTo " + s2 + ": " + s3.compareTo(s2));

// Compare s1 and s4
// It should return 0 as they both have the same ASCII value

System.out.println(s1 + " .compareTo " + s4 + ": " + s1.compareTo(s4));

}
}

Output:

A .equals A: true
A .equals a: false
A .equals a: false
A .equals A: true
A .compareTo A: 0
A .compareTo a: -32
a .compareTo A: 32
A .compareTo A: 0

2. Write a java program to reverse each word of a given string.

Code:-
import java.util.Scanner;

public class Main


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

Scanner scanner = new Scanner(System.in);


System.out.print("Original string : ");
String originalStr = scanner.nextLine();
scanner.close();

String words[] = originalStr.split("\\s");


String reversedString = "";

for (int i = 0; i < words.length; i++)


{
String word = words[i];
String reverseWord = "";
for (int j = word.length() - 1; j >= 0; j--)
{
reverseWord = reverseWord + word.charAt(j);
}
reversedString = reversedString + reverseWord + " ";
}

System.out.print("Reversed string : " + reversedString);


}
}
2. Write a program to perform binary search on
integer array using Array class of java.

import java.util.Arrays;
public class Demo
{
public static void main(String[] args)
{
int arr[] = { 3, 9, 1, 6, 4};
Arrays.sort(arr);
System.out.print("The sorted array is: ");
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println();
int index1 = Arrays.binarySearch(arr, 6);
System.out.println("The integer 6 is at index " + index1);
int index2 = Arrays.binarySearch(arr, 7);
System.out.println("The integer 7 is at index " + index2);
}
}
4. Write a java program to count the total number of
occurrences of a given character in a string without
using any loop?

Code:

import java.util.Scanner;
class OccurenceOfCharInString
{
static void characterCount(String inputString)
{

HashMap<Character, Integer> charCountMap


= new HashMap<Character, Integer>();

// Converting given string to char array

char[] strArray = inputString.toCharArray();

// checking each char of strArray


for (char c : strArray) {
if (charCountMap.containsKey(c)) {
// If char is present in charCountMap,
// incrementing it's count by 1
charCountMap.put(c, charCountMap.get(c) + 1);
}
else {

// If char is not present in charCountMap,


// putting this char to charCountMap with 1 as it's value
charCountMap.put(c, 1);
}
}

for (Map.Entry entry : charCountMap.entrySet()) {


System.out.println(entry.getKey() + " " + entry.getValue());
}
}

public static void main(String[] args)


{
String str = "shrutika";
characterCount(str);
}
}
Output:
s1
h1
r1
u1
t1
I1
k1
a1

5. Write a program to copy and add all the elements of a


Vector to an array.
Code:-

import java.util.Vector;

public class Example {

public static void main(String[] args)


{
//create a Vector object
Vector v = new Vector();

//Add elements to Vector


v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
//declare an array to hold elements of Vector
Object[] objArray = new Object[5];
v.copyInto(objArray);

//display contents of Object array


System.out.println("Vector elements are copied into an Array.
Now Array Contains..");
for(int index=0; index < objArray.length ; index++)
System.out.println(objArray[index]);
}
}

Output:-

Vector elements are copied into an Array. Now Array Contains..


1
2
3
4
5

6. Write a program to get substring of a string.

Code:-
public class TestSubstring
{
public static void main(String args[])
{
String s="Shrutikaninawe";
System.out.println("Original String: " + s);
System.out.println("Substring starting from index 8: " +s.substring(8));//ninawe
System.out.println("Substring starting from index 0 to 6: "+s.substring(0,8)); //Shrutika
}
}

Output:
Original String: Shrutikaninawe
Substring starting from index 6: ninawe
Substring starting from index 0 to 6: ninawe

7. Write a program to Check whether an element exists in Vector or not.

import java.util.Vector;

public class Vector


{

public static void main(String[] args)


{

Vector<String> vColors = new Vector<String>();

vColors.add("Black");
vColors.add("Orange");
vColors.add("Yellow");

System.out.println( vColors.contains("Orange") );

System.out.println( vColors.contains("Red") );
}
}
Output

True
false

8. Write a program to search element in Vector using


index.
Code:-
import java.util.Vector;
public class SearchVector {
public static void main(String[] args) {
// Create a Vector object
Vector<String> vector = new Vector<String>();

//Add elements to Vector


vector.add("Kate");
vector.add("Patt");
vector.add("Kluge");
vector.add("Karon");
vector.add("Patt");
vector.add("Monica");
vector.add("Patt");

//This would return the index of first occurrence


int first_index = vector.indexOf("Patt");
System.out.println("First Occurrence of Patt at index:
"+first_index);

//This would return the index of last occurrence


int last_index = vector.lastIndexOf("Patt");
System.out.println("Last Occurrence of Patt at index:
"+last_index);

int after_index = vector.indexOf("Patt", 2);


System.out.println("Occurrence after index 2: "+after_index);

//This would search the element backward starting from index


6(inclusive)
int before_index = vector.lastIndexOf("Patt", 6);
System.out.println("Occurrence before index 6:
"+before_index);
}
}

Output:
First Occurrence of Patt at index: 1
Last Occurrence of Patt at index: 6
Occurrence after index 2: 4
Occurrence before index 6: 6
9. Write a java program to copy Elements of one Vector
to another and display elements of both the vectors.
Code:-
import java.util.Collections;
import java.util.Vector;
public class VectorCopyExample {
public static void main(String args[])
{
//First Vector of String type
Vector<String> va = new Vector<String>();
//Adding elements to the first Vector
va.add("AB");
va.add("BC");
va.add("CD");
va.add("DE");
//Second Vector
Vector<String> vb = new Vector<String>();
//Adding elements to the second Vector
vb.add("1st");
vb.add("2nd");
vb.add("3rd");
vb.add("4th");
vb.add("5th");
vb.add("6th");

/*Displaying the elements of second vector before


performing the copy operation*/
System.out.println("Vector vb before copy: "+vb);
//Copying all the elements of Vector va to Vector vb
Collections.copy(vb, va);

//Displaying elements after copy


System.out.println("Vector vb after copy: "+vb);
}
}

Output:
Vector vb before copy: [1st, 2nd, 3rd, 4th, 5th, 6th]
Vector vb after copy: [AB, BC, CD, DE, 5th, 6th]

10. Write a program to remove element from specified


index in Vector.
Code:-
import java.util.Vector;
public class GFG {
public static void main(String arg[])
{
// Create an Vector
Vector<Integer> vector = new Vector<>();

// Add elements in the vector


vector.add(10);
vector.add(20);
vector.add(30);
vector.add(20);
vector.add(40);

// display original vector


System.out.println("Values in vector: " + vector);
// remove 2 index element and store the value in r
int r = vector.remove((2));
// display removed element
System.out.println("Removed element: " + r);

// display vector after 2 index element


System.out.println("Values in vector: " + vector);
}
}

Output
Values in vector: [10, 20, 30, 20, 40]
Removed element: 30
Values in vector: [10, 20, 20, 40]

11. Write a program to convert Vector to ArrayList.


Code:-
import java.util.Vector;
import java.util.ArrayList;

public class GFG {

public static void main(String[] args)


{

// Create a Vector that contain strings

Vector<String> v = new Vector<String>();

// add values in vector


v.add("a");
v.add("b");
v.add("c");
v.add("d");
v.add("e");

// Display the Vector

System.out.println(" Vector : " + v);

// Convert Vector to ArrayList

ArrayList<String> Arrlist = new ArrayList<String>(v);

// Display ArrayList

System.out.println("\n ArrayList : " + Arrlist);

Output
Vector : [a, b, c, d, e]

ArrayList : [a, b, c, d, e]

12. Write a program to sort Vector using


Collections.sort().
Code:-
import java.util.Collections;
import java.util.Vector;
public class SortingVectorExample
{
public static void main(String[] args)
{

// Create a Vector
Vector<String> vector = new Vector<String>();

//Add elements to Vector


vector.add("Walter");
vector.add("Anna");
vector.add("Hank");
vector.add("Flynn");
vector.add("Tom");
System.out.println("Vector elements before sorting: ");
for(int i=0; i < vector.size(); i++){
//get(i) method fetches the element from index i
System.out.println(vector.get(i));
}

// Collection.sort() sorts the collection in ascending order


Collections.sort(vector);

//Display Vector elements after sorting using Collection.sort


System.out.println("Vector elements after sorting: :");
for(int i=0; i < vector.size(); i++){
System.out.println(vector.get(i));
}
}
}

Output:
Vector elements before sorting:
Walter
Anna
Hank
Flynn
Tom
Vector elements after sorting: :
Anna
Flynn
Hank
Tom
Walter

13. Write a program to replace an element in Vector.


Code:-
import java.util.Vector;
public class Sias {
public static void main(String args[])
{
try {

// create a instance vector


Vector<Integer> vector = new Vector<>();

// insert the values in vector


vector.add(1);
vector.add(2);
vector.add(3);
vector.add(4);
vector.add(5);
// display the vector
System.out.println("original vector : "
+ vector);

// call set() and replace 2 index value


vector.set(2, 10);

// display vector after replacing value


System.out.println("after replace the value : "
+ vector);
// call set() and replace 9th index value
// which is exception as arrayoutofbound
vector.set(9, 91);

// display vector after replacing value


System.out.println("after replace the value :”+ vector);
}
catch (Exception e) {
System.out.println(e);
}
}
}

Output
original vector : [1, 2, 3, 4, 5]
after replace the value : [1, 2, 10, 4, 5]
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 9
14. 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.
Code:-
import java.util.Scanner;

public class fibonacci {


public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);


int limit;
System.out.println("Enter how many Fibonacci number to
print");
limit = keyboard.nextInt();

System.out.println("The first " + limit + "Fibonacci numbers


are:");
for (int i=0; i < limit; i++){
System.out.println(fibonacci(i));
}
}

public static int fibonacci(int num) {


if (num == 0) {
return 0;
}
else if(num == 1)
{
return 1;
}

return fibonacci(num-1) + fibonacci(num-2);


}
}
Output:-
Enter how many Fibonacci number to print
10
The first 10Fibonacci numbers are:
0 1 1 2 3 5 8 13 21 34

15. Write a program to convert decimal to binary


equivalent using recursion.
Code:-
import java.util.Scanner;

class GFG
{
// Decimal to binary conversion
// using recursion
static int find(int decimal_number)
{
if (decimal_number == 0)
return 0;

else

return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}

// Driver Code
public static void main(String args[])
{
int decimal_number = 10;
System.out.println(find(decimal_number));
}

Output:-

1010

You might also like