You are on page 1of 8

collections Assignment

1. Comparable Interface
Given the list of Address details, sort them based on Pincode. If two address has the
same Pincode,
then sort them based on address line 1.

Solution:

import java.util.*;
class Address implements Comparable<Address> {

private String username;


private String addressLine1;
private String addressLine2;
private int pinCode;

Address(){}
Address(String username, String addressLine1, String addressLine2,int
pinCode)
{
this.username=username;
this.addressLine1=addressLine1;
this.addressLine2=addressLine2;
this.pinCode =pinCode;

}
public String getusername() { return username; }

public void setUsername(String name)


{
this.username = name;
}
public String getAddressLine1() { return addressLine1; }

public void setAddressLine1(String cn)


{
this.addressLine1 = cn;
}
public int getPinCode() { return pinCode; }

public void setPinCode(int cpd)


{
this.pinCode = cpd;
}
public String getAddressLine2() { return addressLine2; }

public void setAddressLine2(String on)


{
this.addressLine2 = on;
}

public int compareTo(Address ad)


{
int r = Integer.compare(this.pinCode,ad.pinCode);
if( r == 0 ) {
r = this.addressLine1.compareTo(ad.addressLine1 );
}
return r;

}
public String toString()
{
return username + "," + addressLine1 + "," + addressLine2 + "," + pinCode;

class Main {
public static void main(String[] args)
{

ArrayList<Address> list= new ArrayList<Address>();


Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of users:");
int n=sc.nextInt();
sc.nextLine();
System.out.println("Enter user address in CSV(Username,AddressLine
1,AddressLine 2,PinCode)");
for(int i=0;i<n;i++)
{
String[] sp=sc.nextLine().split(",");
list.add(new Address(sp[0],sp[1],sp[2],Integer.valueOf(sp[3])));

}
Collections.sort(list);
System.out.println("User Details:\n");

for (Address ad : list)


System.out.println(ad);

}
}
2. Email Search using set
Write a program to search all the email addresses which are given in CSV format.
Consider the Main class. Obtain email addresses from the user and add them to a
Set. At last, get a String that has multiple email addresses in CSV format. Print
"Email addresses are present" if all email addresses are present in the Set, else print
"Email addresses are not present".

Solution:

import java.util.*;

class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Set<String> hash_Set = new HashSet<String>();
String c="yes";

while(c.compareTo("no")!=0)
{
System.out.println("Enter your Email Adress :");
hash_Set.add(sc.next());
System.out.println("Do you want to Continue?(yes/no)");
c=sc.next().toLowerCase();

}
System.out.println("Enter the email addresses to be searched separated by comma");
String str=sc.next();
String[] sp = str.split(",");
List<String> lp = Arrays.asList(sp);
HashSet<String> hash_Set2 = new HashSet<String>(lp);
if(hash_Set.containsAll(hash_Set2))
{
System.out.println("All Email addresses are present");

}
else{
System.out.println("Email addresses are not present");
for(String i : hash_Set2)
{
if(hash_Set.contains(i)!=true)
{
System.out.println(i);
}
}
}

}
}
3. Write a program to take hall objects as input in the list and sort them in the order
of their costPerDay using the sort() method of the comparable interface. Then
display them in tabular form. All class names, attribute names, and method names
should be the same as specified in the problem statement.

Solution:

import java.util.*;
class Hall implements Comparable<Hall> {

private String name;


private String contactNumber;
private Double costPerDay;
private String ownerName;

Hall(String name, String contactNumber, Double costPerDay,String


ownerName)
{
this.name=name;
this.contactNumber=contactNumber;
this.costPerDay=costPerDay;
this.ownerName =ownerName;

}
public String getName() { return name; }

public void setName(String name)


{
this.name = name;
}
public String getContactNumber() { return contactNumber; }

public void setContactNumber(String cn)


{
this.contactNumber = cn;
}
public double getCostPerDay() { return costPerDay; }

public void setCostPerDay(double cpd)


{
this.costPerDay = cpd;
}
public String getOwnerName() { return ownerName; }

public void setOwnerName(String on)


{
this.ownerName = on;
}

public int compareTo(Hall sd)


{
double dif= this.costPerDay - sd.costPerDay;
if (dif > 0) return 1;
if (dif < 0) return -1;
return 0;
}
public String toString()
{
return name + " " + contactNumber + " " + costPerDay + " "
+ ownerName;
}

class Main {
public static void main(String[] args)
{

ArrayList<Hall> list
= new ArrayList<Hall>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of halls:");
int n=sc.nextInt();
sc.nextLine();
for(int i=0;i<n;i++)
{
System.out.println("Enter the details of hall "+(i+1));
String[] sp=sc.nextLine().split(",");
list.add(new Hall(sp[0],sp[1],Double.valueOf(sp[2]),sp[3]));

}
Collections.sort(list);
System.out.println("\nSorted Order (from the least expensive to the
most):\n");
System.out.println("Name Contact number Costperday Owner name
");

for (Hall sd : list)


System.out.println(sd);
}
}

You might also like