Wednesday, June 19, 2024
Introduction about web servers and web clients for class 10 in hindi
Saturday, June 8, 2024
Write a Java program to implement binary search on a sorted array
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the sorted elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.print("Enter the element to search: ");
int key = scanner.nextInt();
scanner.close();
int result = binarySearch(arr, key);
if (result == -1)
System.out.println("Element not present in the array.");
else
System.out.println("Element found at index: " + result);
}
public static int binarySearch(int[] arr, int key) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key)
return mid;
if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
}
Write a Java program to implement a simple singly linked list
public class LinkedList {
Node head;
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public void printList() {
Node n = head;
while (n != null) {
System.out.print(n.data + " ");
n = n.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
list.head.next = second;
second.next = third;
list.printList();
}
}
Write a Java program to find the largest element in an array
import java.util.Scanner;
public class LargestElement {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
scanner.close();
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("Largest element: " + max);
}
}
Write a Java program to check if a string is a palindrome.
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
scanner.close();
boolean isPalindrome = isPalindrome(str);
System.out.println("Is Palindrome: " + isPalindrome);
}
public static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true;
}
}
Write a java program to reverse a String.
The provided Java program reverses a string entered by the user. Let's break down the logic step by step:
Steps of the Program
1. Importing the Scanner Class:
```java
import java.util.Scanner;
```
This line imports the `Scanner` class, which is used to read input from the console.
2. Class Definition:
```java
public class ReverseString {
```
This line defines a public class named `ReverseString`.
3. Main Method:
```java
public static void main(String[] args) {
```
This is the entry point of the program. The `main` method is where the program execution begins.
4. Creating a Scanner Object:
Scanner scanner = new Scanner(System.in);
This line creates a `Scanner` object to read input from the standard input stream (usually the keyboard).
5. Prompting the User for Input:
System.out.print("Enter a string: ");
This line prints a prompt asking the user to enter a string.
6. Reading the User Input:
String str = scanner.nextLine();
This line reads a full line of input from the user and stores it in the variable `str`.
7. Closing the Scanner:
scanner.close();
This line closes the `Scanner` object to prevent resource leaks.
8. Reversing the String:
String reversedStr = new StringBuilder(str).reverse().toString();
This line performs the actual string reversal:
- `new StringBuilder(str)`: Creates a new `StringBuilder` object initialized with the string `str`.
- `.reverse()`: Calls the `reverse` method on the `StringBuilder` object, which reverses the sequence of characters.
- `.toString()`: Converts the reversed `StringBuilder` object back to a `String`.
9. Printing the Reversed String:
System.out.println("Reversed String: " + reversedStr);
This line prints the reversed string to the console.
Logic Breakdown
1. Input Handling: The program uses the `Scanner` class to read a string input from the user.
2. String Reversal:
- StringBuilder: The `StringBuilder` class is used because it has a built-in method `reverse()` that efficiently reverses the sequence of characters in the `StringBuilder` object.
- Conversion: The reversed sequence is converted back to a string using `toString()`.
3. Output: The reversed string is printed to the console.
Why Use StringBuilder?
- Efficiency: `StringBuilder` is mutable, meaning it can be modified without creating a new object for each modification. This makes it more efficient for operations like reversing a string, which involves multiple character manipulations.
- Convenience: The `StringBuilder` class provides a `reverse()` method that simplifies the reversal process compared to manually iterating through the string's characters and constructing the reversed string.
By utilizing `StringBuilder`, the code is both concise and efficient, making it a good choice for this task.
Complete java code:
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
scanner.close();
String reversedStr = new StringBuilder(str).reverse().toString();
System.out.println("Reversed String: " + reversedStr);
}
}
Write a Java program to generate the first n Fibonacci numbers
The Fibonacci series is a sequence of numbers in which each number (after the first two) is the sum of the two preceding ones. The sequence typically starts with 0 and 1, although some variations start with 1 and 1. The mathematical definition of the Fibonacci sequence is:
F(n)=F(n−1)+F(n−2)
With the initial conditions:
F(0)=0 F(1)=1
This means that:
F(2)=F(1)+F(0)=1+0=1 F(3)=F(2)+F(1)=1+1=2 F(4)=F(3)+F(2)=2+1=3 F(5)=F(4)+F(3)=3+2=5 …
So, the beginning of the Fibonacci series looks like this:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The Fibonacci sequence has many interesting properties and appears in various areas of mathematics and nature, such as in the branching of trees, the arrangement of leaves on a stem, the fruit sprouts of a pineapple, the flowering of an artichoke, and the arrangement of a pine cone.
java program sample code:
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();
scanner.close();
int a = 0, b = 1, c;
System.out.print("Fibonacci Series: " + a + " " + b);
for (int i = 2; i < n; i++) {
c = a + b;
System.out.print(" " + c);
a = b;
b = c;
}
}
}