반응형
// TODO: 연습문제) Map 자료구조에 값과 객체를 추가하고 아래와 같이 결과가 출력하도록
// 실행 클래스를 코딩하세요
// 힌트 : put(), get()
// 입력 : 키 | 값
// currentPage 2
// totalNum 3
// dept Dept{dno=10, dname='Sales', loc='부산'}
// sizePerPage 3
// 결과 :
// 2
// 3
// Dept{dno=10, dname='Sales', loc='부산'}
// 3
package chap12.sec01.verify.exam05;
/**
* packageName : chap13.sec01.verify.exam01
* fileName : Dept
* author : kangtaegyung
* date : 2023/04/05
* description :
* 요약 :
* <p>
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2023/04/05 kangtaegyung 최초 생성
*/
public class Dept {
private int dno;
private String dname;
private String loc;
// 생성자
public Dept(int dno, String dname, String loc) {
this.dno = dno;
this.dname = dname;
this.loc = loc;
}
// getter
public int getDno() {
return dno;
}
public String getDname() {
return dname;
}
public String getLoc() {
return loc;
}
@Override
public String toString() {
return "Dept{" +
"dno=" + dno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
}
package chap12.sec01.verify.exam05;
import java.util.HashMap;
import java.util.Map;
/**
* packageName : chap12.sec01.verify.exam05
* fileName : DeptApplication
* author : GGG
* date : 2023-09-26
* description :
* 요약 :
* <p>
* ===========================================================
* DATE AUTHOR NOTE
* —————————————————————————————
* 2023-09-26 GGG 최초 생성
*/
public class DeptApplication {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("currentPage", 2);
map.put("totalNum", 3);
map.put("dept", new Dept(10,"Sales", "부산"));
map.put("sizePerPage", 3);
System.out.println(map.get("currentPage"));
System.out.println(map.get("totalNum"));
System.out.println(map.get("dept"));
System.out.println(map.get("sizePerPage"));
}
}
// TODO: 연습문제) Map 자료구조에 값과 객체를 추가하고 아래와 같이 결과가 출력하도록
// 실행 클래스와 Qna 객체를 디자인(코딩)하세요
// 힌트 : put(), get()
// 입력 : 키 | 값
// currentPage 1
// totalNum 8
// qna Qna{qno=1, question='질문', questioner='질문자', answer='답변', answerer='답변자'}
// sizePerPage 4
// 결과 :
// 1
// 8
// Qna{qno=1, question='질문', questioner='질문자', answer='답변', answerer='답변자'}
// 4
package chap12.sec01.verify.exam06;
/**
* packageName : chap13.sec01.verify.exam06
* fileName : Qna
* author : kangtaegyung
* date : 2023/04/06
* description :
* 요약 :
* <p>
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2023/04/06 kangtaegyung 최초 생성
*/
public class Qna {
private int qno;
private String question;
private String questioner;
private String answer;
private String answerer;
public Qna(int qno, String question, String questioner, String answer, String answerer) {
this.qno = qno;
this.question = question;
this.questioner = questioner;
this.answer = answer;
this.answerer = answerer;
}
public int getQno() {
return qno;
}
public String getQuestion() {
return question;
}
public String getQuestioner() {
return questioner;
}
public String getAnswer() {
return answer;
}
public String getAnswerer() {
return answerer;
}
@Override
public String toString() {
return "Qna{" +
"qno=" + qno +
", question='" + question + '\'' +
", questioner='" + questioner + '\'' +
", answer='" + answer + '\'' +
", answerer='" + answerer + '\'' +
'}';
}
}
package chap12.sec01.verify.exam06;
import java.util.HashMap;
import java.util.Map;
/**
* packageName : chap12.sec01.verify.exam06
* fileName : QnaApplication
* author : GGG
* date : 2023-09-26
* description :
* 요약 :
* <p>
* ===========================================================
* DATE AUTHOR NOTE
* —————————————————————————————
* 2023-09-26 GGG 최초 생성
*/
public class QnaApplication {
public static void main(String[] args) {
Map<String, Object> map = new HashMap();
map.put("currentPage", 1);
map.put("totalNum", 8);
map.put("qna", new Qna(1, "질문", "질문자", "답변", "답변자"));
map.put("sizePerPage", 4);
System.out.println(map.get("currentPage"));
System.out.println(map.get("totalNum"));
System.out.println(map.get("qna"));
System.out.println(map.get("sizePerPage"));
}
}
// TODO: 연습문제) 실행클래스는 결과는 아래와 같다.
// Student 객체의 속성 및 중복을 제거하기 위해 재정의해야할 함수를 포함하여
// Student 객체를 디자인(코딩)하세요
// 결과 :
// 1:홍길동
// 2:신용권
package chap12.sec01.verify.exam07;
import java.util.Objects;
/**
* packageName : chap12.sec01.verify.exam07
* fileName : Student
* author : GGG
* date : 2023-09-26
* description :
* 요약 :
* <p>
* ===========================================================
* DATE AUTHOR NOTE
* —————————————————————————————
* 2023-09-26 GGG 최초 생성
*/
public class Student {
private int studentNum;
private String name;
public Student() {
}
public Student(int studentNum, String name) {
this.studentNum = studentNum;
this.name = name;
}
public int getStudentNum() {
return studentNum;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return studentNum == student.studentNum && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(studentNum, name);
}
}
package chap12.sec01.verify.exam07;
import java.util.HashSet;
import java.util.Set;
/**
* packageName : chap13.sec01.verify.exam08
* fileName : HashSetApplicaton
* author : kangtaegyung
* date : 2022/10/03
* description : HashSet에 Student 객체를 저장하려고 합니다.
* 학번이 같으면 동일한 Student라고 가정하고 중복 저장이 되지 않도록 하고 싶습니다.
* Student 클래스에서 재정의해야 하는 hashCode()와 equals() 함수를 내용을 채워보세요.
* Student의 해시코드는 학번이라고 가정합니다.
* 요약 : 자바 기본 자료구조 ( List, Set, Map )
*
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2022/10/03 kangtaegyung 최초 생성
*/
public class HashSetApplication {
public static void main(String[] args) {
Set<Student> set = new HashSet<Student>();
// HashSet : 키가 중복되었을때 자동으로 중복을 제거해주는 자료구조
// HashSet : 기존에 값이 있으면 안들어감
set.add(new Student(1, "홍길동"));
set.add(new Student(2, "신용권"));
set.add(new Student(1, "홍길동"));
for(Student student : set) {
System.out.println(student.getStudentNum() + ":" + student.getName());
}
}
}
반응형
'Java > Java 연습문제' 카테고리의 다른 글
객체생성 연습문제 (0) | 2023.10.04 |
---|---|
자료구조 연습문제 (0) | 2023.09.26 |
이름 붙은 반복문 심화예제 (0) | 2023.08.12 |
이름 붙은 반복문 (0) | 2023.08.12 |
break문과 continue 문 예제 (0) | 2023.08.12 |