Java/명품 Java Programming

2장 상속 실습문제

M_master 2022. 1. 23. 19:24

1. Scanner 클래스를 이용하여 원화를 입력받아 달러로 바꾸어 다음 예시와 같이 출력하는 프로그램을 작성하라. $1=1100원으로 가정하고 계산하라.

import java.util.Scanner;

public class Problem02_1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
				
		System.out.print("원화를 입력하세요(단위 원)>>");
		int won = sc.nextInt();
		double dollar = won / 1100;
		System.out.println(won +"원은 $"+ dollar +"입니다.");
		
        sc.close();
	}
}

2. Scanner 클래스를 이용하여 2자리의 정수(10~99사이)를 입력받고, 십의 자리와 1의 자리가 같은지 판별하여 출력하는 프로그램을 작성하라.

import java.util.Scanner;

public class Problem02_2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("2자리수 정수 입력(10~99)>>");
		int num = sc.nextInt();
		
		if(num < 10 || num > 99) 
		{
			System.out.println("10~99 사이의 값을 입력해주세요.");
		}
		else 
		{				
			String result = ((num/10) == (num%10)) 
					? "Yes! 10의 자리와 1의 자리가 같습니다." 
					: "No! 10의 자리와 1의 자리가 같지 않습니다."; 
			
			System.out.println(result);
		}
		
		sc.close();
	}
}

 

3. Scanner 클래스를 이용하여 정수로 된 돈의 액수를 입력받아 오만 원권, 만 원권, 천 원권, 500원짜리 동전, 100원짜리 동전, 50원짜리 동전, 10원짜리 동전, 1원짜리 동전 각 몇 개로 변환되는지 출력하라.

import java.util.Scanner;

public class Problem02_3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("금액을 입력하시오>>");
		int num = sc.nextInt();
		
		System.out.println("오만원권 "+ (num/50000) +"매");
		num %= 50000;
		System.out.println("만원권 "+ (num/10000) +"매");
		num %= 10000;
		System.out.println("천원권 "+ (num/1000) +"매");
		num %= 1000;
		System.out.println("백원 "+ (num/100) +"개");
		num %= 100;
		System.out.println("오십원 "+ (num/50) +"개");
		num %= 50;
		System.out.println("십원 "+ (num/10) +"개");
		num %= 10;
		System.out.println("일원 "+ (num/1) +"개");
		
		sc.close();
	}
}

 

4. Scanner 클래스로 정수 3개를 입력받고 3개의 숫자 중 중간 크기의 수를 출력하라.

평균값을 구하는 것이 아님에 주의하라.

import java.util.Scanner;

public class Problem02_4 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수 3개 입력>>");
		
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		
		if(a > b && a < c) {
			System.out.println(a);
		} else if(b > a && b < c) {
			System.out.println(b);
		} else {
			System.out.println(c);
		}
		
		sc.close();
	}
}

 

5. Scanner를 이용하여 삼각형의 변의 길이를 나타내는 정수를 3개 입력받고 이 3개의 수로 삼각형을 만들 수 있는지 판별하라. 삼각형이 되려면 두 변의 합이 다른 한 변의 합보다 커야 한다.

import java.util.Scanner;

public class Problem02_5 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수 3개를 입력하시오>>");
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		
		if((a+b) > c || (a+c) > b || (b+c) > a) {
			 System.out.println("삼각형이 됩니다.");
		} else {
			System.out.println("삼각형이 안됩니다.");
		}
		sc.close();
	}

}

 

6. 369게임을 간단히 작성해보자. 1~99까지의 정수를 입력받고 정수에 3, 6, 9 중 하나가 있는 경우는 "박수짝"을 출력하고 두 개 있는 경우는 "박수짝짝"을 출력하는 프로그램을 작성하라. 예를 들면, 키보드로 입력된 수가 13인 경우 "박수짝"을, 36인 경우 "박수짝짝"을 출력하면 된다.

import java.util.Scanner;

