본문 바로가기
🍺JAVA

[JAVA]직각삼각형(생성자,메소드,스캐너,게터,세터,sqrt)

by 김말자 2023. 1. 4.
728x90
728x90
BIG
package chap06;

public class RightTriangle {


		//클래스의 구성요소 필드,생성자, 메소드
		// 필드 클래스 변수(전역변수) : 일반적으로 private로 캡슐화
		//밑변(double):understool, 높이(double)height 이름(st):name
		//생성자 초기화오버로딩
		//세터게터,일반메소드(getArea, getCircum)

	double understool;
	double height;
	String name;
	/**
	 * @return the understool
	 */
	public double getUnderstool() {
		return understool;
	}
	/**
	 * @param understool the understool to set
	 */
	public void setUnderstool(double understool) {
		this.understool = understool;
	}
	/**
	 * @return the height
	 */
	public double getHeight() {
		return height;
	}
	/**
	 * @param height the height to set
	 */
	public void setHeight(double height) {
		this.height = height;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	public double getArea() {
		double getArea =(understool*height)/2;
		return getArea;
	}
	public double getCircum() {
		double getCircum = understool+height+Math.sqrt(understool*understool+height*height);
		return getCircum;
	}
		public void print() {
		
	System.out.println("이름"+name);
	System.out.println("밑변"+understool);
	System.out.println("높이"+height);
	System.out.println("면적"+getArea());
	System.out.println("둘레"+getCircum());
	}
	
}

직각삼각형의 높이와 둘레를 구해서 프린트하는 클래스를 따로 만들고

메인메소드에서 따로 불러오는 기능을 추가함

package chap06;

import java.util.Scanner;

public class RightTriangleMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		
		RightTriangle rt = new RightTriangle();
		Scanner sc = new Scanner(System.in);
		System.out.println("밑변입력>>");
		rt.setUnderstool(sc.nextDouble());
		System.out.println("높이입력>>");
		rt.setHeight(sc.nextDouble());
		System.out.println("이름입력>>");
		rt.setName(sc.next());
		rt.print();
	}

}​

728x90
반응형
BIG

댓글