0% found this document useful (0 votes)
10 views7 pages

Collections 2

The document contains Java code for three main functionalities: managing ticket sales using a TreeMap, sorting employee details using TreeSets with custom comparators, and calculating and sorting student averages. Each section includes classes for handling data and methods for input, processing, and output. The code demonstrates the use of collections and sorting mechanisms in Java.

Uploaded by

room3773
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Collections 2

The document contains Java code for three main functionalities: managing ticket sales using a TreeMap, sorting employee details using TreeSets with custom comparators, and calculating and sorting student averages. Each section includes classes for handling data and methods for input, processing, and output. The code demonstrates the use of collections and sorting mechanisms in Java.

Uploaded by

room3773
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Collections 2

import java.util.Map;

import java.util.Scanner;

import java.util.TreeMap;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Read the number of events

int n = scanner.nextInt();

TreeMap<Integer, Integer> ticketSales = new TreeMap<>();

// Read event details

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

int price = scanner.nextInt();

int seats = scanner.nextInt();

// Update the number of seats for the given price

ticketSales.put(price, ticketSales.getOrDefault(price, 0) + seats);

// Display the ticket sales in increasing order of price

System.out.println(ticketSales);

scanner.close();

}
import java.util.*;

// Employee class

class Empl {

private String name;

private int salary;

// Default constructor

public Empl() {

// Parameterized constructor

public Empl(String name, int salary) {

this.name = name;

this.salary = salary;

// Getters

public String getName() {

return name;

public int getSalary() {

return salary;

// toString() method for displaying employee details

@Override

public String toString() {

return name + "-" + salary;


}

// Comparator for sorting by name

class NameComparator implements Comparator<Empl> {

@Override

public int compare(Empl e1, Empl e2) {

return e1.getName().compareTo(e2.getName());

// Comparator for sorting by salary

class SalaryComparator implements Comparator<Empl> {

@Override

public int compare(Empl e1, Empl e2) {

return Integer.compare(e1.getSalary(), e2.getSalary());

// Main class

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Read number of employees

int n = sc.nextInt();

sc.nextLine(); // Consume the newline character

// TreeSet for sorting by name

TreeSet<Empl> nameSortedSet = new TreeSet<>(new NameComparator());

// TreeSet for sorting by salary


TreeSet<Empl> salarySortedSet = new TreeSet<>(new SalaryComparator());

// Read employee details and add to both sets

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

String name = sc.nextLine();

int salary = Integer.parseInt(sc.nextLine());

Empl emp = new Empl(name, salary);

nameSortedSet.add(emp);

salarySortedSet.add(emp);

// Display sorted by name

System.out.println("After sorting the employee:");

for (Empl emp : nameSortedSet) {

System.out.println(emp);

// Display sorted by salary

System.out.println("\nAfter sorting the salary:");

for (Empl emp : salarySortedSet) {

System.out.println(emp);

sc.close();

}
import java.util.*;

// Student class

class Student {

private int rollNo, mark1, mark2, mark3;

private String name;

private double avg;

// Constructor

public Student(int rollNo, String name, int mark1, int mark2, int mark3) {

this.rollNo = rollNo;

this.name = name;

this.mark1 = mark1;

this.mark2 = mark2;

this.mark3 = mark3;

calculateAverage();

// Method to calculate the average marks

public void calculateAverage() {

this.avg = (mark1 + mark2 + mark3) / 3;

// Getters

public int getRollNo() {

return rollNo;

public String getName() {

return name;
}

public double getAvg() {

return avg;

// toString method for formatted output

@Override

public String toString() {

return String.format("%-20d%-20s%-20.1f", rollNo, name, avg);

// Main class

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Read number of students

int n = Integer.parseInt(sc.nextLine());

// List to store Student objects

List<Student> students = new ArrayList<>();

// Read student details

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

int rollNo = Integer.parseInt(sc.nextLine());

String name = sc.nextLine();

int mark1 = Integer.parseInt(sc.nextLine());

int mark2 = Integer.parseInt(sc.nextLine());

int mark3 = Integer.parseInt(sc.nextLine());


students.add(new Student(rollNo, name, mark1, mark2, mark3));

// Sort students by average marks in ascending order

students.sort(Comparator.comparingDouble(Student::getAvg));

// Print header

System.out.printf("%-20s%-20s%-20s%n", "Roll No", "Name", "Average");

// Print sorted student details

for (Student student : students) {

System.out.println(student);

sc.close();

You might also like