1. 자바 클래스를 작성하는 연습을 해보자. 다음 main() 메소드를 실행하였을 때 예시와 같이 출력되도록 TV 클래스를 작성하라.
public static void main(String[] args) {
TV myTV = new TV("LG", 2017, 32); //LG에서 만든 2017년 32인치
myTV.show();
}
LG에서 만든 2017년형 32인치 TV |
2. Grade 클래스를 작성해보자. 3 과목의 점수를 입력받아 Grade 객체를 생성하고 성적 평균을 출력하는 main()과 실행 예시는 다음과 같다.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("수학, 과학, 영어 순으로 3개의 정수 입력 >> ");
int math = scanner.nextInt();
int science = scanner.nextInt();
int english = scanner.nextInt();
Grade me = new Grade(math, science, english);
System.out.println("평균은 "+me.average()); // average()는 평균을 계산하여 리턴
scanner.close();
}
수학, 과학, 영어 순으로 3개의 점수 입력>>90 88 96 평균은 91 |
3. 노래 한 곡을 나타내는 Song 클래스를 작성하라. Song은 다음 필드로 구성된다.
● 노래의 제목을 나타내는 title
● 가수를 나타내는 artist
● 노래가 발표된 연도를 나타내는 year
● 국적을 나타내는 country
또한 Song 클래스에 다음 생성자와 메소드를 작성하라.
● 생성자 2개: 기본 생성자와 매개변수로 모든 필드를 초기화하는 생성자
● 노래 정보를 출력하는 show() 메소드
● main() 메소드에서는 1978년, 스웨덴 국적의 ABBA가 부른 "Dancing Queen"을
song 객체로 생성하고 show()를 이용하여 노래의 정보를 다음과 같이 출력하라.
1978년 스웨덴국적의 ABBA가 부른 Dancing Queen
4. 다음 멤버를 가지고 직사각형을 표현하는 Rectangle 클래스를 작성하라.
● int 타입의 x, y, width, height 필드: 사각형을 구성하는 점과 크기 정보
● x, y, width, height 값을 매개변수로 받아 필드를 초기화하는 생성자
● int square() : 사각형 넓이 리턴
● void show() : 사각형의 좌표와 넓이를 화면에 출력
● boolean contatins(Rectangle r) : 매개변수로 받은 r이 현 사각형 안에 있으면 true 리턴
● main() 메소드의 코드와 실행 결과는 다음과 같다
public static void main(String[] args) {
Rectangle r = new Rectangle(2, 2, 8, 7);
Rectangle s = new Rectangle(5, 5, 6, 6);
Rectangle t = new Rectangle(1, 1, 10, 10);
r.show();
System.out.println("s의 면적은 "+s.square());
if(t.contains(r)) System.out.println("t는 r을 포함합니다.");
if(t.contains(s)) System.out.println("t는 s를 포함합니다.");
}
(2,2)에서 크기가 8x7인 사각형
s의 면적은 36
t는 r을 포함합니다.
5. 다음 설명대로 Circle 클래스와 CircleManager 클래스를 완성하라.
6. 앞의 5번 문제는 정답이 공개되어 있다. 이 정답을 참고하여 Circle 클래스와 CircleManager 클래스를 수정하여 다음 실행 결과처럼 되게 하라.
x, y, radius >>3.0 3.0 5
x, y, radius >>2.5 2.7 6
x, y, radius >>5.0 2.0 4
가장 면적인 큰 원은 (2.5,2.7)6
7. 하루의 할 일을 표현하는 클래스 Day는 다음과 같다. 한 달의 할 일을 표현하는 MonthSchedule 클래스를 작성하라.
class Day {
private String work; //하루의 할 일을 나타내는 문자열
public void set(String work) { this.work = work; }
public String get() { return work; }
public void show() {
if(work == null) System.out.println("없습니다.");
else System.out.println(work+"입니다.");
}
}
package donghun;
import java.util.Scanner;
class Day {
private String work; //하루의 할 일을 나타내는 문자열
public void set(String work) { this.work = work; }
public String get() { return work; }
public void show() {
if(work == null) System.out.println("없습니다.");
else System.out.println(work+"입니다.");
}
}
public class dong_hun{
static Scanner scanner = new Scanner(System.in);
private int date;
private Day array[]; //객체 배열
public dong_hun(int date)
{
array = new Day[date];
for(int i=0;i<array.length;i++)
{
array[i] = new Day();
}
}
public void input()
{
System.out.print("날짜(1~30)?");
int day = scanner.nextInt();
System.out.print("할일(빈칸없이 입력)?");
String work = scanner.next();
array[day].set(work);
}
public void view()
{
System.out.print("날짜(1~30)?");
int day = scanner.nextInt();
System.out.println(day+"일의 할 일은");
array[day].show();
}
public void finish()
{
System.out.println("프로그램을 종료합니다");
}
public void run()
{
int ch;
System.out.println("이번달 스케쥴 관리 프로그램.");
while(true)
{
System.out.print("할일(입력:1, 보기:2, 끝내기:3)>>");
ch = scanner.nextInt();
if(ch==1)
{
input();
}
if(ch==2)
{
view();
}
if(ch==3)
{
finish();
break;
}
}
}
public static void main(String[] args) {
dong_hun april = new dong_hun(30);
april.run();
}
}
8. 이름(name), 전화번호(tel) 필드와 생성자 등을 가진 Phone 클래스를 작성하고, 실행 예시와 같이 작동하는 PhonBook 클래스를 작성하라.
인원수 >> 3
이름과 전화번호(이름과 번호는 빈 칸없이 입력)>>황기태 777-7777
이름과 전화번호(이름과 번호는 빈 칸없이 입력)>>나명품 999-9999
이름과 전화번호(이름과 번호는 빈 칸없이 입력)>>최자바 333-1234
저장되었습니다...
검색할 이름 >>황기순
황기순이 없습니다.
검색할 이름 >>최자바
최자바의 번호는 333-1234 입니다.
검색할 이름 >>그만
package donghun;
import java.util.Scanner;
class Phone{
private String name;
private String tel;
public Phone(String name, String tel)
{
this.name = name;
this.tel = tel;
}
public String getName()
{
return name;
}
public void show()
{
if(name==null)
{
System.out.println(name+" 이 없습니다.");
}
else
System.out.println(name+"의 번호는 "+ tel +"입니다");
}
}
public class dong_hun{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("인원 수>>");
int num = scanner.nextInt();
Phone array[] = new Phone[num];
for(int i=0;i<num;i++)
{
System.out.print("이름과 전화번호(이름과 번호는 빈칸없이 입력)>>");
String name = scanner.next();
String tel = scanner.next();
array[i] = new Phone(name,tel);
}
System.out.println("저장되었습니다...");
while(true)
{
System.out.print("검색할 이름>>");
String name = scanner.next();
if(name.equals("그만"))
{
System.out.println("종료합니다.");
break;
}
for(int i=0;i<num;i++)
{
if(name.equals(array[i].getName()))
{
array[i].show();
}
}
}
}
}
9. 다음 2개의 static 가진 ArrayUtil 클래스를 만들어보자. 다음 코드의 실행 결과를 참고하여 concat()와 print()를 작성하여 ArrayUtil 클래스를 완성하라.
[ 1 5 7 9 3 6 -1 100 77 ] |
10. 다음과 같은 Dictionary 클래스가 있다. 실행 결과와 같이 작동하도록 Dictionary 클래스의 kor2Eng() 메소드와 DicApp 클래스를 작성하라.
class Dictionary {
private static String[] kor = {"사랑", "아기", "돈", "미래", "희망"};
private static String[] eng = {"love", "baby", "money", "future","hope"};
public static String kor2Eng(String word) { /*검색 코드 작성*/ }
}
한영 단어 검색 프로그램입니다.
한글 단어?희망
희망은 hope
한글 단어?아가
아가는 저의 사전에 없습니다.
한글 단어?아기
아기는 baby
한글 단어?그만
11. 다수의 클래스를 만들고 활용하는 연습을 해보자. 더하기(+), 빼기(-), 곱하기(*), 나누기(/)를 수행하는 각 클래스 Add, Sub, Mul, Div를 만들어라. 이들은 모두 다음 필드와 메소드를 가진다.
package donghun;
import java.util.Scanner;
class Add{
private int a;
private int b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate()
{
return a+b;
}
}
class Sub{
private int a;
private int b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate()
{
return a-b;
}
}
class Mul{
private int a;
private int b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate()
{
return a*b;
}
}
class Div{
private int a;
private int b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate()
{
return a/b;
}
}
public class dong_hun{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String operator="";
int operand1;
int operand2;
System.out.print("두 정수와 연산자를 입력하시오>>");
operand1 = scanner.nextInt();
operand2 = scanner.nextInt();
operator = scanner.next();
switch(operator)
{
case "+":
Add a = new Add();
a.setValue(operand1,operand2);
System.out.println(a.calculate());
break;
case "-":
Sub b = new Sub();
b.setValue(operand1,operand2);
System.out.println(b.calculate());
break;
case "*":
Mul c = new Mul();
c.setValue(operand1,operand2);
System.out.println(c.calculate());
break;
case "/":
Div d = new Div();
d.setValue(operand1,operand2);
System.out.println(d.calculate());
break;
default:
}
}
}
12. 간단한 콘서트 예약 시스템을 만들어보자. 다수의 클래스를 다루고 객체의 배열을 다루기에는 아직 자바 프로그램 개발이 익숙하지 않은 초보자에게 다소 무리가 있을 것이다. 그러나 반드시 넘어야 할 산이다. 이 도전을 통해 산을 넘어갈 수 있는 체력을 키워보자. 예약 시스템의 기능은 다음과 같다.
package donghun;
import java.util.Scanner;
class reservation{
Scanner scanner = new Scanner(System.in);
private String S[];
private String A[];
private String B[];
public reservation()
{
S = new String[10];
A = new String[10];
B = new String[10];
for(int i=0;i<10;i++)
{
S[i]="---";
A[i]="---";
B[i]="---";
}
}
public void showSeat(String a[])
{
for(int i=0;i<10;i++)
{
System.out.print(a[i]+" ");
}
}
public void reserve()
{
System.out.print("좌석구분 S(1), A(2), B(3)>>");
int seat = scanner.nextInt();
System.out.print("이름>>");
String name = scanner.next();
System.out.print("번호>>");
int number = scanner.nextInt();
if(seat==1)
{
S[number-1] = name;
}
if(seat==2)
{
A[number-1] = name;
}
if(seat==3)
{
B[number-1] = name;
}
}
public void showAll()
{
for(int i=0;i<10;i++)
{
System.out.print(S[i]+" ");
}
System.out.println();
for(int i=0;i<10;i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
for(int i=0;i<10;i++)
{
System.out.print(B[i]+" ");
}
System.out.println();
System.out.println("<<<조회를 완료하였습니다.>>>");
}
public void delete(String a[])
{
System.out.print("이름>>");
String name = scanner.next();
for(int i=0;i<10;i++)
{
if(name.equals(a[i]))
{
a[i]="---";
}
}
}
public void cancel()
{
System.out.print("좌석 S:1, A:2, B:3>>");
int seatNum = scanner.nextInt();
if(seatNum==1)
{
showSeat(S);
System.out.println();
delete(S);
}
if(seatNum==2)
{
showSeat(A);
System.out.println();
delete(A);
}
if(seatNum==3)
{
showSeat(B);
System.out.println();
delete(B);
}
}
public void exit()
{
System.out.println("종료합니다.");
}
}
public class dong_hun{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("명품콘서트홀 예약 시스템입니다.");
reservation a = new reservation();
while(true)
{
System.out.println();
System.out.print("예약:1, 조회:2, 취소:3, 끝내기:4>>");
int num = scanner.nextInt();
if(num==1)
{
a.reserve();
}
if(num==2)
{
a.showAll();
}
if(num==3)
{
a.cancel();
}
if(num==4)
{
a.exit();
break;
}
}
}
}
'JAVA > JAVA 문제풀이' 카테고리의 다른 글
명품 자바 프로그래밍 5장 실습문제 (0) | 2023.08.14 |
---|---|
명품 자바 프로그래밍 5장 이론문제 (0) | 2023.08.14 |
명품 자바 프로그래밍 4장 이론문제 (0) | 2023.07.04 |
명품 자바 프로그래밍 3장 실습문제 (0) | 2023.06.30 |
명품 자바 프로그래밍 3장 이론문제 (0) | 2023.06.30 |