Collection
/*
2. Construct a linked List containing names 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.*;
public class collection1
{
public static void main(String[] args)
{
LinkedList t=new LinkedList();
t.add("red");
t.add("blue");
t.add("yellow");
t.add("orange");
Iterator i=t.iterator();
System.out.println("The Elements Are");
while(i.hasNext())
System.out.println(i.next());
ListIterator i1=t.listIterator();
while(i1.hasNext())
i1.next();
System.out.println("The Elements In Reverse Order");
while(i1.hasPrevious())
System.out.println(i1.previous());
LinkedList t2=new LinkedList();
t2.add("pink");
t2.add("green");
i=t2.iterator();
t.add(2, i.next());
t.add(3, i.next());
Iterator i2=t.iterator();
System.out.println("The Total Elements Are");
while(i2.hasNext())
System.out.println(i2.next());
}
}
***************************************************************************
/*
Assignment no :4 seta2
2. 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.*;
import java.io.*;
import java.util.Map.Entry;
public class a2
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Hashtable hm=new Hashtable();
hm.put("Ganesh",new Double(74.89));
hm.put("Ramesh",new Double(64.89));
hm.put("Ram",new Double(55.89));
hm.put("Mahesh",new Double(84.89));
Set s=hm.entrySet();
Iterator i=s.iterator();
System.out.println("Name Percentage");
while(i.hasNext())
{
Map.Entry me=(Entry) i.next();
System.out.println(""+me.getKey()+" "+me.getValue());
}
System.out.println("Enter the student name for search");
String nm=br.readLine();
Iterator i1=s.iterator();
while(i1.hasNext())
{
Map.Entry me=(Entry) i1.next();
if(nm.equalsIgnoreCase((String) me.getKey()))
System.out.println("The percentage are:"+me.getValue());
}
}
}
/*
output:
Name Percentage
Ganesh 74.89
Mahesh 84.89
Ramesh 64.89
Ram 55.89
Enter the student name for search
ganesh
The percentage are:74.89
*/
**********************************************************************************
/*
1. 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.*;
public class a3
{
public static void main(String[] args) throws IOException
{
int n;
TreeSet t=new TreeSet();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no of elements");
n=Integer.parseInt(br.readLine());
Integer[] no=new Integer[n];
System.out.println("Enter the no");
for(int i=0;i<n;i++)
{
int a;
a=Integer.parseInt(br.readLine());
no[i]=new Integer(a);
t.add(no[i]);
}
Iterator it=t.iterator();
System.out.println("The Sorted data");
while(it.hasNext())
System.out.println(it.next());
}
}
********************************************************************************
/*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.util.*;
import java.io.*;
import java.util.Map.Entry;
class democomp implements Comparator
{
public int compare(Object o,Object o1)
{
Double a,b;
a=(Double)o;
b=(Double)o1;
return b.compareTo(a);
}
}
public class b1
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,cont;
String nm;
Double db;
double d;
HashMap hm=new HashMap();
System.out.println("Enter the no of student");
n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
System.out.println("Enter the name");
nm=br.readLine();
System.out.println("Enter the percentage");
d=Double.parseDouble(br.readLine());
db=new Double(d);
hm.put(nm,db);
}
do
{
System.out.println(" 1.Add Student \n 2.Display details of all students \n 3.Search result \n 4.Search highest marks");
System.out.println("Enter the choice");
int ch=Integer.parseInt(br.readLine());
Set s=hm.entrySet();
switch(ch)
{
case 1:
System.out.println("Enter the name");
nm=br.readLine();
System.out.println("Enter the percentage");
d=Double.parseDouble(br.readLine());
db=new Double(d);
hm.put(nm,db);
break;
case 2:
Iterator i=s.iterator();
System.out.println("Name Percentage");
while(i.hasNext())
{
Map.Entry me=(Entry) i.next();
System.out.println(""+me.getKey()+" "+me.getValue());
}
break;
case 3:
System.out.println("Enter the student name for search");
nm=br.readLine();
Iterator i1=s.iterator();
while(i1.hasNext())
{
Map.Entry me=(Entry) i1.next();
if(nm.equalsIgnoreCase((String) me.getKey()))
System.out.println("The percentage are:"+me.getValue());
}
break;
case 4:
democomp dc=new democomp();
TreeSet t=new TreeSet(dc);
Iterator i2=s.iterator();
while(i2.hasNext())
{
Map.Entry me=(Entry) i2.next();
t.add(me.getValue());
}
i2=t.iterator();
Double d5=(Double) i2.next();
i2=s.iterator();
while(i2.hasNext())
{
Map.Entry me=(Entry) i2.next();
if(d5.equals(me.getValue()))
System.out.println("Name is:"+me.getKey()+"\nThe percentage are:"+me.getValue());
}
break;
}
System.out.println("Do you want continue press 1");
cont=Integer.parseInt(br.readLine());
}while(cont==1);
}
}
*********************************************************************************
/*
list). Perform
a. Union
b. Intersection
c. Combining corresponding elements of the lists into a new list (only if they
are of the same size)
*/
import java.io.*;
import java.util.*;
public class b2
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int cont;
TreeSet t=new TreeSet();
t.add("ganesh");
t.add("ramesh");
t.add("ram");
t.add("mayur");
LinkedList l=new LinkedList(t);
TreeSet t1=new TreeSet();
t1.add("ganesh");
t1.add("mayur");
t1.add("ram");
t1.add("aditya");
LinkedList l1=new LinkedList(t1);
do
{
System.out.println(" 1.Union \n 2.Intersection \n 3.Combining the list");
System.out.println("Enter the choice");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
TreeSet t2=new TreeSet();
Iterator i=l.iterator();
while(i.hasNext())
t2.add(i.next());
i=l1.iterator();
while(i.hasNext())
t2.add(i.next());
LinkedList l2=new LinkedList(t2);
System.out.println("The Union of list\n"+l2);
break;
case 2:
int ic=0,ic1=0;
LinkedList l3=new LinkedList();
i=l1.iterator();
String[] nm=new String[10];
while(i.hasNext())
{
nm[ic]=(String) i.next();
ic++;
}
i=l.iterator();
String[] nm1=new String[10];
while(i.hasNext())
{
nm1[ic1]=(String) i.next();
ic1++;
}
for(int k=0;k<ic;k++)
{
for(int s=0;s<ic1;s++)
{
if(nm[k].equals(nm1[s]))
l3.add(nm[k]);
}
}
System.out.println("The intersection of list\n"+l3);
break;
case 3:
if(l.size()==l1.size())
{
int ic2=0,ic3=0;
LinkedList l5=new LinkedList();
i=l1.iterator();
String[] nm2=new String[10];
while(i.hasNext())
{
nm2[ic2]=(String) i.next();
ic2++;
}
i=l.iterator();
String[] nm3=new String[10];
while(i.hasNext())
{
nm3[ic3]=(String) i.next();
ic3++;
}
String comb[]=new String[ic2];
for(int k=0;k<ic2;k++)
{
comb[k]=""+nm2[k]+" "+nm3[k];
l5.add(comb[k]);
}
System.out.println("The combination of two list\n"+l5);
}
else
System.out.println("The size of linked list are not same");
break;
}
System.out.println("Do you want continue press 1");
cont=Integer.parseInt(br.readLine());
}while(cont==1);
}
}
****************************************************************************
/*Read a text file, specified by the first command line argument, into a list. The
program should then display a menu which performs the following operations on the list:
1. Insert line 2. Delete line 3. Append line 4. Modify line 5. Exit
When the user selects Exit, save the contents of the list to the file and end the program.
*/
import java.io.*;
import java.util.*;
public class c1
{
public static void main(String[] args)throws Exception
{
try
{
String filename=args[0];
String line;
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
LinkedList l=new LinkedList();
FileReader f=new FileReader(filename);
BufferedReader br=new BufferedReader(f);
do
{
line=br.readLine();
if(line==null)
break;
l.add(line);
}while(true);
br.close();
do
{
System.out.println(" 1.Insert line \n 2.Delete line \n 3.Append line \n 4.Modify line \n 5.Exit");
System.out.println("Enter the choice");
int ch=Integer.parseInt(br1.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the po");
int po=Integer.parseInt(br1.readLine());
System.out.println("Enter the line");
String line1=br1.readLine();
l.add(po-1, line1);
break;
case 2:
System.out.println("Enter the po");
int po1=Integer.parseInt(br1.readLine());
l.remove(po1-1);
break;
case 3:
System.out.println("Enter the po");
int po2=Integer.parseInt(br1.readLine());
System.out.println("Enter the line");
String line2=br1.readLine();
l.add(po2, line2);
break;
case 4:
System.out.println("Enter the po");
int po3=Integer.parseInt(br1.readLine());
System.out.println("Enter the line");
String line3=br1.readLine();
l.set(po3-1, line3);
break;
case 5:
FileWriter fw=new FileWriter(filename);
PrintWriter pr=new PrintWriter(fw);
ListIterator li=l.listIterator();
while(li.hasNext())
{
pr.println(li.next());
}
pr.close();
System.exit(0);
break;
}
}while(true);
}catch(Exception e){}
}
}
/*
2. Construct a linked List containing names 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.*;
public class collection1
{
public static void main(String[] args)
{
LinkedList t=new LinkedList();
t.add("red");
t.add("blue");
t.add("yellow");
t.add("orange");
Iterator i=t.iterator();
System.out.println("The Elements Are");
while(i.hasNext())
System.out.println(i.next());
ListIterator i1=t.listIterator();
while(i1.hasNext())
i1.next();
System.out.println("The Elements In Reverse Order");
while(i1.hasPrevious())
System.out.println(i1.previous());
LinkedList t2=new LinkedList();
t2.add("pink");
t2.add("green");
i=t2.iterator();
t.add(2, i.next());
t.add(3, i.next());
Iterator i2=t.iterator();
System.out.println("The Total Elements Are");
while(i2.hasNext())
System.out.println(i2.next());
}
}
***************************************************************************
/*
Assignment no :4 seta2
2. 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.*;
import java.io.*;
import java.util.Map.Entry;
public class a2
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Hashtable hm=new Hashtable();
hm.put("Ganesh",new Double(74.89));
hm.put("Ramesh",new Double(64.89));
hm.put("Ram",new Double(55.89));
hm.put("Mahesh",new Double(84.89));
Set s=hm.entrySet();
Iterator i=s.iterator();
System.out.println("Name Percentage");
while(i.hasNext())
{
Map.Entry me=(Entry) i.next();
System.out.println(""+me.getKey()+" "+me.getValue());
}
System.out.println("Enter the student name for search");
String nm=br.readLine();
Iterator i1=s.iterator();
while(i1.hasNext())
{
Map.Entry me=(Entry) i1.next();
if(nm.equalsIgnoreCase((String) me.getKey()))
System.out.println("The percentage are:"+me.getValue());
}
}
}
/*
output:
Name Percentage
Ganesh 74.89
Mahesh 84.89
Ramesh 64.89
Ram 55.89
Enter the student name for search
ganesh
The percentage are:74.89
*/
**********************************************************************************
/*
1. 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.*;
public class a3
{
public static void main(String[] args) throws IOException
{
int n;
TreeSet t=new TreeSet();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no of elements");
n=Integer.parseInt(br.readLine());
Integer[] no=new Integer[n];
System.out.println("Enter the no");
for(int i=0;i<n;i++)
{
int a;
a=Integer.parseInt(br.readLine());
no[i]=new Integer(a);
t.add(no[i]);
}
Iterator it=t.iterator();
System.out.println("The Sorted data");
while(it.hasNext())
System.out.println(it.next());
}
}
********************************************************************************
/*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.util.*;
import java.io.*;
import java.util.Map.Entry;
class democomp implements Comparator
{
public int compare(Object o,Object o1)
{
Double a,b;
a=(Double)o;
b=(Double)o1;
return b.compareTo(a);
}
}
public class b1
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,cont;
String nm;
Double db;
double d;
HashMap hm=new HashMap();
System.out.println("Enter the no of student");
n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
System.out.println("Enter the name");
nm=br.readLine();
System.out.println("Enter the percentage");
d=Double.parseDouble(br.readLine());
db=new Double(d);
hm.put(nm,db);
}
do
{
System.out.println(" 1.Add Student \n 2.Display details of all students \n 3.Search result \n 4.Search highest marks");
System.out.println("Enter the choice");
int ch=Integer.parseInt(br.readLine());
Set s=hm.entrySet();
switch(ch)
{
case 1:
System.out.println("Enter the name");
nm=br.readLine();
System.out.println("Enter the percentage");
d=Double.parseDouble(br.readLine());
db=new Double(d);
hm.put(nm,db);
break;
case 2:
Iterator i=s.iterator();
System.out.println("Name Percentage");
while(i.hasNext())
{
Map.Entry me=(Entry) i.next();
System.out.println(""+me.getKey()+" "+me.getValue());
}
break;
case 3:
System.out.println("Enter the student name for search");
nm=br.readLine();
Iterator i1=s.iterator();
while(i1.hasNext())
{
Map.Entry me=(Entry) i1.next();
if(nm.equalsIgnoreCase((String) me.getKey()))
System.out.println("The percentage are:"+me.getValue());
}
break;
case 4:
democomp dc=new democomp();
TreeSet t=new TreeSet(dc);
Iterator i2=s.iterator();
while(i2.hasNext())
{
Map.Entry me=(Entry) i2.next();
t.add(me.getValue());
}
i2=t.iterator();
Double d5=(Double) i2.next();
i2=s.iterator();
while(i2.hasNext())
{
Map.Entry me=(Entry) i2.next();
if(d5.equals(me.getValue()))
System.out.println("Name is:"+me.getKey()+"\nThe percentage are:"+me.getValue());
}
break;
}
System.out.println("Do you want continue press 1");
cont=Integer.parseInt(br.readLine());
}while(cont==1);
}
}
*********************************************************************************
/*
list). Perform
a. Union
b. Intersection
c. Combining corresponding elements of the lists into a new list (only if they
are of the same size)
*/
import java.io.*;
import java.util.*;
public class b2
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int cont;
TreeSet t=new TreeSet();
t.add("ganesh");
t.add("ramesh");
t.add("ram");
t.add("mayur");
LinkedList l=new LinkedList(t);
TreeSet t1=new TreeSet();
t1.add("ganesh");
t1.add("mayur");
t1.add("ram");
t1.add("aditya");
LinkedList l1=new LinkedList(t1);
do
{
System.out.println(" 1.Union \n 2.Intersection \n 3.Combining the list");
System.out.println("Enter the choice");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
TreeSet t2=new TreeSet();
Iterator i=l.iterator();
while(i.hasNext())
t2.add(i.next());
i=l1.iterator();
while(i.hasNext())
t2.add(i.next());
LinkedList l2=new LinkedList(t2);
System.out.println("The Union of list\n"+l2);
break;
case 2:
int ic=0,ic1=0;
LinkedList l3=new LinkedList();
i=l1.iterator();
String[] nm=new String[10];
while(i.hasNext())
{
nm[ic]=(String) i.next();
ic++;
}
i=l.iterator();
String[] nm1=new String[10];
while(i.hasNext())
{
nm1[ic1]=(String) i.next();
ic1++;
}
for(int k=0;k<ic;k++)
{
for(int s=0;s<ic1;s++)
{
if(nm[k].equals(nm1[s]))
l3.add(nm[k]);
}
}
System.out.println("The intersection of list\n"+l3);
break;
case 3:
if(l.size()==l1.size())
{
int ic2=0,ic3=0;
LinkedList l5=new LinkedList();
i=l1.iterator();
String[] nm2=new String[10];
while(i.hasNext())
{
nm2[ic2]=(String) i.next();
ic2++;
}
i=l.iterator();
String[] nm3=new String[10];
while(i.hasNext())
{
nm3[ic3]=(String) i.next();
ic3++;
}
String comb[]=new String[ic2];
for(int k=0;k<ic2;k++)
{
comb[k]=""+nm2[k]+" "+nm3[k];
l5.add(comb[k]);
}
System.out.println("The combination of two list\n"+l5);
}
else
System.out.println("The size of linked list are not same");
break;
}
System.out.println("Do you want continue press 1");
cont=Integer.parseInt(br.readLine());
}while(cont==1);
}
}
****************************************************************************
/*Read a text file, specified by the first command line argument, into a list. The
program should then display a menu which performs the following operations on the list:
1. Insert line 2. Delete line 3. Append line 4. Modify line 5. Exit
When the user selects Exit, save the contents of the list to the file and end the program.
*/
import java.io.*;
import java.util.*;
public class c1
{
public static void main(String[] args)throws Exception
{
try
{
String filename=args[0];
String line;
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
LinkedList l=new LinkedList();
FileReader f=new FileReader(filename);
BufferedReader br=new BufferedReader(f);
do
{
line=br.readLine();
if(line==null)
break;
l.add(line);
}while(true);
br.close();
do
{
System.out.println(" 1.Insert line \n 2.Delete line \n 3.Append line \n 4.Modify line \n 5.Exit");
System.out.println("Enter the choice");
int ch=Integer.parseInt(br1.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the po");
int po=Integer.parseInt(br1.readLine());
System.out.println("Enter the line");
String line1=br1.readLine();
l.add(po-1, line1);
break;
case 2:
System.out.println("Enter the po");
int po1=Integer.parseInt(br1.readLine());
l.remove(po1-1);
break;
case 3:
System.out.println("Enter the po");
int po2=Integer.parseInt(br1.readLine());
System.out.println("Enter the line");
String line2=br1.readLine();
l.add(po2, line2);
break;
case 4:
System.out.println("Enter the po");
int po3=Integer.parseInt(br1.readLine());
System.out.println("Enter the line");
String line3=br1.readLine();
l.set(po3-1, line3);
break;
case 5:
FileWriter fw=new FileWriter(filename);
PrintWriter pr=new PrintWriter(fw);
ListIterator li=l.listIterator();
while(li.hasNext())
{
pr.println(li.next());
}
pr.close();
System.exit(0);
break;
}
}while(true);
}catch(Exception e){}
}
}
No comments:
Post a Comment