Write a Java program that first sorts an integer array using bubble sort an_20241230_223135_0000
Write a Java program that first sorts an integer array using bubble sort an_20241230_223135_0000
and then performs binary search to find a given element within the
sorted array
import java.util.Arrays;
int x = 12;
int result = binarySearch(arr, x);
if (result != -1)
{
System.out.println("Element " + x + " is present at index " + result);
}
else
{
System.out.println("Element " + x + " is not present in array");
}
}
11
Create a Java program that decodes a string encoded with integer values,
where each integer represents the position of a letter in the alphabet
Public class StringDecoder
{
Public static String decode(String encodedString)
{
StringBuilder decodedString = new StringBuilder();
String[] indices = encodedString.split(“,”);
For (String indexStr : indices)
{
Try {
Int index = Integer.parseInt(indexStr);
If (index >= 1 && index <= 26)
{
Char decodedChar = (char) (‘a’ + index – 1);
decodedString.append(decodedChar);
}
else
{
decodedString.append(“?”); // Handle invalid indices
}
} catch (NumberFormatException e)
{
decodedString.append(“?”); // Handle non-numeric
characters
}
}
Return decodedString.toString();
}
Public static void main(String[] args)
{
String encodedString = “1,2,3,1,13,5,2,5”;
String decodedString = decode(encodedString);
System.out.println(“Encoded String: “ + encodedString);
System.out.println(“Decoded String: “ + decodedString);
}
12
}
Write a Java program to arrange all the
letters of a given string in alphabetical order
Import java.util.Arrays;
Public class SortStringAlphabetically
{
Public static void main(String[] args)
{
String inputString = “hello”;
// Convert the string to a character array
Char[] charArray = inputString.toCharArray();
// Sort the character array
Arrays.sort(charArray);
// Convert the sorted character array back to a string
String sortedString = new String(charArray);
System.out.println(“Input string: “ + inputString);
System.out.println(“Output string (alphabetical order): “ + sortedString);
}
}
13
Create a Java program that performs binary
search on an array using a recursive function
Public class BinarySearchRecursive
{
Public static int binarySearch(int[] arr, int low, int high, int x)
{
If (high >= low)
{
Int mid = low + (high – low) / 2;
// If the element is present at the middle itself
If (arr[mid] == x)
Return mid;
// If the element is smaller than mid, then it can only
// be present in left subarray
If (arr[mid] > x)
Return binarySearch(arr, low, mid – 1, x);
// Else the element can only be present in right subarray
Return binarySearch(arr, mid + 1, high, x);
}
// We reach here when the element is not present in array
Return -1;
}
Public static void main(String args[])
{
Int arr[] = { 2, 3, 4, 10, 40 };
Int x = 10;
Int n = arr.length;
Int result = binarySearch(arr, 0, n – 1, x);
If (result == -1)
System.out.println(“Element not present”);
Else
System.out.println(“Element found at index “ + result);
}
} 14
Create a Java program that uses recursion to convert a
binary number (represented as a string) to its decimal value
15
Create a Java program that checks
whether a number is a circular prime
Import java.util.Scanner;
Public class CircularPrime
{
Public static boolean isPrime(int num)
{
If (num <= 1)
{
Return false;
}
For (int I = 2; I <= Math.sqrt(num); i++)
{
If (num % I == 0)
{
Return false;
}
}
Return true;
}
Public static int rotate(int num)
{
Int lastDigit = num % 10;
Int remainingDigits = num / 10;
Int rotatedNum = lastDigit * (int) Math.pow(10, String.valueOf(remainingDigits).length()) + remainingDigits;
Return rotatedNum;
}
Public static boolean isCircularPrime(int num)
{
If (!isPrime(num))
{
Return false;
}
Int temp = num;
Int digits = String.valueOf(num).length();
For (int I = 0; I < digits – 1; i++)
{
Temp = rotate(temp);
If (!isPrime(temp))
{
Return false;
}
}
Return true;
}
Public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter a number: “);
Int num = scanner.nextInt();
If (isCircularPrime(num))
{
System.out.println(num + “ is a circular prime number.”);
}
else
{
System.out.println(num + “ is not a circular prime number.”);
}
Scanner.close();
} 16
}
Write a Java program to check if a given even
number satisfies the Goldbach conjecture
Public class GoldbachConjecture
{
Public static boolean isPrime(int num)
{
If (num <= 1)
{
Return false;
}
For (int I = 2; I * I <= num; i++)
{
If (num % I == 0)
{
Return false;
}
}
Return true;
}
Public static boolean isGoldbachNumber(int num)
{
If (num <= 2 || num % 2 != 0)
{
Return false;
}
For (int I = 2; I <= num / 2; i++)
{
If (isPrime(i) && isPrime(num – i))
{
Return true;
}
}
Return false;
}
Public static void main(String[] args)
{
Int evenNumber = 28; // Example even number
If (isGoldbachNumber(evenNumber))
{
System.out.println(evenNumber + “ is a Goldbach number.”);
}
else
{
System.out.println(evenNumber + “ is not a Goldbach number.”);
}
} 17
}
Create a Java program that displays the
current date and time in various formats
Import java.text.SimpleDateFormat;
Import java.util.Date;
Public class DateFormatExample
{
Public static void main(String[] args)
{
// Get current date and time
Date currentDate = new Date();
// Create SimpleDateFormat objects with different patterns
SimpleDateFormat dateFormat1 = new SimpleDateFormat(“yyyy-MM-dd”);
SimpleDateFormat dateFormat2 = new SimpleDateFormat(“dd/MM/yyyy”);
SimpleDateFormat dateFormat3 = new SimpleDateFormat(“MM/dd/yyyy”);
SimpleDateFormat dateFormat4 = new SimpleDateFormat(“E, MMM dd yyyy”);
SimpleDateFormat dateFormat5 = new SimpleDateFormat(“hh:mm:ss a”);
SimpleDateFormat dateFormat6 = new SimpleDateFormat(“HH:mm:ss”);
// Format the date using different patterns
String dateString1 = dateFormat1.format(currentDate);
String dateString2 = dateFormat2.format(currentDate);
String dateString3 = dateFormat3.format(currentDate);
String dateString4 = dateFormat4.format(currentDate);
String dateString5 = dateFormat5.format(currentDate);
String dateString6 = dateFormat6.format(currentDate);
// Print the formatted dates
System.out.println(“Date 1 (yyyy-MM-dd): “ + dateString1);
System.out.println(“Date 2 (dd/MM/yyyy): “ + dateString2);
System.out.println(“Date 3 (MM/dd/yyyy): “ + dateString3);
System.out.println(“Date 4 (E, MMM dd yyyy): “ + dateString4);
System.out.println(“Date 5 (hh:mm:ss a): “ + dateString5);
System.out.println(“Date 6 (HH:mm:ss): “ + dateString6);
}
}
18
Create a Java application that allows a user to enter the principal amount, annual
interest rate, and time period in years. The program should then calculate and
display both the simple interest and compound interest earned on the principal
import java.util.Scanner;
scanner.close();
}
}
19
Create a Java class named 'D2Point' to represent a point in a two-dimensional plane. The class
should have the following:
* Private instance variables for x and y coordinates.
* A constructor to initialize the x and y coordinates.
* Getter methods to access the x and y coordinates.
* Methods to add, subtract, and multiply (by a scalar) two D2Point objects.
* A method to calculate the distance between two D2Point objects.
* A toString() method to provide a string representation of the point
public class D2Point {
private double x;
private double y;
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
@Override
public double calculateArea() {
return width * height;
}
}
import java.util.LinkedList;
import java.util.Queue;
// Enqueue elements
queue.add(1);
queue.add(2);
queue.add(3);
// Dequeue elements
while (!queue.isEmpty()) {
System.out.println("Dequeued: " + queue.remove());
}
}
}
23