Collection
Collection
Collection
Ques
ARRAYLIST - INTRODUCTION
ArrayList
Write a Java program to display the array of Strings. Use for each loop to iterate and
print the elements.
Write a program to get the username and store it in the set and display the unique
number of the username in the set.
Consider a driver class called Main. In the Main method, obtain username input from
the user.
To assist Event organizers, you need to develop a console application that shows the
number of tickets sold in a particular price category. Thus enabling them to increase or
decrease seats allocated for different price levels and thereby boosting ticket sales.
The list of booking details that contains customer and price details is given.
Use TreeMap with price as key and number of seats booked as value.
Consider a driver class named Main. In the main method, obtain details and display the
price along with the number of tickets in increasing order of price.
Input Format:
The first line of the input corresponds to the number of events 'n'.
The next 'n' line of inputs corresponds to the event details in CSV format (Customer
Name, Ticket Price, No of Seats Booked).
Refer to Sample Input and Output for formatting specifications.
Output Format:
The output consists of the number of tickets booked for a particular ticket price in
increasing order of price.
Use ("%-15s %s\n","Ticket Price","Tickets Booked") for the format.
Refer to Sample Input and Output for formatting specifications.
Ques - 4
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. Write a program to sort the
users based on Pincode when certain details (City and state details) are unavailable.
AttributesDatatype
usernameString
addressLine1String
addressLine2String
pinCodeInteger
Include appropriate getters and setters
Create default constructor and a parameterized constructor with arguments in order
Address(String username, String addressLine1, String addressLine2, Integer pinCode).
The Address class implements the comparable interface. Compare Pincode, If Pincode
is the same then compare with addressLine1.
Consider the class Main to test the above class. Obtain input from the console and sort
the user list based on pincode.
Ques
Generic Classes
Consider a generic class Item with data. Write two methods set and get, to set a value
to the data variable and to get the value of the data variable respectively. From the
Main class create two objects for the class Item of types Integer and String.
Enter an integer :
15
Enter a string :
Hello World Generic class
Integer Value :15
String Value :Hello World Generic class
Ans- Main Java
import java.util.*;
public class Main {
public static void main(String args[]) {
obj.add(str);
System.out.println("Do you want to continue?(y/n)");
c = sc.next();
}
while(c.equalsIgnoreCase("y"));
System.out.println("The Names entered are:");
for(String st:obj)
System.out.println(st);
}
Ans - Main
import java.util.*;
do
{
System.out.println("Enter the username");
str=s.next();
obj.add(str);
System.out.println("Do you want to continue?(Y/N)");
c = s.next();
}
while(c.equalsIgnoreCase("Y"));
/*Iterator itr=obj.iterator();
while(itr.hasNext())
{
count++;
}*/
System.out.println("The unique number of usernames is "+obj.size());
}
Ans- Main
import java.util.*;
import java.util.Map.Entry;
public class Main {
for(int i=0;i<t;i++)
{
str=rj.nextLine();
s = str.split(",");
Address a=new Address(s[0],s[1],s[2],Integer.parseInt(s[3]));
l.add(a);
}
System.out.println("User Details:");
Collections.sort(l);
for(Address ad: l)
{
System.out.println(ad.getUsername()+","+ad.getAddressLine1()+","+ad.getAddressLine
2()+","+ad. getPinCode());
}
Ans-Main
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
Address
public class Address implements Comparable{
Item
public class Item <T> {