Java(Class2)_Exam_03

2024. 10. 31. 19:44·Java/기초 내용 정리

2024-10-31

 

 

 

 

 

도형을 선택하세요.(1. 원형, 2. 사각형)
도형 선택 >>> 1
반지름 입력 >>> 50
====================================
원의 면적 : 7850.0

 

package exam;

public interface Shape {
	
	double pai = 3.14;
	
	double findArea();

}

 

package exam;

public class Circle implements Shape {

	int radius;
	
	public Circle() {}
	public Circle(int radius) {
		
		this.radius = radius;
	}
	@Override
	public double findArea() {
		
		
		return (pai * (radius * radius));
	}
	
}

 

package exam;

public class Rectangle implements Shape {

	int width;
	int height;
	
	public Rectangle() {}
	public Rectangle(int width, int height) {
		
		this.width = width;
		this.height = height;
	}
	@Override
	public double findArea() {
		
		
		return (width * height);
	}
}

 

package exam;

import java.util.Scanner;

public class Shape_03 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("도형을 선택하세요.(1. 원형, 2. 사각형)");
		System.out.print("도형 선택 >>> ");
		
		if(sc.nextInt() == 1) {
			
			System.out.print("반지름 입력 >>> ");
			Circle circle = new Circle(sc.nextInt());
			System.out.println("====================================");
			
			System.out.printf("원의 면적 : %.1f\n", circle.findArea());
		}else if(sc.nextInt() == 2){
			
			System.out.print("가로 >>> ");
			int a = sc.nextInt();
			System.out.print("세로 >>> ");
			int b = sc.nextInt();
			Rectangle rectangle = new Rectangle(a, b);
	
			System.out.println("====================================");
			
			System.out.printf("사각형의 면적 : %.1f\\n", rectangle.findArea());
		}else {
			
			System.out.println("잘못 입력하셨습니다.");
		}
		sc.close();
	}
}

'Java > 기초 내용 정리' 카테고리의 다른 글

Java(Class2)_Polymorphism_02  (0) 2024.11.01
Java(Class2)_Polymorphism_01  (0) 2024.11.01
Java(Class2)_Exam_02  (0) 2024.10.31
Java(Class2)_Exam_01  (0) 2024.10.31
Java(Class2)_Interface_02  (0) 2024.10.31
'Java/기초 내용 정리' 카테고리의 다른 글
  • Java(Class2)_Polymorphism_02
  • Java(Class2)_Polymorphism_01
  • Java(Class2)_Exam_02
  • Java(Class2)_Exam_01
mw41817
mw41817
일생의 개발 기록 저장소
  • mw41817
    IT 개발 일지
    mw41817
    • Index (487)
      • HTML (36)
        • 기초 내용 정리 (36)
      • CSS (29)
        • 기초 내용 정리 (29)
      • JavaScript (60)
        • 기초 내용 정리 (60)
      • JQuery (38)
        • 기초 내용 정리 (38)
      • Java (232)
        • 기초 내용 정리 (232)
      • JSP (46)
        • 기초 내용 정리 (46)
      • Spring, Boot (31)
        • 기초 내용 정리 (31)
      • DB (5)
        • Oracle SQL (5)
      • Code WorkBook (6)
        • programmers (6)
        • Baekjoon (0)
      • 기타 (1)
        • 유용한 사이트 (3)
  • 전체
    오늘
    어제
  • 글쓰기 관리
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 카테고리
    • 주인장 GitHub
  • 공지사항

  • 인기 글

  • 태그

    html #코딩 #프로그래밍 #기초
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.0
mw41817
Java(Class2)_Exam_03
상단으로

티스토리툴바