SET A
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). Search for an particular element using predefined search method in the Collection framework.
import java.io.*;import java.util.*;
class Tree{ public static void main(String args[])throws IOException { Scanner sc =new Scanner (System.in); int n,i; System.out.println("Enter how many numbers :"); n=sc.nextInt(); TreeSet ts=new TreeSet(); for(i=0;i<n;i++) { int x=sc.nextInt(); ts.add (new Integer(x)); } System.out.println("Tree set is :"+ts); System.out.println("enter element to be search :"); int y =sc.nextInt(); int flag=0; for(i=0;i<n;i++) { if(ts.contains(new Integer(y))) { System.out.println("Element found"); flag =1 ; break; } else flag=0; } if(flag==0) System.out.println("Element not found ."); }}
import java.io.*;
import java.util.*;
class Tree
{
public static void main(String args[])throws IOException
{
Scanner sc =new Scanner (System.in);
int n,i;
System.out.println("Enter how many numbers :");
n=sc.nextInt();
TreeSet ts=new TreeSet();
for(i=0;i<n;i++)
{
int x=sc.nextInt();
ts.add (new Integer(x));
}
System.out.println("Tree set is :"+ts);
System.out.println("enter element to be search :");
int y =sc.nextInt();
int flag=0;
for(i=0;i<n;i++)
{
if(ts.contains(new Integer(y)))
{
System.out.println("Element found");
flag =1 ;
break;
}
else
flag=0;
}
if(flag==0)
System.out.println("Element not found .");
}
}
2. Construct a linked List containing names of colors: red, blue, yellow and orange. Thenextend 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.*;
import java.io.*;
class Link
{
public static void main(String args[])
{
LinkedList l1=new LinkedList();
l1.add("Red");
l1.add("Blue");
l1.add("Yellow");
l1.add("Orange");
System.out.println("Contents of original linked list :"+l1);
System.out.println("Contents of list using iterator : ");
Iterator itr=l1.iterator();
while(itr.hasNext())
{
Object ele=itr.next();
System.out.println(ele+" ");
}
System.out.println("Contents of list in reverse order : ");
ListIterator ltr=l1.listIterator();
while(ltr.hasNext())
{
ltr.next();
}
while(ltr.hasPrevious())
{
Object ele=ltr.previous();
System.out.println(ele+" ");
}
LinkedList l2=new LinkedList();
l2.add("Pink");
l2.add("Green");
l1.add(2,l2);
System.out.println(" Contents of Linked list after insertion: "+l1);
}
}
3. 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.io.*;
import java.util.*;
class Hash
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Hashtable ht= new Hashtable();
String name=null;
float per;
System.out.println("Enter how many records :");
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
System.out.println("Enter student name:");
name=br.readLine();
System.out.println("Enter student percentage:");
per=Float.parseFloat(br.readLine());
ht.put(name,per);
}
System.out.println("Hash table contents are :"+ht);
Enumeration keys=ht.keys();
Enumeration values=ht.elements();
while(keys.hasMoreElements())
{
System.out.println(keys.nextElement()+"\t"+values.nextElement());
}
//Search student
System.out.println("Enter student name for search:");
String sm=br.readLine();
boolean ans=ht.containsKey(sm);
if(ans==true)
System.out.println("Percentage :"+ht.get(sm));
else
System.out.println("Student not found:");
}
}
SET B
1. Create a java application to store city names and their STD codes using an appropriate
collection. The GUI ahould allow the following operations:
i. Add a new city and its code (No duplicates)
ii. Remove a city from the collection
iii. Search for a cityname and display the code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class city extends JFrame implements ActionListener
{
JTextField t1,t2,t3;
JButton b1,b2,b3;
JTextArea t;
JPanel p1,p2;
Hashtable ts;
city()
{
ts=new Hashtable();
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
b1=new JButton("Add");
b2=new JButton("Search");
b3=new JButton("Remove");
t=new JTextArea(20,20);
p1=new JPanel();
p1.add(t);
p2= new JPanel();
p2.setLayout(new GridLayout(2,3));
p2.add(t1);p2.add(t2);p2.add(b1);
p2.add(t3);
p2.add(b2);
p2.add(b3);
add(p1);
add(p2);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setLayout(new FlowLayout());
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(b1==e.getSource())
{
String name = t1.getText();
int code = Integer.parseInt(t2.getText());
ts.put(name,code);
Enumeration k=ts.keys();
Enumeration v=ts.elements();
String msg="";
while(k.hasMoreElements())
{
msg=msg+k.nextElement()+" = "+v.nextElement()+"\n";
}
t.setText(msg);
t1.setText(""); t2.setText("");
}
else if(b2==e.getSource())
{
String name = t3.getText();
if(ts.containsKey(name))
{
t.setText(ts.get(name).toStrings());
}
else
JOptionPane.showMessageDialog(null,"City not found ...");
}
else if(b3==e.getSource())
{
String name = t3.getText();
if(ts.containsKey(name))
{
ts.remove(name);
JOptionPane.showMessageDialog(null,"City Deleted ...");
}
else
JOptionPane.showMessageDialog(null,"City not found ...");
}
}
public static void main(String a[])
{
new city();
}}
0 Comments
If anyone has Doubts or suggestions please let me know