Funktionel programmering Øvelser

 avatar
unknown
java
2 years ago
6.3 kB
5
Indexable
import java.util.*;
// Problem 0: Include the necessary import statement for the findAll method
import ???;

public class Problems {

    // Problem 1: You have a list of `Products`. Each `Product` has a `name`, `category`, and `price`.
    // Find the most expensive product in the 'Electronics' category.
    public ??? mostExpensiveElectronics(List<Product> products) {
        return products.stream()
                       .???; // Fill in the blanks
    }

    // Problem 2: You have a list of `Songs`. Each `Song` has a `title`, `artist`, and `duration` (in minutes).
    // Find the total duration of all songs by "The Beatles".
    public ??? totalBeatlesSongsDuration(List<Song> songs) {
        return songs.stream()
                    .???; // Fill in the blanks
    }

    // Problem 3: You have a list of `Orders`. Each `Order` has an `orderNumber`, `customerName`, and `status`.
    // Find the number of orders with a status of 'Shipped'.
    public ??? shippedOrdersCount(List<Order> orders) {
        return orders.stream()
                     .???; // Fill in the blanks
    }

    // Problem 4: You have a list of `Animals`. Each `Animal` has a `name`, `species`, and `age`.
    // Find all animals of species "Dog" that are older than 5 years.
    public ??? oldDogs(List<Animal> animals) {
        return animals.stream()
                      .???; // Fill in the blanks
    }

    // Problem 5: You have a list of `Players`. Each `Player` has a `name`, `team`, and `goalsScored`.
    // Find the player who scored the most goals.
    public ??? topScorer(List<Player> players) {
        return players.stream()
                      .???; // Fill in the blanks
    }

    // Problem 6: You have a list of `Cities`. Each `City` has a `name`, `country`, and `population`.
    // Find the first city with a population greater than 10 million.
    public ??? bigCity(List<City> cities) {
        return cities.stream()
                     .???; // Fill in the blanks
    }

    // Problem 7: You have a list of `Books` (again). Each `Book` has a `title`, `author`, `price`, and `pages`.
    // Find the total number of pages across all books.
    public ??? totalPages(List<Book> books) {
        return books.stream()
                    .???; // Fill in the blanks
    }

    // Problem 8: You have a list of `Customers`. Each `Customer` has a `name`, `email`, and `premiumMember` status.
    // Find all premium member email addresses.
    public ??? premiumMembersEmails(List<Customer> customers) {
        return customers.stream()
                        .???; // Fill in the blanks
    }

    // Problem 9: You have a list of `Restaurants`. Each `Restaurant` has a `name`, `type`, and `rating`.
    // Find the highest-rated Italian restaurant.
    public ??? bestItalianRestaurant(List<Restaurant> restaurants) {
        return restaurants.stream()
                          .???; // Fill in the blanks
    }

    // Problem 10: You have a list of `Students` (again). Each `Student` has a `name`, `grade`, and `attendancePercentage`.
    // Find the student with the highest attendance who has a grade less than 'B'.
    public ??? topAttendeeWithLowGrade(List<Student> students) {
        return students.stream()
                       .???; // Fill in the blanks
    }

}



// Problem 0 Answer:
import java.util.stream.Collectors;

public class Solutions {

    // ... (The problem descriptions remain unchanged.)

    // Problem 1 Solution:
    public Product mostExpensiveElectronics(List<Product> products) {
        return products.stream()
                       .filter(p -> p.getCategory().equals("Electronics"))
                       .max(Comparator.comparing(Product::getPrice))
                       .orElse(null);
    }

    // Problem 2 Solution:
    public int totalBeatlesSongsDuration(List<Song> songs) {
        return songs.stream()
                    .filter(s -> s.getArtist().equals("The Beatles"))
                    .mapToInt(Song::getDuration)
                    .sum();
    }

    // Problem 3 Solution:
    public long shippedOrdersCount(List<Order> orders) {
        return orders.stream()
                     .filter(o -> o.getStatus().equals("Shipped"))
                     .count();
    }

    // Problem 4 Solution:
    public List<Animal> oldDogs(List<Animal> animals) {
        return animals.stream()
                      .filter(a -> a.getSpecies().equals("Dog") && a.getAge() > 5)
                      .collect(Collectors.toList());
    }

    // Problem 5 Solution:
    public Player topScorer(List<Player> players) {
        return players.stream()
                      .max(Comparator.comparing(Player::getGoalsScored))
                      .orElse(null);
    }

    // Problem 6 Solution:
    public City bigCity(List<City> cities) {
        return cities.stream()
                     .filter(c -> c.getPopulation() > 10_000_000)
                     .findFirst()
                     .orElse(null);
    }

    // Problem 7 Solution:
    public int totalPages(List<Book> books) {
        return books.stream()
                    .mapToInt(Book::getPages)
                    .sum();
    }

    // Problem 8 Solution:
    public List<String> premiumMembersEmails(List<Customer> customers) {
        return customers.stream()
                        .filter(Customer::isPremiumMember)
                        .map(Customer::getEmail)
                        .collect(Collectors.toList());
    }

    // Problem 9 Solution:
    public Restaurant bestItalianRestaurant(List<Restaurant> restaurants) {
        return restaurants.stream()
                          .filter(r -> r.getType().equals("Italian"))
                          .max(Comparator.comparing(Restaurant::getRating))
                          .orElse(null);
    }

    // Problem 10 Solution:
    public Student topAttendeeWithLowGrade(List<Student> students) {
        return students.stream()
                       .filter(s -> s.getGrade().compareTo('B') < 0)
                       .max(Comparator.comparing(Student::getAttendancePercentage))
                       .orElse(null);
    }

}

Editor is loading...