GENERICS☹
2.Question:
Problem Statement
Rohit, a new programmer, is fascinated by the world of coding and recently stumbled
upon an interesting challenge. He wants to use the flexibility of generics, wildcards, and
bounded types to reverse an array of integers.
Your job is to help Rohit use generics to create a versatile reverseArray method that can
handle different data types. Also, implement printArrayElements using wildcards to print
the reversed array.
Input format :
The first line of input consists of an integer n, representing the size of the array.
The second line consists of n space-separated integers, representing the elements of the
array.
Output format :
The program should output the reversed array on a single line, separated by spaces.
Refer to the sample output for formatting specifications.
Code constraints :
The given test case will fall under the following constraints:
1 ≤ n ≤ 15.
1 ≤ array element ≤ 100
Sample test cases :
Input 1 :
4
1234
Output 1 :
4321
Input 2 :
7
10 20 30 40 50 60 70
Output 2 :
70 60 50 40 30 20 10
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the
test case.
import java.util.*;
class generic{
public static <T> void reverses(T[] array){
int start=0;
int end=array.length-1;
while(start<end){
T temp=array[start];
array[start]=array[end];
array[end]=temp;
start++;
end--;
}
}
public static void printr(List<?> array){
for(Object element:array){
System.out.println(element+" ");
}
}}
public class StringLengthAnalyzer{
public static void main(String args){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
Integer[] num=new Integer[n];
for(int i=0;i<n;i++){
num[i]=s.nextInt();}
generic.reverses(num);
List<Integer> list=Arrays.asList(num);
generic.printr(list);}}
4. Problem Statement
Megna, an avid fitness enthusiast, seeks a program to track her daily workouts.
The program must allow her to input calorie burn rates. With the ability to specify
exercise counts, Megna desires a tool that computes and displays the total calories burned
during her workout sessions, aiding her to sum the calories and monitor her fitness
progress efficiently.
Write a program using a generic method to help Megna.
Input format :
The first line of input consists of an integer N, representing the number of workouts.
The second line consists of N space-separated integers, representing the calories burnt
during each workout.
Output format :
The output prints an integer, representing the total calories burnt.
Code constraints :
1 ≤ N ≤ 102
Sample test cases :
Input 1 :
3
142
Output 1 :
7
Input 2 :
6
11 23 15 26 19 28
Output 2 :
122
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the
test case.
Code:
import java.util.*;
public class UIControls{
public static <T extends Number> void workout(T[] array){
int sum=0;
for(T element:array){
sum+=element.intValue();
}
System.out.println(sum);}
public static void main(String[] args){
Scanner s=new Scanner(System.in);
int n;
n=s.nextInt();
Integer[] num=new Integer[n];
for(int i=0;i<n;i++){
num[i]=s.nextInt();}
workout(num);}}
5. Sarah is organizing her playlist for a road trip, consisting of n songs with unique IDs.
She wants an algorithm, using generics, to rearrange m segments within the playlist by
reversing the songs between specified start and end indices. The algorithm should modify
the playlist accordingly, maintain the song order outside these segments, and output the
updated playlist. Write a program to help her.
Input format :
The first line of the input consists of an integer, n, representing the size of the array.
The second line consists of n integers, representing the elements of the array.
Output format :
The first line of the output displays the original array
The second line displays the reversed integer array.
Refer to the sample output for the formatting specifications.
Code constraints :
1 <= n <= 100
1 <= array elements <= 100
Sample test cases :
Input 1 :
5
12345
Output 1 :
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
Input 2 :
3
987
Output 2 :
[9, 8, 7]
[7, 8, 9]
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the
test case.
import java.util.*;
class UIControls {
// Method to reverse the array and print it
public static <T> void reverse(T[] array) {
int start = 0;
int end = array.length - 1;
while (start < end) {
T temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
// Printing the reversed array
for (T e : array) {
System.out.print(e + " ");
}
System.out.println(); // To ensure a new line after printing
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int n = s.nextInt(); // Read the size of the array
Integer[] ar = new Integer[n];
for (int i = 0; i < n; i++) {
ar[i] = s.nextInt(); // Read each element of the array
}
// Printing the original array
System.out.print("[");
for (int i = 0; i < ar.length; i++) {
System.out.print(ar[i]);
if (i < ar.length - 1) System.out.print(" ");
}
System.out.println("]");
System.out.print("[");
// Call reverse to print the reversed array
reverse(ar);
System.out.print("]");
s.close();
}
}
6. You are given an array of integers and a target integer. Create a Java program that performs the
following operations using a generic class and interface:
1. Search for the target integer in the array and return the index of its first occurrence.
2. Count the total number of occurrences of the target integer in the array.
Input format :
The first line consists of an integer, representing the number of elements in the Integer
array.
The second line consists of space-separated integers representing the elements of the
Integer array.
The third line consists of an integer representing the target integer to search for in the
array.
Output format :
If the target integer is found in the array, the output prints the following:
1. The index of the first occurrence of the target integer in the array.
2. The total number of occurrences of the target integer in the array.
If the target integer is not found in the array: the output prints a message indicating that
the target integer was not found.
Refer to the sample outputs for the formatting specifications.
Code constraints :
The array elements must be positive integers.
Sample test cases :
Input 1 :
5
12345
3
Output 1 :
First occurrence of 3 found at index 2
Total occurrences of 3: 1
Input 2 :
4
10 20 30 40
5
Output 2 :
5 not found in the array.
Input 3 :
6
555555
5
Output 3 :
First occurrence of 5 found at index 0
Total occurrences of 5: 6
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the
test case.
import java.util.*;
interface Gen<T> {
int firstOccurrence(T[] array, T target);
int countOccurrence(T[] array, T target);
}
class Gene<T> implements Gen<T> {
public int firstOccurrence(T[] array, T target) {
for (int i = 0; i < array.length; i++) {
if (array[i].equals(target)) {
return i;
}
}
return -1;
}
public int countOccurrence(T[] array, T target) {
int count = 0;
for (T element : array) {
if (element.equals(target)) {
count++;
}
}
return count;
}
}
public class UIControls {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of elements:");
int n = s.nextInt();
Integer[] num = new Integer[n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
num[i] = s.nextInt();
}
System.out.println("Enter the target:");
int target = s.nextInt();
Gene<Integer> g = new Gene<>();
int first = g.firstOccurrence(num, target);
if (first != -1) {
System.out.println("First occurrence of " + target + " found at index " + first);
int count = g.countOccurrence(num, target);
System.out.println("Total occurrences of " + target + ": " + count);
} else {
System.out.println(target + " not found in the array.");
}
s.close();
}
}
1. Reverse and Print Array
Problem: Implement a generic method reverseArray to reverse an array of any type and
print the reversed array. Use wildcards in a separate method to print the array elements.
Input Format:
First line: Size of the array, n.
Second line: n space-separated elements of any type.
Output:
Reversed array elements in one line.