public class Problem02_6 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("1~99 사이의 정수를 입력하시오>>");
		int num = sc.nextInt();
		
		if(num < 0 || num > 99) {
			System.out.println("1~99 사이의 값을 입력해주세요");
		} else {
			int a = (num/10);
			int b = (num%10);
			
			if((a == 3 || a == 6 || a == 9) && (b == 3 || b == 6 || b == 9)) {
				System.out.println("박수짝짝");
			} else if((a == 3 || a == 6 || a == 9) || (b == 3 || b == 6 || b == 9)) {
				System.out.println("박수짝");
			}
		}
		sc.close();
	}
}

7. 2차원 평면에서 직사각형은 왼쪽 상단 모서리와 오른쪽 하단 모서리의 두 점으로 표현한다. (100, 100)과 (200, 200)의 두 점으로 이루어진 사각형이 있을 때, Scanner를 이용하여 정수 x와 y 값을 입력받고 (x, y)가 이 직사각형 안에 있는지를 판별하는 프로그램을 작성하라.

import java.util.Scanner;

public class Problem02_7 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("점 (x,y)의 좌표를 입력하시오>>");
		int x = sc.nextInt();
		int y = sc.nextInt();
		
		if((x >= 100 && x <= 200) && (y >= 100 && y <= 200)) {
			System.out.println("("+ x +","+ y +")는 사각형 안에 있습니다.");
		} else {
			System.out.println("("+ x +","+ y +")는 사각형 안에 없습니다.");
		}
		
		sc.close();
	}
}

8. 2차원 평면에서 직사각형은 문제 7번처럼 두 점으로 표현된다. 키보드로부터 직사각형을 구성하는 두 점 (x1, y1), (x2, y2)를 입력받아 (100, 100), (200, 200)의 두 점으로 이루어진 직사각형과 충돌하는지 판별하는 프로그램을 작성하라.

import java.util.Scanner;

public class Problem02_8 {
	
	public static boolean inRect(int x, int y, int rectx1, int recty1, int rectx2, int recty2) {
		if((x >= rectx1 && x <= rectx2) && (y >= recty1 && y <= recty2))
			return true;
		else 
			return false;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("점 (x1,y1)의 좌표를 입력하시오>>");
		int x1 = sc.nextInt();
		int y1 = sc.nextInt();
		System.out.print("점 (x2, y2)의 좌표를 입력하시오>>");
		int x2 = sc.nextInt();
		int y2 = sc.nextInt();
		
		boolean isRect1 = inRect(x1, y1, 100, 100, 200, 200);
		boolean isRect2 = inRect(x2, y2, 100, 100, 200, 200);
		
		if(isRect1 == true || isRect2 == true) {
			System.out.println("사각형 충돌 합니다.");
		} else {
			System.out.println("사각형 충돌 하지 않습니다.");
		}
		
		sc.close();
	}
}

9. 원의 중심을 나타내는 한 점과 반지름을 실수 값으로 입력받아라. 그리고 실수 값으로 다른 점(x, y)를 입력받아 이 점이 원의 내부에 있는지 판별하여 출력하라.

import java.util.Scanner;

public class Problem02_9 {
	
	public static boolean inRect(int x, int y, int rectx1, int recty1, int rectx2, int recty2) {
		if((x >= rectx1 && x <= rectx2) && (y >= recty1 && y <= recty2))
			return true;
		else 
			return false;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("원의 중심과 반지름 입력>>");
		double r_x = sc.nextDouble();
		double r_y = sc.nextDouble();
		double radius = sc.nextDouble();
				
		System.out.print("점 입력>>");
		double x = sc.nextDouble();
		double y = sc.nextDouble();
		
		double result = Math.sqrt((x-r_x)*(x-r_x)+(y-r_y)*(y-r_y));
		
		if(result < radius) {
			System.out.println("점 ("+ (double)x +","+ (double)y +")는 안에 있다.");
		} else {
			System.out.println("점 ("+ (double)x +","+ (double)y +")는 안에 없습니다.");
		}
		
		sc.close();
	}
}

10. 원의 정보를 받기 위해 키보드로부터 원의 중심을 나타내는 한 점과 반지름을 입력받는다. 두 개의 원을 입력받고 두 원이 서로 겹치는지 판단하여 출력하라.

import java.util.Scanner;

