-
쉽게 배우는 자바 프로그래밍 2판 10장 프로그래밍 문제 Part.2 (6번 ~ 7번)JAVA/연습문제 2022. 11. 3. 20:44반응형

# 개발 공부하는 초보자가 작성한 답안이니 정답이 아닐 수 있습니다!!!
6. 다음과 같은 Animal 및 Dog 클래스, 테스트 프로그램의 일부가 있다. 테스트 프로그램 AnimalTest를 완성하라.
📢주어진 조건📢 public class Animal { public void sound() { System.out.println("ㅁㅁㄲㄲ ..."); } } public class Dog extends Animal { public void sound() { System.out.println("멍멍"); } } import java.util.function.Supplier; public class AnimalTest { public static void soundAnimal(Supplier<Animal> s) { // soundAnimal(Supplier<Animal> s) 메서드 public static void main(String[] args) { // soundAnimal() 메서드 호출 } }🧤정답🧤 public class Animal { public void sound() { System.out.println("ㅁㅁㄲㄲ ..."); } } public class Dog extends Animal { public void sound() { System.out.println("멍멍"); } } import java.util.function.Supplier; public class AnimalTest { public static void soundAnimal(Supplier<Animal> s) { s.get().sound(); } public static void main(String[] args) { Animal[] animalsList = { new Animal(), new Dog() }; for (Animal animal : animalsList) { Supplier<Animal> dSupplier3 = () -> animal; soundAnimal(dSupplier3); } // for (int i = 0; i < animalsList.length; i++) { // 람다식을 쓸 때 지역변수는 final 취급하기 때문에 값이 변하면 안된다. // Supplier<Animal> dSupplier = () -> animalsList[i]; // soundAnimal(dSupplier); // } } }7. 람다식을 사용하여 사람의 신장과 체중의 평균을 구하는 프로그램을 다음을 참조하여 작성하라.
7-1. Person 클래스는 이름, 신장, 체중을 나타내는 필드가 있고 getter 메서드를 가진다.
🧤정답🧤 public class Person { private String name; private double height, weight; public String getName() { return name; } public double getHeight() { return height; } public double getWeight() { return weight; } }7-2. Person 클래스는 다음과 같은 5명의 Person 객체를 포함하는 ArrayList<Person> 타입 persons가 있다.
📢주어진 조건📢 황진이, 160, 45 이순신, 180, 80 김삿갓, 175, 65 홍길동, 170, 68 배장화, 155, 48🧤정답🧤 import java.util.ArrayList; import java.util.Arrays; public class Person { private String name; private double height, weight; public Person(String name, double height, double weight) { super(); this.name = name; this.height = height; this.weight = weight; } public String getName() { return name; } public double getHeight() { return height; } public double getWeight() { return weight; } public static final ArrayList<Person> persons = new ArrayList<>( Arrays.asList(new Person("황진이", 160, 45), new Person("이순신", 180, 80), new Person("홍길동", 170, 68), new Person("김삿갓", 175, 65), new Person("배장화", 155, 48))); }7-3. 테스트 프로그램 PersonTest 클래스에 신장이나 체중에 대하여 평균을 반환하는 정적 메서드 average()를 작성하라. 단, average() 메서드의 매개변수를 Function 유형의 타입으로 사용한다.
🧤정답🧤 import java.util.ArrayList; import java.util.Arrays; public class Person { private String name; private int height, weight; public Person(String name, int height, int weight) { this.name = name; this.height = height; this.weight = weight; } String getName() { return name; } int getHeight() { return height; } int getWeight() { return weight; } public static final ArrayList<Person> persons = new ArrayList<>(Arrays.asList( new Person("황진이", 160, 45), new Person("이순신", 180, 80), new Person("김삿갓", 175, 65), new Person("홍길동", 170, 68), new Person("배장화", 155, 48)) ); } import java.util.ArrayList; import java.util.List; import java.util.function.DoubleFunction; import java.util.function.ToIntFunction; public class PersonTest { public static void main(String[] args) { double averageHeight = average(Person.persons, c-> c.getHeight()); double averageWeight = average(Person.persons, c-> c.getWeight()); System.out.println("평균 키 : " + averageHeight); System.out.println("평균 몸무게 : " + averageWeight); } public static double average(List<Person> personList, ToIntFunction<Person> df) { double sum = 0.0; for (Person p : personList){ sum += df.applyAsInt(p); } return sum / personList.size(); } }반응형'JAVA > 연습문제' 카테고리의 다른 글
쉽게 배우는 자바 프로그래밍 2판 10장 프로그래밍 문제 Part.1 (1번 ~ 5번) (0) 2022.11.02 쉽게 배우는 자바 프로그래밍 2판 10장 연습문제 Part.2 (6번 ~ 10번) (0) 2022.11.02 쉽게 배우는 자바 프로그래밍 2판 10장 연습문제 Part.1 (1번 ~ 5번) (0) 2022.11.01 쉽게 배우는 자바 프로그래밍 2판 10장 도전과제 (0) 2022.11.01 쉽게 배우는 자바 프로그래밍 2판 9장 프로그래밍 문제 Part.2 (6번 ~ 7번) (0) 2022.11.01