프로그래밍/java
[JAVA] 명품 자바 프로그래밍 6장 실습문제
승민아
2022. 6. 5. 17:19
1번 문제 코드
import java.util.Scanner;
class MyPoint
{
int x;
int y;
MyPoint(int x, int y)
{
this.x=x;
this.y=y;
}
public String toString()
{
return "Point("+x+","+y+")";
}
boolean equals(MyPoint mp)
{
if(this.x==mp.x&&this.y==mp.y)
return true;
return false;
}
}
public class Extest{
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("다른 점");
}
}
2번 문제 코드
import java.util.Scanner;
class Circle
{
int x;
int y;
int radius;
Circle(int x, int y, int radius)
{
this.x=x;
this.y=y;
this.radius=radius;
}
boolean equals(Circle c)
{
if(this.x==c.x&&this.y==c.y)
return true;
return false;
}
public String toString()
{
return "Circle("+x+","+y+")"+"반지름"+radius;
}
}
public class Extest{
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("서로 다른 원");
}
}
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("Circle"); }
}
package app;
import derived.Circle;
import base.Shape;
public class GraphicEditor {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
}
}
5, 6번은 생략 (Calendar 클래스 사용)
7-1번 문제 코드
import java.util.Scanner;
import java.util.StringTokenizer;
public class Extest{
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 st = new StringTokenizer(str," ");
int count = st.countTokens();
System.out.println("어절 개수는 "+count);
}
}
}
7-2번 문제 코드
import java.util.Scanner;
public class Extest{
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);
}
}
}
8번 문제 코드
import java.util.Scanner;
public class Extest{
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,str.length()));
System.out.println(str.subSequence(0,i));
}
}
}
9번 문제 코드
class Com{
public static void print(int ins ,int com)
{
if(ins==1) {
System.out.print("철수(가위)");
if(com==1) {
System.out.println(" : 컴퓨터(가위)");
System.out.println("비김");
}
else if(com==2) {
System.out.println(" : 컴퓨터(바위)");
System.out.println("컴퓨터 승");
}
else {
System.out.println(" : 컴퓨터(보)");
System.out.println("철수 승");
}
}
else if(ins==2) {
System.out.print("철수(바위)");
if(com==1) {
System.out.println(" : 컴퓨터(가위)");
System.out.println("철수 승");
}
else if(com==2) {
System.out.println(" : 컴퓨터(바위)");
System.out.println("비김");
}
else {
System.out.println(" : 컴퓨터(보)");
System.out.println("컴퓨터 승");
}
}
else
{
System.out.print("철수(보)");
if(com==1) {
System.out.println(" : 컴퓨터(가위)");
System.out.println("컴퓨터 승");
}
else if(com==2) {
System.out.println(" : 컴퓨터(바위)");
System.out.println("철수 승");
}
else {
System.out.println(" : 컴퓨터(보)");
System.out.println("비김");
}
}
}
}
public class Extest{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.print("철수[가위(1), 바위(2), 보(3), 끝내기(4)]>>");
int ins = scanner.nextInt();
if(ins==4)
break;
int com = (int)((Math.random()*3)+1);
Com.print(ins, com);
}
}
}
10번 문제 코드
class Person
{
String name;
Person(String name)
{
this.name=name;
}
}
public class Extest{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name;
System.out.print("1번째 선수의 이름:");
name=scanner.next();
Person p1 = new Person(name);
System.out.print("2번째 선수의 이름:");
name=scanner.next();
Person p2 = new Person(name);
scanner.nextLine();
while(true)
{
System.out.print("["+p1.name+"]");
scanner.nextLine();
int n1=(int)(Math.random()*3+1);
int n2=(int)(Math.random()*3+1);
int n3=(int)(Math.random()*3+1);
System.out.print(n1+", "+n2+", "+n3+" ");
if(n1==3&&n2==3&&n3==3)
{
System.out.println(p1.name+"님이 이겼습니다!");
break;
}
else
System.out.println("아쉽습니다.");
System.out.print("["+p2.name+"]");
scanner.nextLine();
n1=(int)(Math.random()*3+1);
n2=(int)(Math.random()*3+1);
n3=(int)(Math.random()*3+1);
System.out.print(n1+", "+n2+", "+n3+" ");
if(n1==3&&n2==3&&n3==3)
{
System.out.println(p2.name+"님이 이겼습니다!");
break;
}
else
System.out.println("아쉽습니다.");
}
}
}
11번 문제 코드
public class Extest{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(">>");
String str = scanner.nextLine();
StringBuffer sb = new StringBuffer(str);
while(true)
{
System.out.print("명령:");
String cmd = scanner.next();
if(cmd.equals("그만"))
break;
String token[] = cmd.split("!");
if(token.length!=2)
{
System.out.println("잘못된 명령입니다.");
continue;
}
int index = sb.indexOf(token[0]);
if(index==-1)
{
System.out.println("찾을 수 없습니다.");
continue;
}
sb.replace(index, index+token[0].length(),token[1]);
System.out.println(sb);
}
}
}
12번 문제 코드
class Person
{
String name;
Person(String name)
{
this.name=name;
}
}
class Play
{
Person[] p;
Play(int n)
{
p=new Person[n];
}
}
public class Extest{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("참여자 수:");
int n=scanner.nextInt();
Play play = new Play(n);
for(int i=1;i<=n;i++)
{
String name;
System.out.print(i+"번째 선수의 이름:");
name=scanner.next();
Person person = new Person(name);
play.p[i-1]=person;
}
scanner.nextLine();
int cnt=0;
while(true)
{
System.out.print("["+play.p[cnt%n].name+"]");
scanner.nextLine();
int n1=(int)(Math.random()*3+1);
int n2=(int)(Math.random()*3+1);
int n3=(int)(Math.random()*3+1);
System.out.print(n1+", "+n2+", "+n3+" ");
if(n1==3&&n2==3&&n3==3)
{
System.out.println(play.p[cnt%n].name+"님이 이겼습니다!");
break;
}
else
System.out.println("아쉽습니다.");
cnt++;
}
}
}