• 명품 자바 프로그래밍(개정4판) 제 6장 실습문제

    2020. 11. 22.

    by. Hyeon-Uk

    반응형

    [1번]

    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(MyPoint p) {
    		return this.x==p.x&&this.y==p.y;
    	}
    }
    
    public class p0601 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		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번]

    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 String toString() {
    		return "Circle("+x+","+y+")반지름"+r;
    	}
    	public boolean equals(Circle a) {
    		return x==a.x&&y==a.y&&r==a.y;
    	}
    	
    }
    
    public class p0602 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		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 main;
    import etc.Calc;
    public class p0603 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Calc c=new Calc(10,20);
    		System.out.println(c.sum());
    	}
    
    }
    
    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;
    	}
    }

    [4번]

    package app;
    import derived.Circle;
    import base.Shape;
    public class p0604 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Shape shape=new Circle();
    		shape.draw();
    	}
    
    }
    
    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");}
    }
    

    [5번]

    import java.util.Calendar;
    public class p0605 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Calendar c= Calendar.getInstance();
    		int h=c.get(Calendar.HOUR_OF_DAY);
    		int m=c.get(Calendar.MINUTE);
    		System.out.println("현재 시간은 "+h+"시 "+m+"분 입니다.");
    		if(h>=4&&h<=12) {
    			System.out.println("Good Morning");
    		}
    		else if(h<=18){
    			System.out.println("Good Evening");
    		}
    		else {
    			System.out.println("Good Night");
    		}
    		
    	}
    
    }
    

    [6번]

    import java.util.Calendar;
    import java.util.Scanner;
    
    class game{
    	String name;
    	int sec;
    	public game(String name) {
    		this.name=name;
    	}
    	public void go() {
    		System.out.print(name+" 시작 <Enter>키 >>");
    		Scanner sc=new Scanner(System.in);
    		sc.nextLine();
    		Calendar c= Calendar.getInstance();
    		int r1=c.get(Calendar.SECOND);
    		System.out.println("\t현재 초 시간 = "+r1);
    		System.out.print("10초 예상 후 <Enter>키 >>");
    		sc.nextLine();
    		c=Calendar.getInstance();
    		int r2=c.get(Calendar.SECOND);
    		System.out.println("\t현재 초 시간 = "+r2);
    		sec=r2<r1?r2+60-r1:r2-r1;
    	}
    	public void print() {
    		System.out.print(name+"의 결과 "+sec+",");
    	}
    }
    
    public class p0606 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner sc=new Scanner(System.in);
    		System.out.println("10초에 가까운 사람이 이기는 게임입니다.");
    		game a=new game("황기태");
    		game b=new game("이재문");
    		a.go();
    		b.go();
    		a.print();
    		b.print();
    		if(Math.abs(10-a.sec)>Math.abs(10-b.sec)) {
    			System.out.println("승자는 "+b.name);
    		}
    		else if(Math.abs(10-a.sec)<Math.abs(10-b.sec)) {
    			System.out.println("승자는 "+a.name);
    		}
    		else {
    			System.out.println("비겼습니다.");
    		}
    	}
    
    }
    

    [7번]

    import java.util.Scanner;
    
    public class p0607 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner sc=new Scanner(System.in);
    		while(true) {
    			String st=sc.nextLine();
    			if(st.equals("그만")) {
    				break;
    			}
    			String arr[]=st.split(" ");
    			System.out.println("어절 개수는 "+arr.length);
    		}
    		System.out.println("종료합니다....");
    	}
    
    }
    

    [8번]

    import java.util.Scanner;
    
    public class p0608 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner sc=new Scanner(System.in);
    		System.out.println("문자열을 입력하세요. 빈칸이 있어도되고 영어 한글 모두 됩니다.");
    		String str=sc.nextLine();
    		int len=str.length();
    		for(int i=0;i<len;i++) {
    			System.out.print(str.substring(i+1,len));
    			System.out.print(str.substring(0,i+1));
    			System.out.println();
    		}
    	}
    
    }
    

    [9번]

    import java.util.Random;
    import java.util.Scanner;
    
    class rsp{
    	String n;
    	String name;
    	public rsp(String name) {
    		this.name=name;
    	}
    	public void Set(int op) {
    		if(op==1) {
    			n="가위";
    		}
    		else if(op==2) {
    			n="바위";
    		}
    		else {
    			n="보";
    		}
    	}
    	public void compare(rsp b) {
    		if(n.equals("가위")) {
    			if(b.n.equals("가위")) {
    				System.out.println("비겼습니다.");
    			}
    			else if(b.n.equals("바위")) {
    				System.out.println(b.name+"가 이겼습니다.");
    			}
    			else {
    				System.out.println(name+"가 이겼습니다.");
    			}
    		}
    		else if(n.equals("바위")) {
    			if(b.n.equals("가위")) {
    				System.out.println(name+"가 이겼습니다.");
    			}
    			else if(b.n.equals("바위")) {
    				System.out.println("비겼습니다.");
    			}
    			else {
    				System.out.println(b.name+"가 이겼습니다.");
    			}
    		}
    		else {
    			if(b.n.equals("가위")) {
    				System.out.println(b.name+"가 이겼습니다.");
    			}
    			else if(b.n.equals("바위")) {
    				System.out.println(name+"가 이겼습니다.");
    			}
    			else {
    				System.out.println("비겼습니다.");
    			}
    		}
    	}
    	public String toString() {
    		return name+"("+n+")";
    	}
    }
    
    public class p0609 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner sc=new Scanner(System.in);
    		rsp cs=new rsp("철수");
    		rsp com=new rsp("컴퓨터");
    		Random r=new Random();
    		r.setSeed(System.currentTimeMillis());
    		while(true) {
    			System.out.print("철수[가위(1), 바위(2), 보(3), 끝내가(4)]>>");
    			int op=sc.nextInt();
    			if(op==4) {
    				break;
    			}
    			cs.Set(op);
    			com.Set(r.nextInt(3)+1);
    			System.out.println(cs+" : "+com);
    			cs.compare(com);
    		}
    	}
    
    }
    

    [10번]

    import java.util.Random;
    import java.util.Scanner;
    
    class Person{
    	String name;
    	int r1,r2,r3;
    	Scanner sc;
    	public Person(String name) {
    		this.name=name;
    		sc=new Scanner(System.in);
    	}
    	public boolean run() {
    		System.out.print("["+name+"]:<Enter>");
    		String enter=sc.nextLine();
    		Random r=new Random();
    		r.setSeed(System.currentTimeMillis());
    		r1=r.nextInt(3)+1;
    		r2=r.nextInt(3)+1;
    		r3=r.nextInt(3)+1;
    
    		System.out.print("\t"+r1+"\t"+r2+"\t"+r3+"\t");
    		if(r1==r2&&r2==r3) {
    			System.out.println(name+"님이 이겼습니다!");
    			return true;
    		}
    		else {
    			System.out.println("아쉽군요!");
    			return false;
    		}
    	}
    }
    
    public class p0610 {
    	public static void main(String[] args) {
    		Scanner sc=new Scanner(System.in);
    		System.out.print("1번째 선수 이름>>");
    		String n=sc.next();
    		Person a=new Person(n);
    		System.out.print("2번째 선수 이름>>");
    		n=sc.next();
    		Person b=new Person(n);
    		while(true) {
    			if(a.run()) break;
    			if(b.run()) break;
    			
    		}
    	}
    }
    

    [11번]

    import java.util.Scanner;
    
    public class p0611 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner sc=new Scanner(System.in);
    		System.out.print(">>");
    		String st=sc.nextLine();
    		StringBuffer sb=new StringBuffer(st);
    		while(true) {
    			System.out.print("명령: ");
    			String op=sc.next();
    			if(op.equals("그만")) {
    				break;
    			}
    			String str[]=op.split("!");
    			int index=sb.indexOf(str[0]);
    			if(index==-1) {
    				System.out.println("찾을 수 없습니다!");
    			}
    			else if(str[0].equals("")) {
    				System.out.println("잘못된 명령입니다!");
    			}
    			else {
    				sb.replace(index, index+str[0].length(), str[1]);
    				System.out.println(sb);
    			}
    		}
    		System.out.println("종료합니다");
    	}
    
    }
    

    [12번]

    import java.util.Random;
    import java.util.Scanner;
    
    class Person{
    	String name;
    	int r1,r2,r3;
    	Scanner sc;
    	public Person(String name) {
    		this.name=name;
    		sc=new Scanner(System.in);
    	}
    	public boolean run() {
    		System.out.print("["+name+"]:<Enter>");
    		String enter=sc.nextLine();
    		Random r=new Random();
    		r.setSeed(System.currentTimeMillis());
    		r1=r.nextInt(3)+1;
    		r2=r.nextInt(3)+1;
    		r3=r.nextInt(3)+1;
    
    		System.out.print("\t"+r1+"\t"+r2+"\t"+r3+"\t");
    		if(r1==r2&&r2==r3) {
    			System.out.println(name+"님이 이겼습니다!");
    			return true;
    		}
    		else {
    			System.out.println("아쉽군요!");
    			return false;
    		}
    	}
    }
    
    public class p0612 {
    	public static void main(String[] args) {
    		Scanner sc=new Scanner(System.in);
    		System.out.print("겜블링 게임에 참여할 선수 숫자>>");
    		int n=sc.nextInt();
    		Person arr[]=new Person[n];
    		for(int i=0;i<n;i++) {
    			System.out.print((i+1)+"번째 선수 이름>>");
    			String name=sc.next();
    			arr[i]=new Person(name);
    		}
    		boolean flag=true;;
    		while(flag) {
    			for(int i=0;i<n;i++) {
    				if(arr[i].run()) {
    					flag=false;
    					break;
    				}
    			}
    		}
    	}
    }
    
    반응형

    댓글