대학교 수업/Java 프로그래밍

[JAVA] 명품 자바 프로그래밍 6장 실습문제

2022. 6. 12. 02:42

실습문제

1번

import java.util.Scanner;
class MyPoint{
    private int x, y;
    public MyPoint(int x, int y) {this.x = x; this.y = y;}
    public String toString(){
        return "Point(" + x + ", " + y + ")";
    }
    public boolean equals(Object obj){
        MyPoint p = (MyPoint)obj;
        if(x == p.x && y == p.y){
            return true;
        }
        return false;
    }
}

public class main {
    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("다른 점");

    }
}

1번

2번

import java.util.Scanner;
class Circle{
    private int x, y, radius;
    public Circle(int x, int y, int radius) {this.x = x; this.y = y; this.radius = radius;}
    public String toString(){
        return "Circle(" + x + ", " + y + ")반지름" + radius;
    }
    public boolean equals(Object obj){
        Circle p = (Circle)obj;
        if(x == p.x && y == p.y){
            return true;
        }
        return false;
    }
}

public class main {
    public static void main(String[] args) {
        Circle a = new Circle(2, 3, 5); // 중심 (2, 3)에 반지름 5인 원
        Circle b = new Circle(2, 3, 30); // 중심 (2, 3)에 반지름 30인 원
        System.out.println("원 a : " + a);
        System.out.println("원 b : " + b);
        if(a.equals(b))
            System.out.println("같은 원");
        else
            System.out.println("서로 다른 원");

    }
}

2번

 

3번

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());
	}
}

 

4번

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("Cirlce"); }
}

package app;
import base.Shape
import derived.Circle
public class GraphicEditor {
	public static void main(String[] args) {
		Shape shape = new Circle();
		shape.draw();
	}
}

 

 

5번

import java.util.Calendar;

public class main {
    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();

        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        System.out.println("현재 시간은 " + hour + "시 " + minute + "분입니다.");

        if(hour > 4 && hour < 12)
            System.out.println("Good Morning");
        else if(hour >= 12 && hour < 18)
            System.out.println("Good Afternoon");
        else if(hour >= 18 && hour < 22)
            System.out.println("Good Evening");
        else
            System.out.println("Good Night");
    }
}

5번
현재 시각

 

6번

import java.util.Calendar;
import java.util.Scanner;

class Enters{
    private Scanner sc;
    private Calendar now;
    private String enter;
    private int time;
    public int enter(){
        sc = new Scanner(System.in);
        enter = sc.nextLine();
        now = Calendar.getInstance();
        time =now.get(Calendar.SECOND);
        System.out.println("\t현재 초 시간 = "+ time);
        return time;
    }
}

public class main {
    public static void main(String[] args) {
        String names[] = {"황기태", "이재문"};
        Enters enter = new Enters();
        int time1, time2;
        int times[] = new int[2];
        System.out.println("10초에 가까운 사람이 이기는 게임입니다.");

        for(int i = 0; i < 2; i++){
            System.out.print(names[i] + " 시작 <Enter>키 >>");
            time1 = enter.enter();
            System.out.print(names[i] + " 10초 예상 후 <Enter>키 >>");
            time2 = enter.enter();
            if(time1 > time2)
                times[i] = (60 - time1) - time2;
            else
                times[i] = time1 - time2;
        }
        times[0] = Math.abs(times[0]);
        times[1] = Math.abs(times[1]);
        if(times[0] > times[1])
            System.out.println("황기태의 결과 "+times[0]+", 이재문의 결과 "+times[1]+", 승자는 황기태");
        else
            System.out.println("황기태의 결과 "+times[0]+", 이재문의 결과 "+times[1]+", 승자는 이재문");

    }
}

6번

 

7번

(1)

import java.util.Scanner;
import java.util.StringTokenizer;


public class main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.print(">>");
            String s = scanner.nextLine();
            if (s.equals("그만")) {
                System.out.println("종료합니다....");
                break;
            }
            StringTokenizer st = new StringTokenizer(s, " ");
            System.out.println("어절 개수는 " + st.countTokens());
        }
    }
}

 

(2)

import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.print(">>");
            String s = scanner.nextLine();
            if (s.equals("그만")) {
                System.out.println("종료합니다....");
                break;
            }
            String str[] = s.split(" ");
            System.out.println("어절 개수는 " + str.length);
        }


    }
}

