1. 로또 프로그램을 작성하시오.(Set 으로)
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
class Const {
static final int LOTTO_COUNT = 6;
static final int LOTTO_MIN = 1;
static final int LOTTO_MAX = 45;
}
class Print {
private static StringBuilder print = new StringBuilder();
private static void printlnAndReset() {
System.out.println(print);
print.setLength(0);
}
static void printlnObject(Object obj) {
print.append(obj);
printlnAndReset();
}
}
class Functions {
private static Set<Integer> lottoNums = new HashSet<Integer>();
private static ThreadLocalRandom random;
static void run() {
while(true) {
try {
lotto();
break;
} catch(Exception e) {
e.printStackTrace();
};
};
}
private static void lotto() {
while(lottoNums.size() < Const.LOTTO_COUNT) {
lottoNums.add(random.current().nextInt(Const.LOTTO_MIN, Const.LOTTO_MAX + 1));
}
Print.printlnObject(lottoNums);
}
}
class LottoMain {
public static void main(String[] args) {
Functions.run();
}
}
2. Set에 대하여 설명하시오.
- It is similar with the set in math.
- It doesnt care elements order.
- During
.add()
, it categorizes the elements by hashCode()
method. Then it compares the elements by equals()
method. If there is same elements from the processes of hashCode()
and equals()
, then it removes old one and adds new one in the set.
- Overriding
hashCode()
and equals()
can make easier to distinguish the set elements in detail.
3. 아래의 출력이 나오도록 만드시오.
public class Test1 {
public static void main(String[] args) {
HashSet<Person> hSet = new HashSet<Person>();
hSet.add(new Person("LEE", 10));
hSet.add(new Person("LEE", 10));
hSet.add(new Person("PARK", 35));
hSet.add(new Person("PARK", 35));
System.out.println("저장된 데이터 수: " + hSet.size());
System.out.println(hSet);
}
}
class Person {
private String name;
private int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
return (this.age);
}
@Override
public boolean equals(Object obj) {
boolean equal = false;
if (obj instanceof Person) {
Person person = (Person)obj;
if (this.name == person.name) {
equal = true;
};
};
return equal;
}
@Override
public String toString() {
return (this.name.concat("(").concat(String.valueOf(this.age)).concat("세)"));
}
}
4. 아래와 같이 출력이 나오도록 프로그래밍을 하시오.(개별진척도1번)
public class Test2 {
public static void main(String[] args) {
HashSet<Num> set = new HashSet<>();
set.add(new Num(7799));
set.add(new Num(9955));
set.add(new Num(7799));
System.out.println("인스턴스 수: " + set.size());
for(Num n : set)
System.out.print(n.toString() + '\t');
System.out.println();
}
}
class Num {
private int number;
Num(int number) {
this.number = number;
}
@Override
public int hashCode() {
return (this.number);
}
@Override
public boolean equals(Object obj) {
boolean equal = false;
if (obj instanceof Num) {
equal = true;
};
return equal;
}
@Override
public String toString() {
return(String.valueOf(this.number));
}
}
5.TreeSet 과 HashSet 의 차이는?
TreeSet
has order of elements but HashSet
doesnt have.
- Elements of
TreeSet
are connected like tree.
- Elements of
HashSet
are categorized by hashcode.
6. Set 호출되는 원리와 순서를 설명하시오.
- There are sets in a
Set
.
- Call the set of element first.
- Find the element from the set and unbox it.