Java Assignment
Create an array with the values (1, 2, 3, 4, 5, 6, 7) and shuffle it
import java.util.Random;
public class NewArray {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7};
shuffleArray(array);
System.out.println("Shuffled Array:");
for (int element : array) {
System.out.print(element + " ");
}
}
public static void shuffleArray(int[] array) {
Random rand = new Random();
// using swapping of two numbers method can do this problem
for (int i = array.length - 1; i > 0; i--) {
int j = rand.nextInt(i + 1);
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Check if the input is pangram or not
import java.util.Scanner;
public class Pangram {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Pangram Checker");
System.out.print("Enter a sentence: ");
String input = scanner.nextLine();
scanner.close();
boolean isPangram = checkPangram(input);
if (isPangram) {
System.out.println("It's a pangram!");
} else {
System.out.println("It's not a pangram.");
}
}
public static boolean checkPangram(String input) {
input = input.toLowerCase();
String alphabet = "abcdefghijklmnopqrstuvwxyz";
int alphabetLength = alphabet.length();
for (int i = 0; i < alphabetLength; i++) {
char letter = alphabet.charAt(i);
if (!input.contains(String.valueOf(letter))) {
return false;
}
}
return true;
}
}
/*
THis program takes input and checks for every letter of
english aplhabet if it is present in the input then
it is pangram or else false
*/
Enter a Roman Number as input and convert it to an integer. (ex IX = 9)
import java.util.Scanner;
public class IntegerV {
public static int rToI(String s) {
int result = 0;
int PV = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char currentChar = s.charAt(i);
int CV = 0;
if (currentChar == 'I') {
CV = 1;
} else if (currentChar == 'V') {
CV = 5;
} else if (currentChar == 'X') {
CV = 10;
} else if (currentChar == 'L') {
CV = 50;
} else if (currentChar == 'C') {
CV = 100;
} else if (currentChar == 'D') {
CV = 500;
} else if (currentChar == 'M') {
CV = 1000;
}
if (CV < PV) {
result -= CV;
} else {
result += CV;
}
PV = CV;
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a Roman number: ");
String romanNumber = scanner.nextLine();
int integer = rToI(romanNumber);
System.out.println(romanNumber + " = " + integer);
scanner.close();
}
}