1.
import java.util.*;
public class RandomList<T> {
Random r = new Random();
String[] sample = {"i", "walk", "the", "line"};
List<T> list = (List<T>) Arrays.asList(sample);
public void add(T item){
list.add(item);
}
public T select() {
int num = r.nextInt(list.size());
return list.get(num);
}
}
2.
class MyMath<T extends Number> {
double v=0.0;
public double getAverage(T[] a){
for (int i = 0; i < a.length; i++)
v = v + a[i].doubleValue();
return v/a.length;
}
}
public class Test
{
public static void main(String[] args) {
Integer[] list = { 1, 2, 3, 4, 5, 6 };
MyMath<Integer> m = new MyMath<Integer>();
System.out.println(m.getAverage(list));
}
}
3.
class MyPair<T> {
private T str1, str2;
public MyPair(T s1, T s2){
str1 = s1;
str2 = s2;
}
public void getStr1(T s1){
str1 = s1;
}
public void getStr2(T s2){
str2 = s2;
}
public T setStr1(){
return str1;
}
public T setStr2(){
return str2;
}
public String toString(){
return "str1 : " + str1 + " str2 : " + str2;
}
}
public class MyPairTest{
public static void main(String[] args){
MyPair<String> fruit = new MyPair<String>("사과", "포도");
System.out.println(fruit.toString());
}
}
4.
public class Test
{
public static <T> void displayArray(T a)
{
System.out.println(a.getClass().getName());
}
public static void main(String[] args) {
int x=0;
float y=0.0F;
Test.<Object>displayArray(x);
Test.<Object>displayArray(y);
}
}
5.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Test
{
public static void main(String[] args) {
ArrayList<Double> list = new ArrayList<Double>();
Scanner sc = new Scanner(System.in);
for(int i=0;i<10; i++){
System.out.print("심사위원의 점수: ");
double value = sc.nextDouble();
list.add(value);
}
double max = Collections.max(list);
double min = Collections.min(list);
double sum = 0.0;
double value = 0.0;
for(int i=0;i<10; i++){
value = list.get(i);
if( value != min && value != max )
sum += value;
}
System.out.print("점수의 합: "+ sum);
}
}
6.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Student implements Comparable<Student>
{
public Student(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}
public int compareTo(Student s) {
return name.compareTo(s.name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Student [name=" + name + ", address=" + address + ", phone="
+ phone + "]";
}
String name;
String address;
String phone;
}
public class Test
{
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
Scanner sc = new Scanner(System.in);
for(int i=0;i<10; i++){
list.add(new Student("name"+i, "address"+i, "phone"+i));
}
Collections.sort(list);
int pos = Collections.binarySearch(list, new Student("name3", "address3", "phone3"));
System.out.println(pos);
}
}
7.
import java.util.HashSet;
public class Lotto {
@Override
public String toString() {
return "Lotto [set=" + set + "]";
}
HashSet<Integer> set = new HashSet<Integer>();
public Lotto() {
int d;
for (int i = 0; i < 6; i++) {
do {
d = (int) (Math.random() * 44.0 + 1.0);
} while (set.contains(d));
set.add(d);
}
}
public static void main(String[] args) {
Lotto lotto = new Lotto();
System.out.println(lotto);
}
}
'솔루션모음 > 파워자바 프로그래밍' 카테고리의 다른 글
[파워자바] 24장 프로그래밍 솔루션 답지 (0) | 2023.02.16 |
---|---|
[파워자바] 23장 프로그래밍 솔루션 답지 (0) | 2023.02.16 |
[파워자바] 21장 프로그래밍 솔루션 답지 (0) | 2023.02.16 |
[파워자바] 20장 프로그래밍 솔루션 답지 (0) | 2023.02.16 |
[파워자바] 17장 프로그래밍 솔루션 답지 (0) | 2023.02.16 |
댓글