public class Problem02_10 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("첫번째 원의 중심과 반지름 입력>>");
		double r1_x = sc.nextDouble();
		double r1_y = sc.nextDouble();
		double r1_radius = sc.nextDouble();
		
		System.out.print("두번째 원의 중심과 반지름 입력>>");
		double r2_x = sc.nextDouble();
		double r2_y = sc.nextDouble();
		double r2_radius = sc.nextDouble();
		
		double dis  = Math.sqrt((r1_x - r2_x)*(r1_y - r2_y));
		
		if(dis <= (r1_radius + r2_radius)) {
			System.out.println("두 원은 서로 겹친다");
		} else {
			System.out.println("두 원은 서로 겹치지 않는다");
		}
		
		sc.close();
	}
}

※ 참고 자료

https://dojang.io/mod/page/view.php?id=427 (피타고라스 공식을 통한 거리 구하기)

- 중심에서 점 (x,y) 사이의 거리가 반지름보다 작거나 같으면 원의 내부에 있다.

 

11. 숫자를 입력받아 3~5는 "봄". 6~8은 "여름", 9~11은 "가을", 12,1,2의 경우 "겨울"을, 그 외 숫자를 입력한 경우 "잘못입력"을 출력하는 프로그램을 작성하라.

import java.util.Scanner;

public class Problem02_11 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("달을 입력하세요(1~12)>>");
		int month = sc.nextInt();
		
		/*
		if(month >= 3 && month <= 5) {
			System.out.println("봄");
		} else if(month >= 6 && month <= 8) {
			System.out.println("여름");
		} else if(month >= 9 && month <= 11) {
			System.out.println("가을");
		} else if(month == 12 || (month >= 1 && month <= 2)) {
			System.out.println("겨울");
		} else {
			System.out.println("잘못입력");
		}
		*/
		
		switch(month) {
			case 3: case 4: case 5: 
				System.out.println("봄"); 
				break;
			case 6: case 7: case 8: 
				System.out.println("여름"); 
				break;
			case 9: case 10: case 11: 
				System.out.println("가을"); 
				break;
			case 12: case 1: case 2: 
				System.out.println("겨울"); 
				break;
			default: 
				System.out.println("잘못입력"); 
		}
		
		sc.close();
	}
}

12. 사칙 연산을 입력받아 계산하는 프로그램을 작성하고자 한다. 연산자는 +, =, *, /의 네 가지로 하고 피연산자는 모두 실수로 한다. 피연산자와 연산자는 실행 사례와 같이 빈 칸으로 분리하여 입력한다. 0으로 나누기 시 "0으로 나눌 수 없습니다."를 출력하고 종료한다.

import java.util.Scanner;

public class Problem02_12 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String result;
		
		System.out.print("연산>>");
		int a = sc.nextInt();
		String calc = sc.next();
		int b = sc.nextInt();
		
		/*
		if(calc.equals("+") == true) {
			result = a +"+"+ b +"의 계산 결과는 "+ (a+b);
		} else if(calc.equals("-") == true) {
			result = a +"-"+ b +"의 계산 결과는 "+ (a-b);
		} else if(calc.equals("*") == true) {
			result = a +"*"+ b +"의 계산 결과는 "+ (a*b);
		} else if(calc.equals("/") == true) {
			if(a <= 0) {
				result = "0이하로는 나눌 수 없습니다.";
			} else {
				result = a +"/"+ b +"의 계산 결과는 "+ (a/b);
			}
		} else {
			result = "올바른 연산자를 입력해주세요.";
		}
		*/
		
		switch(calc) {
			case "+": 
				result = a +"+"+ b +"의 계산 결과는 "+ (a+b); 
				break;
			case "-": 
				result = a +"-"+ b +"의 계산 결과는 "+ (a-b); 
				break;
			case "*":
				result = a +"*"+ b +"의 계산 결과는 "+ (a*b);
				break;
			case "/":
				if(a <= 0) {
					result = "0이하로는 나눌 수 없습니다.";
				} else {
					result = a +"/"+ b +"의 계산 결과는 "+ (a/b);
				}
				break;
			default:
					result = "올바른 연산자를 입력해주세요.";
		}
		
		System.out.println(result);
		
		sc.close();
	}
}
728x90