Uploaded by Sam Alt

SRM Advance Programing Practice - week 5

advertisement
21CSC203P ADVANCED PROGRAMMING PRACTICE
Week 5 – Tutorial Assignment
Kotipalli Srikesh
RA22
1. Write a Java program (using function) to print the mirror image of the given string.
Code
import java.util.Scanner;
public class ReversedStringPrinter {
public static String getReversedString(String str) {
StringBuilder reversedStr = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
reversedStr.append(str.charAt(i));
}
return reversedStr.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
String reversedString = getReversedString(str);
System.out.println("The reversed string is: " + reversedString);
scanner.close();
}
}
Output
Enter a string: srikesh
The reversed string is: hsekirs
2.
Write a Java program (using function) to check if two strings are rotationally
equivalent.
Sample Output
string 1 is : srmist
string 2 is : tsrmis
Are two strings Rotationally equal ? : True
Code
import java.util.Scanner;
public class StringRotationEquivalenceChecker {
public static boolean isRotationallyEquivalent(String str1, String str2) {
if (str1.length() != str2.length()) {
return false;
}
String concatenatedStr = str1 + str1;
return concatenatedStr.contains(str2);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first string: ");
String str1 = scanner.nextLine();
System.out.print("Enter the second string: ");
String str2 = scanner.nextLine();
scanner.close();
System.out.println("String 1 is: " + str1);
System.out.println("String 2 is: " + str2);
boolean isRotationallyEquivalent = isRotationallyEquivalent(str1, str2);
System.out.println("Are the two strings rotationally equivalent? : " +
isRotationallyEquivalent);
}
}
Output
Enter the first string: srikesh
Enter the second string: srik
String 1 is: srikesh
String 2 is: srik
Are the two strings rotationally equivalent? : false
3. Write a Java program (using function) to print the even numbers from a given list.
(Note : function type - with arguments but no return values)
Code
import java.util.ArrayList;
import java.util.Scanner;
public class EvenNumberPrinterWithFunction {
public static void printEvenNumbers(ArrayList<Integer> numbers) {
System.out.println("Even numbers in the list:");
for (int num : numbers) {
if (num % 2 == 0) {
System.out.println(num);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numberList = new ArrayList<>();
System.out.print("Enter the number of elements in the list: ");
int n = scanner.nextInt();
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
int num = scanner.nextInt();
numberList.add(num);
}
scanner.close();
printEvenNumbers(numberList);
}
}
Output
Enter the number of elements in the list: 3
Enter the elements:
1
2
3
4
Even numbers in the list:
4
4. Write a Java function (using function) that checks whether a passed string is
palindrome or
not. (Note : function type - No arguments with return values)
Code:
import java.util.Scanner;
public class PalindromeCheckerWithFunction {
public static boolean isPalindrome() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
scanner.close();
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
public static void main(String[] args) {
boolean isPalindromic = isPalindrome();
if (isPalindromic) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}
}
}
Output
Enter a string: srikesh
The string is not a palindrome.
5. Write a Java function (using function) that checks whether a given number is prime
or not
(Note : function type - with arguments with return values)
Code
import java.util.Scanner;
public class PrimeNumberCheckerWithFunction {
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
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();
scanner.close();
boolean isNumberPrime = isPrime(num);
if (isNumberPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}
Output
Enter a number: 12
12 is not a prime number.
6. Write a Java program to find the digits which are absent in a given mobile number
(using
function)
Code
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class MissingDigitsInPhoneNumber {
public static Set<Integer> findAbsentDigits(String phoneNumber) {
Set<Integer> allDigits = new HashSet<>();
Set<Integer> presentDigits = new HashSet<>();
Set<Integer> absentDigits = new HashSet<>();
for (int i = 0; i < 10; i++) {
allDigits.add(i);
}
for (char digitChar : phoneNumber.toCharArray()) {
if (Character.isDigit(digitChar)) {
int digit = Character.getNumericValue(digitChar);
presentDigits.add(digit);
}
}
for (int digit : allDigits) {
if (!presentDigits.contains(digit)) {
absentDigits.add(digit);
}
}
return absentDigits;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a mobile number: ");
String phoneNumber = scanner.nextLine();
scanner.close();
Set<Integer> absentDigits = findAbsentDigits(phoneNumber);
if (absentDigits.isEmpty()) {
System.out.println("All digits are present in the phone number.");
} else {
System.out.print("Digits absent in the phone number:");
for (int digit : absentDigits) {
System.out.print(" " + digit);
}
System.out.println();
}
}
}
Output
Enter a mobile number: 248287381901
Digits absent in the phone number:5 6
7. Write a Java program using function that will return true if the two given integer
values are equal or their sum or difference is 5.
Code
import java.util.Scanner;
public class NumberConditionChecker {
public static boolean checkConditions(int value1, int value2) {
return value1 == value2 || value1 + value2 == 5 || Math.abs(value1 - value2) == 5;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second integer: ");
int num2 = scanner.nextInt();
scanner.close();
boolean meetsConditions = checkConditions(num1, num2);
if (meetsConditions) {
System.out.println("The conditions are met: the two integers are equal, or their
sum/difference is 5.");
} else {
System.out.println("The conditions are not met.");
}
}
}
Output
Enter the first integer: 123
Enter the second integer: 321
The conditions are not met.
8. Write a Java program using function to count the number of each character of a
given
text/string.
Code
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CharacterCounter {
public static Map<Character, Integer> countCharacters(String text) {
Map<Character, Integer> charCountMap = new HashMap<>();
for (char c : text.toCharArray()) {
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
return charCountMap;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a text or string: ");
String inputText = scanner.nextLine();
scanner.close();
Map<Character, Integer> charCounts = countCharacters(inputText);
System.out.println("Character counts:");
for (Map.Entry<Character, Integer> entry : charCounts.entrySet()) {
System.out.println("'" + entry.getKey() + "': " + entry.getValue());
}
}
}
Output
Enter a text or string: srikesh
Character counts:
'r': 1
's': 2
'e': 1
'h': 1
'i': 1
'k': 1
9. Write a Java program using function to print all Possible Combinations from the
three
Digits
Code
import java.util.Scanner;
public class ThreeDigitCombinationPrinter {
public static void printCombinations(int[] nums) {
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
for (int k = 0; k < nums.length; k++) {
System.out.println(nums[i] + "" + nums[j] + "" + nums[k]);
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] digits = new int[3];
for (int i = 0; i < 3; i++) {
System.out.print("Enter digit " + (i + 1) + ": ");
digits[i] = scanner.nextInt();
}
scanner.close();
printCombinations(digits);
}
}
Output
Enter digit 1: 2
Enter digit 2: 3
Enter digit 3: 4
222
223
224
232
233
234
242
243
244
322
323
324
332
333
334
342
343
344
422
423
424
432
433
434
442
443
444
10. Write a Java program using function to count unique values in an array of 15
elements.
Code
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class UniqueValueCounterProgram {
public static int countUniqueValues(int[] inputArray) {
Set<Integer> uniqueValues = new HashSet<>();
for (int num : inputArray) {
uniqueValues.add(num);
}
return uniqueValues.size();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] elements = new int[15];
for (int i = 0; i < 15; i++) {
System.out.print("Enter element " + (i + 1) + ": ");
elements[i] = scanner.nextInt();
}
scanner.close();
int uniqueCount = countUniqueValues(elements);
System.out.println("Number of unique values in the array: " + uniqueCount);
}
}
Output
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
Enter element 6: 6
Enter element 7: 7
Enter element 8: 8
Enter element 9: 9
Enter element 10: 10
Enter element 11: 11
Enter element 12: 12
Enter element 13: 31
Enter element 14: 23
Enter element 15: 12
Number of unique values in the array: 14
Download