티스토리 뷰
반응형
✏️ 6장 실습문제 - 1
package ex;
import java.util.*;
class MyPoint{
private int x,y;
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(MyPoint p) {
if(this.x==p.x && this.y==p.y)
return true;
else
return false;
}
public String toString(){
String str = "point("+this.x+","+this.y+")";
return str;
}
}
public class SolveProblem{
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("다른 점");
}
}
✏️ 6장 실습문제 - 2
package ex;
import java.util.*;
class Circle{
private int x,y,r;
public Circle(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
}
public boolean equals(Circle c){
if(this.x==c.x&&this.y==c.y)
return true;
else
return false;
}
public String toString(){
String str = "Circle("+this.x+","+this.y+")반지름"+this.r;
return str;
}
}
public class SolveProblem{
public static void main(String[] args) {
Circle a = new Circle(2,3,5);
Circle b = new Circle(2,3,30);
System.out.println("원 a : "+a);
System.out.println("원 b : "+b);
if(a.equals(b))
System.out.println("같은 원");
else
System.out.println("다른 원");
}
}
✏️ 6장 실습문제 - 3
// main 패키지
package ex;
import etc.Calc;
import java.util.*;
public class SolveProblem{
public static void main(String[] args) {
Calc c = new Calc(10,20);
System.out.println(c.sum());
}
}
// etc 패키지
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;
}
}
✏️ 6장 실습문제 - 4
// Circle 클래스
package derived;
import base.Shape;
public class Circle extends Shape{
public void draw(){
System.out.println("Circle");
}
}
// Shape 클래스
package base;
public class Shape {
public void draw(){
System.out.println("Shape");
}
}
// GraphicEditor 클래스
package app;
import base.Shape;
import derived.Circle;
public class GraphicEditor {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
}
}
✏️ 6장 실습문제 - 5
package ex;
import java.util.*;
public class SolveProblem{
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int min = calendar.get(Calendar.MINUTE);
System.out.println("현재 시간은 "+hour+"시 "+min+"분입니다.");
if(hour>4 && hour<12)
System.out.println("Good Morning");
else if(hour<18&&hour>=12)
System.out.println("Good Afternoon");
else if(hour>=18&&hour<22)
System.out.println("Good Evening");
else
System.out.println("Good night");
}
}
✏️ 6장 실습문제 - 6
import java.util.Calendar;
import java.util.Scanner;
class Player {
Calendar now = Calendar.getInstance();
Scanner sc = new Scanner(System.in);
private String name;
private int sec1, sec2;
public Player(String name) {
this.name = name;
}
public int run() {
System.out.print(name + "시작 <Enter>키>>");
sec1 = enter();
System.out.print("10초 예상 후 <Enter>키>>");
sec2 = enter();
if(sec1 >= sec2)
return (sec2 + 60) - sec1;
else return sec2 - sec1;
}
public int enter() {
sc.nextLine();
now = Calendar.getInstance();
System.out.println("현재 초 시간 = " + now.get(Calendar.SECOND));
return now.get(Calendar.SECOND);
}
}
public class Prac06 {
public static void main(String[] args) {
Player player1 = new Player("황기태");
Player player2 = new Player("이재문");
System.out.println("10초에 가까운 사람이 이기는 게임입니다.");
int resultP1 = player1.run();
int resultP2 = player2.run();
if(Math.abs(resultP1 - 10) < Math.abs(resultP2 - 10))
System.out.println("황기태의 결과 " + resultP1 + ", 이재문의 결과 " + resultP2 + ", 승자는 황기태");
else
System.out.println("황기태의 결과 " + resultP1 + ", 이재문의 결과 " + resultP2 + ", 승자는 이재문");
}
}
✏️ 6장 실습문제 - 7
// 1번
package ex;
import java.util.*;
public class SolveProblem{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
System.out.print(">>");
String str = scanner.nextLine();
if(str.equals("그만")){
System.out.println("종료합니다...");
break;
}
StringTokenizer stringTokenizer = new StringTokenizer(str," ");
System.out.println("어절 개수는 "+stringTokenizer.countTokens());
}
scanner.close();
}
}
// 2번
package ex;
import java.util.*;
public class SolveProblem{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
System.out.print(">>");
String str = scanner.nextLine();
if(str.equals("그만")){
System.out.println("종료합니다...");
break;
}
String [] s =str.split(" ");
System.out.println("어절 개수는 "+s.length);
}
scanner.close();
}
}
✏️ 6장 실습문제 - 8
package ex;
import java.util.*;
public class SolveProblem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("문자열을 입력하세요. 빈칸이 있어도 되고 영어 한글 모두 됩니다.");
String str = scanner.nextLine();
for(int i=1;i<str.length()+1;i++){
System.out.print(str.substring(i));
for(int j=0;j<i;j++){
System.out.print(str.charAt(j));
}
System.out.println();
}
scanner.close();
}
}
✏️ 6장 실습문제 - 9
package ex;
import java.util.*;
class Player {
private String name;
Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int select() {
Scanner sc = new Scanner(System.in);
System.out.print("철수[가위(1), 바위(2), 보(3), 끝내기(4)]>>");
return sc.nextInt();
}
}
class Computer extends Player {
public Computer(String name) {
super(name);
}
public int select() {
return (int)(Math.random() * 3 + 1);
}
}
class Game {
private String[] game = {"가위", "바위", "보"};
Player[] player = new Player[2];
public Game() {
player[0] = new Player("철수");
player[1] = new Computer("컴퓨터");
}
public void run() {
int playerNum, computerNum;
while(true) {
playerNum = player[0].select();
if(playerNum < 1 || playerNum > 4)
System.out.println("잘못 입력했습니다.");
else if(playerNum == 4)
break;
computerNum = player[1].select();
System.out.print(player[0].getName() + "(" + game[playerNum - 1] + ") : ");
System.out.println(player[1].getName() + "(" + game[computerNum - 1] + ")");
if(playerNum == computerNum)
System.out.println("비겼습니다.");
else if((playerNum == 1 && computerNum == 2) || (playerNum == 2 && computerNum == 3) || (playerNum == 3 && computerNum == 1))
System.out.println(player[1].getName() + "가 이겼습니다.");
else if((playerNum == 1 && computerNum == 3) || (playerNum == 2 && computerNum == 1) || (playerNum == 3 && computerNum == 2))
System.out.println(player[0].getName() + "가 이겼습니다.");
}
}
}
public class Prac09 {
public static void main(String[] args) {
Game game = new Game();
game.run();
}
}
✏️ 6장 실습문제 - 10
package ex;
import java.util.*;
class Person{
private String name;
private Scanner sc;
private int[] arr;
public Person(String name) {
this.name = name;
sc = new Scanner(System.in);
arr = new int[3];
}
public String getName() {
return name;
}
public boolean run(){
System.out.print("["+this.name+"]:<Enter>");
boolean check = enter();
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+"\t");
arr[i]=0;
}
if(check)
return true;
else {
System.out.println("아쉽군요!");
return false;
}
}
public boolean enter(){
String s = sc.nextLine();
for(int i=0;i<arr.length;i++){
int n = (int)(Math.random()*3+1);
arr[i]=n;
}
if(arr[0]==arr[1]&&arr[1]==arr[2])
return true;
else {
return false;
}
}
}
public class SolveProblem{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("1번째 선수 이름>>");
String player1 = scanner.nextLine();
System.out.print("2번째 선수 이름>>");
String player2 = scanner.nextLine();
Person p1 = new Person(player1);
Person p2 = new Person(player2);
while(true){
boolean check = p1.run();
if(check){
System.out.println(p1.getName()+"님이 이겼습니다!");
break;
}
boolean check2 = p2.run();
if(check2){
System.out.println(p2.getName()+"님이 이겼습니다!");
break;
}
}
}
}
✏️ 6장 실습문제 - 11
package ex;
import java.util.*;
public class SolveProblem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(">>");
String str = scanner.nextLine();
StringBuffer stringBuffer = new StringBuffer(str);
while (true) {
System.out.print("명령: ");
String s = scanner.nextLine();
if (s.equals("그만")) {
System.out.println("종료합니다!");
break;
}
String[] tokens = s.split("!");
if (tokens.length != 2) {
System.out.println("잘못된 입력입니다");
continue;
} else {
if (tokens[0].length() == 0 || tokens[1].length() == 0) {
System.out.println("잘못된 명령입니다!");
continue;
}
int index = stringBuffer.indexOf(tokens[0]);
System.out.println(index);
if(index==-1){
System.out.println("잘못된 명령입니다!");
continue;
}
stringBuffer.replace(index,index+tokens[0].length(),tokens[1]);
System.out.println(stringBuffer.toString());
}
}
}
}
✏️ 6장 실습문제 - 12
package ex;
import java.util.*;
class Person{
private String name;
private Scanner sc;
private int[] arr;
public Person(String name) {
this.name = name;
sc = new Scanner(System.in);
arr = new int[3];
}
public String getName() {
return name;
}
public boolean run(){
System.out.print("["+this.name+"]:<Enter>");
boolean check = enter();
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+"\t");
arr[i]=0;
}
if(check)
return true;
else {
System.out.println("아쉽군요!");
return false;
}
}
public boolean enter(){
String s = sc.nextLine();
for(int i=0;i<arr.length;i++){
int n = (int)(Math.random()*3+1);
arr[i]=n;
}
if(arr[0]==arr[1]&&arr[1]==arr[2])
return true;
else {
return false;
}
}
}
public class SolveProblem{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("겜블링 게임에 참여할 선수 숫자>>");
int size = scanner.nextInt();
Person [] p = new Person[size];
for(int i=0;i<p.length;i++){
System.out.print(i+1+"번째 선수 이름>>");
String player = scanner.next();
p[i]=new Person(player);
}
while(true){
boolean isFinsih = false;
for(int i=0;i<p.length;i++){
boolean check = p[i].run();
if(check){
System.out.println(p[i].getName()+"님이 이겼습니다!");
isFinsih=true;
break;
}
}
if(isFinsih)
break;
}
}
}
반응형
'PL > JAVA' 카테고리의 다른 글
명품 자바 프로그래밍 8장 실습 문제 (0) | 2024.06.21 |
---|---|
명품 자바 프로그래밍 7장 실습 문제 (0) | 2024.06.20 |
명품 자바 프로그래밍 5장 실습 문제 (0) | 2024.06.19 |
명품 자바 프로그래밍 4장 실습 문제 (0) | 2024.06.18 |
명품 자바 프로그래밍 3장 실습 문제 (0) | 2024.06.18 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백준
- BFS
- Do it!
- 자바
- 이분 매칭
- 알고리즘
- 카운팅 정렬
- C++
- DFS
- 세그먼트 트리
- 알고리즘 공부
- 에라토스테네스의 체
- 유클리드 호제법
- 투 포인터
- 자바스크립트
- 스택
- C++ Stack
- 반복문
- 백준 풀이
- CSS
- java
- 스프링 부트 crud 게시판 구현
- 자료구조
- html
- HTML5
- 우선순위 큐
- c++ string
- js
- DP
- 유니온 파인드
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
글 보관함