ComeçarComece de graça

Computing percentiles

You have computed the mean, minimum, and maximum transaction amounts of sample coffee sales. Now find the median, 25th, and 75th percentiles of the transaction amounts formatted to two decimal places.

date coffee_name payment_method quantity amount
3/1/24 Latte cash 1 $6.26
3/5/24 Hot Chocolate card 3 $32.51
3/6/24 Americano card 2 $18.70

Este exercício faz parte do curso

Cleaning Data in Java

Ver curso

Instruções do exercício

  • Compute the median transaction amount, or the 50th percentile, of the coffee sales.
  • Compute the 25th and 75th percentiles of the transaction amounts.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

public class CoffeeSalesExample {
    private record CoffeeSales(LocalDate date, String coffeeName,
            String paymentMethod, int quantity, double amount) {
    }

    public static void main(String[] args) {
        List sales = Arrays.asList(
                new CoffeeSales(LocalDate.of(2024, 3, 1), "Latte", "cash", 1, 6.26),
                new CoffeeSales(LocalDate.of(2024, 3, 5), "Hot Chocolate", "card", 3, 32.51),
                new CoffeeSales(LocalDate.of(2024, 3, 6), "Americano", "card", 2, 18.70));

        DescriptiveStatistics stats = new DescriptiveStatistics();
        sales.forEach(sale -> stats.addValue(sale.amount()));

        System.out.printf("\nSales analyzed: %d%n", sales.size());
        System.out.printf("Average amount: $%.2f%n", stats.getMean());
        System.out.printf("Amount range: $%.2f - $%.2f%n",
                stats.getMin(), stats.getMax());
	    
        // Compute the median transaction amount
        System.out.printf("Median amount: $%.2f%n", stats.____(____));
        
        // Compute the 25th and 75th percentiles of the amounts
        System.out.printf("Normal range: $%.2f - $%.2f%n",
                stats.____(____), stats.____(____));
    }
}
Editar e executar o código