7번

 

8번

import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("문자열을 입력하세요. 빈칸이나 있어도 되고 영어 한글 모두 됩니다.");
        String sc = scanner.nextLine();
        for(int i = 0; i < sc.length(); i++){
            System.out.print(sc.substring(i));
            System.out.println(sc.substring(0, i));
        }

    }
}

8번

 

9번

import java.util.Scanner;

class Player {
    private String name;

    public 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 RSPGame {
    private final String rsp[] = {"가위", "바위", "보"};
    Player[] player = new Player[2];

    public RSPGame() {
        player[0] = new Player("철수");
        player[1] = new Computer("컴퓨터");
    }

    public void Run() {
        int userchoice, computerchoice;

        while(true) {
            userchoice = player[0].select();

            if(userchoice<1 || 4<userchoice)
                System.out.println("잘못된 입력");
            else if(userchoice == 4)
                break;

            computerchoice = player[1].select();

            System.out.println(player[0].getName() + "(" + rsp[userchoice-1] + ") : " + player[1].getName() + "(" + rsp[computerchoice-1] + ")");

            switch(userchoice-computerchoice) {
                case 0:
                    System.out.println("무승부입니다.");
                    break;
                case 2:
                case -1:
                    System.out.println(player[1].getName() + "가 이겼습니다.");
                    break;
                case -2:
                case 1:
                    System.out.println(player[0].getName() + "가 이겼습니다.");
                    break;
            }
        }
    }
}

public class main {
    public static void main(String[] args) {
        RSPGame game = new RSPGame();
        game.Run();
    }
}

9번

 

10번

import java.util.Random;
import java.util.Scanner;

class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}


class GambleGame {
    private Person[] player = new Person[2];
    public Scanner sc;
    public int index;
    public String buffer;

    public GambleGame() {
        sc = new Scanner(System.in);
        index = 0;
    }

    public void run() {
        System.out.print("1번째 선수 이름 >>");
        player[0] = new Person(sc.nextLine());
        System.out.print("2번째 선수 이름 >>");
        player[1] = new Person(sc.nextLine());


        while(true) {
            System.out.print("[" + player[index].getName() + "]:<Enter>");
            sc.nextLine();
            int a = (int)(Math.random()*3+1);
            int b = (int)(Math.random()*3+1);
            int c = (int)(Math.random()*3+1);
            System.out.print("\t" + a + "\t" + b + "\t" + c + "\t");
            if(a == b && b == c){
                System.out.println(player[index].getName() + "님이 이겼습니다!");
                break;
            }
            else{
                System.out.println("아쉽군요!");
                index = index == 1 ? 0 : 1;
            }
        }
    }
}

public class main {
    public static void main(String[] args) {
        GambleGame game = new GambleGame();
        game.run();
    }
}

10번

 

11번

import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        System.out.print(">>");
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();
        StringBuffer sb = new StringBuffer(text);

        while(true) {
            System.out.print("명령: ");
            String cmd = scanner.nextLine(); // 한 라인을 문자열로 읽는다.
            if(cmd.equals("그만")) {
                System.out.print("종료합니다");
                break;
            }
            String [] tokens = cmd.split("!");
            if(tokens.length != 2) {
                System.out.println("잘못된 명령입니다!");
            }
            else {
                if(tokens[0].length() == 0 || tokens[1].length() == 0) {
                    System.out.println("잘못된 명령입니다!");
                    continue;
                }

                int index = sb.indexOf(tokens[0]);
                if(index == -1) { // not found!
                    System.out.println("찾을 수 없습니다!");
                    continue;
                }
                sb.replace(index, index+tokens[0].length(), tokens[1]);
                System.out.println(sb.toString());
            }
        }
    }
}

11번

 

12번

import java.util.Random;
import java.util.Scanner;

class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}


class GambleGame {
    private Person[] player;
    public Scanner sc;
    public int index, temp;
    public String buffer;

    public GambleGame() {
        sc = new Scanner(System.in);
        index = 0;
        temp = 0;
    }

