[go: up one dir, main page]

0% found this document useful (0 votes)
14 views15 pages

APP Week - 5 Assignment Abiram

The document contains a programming assignment with ten Java programs that demonstrate various functionalities such as printing mirror images of strings, checking for rotational equivalence, printing even numbers from a list, checking for palindromes, determining prime numbers, finding missing digits in a mobile number, comparing integers, counting character occurrences, generating digit combinations, and counting unique values in an array. Each program includes code snippets, sample outputs, and explanations of their functionalities. The assignment is aimed at enhancing programming skills in Java.

Uploaded by

kpnx8cpmmk
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)
14 views15 pages

APP Week - 5 Assignment Abiram

The document contains a programming assignment with ten Java programs that demonstrate various functionalities such as printing mirror images of strings, checking for rotational equivalence, printing even numbers from a list, checking for palindromes, determining prime numbers, finding missing digits in a mobile number, comparing integers, counting character occurrences, generating digit combinations, and counting unique values in an array. Each program includes code snippets, sample outputs, and explanations of their functionalities. The assignment is aimed at enhancing programming skills in Java.

Uploaded by

kpnx8cpmmk
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
You are on page 1/ 15

ADVANCED PROGRAMMING

PRACTICE

ASSIGNMENT-5

RA2211026010486
G.Abiram
Z2-cse Aiml

PROGRAMS:

1.Writea Java program (using function) to print the mirror image of


the given string ?

Ans:

import java.util.Scanner;

public class MirrorImageString {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();

String mirrorImage = getMirrorImage(input);


System.out.println("Mirror Image: " + mirrorImage);

sc.close();
}

// Function to get the mirror image of a string


public static String getMirrorImage(String str) {
StringBuilder mirror = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
mirror.append(str.charAt(i));
}
return mirror.toString();
}
}

Output:
Enter a string: Hello
Mirror Image: olleH

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

Ans:

public class RotationallyEquivalentStrings {


public static void main(String[] args) {
String str1 = "srmist";
String str2 = "tsrmis";
boolean areRotationallyEquivalent =
areRotationallyEquivalentStrings(str1, str2);

System.out.println("String 1 is: " + str1);


System.out.println("String 2 is: " + str2);
System.out.println("Are two strings Rotationally equal? : " +
areRotationallyEquivalent);
}

public static boolean areRotationallyEquivalentStrings(String str1,


String str2) {
if (str1.length() != str2.length()) {
return false; // If the lengths are different, they can't be
rotationally equivalent.
}

String concatenated = str1 + str1; // Concatenate str1 with


itself.

// Check if str2 is a substring of the concatenated string.


return concatenated.contains(str2);
}
}

Output:

String 1 is: srmist


String 2 is: tsrmis
Are two strings Rotationally equal? : true

3.Write a Java program (using function) to print the even numbers


from a given list?
Ans:

import java.util.ArrayList;
import java.util.List;

public class PrintEvenNumbers {


public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);

System.out.println("Original list: " + numbers);


System.out.print("Even numbers: ");

printEvenNumbers(numbers);
}

public static void printEvenNumbers(List<Integer> numbers) {


for (Integer number : numbers) {
if (number % 2 == 0) {
System.out.print(number + " ");
}
}
System.out.println();
}
}

Output:
Original list: [1, 2, 3, 4, 5, 6]
Even numbers: 2 4 6

4.Write a Java function (using function) that checks whether a


passed string is palindrome or not.

Ans:

import java.util.Scanner;

public class PalindromeChecker {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();

checkPalindrome(input);

sc.close();
}

public static void checkPalindrome(String str) {


String original = str;
String reversed = new StringBuilder(str).reverse().toString();

if (original.equalsIgnoreCase(reversed)) {
System.out.println("The string '" + str + "' is a palindrome.");
} else {
System.out.println("The string '" + str + "' is not a
palindrome.");
}
}
}

Output:

Enter a string: racecar


The string 'racecar' is a palindrome.

5.Write a Java function (using function) that checks whether a given


number is prime or not

Ans:
import java.util.Scanner;

public class PrimeChecker {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();

boolean isPrime = isPrimeNumber(number);

if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}

sc.close();
}

public static boolean isPrimeNumber(int num) {


if (num <= 1) {
return false;
}

if (num <= 3) {
return true;
}

if (num % 2 == 0 || num % 3 == 0) {
return false;
}

for (int i = 5; i * i <= num; i += 6) {


if (num % i == 0 || num % (i + 2) == 0) {
return false;
}
}

return true;
}
}

