Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Collection

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

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.

Consider the class Main. It includes the method main.


In the Main method,
Get the usernames and store them in an ArrayList.
After getting all the names, just display them in the same order.

The link to download the template code is given below


Code Template

Input and Output format:

Refer to sample Input and Output for formatting specifications.


All Texts in bold corresponds to the input and the rest are output

Sample Input and Output 1:

Enter the username 1


John
Do you want to continue?(y/n)
y
Enter the username 2
Joe
Do you want to continue?(y/n)
n
Ques 2
Set Introduction

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.

The link to download the template code is given below


Code Template

Input and Output format:


Refer to sample Input and Output for formatting specifications.

Sample Input and Output:


[All text in bold corresponds to the input and rest corresponds to output]

Enter the username


John
Do you want to continue? (Y/N)
Y
Enter the username
Christoper
Do you want to continue? (Y/N)
Y
Enter the username
Ahamed
Do you want to continue? (Y/N)
Y
Ques - 3
TreeMap()

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.

Strictly adhere to the Object-Oriented specifications given in the problem statement.


All class names, attribute names, and method names should be the same as specified
in the problem statement.

Consider the class Address with the following private attributes

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.

The link to download the template code is given below

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.

The link to download the template code is given below


Code Template

Refer sample input-output form input-output format

Sample Input and Output:

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[]) {

Scanner sc= new Scanner(System.in);


String str,c;
int i=1;
ArrayList<String> obj = new ArrayList<String>();
do {
System.out.println("Enter the username "+i++);
str = sc.next();

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.*;

public class Main {

public static void main(String[] args){

Scanner s=new Scanner(System.in);


String str,c;
int count=0;
Set obj=new HashSet();

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 {

public static void main(String[] args){


Scanner rj=new Scanner(System.in);
String name;
String [] s;
int price,seats,s1;
TreeMap<Integer,Integer> tm=new TreeMap<>();
System.out.println("Enter the number of events:");
int t=rj.nextInt();
System.out.println("Enter event details in CSV(Customer Name,Ticket Price,No of
Seats Booked)");
for(int i=0;i<t;i++)
{
name=rj.next();
s=name.split(",");
price=Integer.parseInt(s[1]);
seats=Integer.parseInt(s[2]);
if(tm.containsKey(price))
{
s1=tm.get(price);
seats=s1+seats;
tm.replace(price, s1, seats);
}
else
{
tm.put(price,seats);
Ans- Main
import java.util.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner rj=new Scanner(System.in);
List<Address> l=new ArrayList<>();
System.out.println("Enter the number of users:");
int t=rj.nextInt();
rj.nextLine();
String str;
String[]s;
System.out.println("Enter user address in CSV(Username,AddressLine
1,AddressLine 2,PinCode)");

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
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("Enter a integer :");
int n=Integer.parseInt(br.readLine());
System.out.println("Enter a string :");
String str=br.readLine();

Item<Integer> obj=new Item<Integer>();


obj.set(n);
Item<String>istr=new Item<String>();
istr.set(str);

System.out.println("Integer Value :" + obj.get());


System.out.println("String Value :" + istr.get());
}
}
//Your code here
}

Address
public class Address implements Comparable{

private String username;


private String addressLine1;
private String addressLine2;
private Integer pinCode;

public String getUsername() {


return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddressLine1() {
return addressLine1;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
public String getAddressLine2() {
return addressLine2;
}
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
public Integer getPinCode() {
return pinCode;
}
public void setPinCode(Integer pinCode){

Item
public class Item <T> {

// fill your code here


T var;
public void set(T var)
{
this.var=var;
}
public T get()
{
return var;
}
}

You might also like