Professional Documents
Culture Documents
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)
{
// 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 s4
// It should return true as they both have the same content
System.out.println(s1 + " .equals " + s4+ ": " + s1.equals(s4));
// Compare s1 and s2
// It should return 0 as they both have the same ASCII value
// Compare s1 and s3
// It should return -32 as they both have the different ASCII value
// Compare s3 and s2
// It should return 32 as they both have the different ASCII value
// Compare s1 and s4
// It should return 0 as they both have the same ASCII value
}
}
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
Code:-
import java.util.Scanner;
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)
{
import java.util.Vector;
Output:-
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
import java.util.Vector;
vColors.add("Black");
vColors.add("Orange");
vColors.add("Yellow");
System.out.println( vColors.contains("Orange") );
System.out.println( vColors.contains("Red") );
}
}
Output
True
false
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");
Output:
Vector vb before copy: [1st, 2nd, 3rd, 4th, 5th, 6th]
Vector vb after copy: [AB, BC, CD, DE, 5th, 6th]
Output
Values in vector: [10, 20, 30, 20, 40]
Removed element: 30
Values in vector: [10, 20, 20, 40]
// Display ArrayList
Output
Vector : [a, b, c, d, e]
ArrayList : [a, b, c, d, e]
// Create a Vector
Vector<String> vector = new Vector<String>();
Output:
Vector elements before sorting:
Walter
Anna
Hank
Flynn
Tom
Vector elements after sorting: :
Anna
Flynn
Hank
Tom
Walter
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;
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