Output:

Enter a number: 17
17 is a prime number.

6.Write a Java program to find the digits which are absent in a given
mobile number (using function) ?

Ans:

import java.util.Scanner;
public class MissingDigitsInMobileNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a mobile number: ");
String mobileNumber = sc.nextLine();

String missingDigits = findMissingDigits(mobileNumber);

if (missingDigits.isEmpty()) {
System.out.println("All digits are present in the mobile
number.");
} else {
System.out.println("Missing digits in the mobile number: " +
missingDigits);
}

sc.close();
}

public static String findMissingDigits(String mobileNumber) {


String allDigits = "0123456789";
StringBuilder missingDigits = new StringBuilder();

for (char digit : allDigits.toCharArray()) {


if (mobileNumber.indexOf(digit) == -1) {
missingDigits.append(digit);
}
}

return missingDigits.toString();
}
}
Output:

Enter a mobile number: 12345


Missing digits in the mobile number: 67890

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.

Ans:

import java.util.Scanner;

public class IntegerComparison {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int num1 = sc.nextInt();
System.out.print("Enter the second integer: ");
int num2 = sc.nextInt();

boolean result = checkIntegers(num1, num2);

if (result) {
System.out.println("The two integers meet the condition.");
} else {
System.out.println("The two integers do not meet the
condition.");
}

sc.close();
}

public static boolean checkIntegers(int num1, int num2) {


return (num1 == num2) || (num1 + num2 == 5) ||
(Math.abs(num1 - num2) == 5);
}
}

Output:

1. When the two integers are equal:

Enter the first integer: 3


Enter the second integer: 3
The two integers meet the condition.

2. When the sum of the two integers is 5:

Enter the first integer: 2


Enter the second integer: 3
The two integers meet the condition.

3. When the difference between the two integers is 5:

Enter the first integer: 10


Enter the second integer: 5
The two integers meet the condition.
4. When none of the conditions are met:

Enter the first integer: 2


Enter the second integer: 4
The two integers do not meet the condition.

8.Write a Java program using function to count the number of each


character of a given text/string.

Ans:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CharacterCounter {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a text/string: ");
String input = sc.nextLine();

Map<Character, Integer> charCount = countCharacters(input);

System.out.println("Character counts:");
for (Map.Entry<Character, Integer> entry :
charCount.entrySet()) {
System.out.println("'" + entry.getKey() + "': " +
entry.getValue());
}

sc.close();
}
public static Map<Character, Integer> countCharacters(String
text) {
Map<Character, Integer> charCount = new HashMap<>();

for (char character : text.toCharArray()) {


// Ignore whitespace and convert characters to lowercase for
case-insensitive counting
if (!Character.isWhitespace(character)) {
character = Character.toLowerCase(character);
charCount.put(character,
charCount.getOrDefault(character, 0) + 1);
}
}

return charCount;
}
}

Output:

Enter a text/string: Hello, World!


Character counts:
'h': 1
'e': 1
'l': 3
'o': 2
',': 1
'w': 1
'r': 1
'd': 1
'!': 1
9.Write a Java program using function to print all Possible
Combinations from the three Digits.

Ans:

public class ThreeDigitCombinations {


public static void main(String[] args) {
System.out.println("All Possible Combinations of Three
Digits:");

generateCombinations();
}

public static void generateCombinations() {


for (int digit1 = 0; digit1 <= 9; digit1++) {
for (int digit2 = 0; digit2 <= 9; digit2++) {
for (int digit3 = 0; digit3 <= 9; digit3++) {
System.out.println(digit1 + "" + digit2 + "" + digit3);
}
}
}
}
}

Output:

All Possible Combinations of Three Digits:


000
001
002
003
004
005
006
007
008
009
010
011
012
013
...
...
995
996
997
998
999

10.Write a Java program using function to count unique values in


an array of 15 elements

Ans:

import java.util.HashSet;
import java.util.Set;

public class UniqueValueCounter {


public static void main(String[] args) {
int[] array = {1, 2, 3, 2, 4, 5, 1, 6, 7, 8, 9, 3, 10, 11, 12};

int uniqueCount = countUniqueValues(array);

System.out.println("Number of unique values in the array: " +


uniqueCount);
}
public static int countUniqueValues(int[] array) {
Set<Integer> uniqueValues = new HashSet<>();

for (int element : array) {


uniqueValues.add(element);
}

return uniqueValues.size();
}
}

Output:

Number of unique values in the array: 12

You might also like