Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Showing posts with label Generics. Show all posts
Showing posts with label Generics. Show all posts

Tuesday, 9 April 2019

Generic Storing of Number

Question:
Write a Java program the implements a generic sort method using any of the sorting techniques.

Solution:

public class Students{

   public static <T extends Comparable<T>> void sortingMethod(Number[] i2)   {

    int i,j,n;

    n=i2.length;

    for(i=0;i<n;i++)

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

      if(((Comparable<T>) i2[j]).compareTo((T) i2[j+1])>0)

      {

       T temp;

       temp=(T) i2[j];

       i2[j]=i2[j+1];

       i2[j+1]=(Number) temp;

      }

    System.out.println();

    System.out.println("After Sorting The Elements");

    for(Number element: i2)

    System.out.printf("%s, ", element);

   }

   public static void main(String args[])

   {

    Number i[]={3.2,2.5,10.02,7.012,7.06};

    System.out.println("Before Sorting The Elements");

    for(Number element:i)

     System.out.printf("%s, ",element);

    sortingMethod(i);

   }



}





Output:

Before Sorting The Elements

3.2, 2.5, 10.02, 7.012, 7.06, 

After Sorting The Elements

2.5, 3.2, 7.012, 7.06, 10.02, 



Credits: Gaurav

Thursday, 7 March 2019

Sixth Week Term Work Program 3 [ IA-1 2 B) ]

Question:
Write a Java program the implements a generic sort method using any of the sorting techniques.

public class GenericSorting {
//Modification: Sorting Wrt To String Length.
public static <T extends Comparable<T>> void sortingMethod1(T array[])
   {
   int i,j,n;
   n=array.length;
   for(i=0;i<n;i++)
   for(j=0;j<n-i-1;j++)
   if((((String) array[j]).length())>((String) array[j+1]).length())
   {
   T temp;
   temp=array[j];
   array[j]=array[j+1];
   array[j+1]=temp;
   }
   
   for(T element: array)
  System.out.printf("%s ", element);
   System.out.println();
   }

   public static <T extends Comparable<T>> void sortingMethod(T array[])
   {
   int i,j,n;
   n=array.length;
   for(i=0;i<n;i++)
   for(j=0;j<n-i-1;j++)
   if(array[j].compareTo(array[j+1])>0)
   {
   T temp;
   temp=array[j];
   array[j]=array[j+1];
   array[j+1]=temp;
   }
   
   for(T element: array)
  System.out.printf("%s ", element);
   System.out.println();
   }
   public static void main(String args[])
   {
   Integer i[]={3,2,1,7,6};
           String s[]={"pear","orange","apple"};
   sortingMethod(i);
 sortingMethod(s);
 
 System.out.println("\nSorting As per String Length");
 sortingMethod1(s);
 
   }
}

Output:
1 2 3 6 7 
apple orange pear 

Sorting As per String Length

pear apple orange 

Blue Marked Are Modification Asked By Ma'am In B1 Batch.

Sixth Week Term Work Program 1 [ IA-1 3 A) ]

Question:
 Write a Java program to create the below-mentioned Student record using appropriate
Collection class to perform the following operations on a single student:
a. Add a student
b. Display all the details of the student
c. Delete/remove student record
Members of the class Student is given below:

Student
name
USN
division
CGPA


Solution:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class SingleStudentRecord {
 ArrayList<Comparable> stud=new   ArrayList<Comparable>(4);
static Scanner in=new Scanner(System.in);
public void EnterRecord() {
System.out.print("Enter The Name:");
stud.add(in.next());
System.out.println();
System.out.print("Enter The USN:");
stud.add(in.next());
System.out.println();
System.out.print("Enter The Division:");
stud.add(in.next());
System.out.println();
System.out.print("Enter The CGPA:");
stud.add(in.nextFloat());
System.out.println();
}
public void DisplayRecord() {
Iterator<Comparable> itr=stud.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}        

}
public void DeleteRecord() {
System.out.println("Deleted The Student Record");
stud.removeAll(stud);
}
public static void main(String[] args) {
SingleStudentRecord s=new SingleStudentRecord();
int choice;
int i=0;
    while (true) {
        System.out.println("\n\n*******************STUDENT RECORD*******************\n");
        System.out.println("1. Enter Student Record");
        System.out.println("2. Display Record");
        System.out.println("3. Delete Record");
        System.out.println("4. Exit");
        System.out.println("Enter choice: ");
        choice = in.nextInt();
        switch (choice) {
        case 1: if(i==0)
        {
        s.EnterRecord();
        i++;
        }
        else {
        System.out.println("U Can Store Only One Student Record");
        }
                break;
        case 2:
        if(i==1)
        {
        s.DisplayRecord();
        }
        else {
        System.out.println("No Record Exists");
        }
        break;
        case 3:if(i==1) {
        s.DeleteRecord();
        i--;
        }
        else {
        System.out.println("No Record To delete");
        }
        break;
        case 4:
        System.out.println("THANKS FOR VISTING");
        System.exit(0);
        break;
        default:
        System.out.println("Enter A Valid Option");

}
}
}
}

Output:

**********STUDENT RECORD**********

1. Enter Student Record
2. Display Record
3. Delete Record
4. Exit
Enter choice: 
1
Enter The Name:Amit

Enter The USN:2Sd16cs012

Enter The Division:B

Enter The CGPA:8.4


Enter choice: 
2
Amit
2Sd16cs012
B
8.4


Enter choice: 
3
Deleted The Student Record


Enter choice: 
4
THANKS FOR VISTING