728x90
728x170
명품 JAVA Programming 4장 실습문제 정답
명품 JAVA Programming 4장 실습문제 정답
1번
public class MyExp {
String TV;
int y;
int d;
public MyExp(String TV, int y, int d) {
this.TV = TV;
this.y = y;
this.d = d;
}
void show() {
System.out.println(this.TV + "에서 만든 " + this.y + "년형 " + this.d + "인치 TV");
}
public static void main(String[] args) {
MyExp MyExp = new MyExp("LG", 2017, 32);
MyExp.show();
}
}
2번
import java.util.Scanner;
public class Grade {
private int math, science, english;
public Grade(int math, int science, int english) {
this.math = math;
this.science = science;
this.english = english;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getScience() {
return science;
}
public void setScience(int science) {
this.science = science;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int average() {
return (math + science + english) / 3;
}
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());
scanner.close();
}
}
3번
public class Song {
private String title, artist, country, composer[];
private int year, track;
public Song(String title, String artist, int year, String country) {
this.title = title;
this.artist = artist;
this.year = year;
this.country = country;
}
public Song() {
this(null, null, 0, null);
}
public void show() {
System.out.println("노래 제목 : " + title);
System.out.println("가수 : " + artist);
System.out.println("연도 : " + year);
System.out.println("나라 : " + country);
}
public static void main(String[] args) {
Song song = new Song("Dancing Queen", "ABBA", 1978, "스웨덴국적의");
song.show();
}
}
4번
public class Rectangle {
int x, y, width, height;
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int square() {
return this.width * this.height;
}
public void show() {
System.out.printf("(%d,%d) 에서 크기가 %dX%d인 사각형\n", this.x, this.y, this.width, this.height);
}
public boolean contains(Rectangle cR) {
if (cR.x > this.x && cR.y > this.y) {
if (cR.x + cR.width < this.x + this.width && cR.y + cR.height < this.y + this.height) {
return true;
}
}
return false;
}
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를 포함합니다.");
}
}
5번
this.x = x; this.y = y; this.radius = radius;
System.out.println("(" + x + "," + y + ")" + radius);
new Scanner(System.in);
new Circle[3];
c.length
double x = scanner.nextDouble();
double y = scanner.nextDouble();
int radius = scanner.nextInt();
new Circle(x, y, radius);
c[i].show.close();
6번
import java.util.Scanner;
public class Circle {
private double x, y;
private int radius;
public Circle(double x, double y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public double area() {
return radius * radius * 3.14;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Circle c[] = new Circle[3];
double max;
int j = 0;
for (int i = 0; i < c.length; i++) {
System.out.print("x, y, radius==>");
Double x = s.nextDouble();
Double y = s.nextDouble();
int radius = s.nextInt();
c[i] = new Circle(x, y, radius);
}
max = c[0].area();
for (int i = 0; i < c.length; i++) {
if (max < c[i].area()) {
max = c[i].area();
j = i;
}
}
System.out.println("가장 큰 면적인 큰 원은" + "(" + c[j].x + "," + c[j].y + ")" + c[j].radius);
}
}
7번
public class Day {
private String work;
public Day(String work) {
this.work = 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 + "입니다.");
System.out.println();
}
}
import java.util.Scanner;
public class MonthSchedule {
private int x;
Day[] day = null;
Scanner sc = new Scanner(System.in);
public MonthSchedule(int x) {
this.x = x;
day = new Day[x];
}
public void input() {
System.out.print("날짜(1~30)?");
int i = sc.nextInt();
System.out.print("할일(빈칸없이입력)?");
String work = sc.next();
day[i] = new Day(work);
System.out.println();
}
public void view() {
System.out.print("날짜(1~30)?");
int date = sc.nextInt();
System.out.print(date + "일의 " + "할 일은 " + day[date]);
}
public void finish() {
System.out.println("프로그램을 종료합니다.");
}
public void run() {
while (true) {
System.out.print("할일(입력:1, 보기:2, 끝내기:3)" + " >>");
int sel = sc.nextInt();
if (sel == 1) {
input();
} else if (sel == 2) {
view();
} else if (sel == 3) {
finish();
break;
} else {
System.out.println("1, 2, 3중에 입력하세요.");
}
}
}
public static void main(String[] args) {
MonthSchedule april = new MonthSchedule(30);
System.out.println("이번달 스케쥴 관리 프로그램.");
april.run();
}
}
8번
9번
10번
11번
import java.util.Scanner;
class Add {
int a, b;
void setValue(int a, int b) {
this.a = a;
this.b = b;
}
int calculate() {
return a + b;
}
}
class Sub {
int a, b;
void setValue(int a, int b) {
this.a = a;
this.b = b;
}
int calculate() {
return a - b;
}
}
class Mul {
int a, b;
void setValue(int a, int b) {
this.a = a;
this.b = b;
}
int calculate() {
return a * b;
}
}
class Div {
int a, b;
void setValue(int a, int b) {
this.a = a;
this.b = b;
}
double calculate() {
return (double) a / b;
}
}
public class Cal {
public static void main(String[] args) {
System.out.print("두 정수와 연산자를 입력하시오>>");
Scanner sd = new Scanner(System.in);
int x = sd.nextInt();
int y = sd.nextInt();
char ch = sd.next().charAt(0);
switch (ch) {
case '+':
Add a = new Add();
a.setValue(x, y);
System.out.println(a.calculate());
break;
case '-':
Sub s = new Sub();
s.setValue(x, y);
System.out.println(s.calculate());
break;
case '*':
Mul m = new Mul();
m.setValue(x, y);
System.out.println(m.calculate());
break;
case '/':
Div d = new Div();
d.setValue(x, y);
System.out.println(d.calculate());
break;
}
}
}
12번
import java.util.Scanner;
class Reservesystem {
String[] sname;
String[] aname;
String[] bname;
int seat;
Reservesystem() {
sname = new String[10];
aname = new String[10];
bname = new String[10];
for (int i = 0; i < sname.length; i++) {
sname[i] = "---";
aname[i] = "---";
bname[i] = "---";
}
}
void printSname() {
System.out.print("S>>");
for (int i = 0; i < sname.length; i++) {
System.out.print(" " + sname[i]);
}
System.out.println();
}
void printAname() {
System.out.print("A>>");
for (int i = 0; i < aname.length; i++) {
System.out.print(" " + aname[i]);
}
System.out.println();
}
void printBname() {
System.out.print("B>>");
for (int i = 0; i < bname.length; i++) {
System.out.print(" " + bname[i]);
}
System.out.println();
}
void inquiry() {
printSname();
printAname();
printBname();
System.out.println("<<<조회를 완료하였습니다.>>>");
}
void seatchoice() {
System.out.print("좌석 구분 S(1), A(2), B(3) >> ");
Scanner s = new Scanner(System.in);
seat = s.nextInt();
}
void setSeat(String name, int num) {
switch (seat) {
case 1:
if (sname[num - 1] == "---") // 중복 검
sname[num - 1] = name;
else
System.out.println("이미 예약된 자리");
break;
case 2:
if (aname[num - 1] == "---")
aname[num - 1] = name;
else
System.out.println("이미 예약된 자리");
break;
case 3:
if (bname[num - 1] == "---")
bname[num - 1] = name;
else
System.out.println("이미 예약된 자리");
break;
default:
System.out.println("없는 메뉴");
break;
}
}
void printAllseat() {
switch (seat) {
case 1:
printSname();
break;
case 2:
printAname();
break;
case 3:
printBname();
break;
default:
System.out.println("없는 메뉴");
break;
}
}
void reservation() {
seatchoice();
String name;
int num;
printAllseat();
System.out.print("이름 : ");
Scanner s = new Scanner(System.in);
name = s.next();
System.out.print("번호 : ");
num = s.nextInt();
if (num > 0 && num < 11)
setSeat(name, num);
else
System.out.println("없는 번호");
}
void cancel() {
seatchoice();
printAllseat();
String name;
int i = 0;
System.out.print("이름 : ");
Scanner s = new Scanner(System.in);
name = s.next();
switch (seat) {
case 1:
for (; i < sname.length; i++) {
if (sname[i].equals(name)) {
sname[i] = "---";
break;
}
}
break;
case 2:
for (; i < aname.length; i++) {
if (aname[i].equals(name)) {
aname[i] = "---";
break;
}
}
break;
case 3:
for (; i < bname.length; i++) {
if (bname[i].equals(name)) {
bname[i] = "---";
break;
}
}
break;
default:
System.out.println("없는 메뉴");
break;
}
if (i == 10) {
System.out.println("없는 이름");
}
}
}
import java.util.Scanner;
public class Concert {
public static void main(String[] args) {
Reservesystem r = new Reservesystem();
while (true) {
System.out.print("예약(1), 조회(2), 취소(3), 끝내기(4) >> ");
Scanner sd = new Scanner(System.in);
int ch = sd.nextInt();
switch (ch) {
case 1:
r.reservation();
break;
case 2:
r.inquiry();
break;
case 3:
r.cancel();
case 4:
break;
default:
System.out.println("없는 메뉴");
break;
}
if (ch == 4) {
break;
}
}
}
}
728x90
그리드형
'IT > 프로그래밍' 카테고리의 다른 글
명품 JAVA Programming 6장 실습문제 정답 (0) | 2020.12.12 |
---|---|
명품 JAVA Programming 5장 실습문제 정답 (0) | 2020.12.12 |
명품 JAVA Programming 3장 실습문제 정답 (0) | 2020.12.12 |
명품 JAVA Programming 2장 실습문제 정답 (0) | 2020.12.12 |
명품 JAVA Programming 1장 실습문제 정답 (0) | 2020.12.12 |
댓글