[Go to site: main page, start]

0% found this document useful (0 votes)
11 views8 pages

Java Programs: Basic to Advanced Examples

The document contains a series of Java programming exercises ranging from basic to advanced levels. It includes implementations for a simple calculator, string manipulations, array operations, sorting algorithms, and more. Each program demonstrates fundamental programming concepts and techniques in Java.

Uploaded by

chandeditz13
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Java Programs: Basic to Advanced Examples

The document contains a series of Java programming exercises ranging from basic to advanced levels. It includes implementations for a simple calculator, string manipulations, array operations, sorting algorithms, and more. Each program demonstrates fundamental programming concepts and techniques in Java.

Uploaded by

chandeditz13
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic to advanced java programs

part - 2
23. Implementing a Simple Calculator
import [Link];
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter first number: ");

double num1 = [Link]();

[Link]("Enter second number: ");

double num2 = [Link]();

[Link]("Enter operation (+, -, *, /): ");

char operation = [Link]().charAt(0);

double result;

switch (operation) {
case '+': result = num1 + num2; break;

case '-': result = num1 - num2; break;

case '*': result = num1 * num2; break;

case '/': result = num1 / num2; break;

default: throw new IllegalArgumentException("Invalid operation");


}
[Link]("Result: " + result);
}
}
24. Find the Length of a String
public class StringLength {

public static void main(String[] args) {

String str = "Automation";

[Link]([Link]());

}
} 25. Check if a String is Empty
public class CheckEmptyString {

public static void main(String[] args) {

String str = "";

[Link]([Link]());

}
}

26. Count the Occurrences of a Character in a String


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

String str = "Automation";

char ch = 'a';

int count = 0;

for (char c : [Link]()) {

if (c == ch) count++;

[Link](count);

}
}
27. Find the First Non-Repeated Character in a String
import [Link];

import [Link];

public class FirstNonRepeatedCharacter {

public static void main(String[] args) {

String str = "swiss";

Map<Character, Integer> charCount = new LinkedHashMap<>();

for (char c : [Link]()) {

[Link](c, [Link](c, 0) + 1);

for ([Link]<Character, Integer> entry : [Link]()) {

if ([Link]() == 1) {

[Link]([Link]());

break;

}
28. Remove All Whitespaces from a String
}
public class RemoveWhitespaces {
}
public static void main(String[] args) {

String str = " A u t o m a t i o n ";

String result = [Link]("\\s+", "");

[Link](result);

29. Find the Common Elements in Two Arrays


import [Link];

public class CommonElements {


public static void main(String[] args) {

int[] arr1 = {1, 2, 3, 4}; int[] arr2 = {3, 4, 5,


6}; HashSet<Integer> set = new HashSet<>
(); for (int num : arr1) {
[Link](num);

}
for (int num : arr2) {

if ([Link](num)) {

[Link](num + " ");


}
}
}
}

30. Find the Factorial of a Number using Recursion


public class FactorialRecursion {

public static void main(String[] args) {

int num = 5;

[Link](factorial(num));

static int factorial(int n) {

if (n == 0) return 1;
return n * factorial(n - 1);

}
}
31. Generate Random Numbers
import [Link];
public class RandomNumbers {
public static void main(String[] args) {

Random random = new Random();

for (int i = 0; i < 5; i++) {

[Link]([Link](100)); // Random number between 0-99


}
}
}

32. Check if a Year is Leap Year


public class LeapYear {

public static void main(String[] args) {

int year = 2024;

boolean isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

[Link](isLeap);
}
}

33. Find the Sum of First N Natural Numbers


public class SumOfNaturalNumbers {

public static void main(String[] args) {

int n = 10, sum = n * (n + 1) / 2;


[Link](sum);

}
}
34. Implement a Simple Login System
import [Link];
public class SimpleLogin {
public static void main(String[] args) {
String username = "admin";
String password = "password";
Scanner scanner = new Scanner([Link]);
[Link]("Enter username: ");

String inputUsername = [Link]();

[Link]("Enter password: ");

String inputPassword = [Link]();

if ([Link](inputUsername) && [Link](inputPassword)) {

[Link]("Login successful!");

} else {

[Link]("Login failed!");
}
}
}
35. Check if a String Contains Another String
public class StringContains {

public static void main(String[] args) {

String str1 = "Automation Testing";

String str2 = "Testing";

[Link]([Link](str2));
}
}

36. Find the Maximum Occurring Character in a String


import [Link];

public class MaxOccurringCharacter {


public static void main(String[] args) {

String str = "programming";

HashMap<Character, Integer> charCount = new HashMap<>();

for (char c : [Link]()) {

[Link](c, [Link](c, 0) + 1);

char maxChar = [Link](0);


int maxCount = 0;
for (char c : [Link]()) {

if ([Link](c) > maxCount) {


maxCount = [Link](c);
maxChar = c;

}
[Link](maxChar);

}
}
37. Implementing Bubble Sort
public class BubbleSort {
public static void main(String[] args) {

int[] arr = {64, 34, 25, 12, 22, 11, 90};

int n = [Link];

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

for (int num : arr) {

[Link](num + " ");


}
}
}
38. Implementing Selection Sort
public class SelectionSort {

public static void main(String[] args) {

int[] arr = {64, 25, 12, 22, 11};

int n = [Link];

for (int i = 0; i < n - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < n; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

int temp = arr[minIndex];

arr[minIndex] = arr[i]; 39. Find the Sum of Digits


arr[i] = temp;
of a Number
}
public class SumOfDigits {
for (int num : arr) { public static void main(String[] args) {
[Link](num + " "); int num = 12345, sum = 0;
} while (num != 0) {
} sum += num % 10;
} num /= 10;

[Link](sum);

}
}

You might also like