Day 7 - Java

Problem Statement: Chocolate Sorting

You are given a list of chocolates, each with a type and sweetness level. Your task is to sort these chocolates in descending order of sweetness. If two chocolates have the same sweetness level, sort them by their type in ascending order.

Write a Java program called ChocolateFactory that takes an integer n followed by n lines, each containing two space-separated integers representing the type and sweetness level of a chocolate respectively. The program should output the sorted list of chocolates.

Input Format:

The first line contains an integer n, the number of chocolates.

The next n lines contain two space-separated integers each, representing the type and sweetness level of a chocolate.

Output Format:

Output the sorted list of chocolates. Each chocolate should be printed on a new line, with its type and sweetness level separated by a space.

Sample Input:

3

1 10

2 15

1 8

Sample Output:

(2, 15)

(1, 10)

(1, 8)

Test Cases:

Test Case 1:

Input:

5

3 20

1 15

2 10

4 20

2 15

Output:

(3, 20)

(4, 20)

(1, 15)

(2, 15)

(2, 10)

Test Case 2:

Input:

4

1 25

2 30

3 20

4 20

Output:

(2, 30)

(1, 25)

(3, 20)

(4, 20)

Test Case 3:

Input:

2

5 10

5 10

Output:

(5, 10)

(5, 10)


SOLUTION:

import java.util.*;

class Chocolate {

    int type;

    int sweetness;

    Chocolate(int type, int sweetness) {

        this.type = type;

        this.sweetness = sweetness;

    }

}

public class ChocolateFactory {

    public static List<Chocolate> sortChocolates(List<Chocolate> chocolates) {

        Collections.sort(chocolates, (c1, c2) -> {

            if (c1.sweetness != c2.sweetness) {

                return Integer.compare(c2.sweetness, c1.sweetness);

            } else {

                return Integer.compare(c1.type, c2.type);

            }

        });

        return chocolates;

    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();

        List<Chocolate> chocolates = new ArrayList<>();

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

            int type = scanner.nextInt();

            int sweetness = scanner.nextInt();

            chocolates.add(new Chocolate(type, sweetness));

        }


        List<Chocolate> sortedChocolates = sortChocolates(chocolates);

        for (Chocolate chocolate : sortedChocolates) {

            System.out.println("(" + chocolate.type + ", " + chocolate.sweetness + ") ");

        }

    }

}


Insights:
  • The sortChocolates method is declared as static, which means it belongs to the class ChocolateFactory rather than any specific instance of it. This allows the method to be invoked directly using the class name.
  • The sortChocolates method takes a List of Chocolate objects as a parameter. This list contains the chocolates that need to be sorted based on their sweetness level and type.
  • Within the sortChocolates method, a lambda expression is used as an argument to the Collections.sort method. This lambda expression defines the custom sorting logic based on sweetness and type.
  • The lambda expression compares two Chocolate objects (c1 and c2) based on their sweetness levels. If the sweetness levels are different, it sorts them in descending order of sweetness. If sweetness levels are equal, it sorts them in ascending order of type.
  • An ArrayList named chocolates is used to dynamically store the Chocolate objects created during input processing. This dynamic array allows for flexible storage of chocolates without requiring a fixed size upfront.
Enjoy Coding with Chocolates from our special Lambda Chocolate Factory! :)



Comments