728x90
반응형
💡
[정답]
import java.util.Objects;
class MyPoint {
private int x, y;
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point (" + x + ", " + y + ")";
}
@Override
public boolean equals(Object obj) {
if (obj instanceof MyPoint) {
MyPoint p = (MyPoint) obj;
return this.x == p.x && this.y == p.y;
}
return false;
}
}
public class DaraSolution {
public static void main(String[] args) {
MyPoint p = new MyPoint(3, 50);
MyPoint q = new MyPoint(4, 50);
System.out.println(p);
if (p.equals(q)) {
System.out.println("같은 점");
} else {
System.out.println("다른 점");
}
}
}
💡
[정답]
import java.util.Objects;
class Circle {
private int x, y;
private int radius;
public Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
@Override
public String toString() {
return "Circle(" + x + ", " + y + ") 반지름 " + radius;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Circle) {
Circle c = (Circle) obj;
return x == c.x && y == c.y && radius == c.radius;
}
return false;
}
}
public class DaraSolution {
public static void main(String[] args) {
Circle a = new Circle(2, 3, 5);
Circle b = new Circle(2, 3, 50);
System.out.println("원 a: " + a);
System.out.println("원 b: " + a);
if (a.equals(b)) {
System.out.println("같은 원");
} else {
System.out.println("서로 다른 원");
}
}
}
💡
[정답]
package etc;
public class Calc {
private int x, y;
public Calc(int x, int y) { this.x = x; this.y = y; }
public int sum() { return x+y; }
}
package main;
import etc.Calc;
public class MainApp {
public static void main(String[] args) {
Calc c = new Calc(10, 20);
System.out.println(c.sum());
}
}
💡
[정답]
package base;
public class Shape {
public void draw() { System.out.println("Shape"); }
}
package derived;
import base.Shape;
public class Circle extends Shape {
public void draw() { System.out.println("Circle"); }
}
package app;
import base.Shape;
import derived.Circle;
public class GraphicEditor {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
}
}
💡
[정답]
import java.util.Calendar;
public class DaraSolution {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
int hourOfDay = now.get(Calendar.HOUR_OF_DAY);
if(4<=hourOfDay && hourOfDay<12)
System.out.println("Good Morning");
else if(12<=hourOfDay && hourOfDay<18)
System.out.println("Good Afternoon");
else if(18<=hourOfDay && hourOfDay<22)
System.out.println("Good Evening");
else
System.out.println("Good Night");
}
}
💡
[정답]
import java.util.Scanner;
class Player {
private String name;
private long elapsedTime;
Player(String name) {
this.name = name;
this.elapsedTime = 0;
}
String getName() {
return name;
}
void setElapsedTime(long elapsedTime) {
this.elapsedTime = elapsedTime;
}
long getElapsedTime() {
return elapsedTime;
}
}
class StopWatch {
private long startTime;
void start() {
startTime = System.currentTimeMillis();
}
long stop() {
return (System.currentTimeMillis() - startTime) / 1000;
}
}
class TimeGame {
private Player player1;
private Player player2;
private StopWatch stopWatch;
TimeGame(Player player1, Player player2) {
this.player1 = player1;
this.player2 = player2;
this.stopWatch = new StopWatch();
}
void playGame() {
Scanner scanner = new Scanner(System.in);
System.out.println("10초에 가까운 사람이 이기는 게임입니다.");
// Player 1
System.out.println(player1.getName() + " 시작 <Enter>키 >>");
scanner.nextLine();
stopWatch.start();
System.out.println("현재 초 시간 = " + (System.currentTimeMillis() / 1000 % 60));
System.out.println("10초 예상후 <Enter>키 >>");
scanner.nextLine();
long player1ElapsedTime = stopWatch.stop();
player1.setElapsedTime(player1ElapsedTime);
System.out.println("현재 초 시간 = " + (System.currentTimeMillis() / 1000 % 60));
// Player 2
System.out.println(player2.getName() + " 시작 <Enter>키 >>");
scanner.nextLine();
stopWatch.start();
System.out.println("현재 초 시간 = " + (System.currentTimeMillis() / 1000 % 60));
System.out.print("10초 예상후 <Enter>키 >>");
scanner.nextLine();
long player2ElapsedTime = stopWatch.stop();
player2.setElapsedTime(player2ElapsedTime);
System.out.println("현재 초 시간 = " + (System.currentTimeMillis() / 1000 % 60));
// Show results
System.out.println(player1.getName() + "의 결과 " + player1.getElapsedTime() + "초, " +
player2.getName() + "의 결과 " + player2.getElapsedTime() +
"초, 승자는 " + decideWinner().getName());
}
Player decideWinner() {
long targetTime = 10; // 10 seconds
long player1Difference = Math.abs(targetTime - player1.getElapsedTime());
long player2Difference = Math.abs(targetTime - player2.getElapsedTime());
if (player1Difference < player2Difference) {
return player1;
} else {
return player2;
}
}
}
public class DaraSolution {
public static void main(String[] args) {
Player player1 = new Player("황기태");
Player player2 = new Player("이재문");
TimeGame timeGame = new TimeGame(player1, player2);
timeGame.playGame();
}
}
💡
[정답]
import java.util.Scanner;
public class DaraSolution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print(">> ");
String input = scanner.nextLine();
if (input.equals("그만")) {
System.out.println("종료합니다...");
break;
}
String[] words = input.split(" ");
System.out.println("어절 개수는 " + words.length);
}
scanner.close();
}
}
💡
[정답]
import java.util.Scanner;
public class DaraSolution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("문자열을 입력하세요. 빈칸이 있어도 되고 영어 한글 모두 됩니다.");
String str = scanner.nextLine();
// 문자열의 길이만큼 반복
for (int i = 0; i < str.length(); i++) {
String rotated = str.substring(i) + str.substring(0, i);
System.out.println(rotated);
}
}
}
💡
[정답]
import java.util.Scanner;
public class DaraSolution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("가위바위보 게임입니다.");
System.out.print("철수[가위(1), 바위(2), 보(3), 끝내기(4)]>>");
int user = scanner.nextInt();
int computer = (int) (Math.random() * 3) + 1;
// 가위바위보 결과 출력
while (true) {
if (user == 4) {
System.out.println("게임을 종료합니다...");
break;
}
if (user < 1 || user > 4) {
System.out.println("잘못 입력하셨습니다. 다시 입력해주세요.");
System.out.print("철수[가위(1), 바위(2), 보(3), 끝내기(4)]>> ");
user = scanner.nextInt();
continue;
}
if (user == 1) {
System.out.print("철수: 가위, ");
} else if (user == 2) {
System.out.print("철수: 바위, ");
} else if (user == 3) {
System.out.print("철수: 보, ");
}
if (computer == 1) {
System.out.println("컴퓨터: 가위");
} else if (computer == 2) {
System.out.println("컴퓨터: 바위");
} else if (computer == 3) {
System.out.println("컴퓨터: 보");
}
// 가위바위보 결과 판별
if (user == computer) {
System.out.println("비겼습니다.");
} else if ((user == 1 && computer == 3) ||
(user == 2 && computer == 1) || (user == 3 && computer == 2)) {
System.out.println("철수가 이겼습니다.");
} else {
System.out.println("컴퓨터가 이겼습니다.");
}
System.out.print("철수[가위(1), 바위(2), 보(3), 끝내기(4)]>>");
user = scanner.nextInt();
computer = (int) (Math.random() * 3) + 1;
}
}
}
💡
[정답]
import java.util.Scanner;
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class GamblingGame {
private Person p1, p2;
public GamblingGame(String name1, String name2) {
p1 = new Person(name1);
p2 = new Person(name2);
}
public void startGame() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("[" + p1.getName() + "]: <Enter> 키 >>");
scanner.nextLine();
int num1 = (int) (Math.random() * 3) + 1;
int num2 = (int) (Math.random() * 3) + 1;
int num3 = (int) (Math.random() * 3) + 1;
System.out.println("\t" + num1 + " " + num2 + " " + num3);
System.out.print("[" + p2.getName() + "]: <Enter> 키 >>");
scanner.nextLine();
int num4 = (int) (Math.random() * 3) + 1;
int num5 = (int) (Math.random() * 3) + 1;
int num6 = (int) (Math.random() * 3) + 1;
System.out.println("\t" + num4 + " " + num5 + " " + num6);
if (num1 == num2 && num2 == num3) {
System.out.println(p1.getName() + "님이 이겼습니다!");
break;
} else if (num4 == num5 && num5 == num6) {
System.out.println(p2.getName() + "님이 이겼습니다!");
break;
} else {
System.out.println("아쉽군요!");
}
}
}
}
public class DaraSolution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("1번째 선수 이름>> ");
String name1 = scanner.next();
System.out.print("2번째 선수 이름>> ");
String name2 = scanner.next();
GamblingGame game = new GamblingGame(name1, name2);
game.startGame();
scanner.close();
}
}
💡
[정답]
import java.util.Scanner;
public class DaraSolution {
public static void main(String[] args) {
// 문자열 입력받기
Scanner scanner = new Scanner(System.in);
System.out.print(">>");
StringBuffer text = new StringBuffer(scanner.nextLine());
while (true) {
// 명령어 입력받기
System.out.print("명령 : ");
String command = scanner.nextLine();
if (command.equals("그만")) {
System.out.println("종료합니다.");
break;
} else if (command.contains("!")) {
// 명령어 처리
String[] parts = command.split("!");
if (parts.length == 2) {
String target = parts[0];
String replacement = parts[1];
int index = text.indexOf(target);
// 문자열 치환
if (index != -1) {
text.replace(index, index + target.length(), replacement);
} else {
System.out.println("찾을 수 없습니다!");
}
} else {
System.out.println("잘못된 명령입니다!");
}
} else {
System.out.println("잘못된 명령입니다!");
}
System.out.println(text);
}
}
}
💡
[정답]
import java.util.Scanner;
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class GamblingGame {
private Person[] players;
public GamblingGame(Person[] players) {
this.players = players;
}
public void startGame() {
Scanner scanner = new Scanner(System.in);
while (true) {
for (Person player : players) {
System.out.print("[" + player.getName() + "]: <Enter> 키 >>");
scanner.nextLine();
int num1 = (int) (Math.random() * 3) + 1;
int num2 = (int) (Math.random() * 3) + 1;
int num3 = (int) (Math.random() * 3) + 1;
System.out.println("\t" + num1 + " " + num2 + " " + num3);
if (num1 == num2 && num2 == num3) {
System.out.println(player.getName() + "님이 이겼습니다!");
return;
}
}
System.out.println("아쉽군요! 다시 시도하세요.");
}
}
}
public class DaraSolution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("갬블링 게임에 참여한 선수 숫자>> ");
int numPlayers = scanner.nextInt();
scanner.nextLine();
Person[] players = new Person[numPlayers];
for (int i = 0; i < numPlayers; i++) {
System.out.print((i + 1) + "번째 선수 이름>> ");
String name = scanner.nextLine();
players[i] = new Person(name);
}
GamblingGame game = new GamblingGame(players);
game.startGame();
scanner.close();
}
}
반응형
'대학교 > 명품 Java programming 문제' 카테고리의 다른 글
[Java] 명품 Java Programming 7장 연습 문제 풀이 해설 (실습 문제) (2) | 2024.06.05 |
---|---|
[Java] 명품 Java Programming 7장 연습 문제 풀이 해설 (이론 문제) (0) | 2024.06.04 |
[Java] 명품 Java Programming 6장 연습 문제 풀이 해설 (이론 문제) (0) | 2024.05.24 |
[Java] 명품 Java Programming 5장 연습 문제 풀이 해설 (실습 문제) (0) | 2024.05.22 |
[Java] 명품 Java Programming 5장 연습 문제 풀이 해설 (이론 문제) (0) | 2024.05.21 |