Anda di halaman 1dari 6

Advanced Java Assignments

Collections
Concepts: Collections

1. Write a program to add list of student names to ArrayList and it should find a particular name whether
it exists or not in the list.
SOLUTION

package advjavaass2;

import java.util.ArrayList;

import java.util.Collections;
import java.util.Scanner;

public class Student {

public static void main(String[] args) {


ArrayList<String> al=new ArrayList<String>();
int n;
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of students:");
n=sc.nextInt();
System.out.println("enter the student names:");
for(int i=0;i<n;i++)
{
al.add(sc.next());
}
System.out.println("student list:");
for(String a:al)
{
System.out.println(a);
}
System.out.println("enter the name of student to be searched:");
String st=sc.next();
int position=Collections.binarySearch(al,st);
System.out.println("position of "+st+"is:"+position);
}

2. Create a Product class with Product Id & Product Name. Write a program to accept information of 10
products and store that in Hash Table. Search a particular product in the Hash Table. Remove a
particular product id and product name from the Hash Table.
The product list is as follows:

Product Id Product Name


P001 Maruti800
P002 MarutiZen
P003 MarutiEsteem

SOLUTION
package advjavaass2;
import java.util.Hashtable;
import java.util.Scanner;
public class Product {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
Hashtable<String,String> hm=new Hashtable<String,String>();
System.out.println("enter the product id and name:");
for(int i=0;i<3;i++)
{
hm.put(sc.next(), sc.next());
}
System.out.println("the product list is:");
System.out.println(hm);
System.out.println("enter the product id to be removed:");
String id=sc.next();
hm.remove(id);
System.out.println("item removed");
System.out.println("the product list is:");
System.out.println(hm.toString());
System.out.println("enter the product id to be searched:");
String sid=sc.next();
if(hm.containsKey(sid))
System.out.println(hm.get(sid));
else
System.out.println("do not exist");
}
}

3. Implement vector class for this problem


1. Create an Employee class which will have details like EmployeeNo, EmployeeName and Address.
You should pass value for EmployeeNo, EmployeeName and Address through constructor.
2. Create a method addInput( ) which will add employee details to vector.
3. Create method display( ) which should display all data from vector using Enumeration.

Note : addInput( ) and display( ) should not be member functions of Employee class.

SOLUTION
package advjavaass2;

public class Employee {


private int empid;
private String ename,address;
public Employee(int empid, String ename, String address) {
super();
this.empid = empid;
this.ename = ename;
this.address = address;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

}
package advjavaass2;

import java.util.Vector;
public class TestEmployeeCollection {

public static void main(String[] args) {

Vector<Employee> v =addInput();
display(v);

}
public static Vector<Employee> addInput()
{
Employee e1=new Employee(101,"deepika","uran");
Employee e2=new Employee(101,"bharat","patna");
Employee e3=new Employee(103,"sana","thane");
Vector<Employee> v = new Vector<Employee>();
v.add(e1);
v.add(e2);
v.add(e3);
return v;

}
public static void display(Vector<Employee>v)
{
for(Employee e:v)
{
System.out.println(e.getEmpid()+"\t"+e.getEname()+"\t"+e.getAddress());
}

4. Write a program having user interface like

1. accept first name and surname


2. display total name
3. exit

Option A should accept First Name and SurName from command prompt
and save that to Vector object

Option B it has to display how many names entered in the vector object

This menu should be repeated till users selects exit.


SOLUTION
To store first name and surname, create a class Name with these two attributes.
SOLUTION
package advjavaass2;

public class Name {


String name,sname;

public Name(String name, String sname) {


super();
this.name = name;
this.sname = sname;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getSname() {


return sname;
}

public void setSname(String sname) {


this.sname = sname;
}

package advjavaass2;

import java.util.Scanner;
import java.util.Vector;

public class TestName {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.println("enter the choice:");
System.out.println("\n1.Add new phone book entry \n2.Search Phone Number
\n3.Quit.");
int choice=sc.nextInt();

Vector v1=new Vector ();


Vector<Name> v2=new Vector<Name>();
while(choice !=3)
{

if(choice==1)
{

System.out.println("enter the number of entry:");


int n=sc.nextInt();
for(int i=0;i<=n;i++)
{

v1.add(new Name(sc.next(),sc.next()));

}
}
System.out.println("enter the choice:");
System.out.println("\n1.Add new phone book entry \n2.Search Phone Number
\n3.Quit.");
choice=sc.nextInt();
if(choice==2)
{
System.out.println("number of entry:"+v1.size());
}

System.out.println("thank you");

5. Create Phone book having user interface like

1. Add new phone book entry


2. Search Phone Number
3. Quit.

Option 1 it allows add name and Phone no.


Option 2 it has to take name as input from the user based on that it should return phone No
Option 3: will terminate the program

Use HashMap to store phone book entries.

Anda mungkin juga menyukai