Pages

Monday, 7 March 2011

Sem 2 JAVA COLLECTION


/***************************************************************
NAME     :
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :

Accept ‘n’ integers from the user and store them in a collection. Display them in the sorted order. The collection should not accept duplicate elements. (Use a suitable collection).
***************************************************************/

import java.util.*;
import java.io.*;

class cola1
{
  public static void main(String[] args) throws Exception
  {
   int num,ele,i;
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   ArrayList al=new ArrayList();
   System.out.println("Enter the no for how many element to have:");
   num=Integer.parseInt(br.readLine());
   for(i=0;i<num;i++)
   {
     System.out.println("Enter the elements in position:"+i);
     ele=Integer.parseInt(br.readLine());
    al.add(ele);
   }
 
     System.out.println("The elements of ArrayList before:"+al);
     Collections.sort(al);
     System.out.println("The elements of ArrayList after sort:"+al);
}
}

/*****************************output****************************

[root@localhost ~]# java collection1
Enter the no for how many element to have:
5
Enter the elements in position:0
5
Enter the elements in position:1
3
Enter the elements in position:2
7
Enter the elements in position:3
2
Enter the elements in position:4
8
The elements of ArrayList before:[5, 3, 7, 2, 8]
The elements of ArrayList after sort:[2, 3, 5, 7, 8]

*****************************************************************************/


/***************************************************************
NAME     : 
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. : 

Construct a linked List containing name of colors: red, blue, yellow and orange. Then extend your program to do the following: 
i.Display the contents of the List using an Iterator; 
ii.Display the contents of the List in reverse order using a ListIterator; 
iii.Create another list containing  pink and  green. Insert the elements of this list between blue and yellow.
***************************************************************/

import java.util.*;

class collection2
{
  public static void main(String[] args)
  {
   LinkedList ll=new LinkedList();
   ll.add("Red");
   ll.add("Blue");
   ll.add("Yellow");
   ll.add("Orange");
   Iterator i=ll.iterator();
   System.out.println("contents of the List using an Iterator:");
   while(i.hasNext()) 
    {
     String s=(String)i.next();
     System.out.println(s);
    }
 ListIterator litr = ll.listIterator();
    while(litr.hasNext())
   {
     String elt = (String)litr.next();
   }
   System.out.println("contents of the List in reverse order using a ListIterator : ");
while(litr.hasPrevious())
    {
    System.out.println(litr.previous());
    }
   ll.add(2,"Pink");
     ll.add(3,"Green");
    System.out.println("list between blue and yellow is:");
    System.out.println(ll);
   }
}

/******************************output***************************

[root@localhost ~]# java collection2
contents of the List using an Iterator:
Red
Blue
Yellow
Orange
contents of the List in reverse order using a ListIterator : 
Orange
Yellow
Blue
Red
list between blue and yellow is:
[Red, Blue, Pink, Green, Yellow, Orange]

*******************************************************************************************/

/***************************************************************
NAME     : 
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. : 

Create a Hash table containing student name and percentage. Display the details of the hash table. Also search for a specific student and display percentage of that student.
***************************************************************/

import java.util.*;

class collection3
{
 public static void main(String[] args) throws Exception
  {
   Hashtable ht=new Hashtable();
   ht.put("Anand",66.2);
   ht.put("Anil",65.7);
   ht.put("Shashank",64.8);
   Enumeration keys=ht.keys();
   Enumeration values=ht.elements();
   while(keys. hasMoreElements())
     {
     while(values. hasMoreElements())
       {
        System.out.println("student name:"+keys.nextElement()+"\tpercentage:"+values.nextElement());
        }
      }
   System.out.println("specific student name is Anand & percentage is:"+ht.get("Anand"));
  }
}

/*****************************output****************************

[root@localhost ~]# java collection3
student name:Anand percentage:66.2
student name:Shashank percentage:64.8
student name:Anil percentage:65.7
specific student name is Anand & percentage is:66.2

**************************************************************************************************/


/***************************************************************
NAME     : 
CLASS    : T.Y.B.Sc(Comp.Sci.)
ROLL NO. : 
Implement Hash table to store ‘n’ records of students (Name, Percentage). Write a menu driven program to: 
 1.  Add student 
 2.  Display details of all students 
 3.  Search student 
 4.  Find out highest marks
***************************************************************/

import java.io.*;
import java.util.*;
public class collection4
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

Hashtable hashtable = new Hashtable();
String str, name = null;
int n=Integer.parseInt(br.readLine());
String nm="";
double per;
double max=0;
for(int i=0;i<n;i++)
{
System.out.println("Enter the Student Name?");
nm=br.readLine();
System.out.println("Enter the Student Percentage ?");
per=Double.parseDouble(br.readLine());
hashtable.put(nm,per);
}
int ch;
do
{
System.out.println("1:Add Stduent \n2:Display details of Student\n3:Serach\n4:Findout Highest Marks ");
    System.out.println("Enter the Choice");
    ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the Student Name?");
nm=br.readLine();
System.out.println("Enter the Student Percentage ?");
per=Double.parseDouble(br.readLine());
hashtable.put(nm,per);
break;
case 2:
System.out.println("Retriving all Name from the Hashtable");
Enumeration keys = hashtable.keys();
while( keys. hasMoreElements() )
System.out.println( keys.nextElement() );

System.out.println("Retriving all Percentage from the table");
Enumeration values = hashtable.elements();
while( values. hasMoreElements() )
System.out.println( values.nextElement() );
break;
case 3:
System.out.println("Enter the Student Name?");
String nm1=br.readLine();
if(hashtable.containsKey(nm1))
{
System.out.println("Name "+nm1+" is Present");
}
else
System.out.println("Name "+nm1+"\t Not Present");
break;
case 4:
keys = hashtable.keys();
values = hashtable.elements();
while( values. hasMoreElements() )
{
double per1=((Double)values.nextElement()).doubleValue();
nm1=(String)keys.nextElement();
if(per1>max){
max=per1;
name=nm1;
}
}
System.out.println("Max Percentage ="+max);
System.out.println("Name"+name);
}
}while(ch!=5);

}
}

/******************************output***************************

[root@localhost collection1]# java collection4 
2
Enter the Student Name?
Anand Warghad
Enter the Student Percentage ?
65
Enter the Student Name?
Anil lokhande
Enter the Student Percentage ?
70
1:Add Stduent 
2:Display details of Student
3:Serach
4:Findout Highest Marks 
Enter the Choice
1
Enter the Student Name?
Gaurav Kadam
Enter the Student Percentage ?
60
1:Add Stduent 
2:Display details of Student
3:Serach
4:Findout Highest Marks 
Enter the Choice
2
Retriving all Name from the Hashtable
Anil lokhande
Gaurav Kadam
Anand Warghad
Retriving all Percentage from the table
70.0
60.0
65.0
1:Add Stduent 
2:Display details of Student
3:Serach
4:Findout Highest Marks 
Enter the Choice
3
Enter the Student Name?
Anand Warghad
Name Anand Warghad is Present
1:Add Stduent 
2:Display details of Student
3:Serach
4:Findout Highest Marks 
Enter the Choice
4
Max Percentage =70.0
NameAnil lokhande
1:Add Stduent 
2:Display details of Student
3:Serach
4:Findout Highest Marks

***************************************************************/

1 comment: