JAVA/chapter11_reference_array
Test . reference_array
GAWON
2023. 5. 26. 18:25
Q1.
Cricle.java - 필드 : double radius
- 메소드 : Constructor, calcArea, output
CricleMain.java Circle 3개 생성 (반지름 입력 받아서 처리), 가장 크기가 큰 Circle 출력
★
calcArea => Math.PI * Math.pow(radius, 2)
output => 반지름, 크기 출력
Q2.
Song.java - 필드 : String title, String country
- 메소드 : Constructor, output
Singer.java - 필드 : String name,
Song[] songList(Song의 객체 n개를 메인으로부터 받아서 처리),
int idx(배열 인덱스에 접근용도)
- 메소드 : Constructor, setSong(song), output
SingerSongMain.java
Q3.
Day.java - 필드 : String schedule
- 메소드 : Constructor, getSchedule() : 스케쥴반환,
setSchedule(String schedule) : 스케쥴 저장, output()
WeekScheduler.java - 필드 : Day[] days, Scanner scanner,
String[] week : {일, 월, 화 ... , 토} 까지 데이터 초기화
- 메소드 : Constructor,
menu() : 목록 (1. 스케줄 생성, 2. 삭제, 3. 수정, 4. 보기, 0. 종료)
makeSchedule() : 요일과 스케쥴을 입력 받아 해당 요일에 스케쥴 저장
removeSchedule() : 요일을 입력 받아해당 요일의 스케쥴 삭제
modifySchedule() : 요일과 스케쥴입력받아 해당 요일의 스케쥴수정
기존 스케쥴이 없는 경우에는 새로운 스케쥴 저장
output() : 일주일간의 스케쥴전체출력
exit() : 스케쥴러 종료(종료 명령 전까지계속 스케쥴러는 작동해야함)
run() : 스케쥴러 실행
WeekSchedulerMain.java
WeekScheduler scheduler = new WeekScheduler();
scheduler.run();
package org.joonzis.test;
//Cricle.java - 필드 : double radius
//- 메소드 : Constructor, calcArea, output
public class Circle {
double radius;
public Circle() {}
public Circle(double radius) {
this.radius = radius;
}
double calcArea() {
return Math.PI * Math.pow(radius, 2);
}
void output() {
System.out.println("반지름 : " + radius);
System.out.println("크기 : " + calcArea());
}
}
package org.joonzis.test;
import java.util.Scanner;
public class CircleMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Circle 객체를 담을 배열
Circle[] circles = new Circle[3];
// circles 배열에 객체 저장
// 1. 개별로 담는 방법
circles[0] = new Circle(3);
circles[1] = new Circle(4);
circles[2] = new Circle(5);
circles[0].output();
circles[1].output();
circles[2].output();
// 2. 반복문 이용
for(int i=0; i<circles.length; i++) {
System.out.print("반지름 입력 >>");
double r = sc.nextDouble();
circles[i] = new Circle(r);
}
for(int i=0; i<circles.length; i++) {
circles[i].output();
}
// 가장 큰 원... 가장 큰 원의 크기, 인덱스 번호
// 최대 값 구하기
double max = circles[0].calcArea();
int idx = 0; // 인덱스 변수
for(int i=0; i<circles.length; i++) {
if(max < circles[i].calcArea()) {
max = circles[i].calcArea();
idx = i; // 가장 큰 원의 인덱스 번호 저장
}
}
System.out.println("가장 큰 원의 인덱스 번호 : " + idx);
System.out.println("가장 큰 원의 크기 : " + max);
System.out.println("가장 큰 원의 정보 : " );
circles[idx].output();
}
}
package org.joonzis.test;
public class Song {
String title, country;
public Song() {}
public Song(String title, String country) {
this.title = title;
this.country = country;
}
void output() {
System.out.println("노래 제목 : " + title);
System.out.println("국가 : " + country);
}
}
package org.joonzis.test;
public class Singer {
String name;
Song[] songList;
int idx;
public Singer() {}
public Singer(String name, int songCount) {
this.name = name;
songList = new Song[songCount];
}
void setSong(Song song) {
songList[idx] = song;
idx++;
}
void output() {
System.out.println("가수 이름 : " + name);
System.out.println("노래 모음------------");
for(int i=0; i<idx; i++) {
songList[i].output();
}
}
}
package org.joonzis.test;
public class SingerSongMain {
public static void main(String[] args) {
// 1. Singer 객체 생성 -> 가수 이름, 배열 크기 전달
// 2. Song 객체 생성 -> Sing 객체에 전달
// 3. Singer 객체를 이용하여 정보 출력
Singer singer = new Singer("IVE (아이브)", 2);
// 노래 정보 삽입
// 1.
Song s1 = new Song("Kitsch", "대한민국");
singer.setSong(s1);
// 2.
singer.setSong(new Song("LOVE DIVE","대한민국"));
singer.output();
}
}
package org.joonzis.test;
public class Day {
String schedule;
public Day() {}
public String getSchedule() {
return schedule;
}
public void setSchedule(String schedule) {
this.schedule = schedule;
}
void output() {
if(schedule == null) {
System.out.println("없음");
}else {
System.out.println(schedule);
}
}
}
package org.joonzis.test;
import java.util.Scanner;
public class WeekScheduler {
Day[] days;
Scanner sc = new Scanner(System.in);
String[] week = {"일", "월", "화", "수", "목", "금" , "토"};
public WeekScheduler() {
days = new Day[7];
for(int i=0; i<days.length; i++) {
days[i] = new Day();
}
}
void run() {
while(true) {
menu();
System.out.print("작업 선택 >> ");
int choice = sc.nextInt();
sc.nextLine();
switch(choice) {
case 1:makeSchedule();
break;
case 2:removeSchedule();
break;
case 3:modifySchedule();
break;
case 4:output();
break;
case 0:exit();
break;
default : System.out.println("다시 입력하세요.");
}
}
}
void menu() {
System.out.println("************");
System.out.println("1. 스케줄 생성");
System.out.println("2. 스케줄 삭제");
System.out.println("3. 스케줄 수정");
System.out.println("4. 스케줄 보기");
System.out.println("0. 종료");
System.out.println("************");
}
void makeSchedule() {
System.out.print("스케줄을 등록할 요일 선택 (일~토) >> ");
String weekday = sc.next(); // 1. 스케줄 생성 요일 입력
for(int i=0; i<week.length; i++) { // 2. 요일에 해당하는 스케줄 저장 위치 찾기
if(weekday.equals(week[i])) {
// 입력한 요일과 요일 배열 값이 같으면
if(days[i].getSchedule() == null) { // 3. 해당 요일에 스케줄이 있는지 확인
// 해당 요일에 스케줄이 비어있으면
System.out.print("등록할 스케줄 입력 >> "); // 4. 등록할 스케줄 입력
String input = sc.next();
days[i].setSchedule(input); // 5. 스케줄을 days 배열 내 객체에 저장
}else {
// 해당 요일에 스케줄이 이미 있으면
System.out.println(week[i] + "요일에 이미 스케줄이 있습니다.");
}
}
}
}
void removeSchedule() {
// 1. 삭제할 요일 선택
// 2. 해당 요일 배열에서 찾기
// 3. 해당 요일에 스케줄이 있는지 확인
// 4. 스케줄이 있으면 삭제 > Day 객체의 필드 값 null로 변경
// 5. 스케줄이 없으면 > 해당 요일에 삭제할 스케줄이 없습니다. 출력
// ex) 월요일에 삭제할 스케줄이 없습니다.
System.out.print("스케줄을 삭제할 요일 선택 (일~토) >> ");
String weekday = sc.next(); // 1. 스케줄 삭제 요일 입력
for(int i=0; i<week.length; i++) { // 2. 요일에 해당하는 스케줄 저장 위치 찾기
if(weekday.equals(week[i])) {
if(days[i].getSchedule() == null) { // 3. 해당 요일에 스케줄이 있는지 확인
// 삭제할 스케줄이 없으면
System.out.println(week[i] + "요일에 삭제할 스케줄이 없습니다.");
}else {
// 스케줄이 있으면
days[i].setSchedule(null);
System.out.println(week[i] + "요일 스케줄을 삭제했습니다.");
}
}
}
}
void modifySchedule() {
// 1. 수정할 요일 선택
// 2. 해당 요일 배열에서 찾기
// 3. 해당 요일에 스케줄이 있는지 확인
// 4. 스케줄이 있으면 > 해당 요일 스케줄 변경
// ex) "월요일의 기존 스케줄을 변경합니다."
// "변경할 스케줄 입력 >> "
// 5. 스케줄이 없으면 > 선택
// ex) "월요일에 스케줄이 없습니다."
// "새 스케줄을 등록하시겠습니까? (yes/no) >> "
// 6. yes 입력 시 새 스케줄 입력
// 7. no 입력 시 변경된 스케줄이 없습니다. 출력
System.out.print("스케줄 수정할 요일 선택 (일~토) >> ");
String weekday = sc.next(); // 1. 스케줄 수정 요일 입력
for(int i=0; i<week.length; i++) { // 2. 요일에 해당하는 스케줄 저장 위치 찾기
if(weekday.equals(week[i])) {
if(days[i].getSchedule() == null) { // 3. 해당 요일에 스케줄이 있는지 확인
// 삭제할 스케줄이 없으면
System.out.println(week[i] + "요일의 스케줄이 없습니다.");
System.out.print("새 스케줄을 등록하시겠습니까? (yes/no) >> ");
String yn = sc.next();
if(yn.equalsIgnoreCase("yes")) {
System.out.print("등록할 스케줄 입력");
String schedule = sc.next();
days[i].setSchedule(schedule);
}else {
System.out.print("변경된 스케줄이 없습니다.");
}
}else {
// 스케줄이 있으면
System.out.println(week[i] + "요일의 기존 스케줄을 변경합니다.");
System.out.print("변경할 스케줄 입력");
String schedule = sc.next();
days[i].setSchedule(schedule);
}
}
}
}
void output() {
System.out.println("<일주일 전체 스케줄 출력>");
for(int i=0; i<days.length; i++) {
System.out.print(week[i] + "요일 스케줄 : " );
days[i].output();
}
}
void exit() {
System.out.println("<스케줄러를 종료합니다.>");
sc.close();
System.exit(0);
}
}
package org.joonzis.test;
public class WeekSchedulerMain {
public static void main(String[] args) {
WeekScheduler scheduler = new WeekScheduler();
scheduler.run();
}
}