    public void run() {

        System.out.print("겜블링 게임에 참여할 선수 숫자>>");
        index = sc.nextInt();
        player = new Person[index];
        buffer = sc.nextLine();
        for(int i = 0; i < index; i++){
            System.out.print(i+1 + "번째 선수 이름>>");
            player[i] = new Person(sc.nextLine());
        }

        while(true) {
            System.out.print("[" + player[temp].getName() + "]:<Enter>");
            sc.nextLine();
            int a = (int)(Math.random()*3+1);
            int b = (int)(Math.random()*3+1);
            int c = (int)(Math.random()*3+1);
            System.out.print("\t" + a + "\t" + b + "\t" + c + "\t");
            if(a == b && b == c){
                System.out.println(player[temp].getName() + "님이 이겼습니다!");
                break;
            }
            else{
                System.out.println("아쉽군요!");
                temp++;
                temp = index - 1 >= temp ? temp : 0;
            }
        }
    }
}

public class main {
    public static void main(String[] args) {
        GambleGame game = new GambleGame();
        game.run();
    }
}

12번

저작자표시 비영리 (새창열림)

'대학교 수업 > Java 프로그래밍' 카테고리의 다른 글

[JAVA] 명품 자바 프로그래밍 9장 실습문제  (0) 2022.10.03
[JAVA] 명품 자바 프로그래밍 7장 실습문제  (0) 2022.06.14
[JAVA] 명품 자바 프로그래밍 5장 실습문제  (0) 2022.06.11
[JAVA] 명품 자바 프로그래밍 4장 실습문제  (0) 2022.04.17
[JAVA] 명품 자바 프로그래밍 3장 실습문제  (0) 2022.04.15
'대학교 수업/Java 프로그래밍' 카테고리의 다른 글
  • [JAVA] 명품 자바 프로그래밍 9장 실습문제
  • [JAVA] 명품 자바 프로그래밍 7장 실습문제
  • [JAVA] 명품 자바 프로그래밍 5장 실습문제
  • [JAVA] 명품 자바 프로그래밍 4장 실습문제
Jongung
Jongung
프론트 개발을 주로하는 사람
Jongung
기록하는 습관
Jongung
전체
오늘
어제
  • 분류 전체보기 (294)
    • 회고록 (4)
    • 프론트엔드 개발 (24)
      • 온누리학교 웹 개발 프로젝트 (8)
      • Trend In One 프로젝트 (0)
      • Mega Waka Board 프로젝트 (1)
      • React (11)
      • React Native (3)
      • Recoil (1)
    • 백엔드 개발 (5)
      • Node.js (2)
      • Mega Waka Board 프로젝트 (2)
      • DB & SQL (1)
    • Flutter 개발 (8)
      • Focusit 앱 프로젝트 (4)
      • Flutter 개념 (3)
    • 파이썬 개발 (5)
      • 디스코드 봇 개발 (2)
    • CS 공부 (6)
      • 기초 알고리즘 (5)
      • GIT (1)
    • 백준 알고리즘 (70)
      • Lang-C | C++ (26)
      • Lang-C# (12)
      • Lang-node.js (26)
      • Lang-Python (6)
    • Codeup.kr (101)
      • C언어 기초 100제 (93)
      • 기초 100문제 후기 (1)
      • Lang-C (7)
    • 대학교 수업 (44)
      • C 프로그래밍 (4)
      • C++ 프로그래밍 (13)
      • Java 프로그래밍 (15)
      • 데이터 통신 네트워크 (12)
    • 소통하는 웅이 (6)
      • 티스토리 이동기 (3)
      • IT 제품 리뷰 (2)
    • 대외 활동 (10)
      • SW마에스트로 (2)
      • DND 동아리 (4)
      • 정보 (3)
      • 메가브레인 동아리 (1)
    • C# 노트 (1)
      • 기초 C# (1)
    • 타 알고리즘 사이트 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 데이터 통신 네트워크
  • 개발
  • 플러터
  • 큐
  • 온누리국제학교
  • wakatime
  • codeup
  • 개발자
  • 인제대학교
  • 중간고사
  • 파이썬
  • BOJ
  • 코드업
  • 자바스크립트
  • 소마
  • C
  • js
  • DND
  • javascript
  • c언어
  • 스택
  • 리액트
  • Code Up
  • 백준
  • 자바
  • react
  • vue
  • 그리디
  • 알고리즘
  • C#

최근 댓글

최근 글

hELLO · Designed By 정상우.
Jongung
[JAVA] 명품 자바 프로그래밍 6장 실습문제
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.