Fan 클래스 - 상태와 생성자 결정

misonaru·2022년 10월 5일

자바 공부

목록 보기
11/13

FanRunner

package orientedProgramming;

public class FanRunner {

	public static void main(String[] args) {
		Fan fan= new Fan("SAMSUNG", 1, "WHITE");
		
		System.out.println(fan.toString());
	}

}

Fan

package orientedProgramming;

public class Fan {
	
	private String make;
	private double radious;
	private String color;
	
	private boolean isOn;
	private byte speed;
	
	// 생성자
	public Fan(String make, double radious, String color) 
	{
		this.make = make;
		this.radious = radious;
		this.color = color;
	}
	
	public String toString() 
	{
		return String.format("make : %s, radious : %f, color : %s, isOn : %b, speed : %d"
				, make, radious, color, isOn, speed);
	}
	
}

String.format 을 이용한 문자열 형식 설정

String 의 static 메서드인 format 메서드는 문자열의 형식을 설정하는 메서드.

  • %d (10진수 형식)
    10진수 integer의 형식을 설정할 때 사용
int i = 23;

System.out.println(String.format("%d_", i));
System.out.println(String.format("%5d_", i));
System.out.println(String.format("%-5d_", i));
System.out.println(String.format("%05d_", i));

----------------------------------------------

23_
   23_
23   _
00023_

-----------------------------------------------

%5d 와 같이 %와 d 사이에 정수를 설정하면, 글자 길이를 설정할 수 있습니다.

기본적으로 오른쪽 정렬이고, -를 붙일 경우 왼쪽정렬입니다.(ln 4~5)

표현할 숫자인 i의 길이가 5보다 작을 경우 0을 붙입니다.(leading 0s) (ln 6)

% 바로 뒤에 , 를 붙이면 3자리 단위로 쉼표를 찍어줍니다.
  • %s (문자열 형식)
  • %f (실수형 형식)
  • Locale 설정
  • %t (날짜시간 형식)
  • %c (유니코드 문자 형식)
  • %o, %x(8진수, 16진수 형식)

출처: https://blog.jiniworld.me/68#a01 [hello jiniworld:티스토리]

profile
미소와 나루 집사

0개의 댓글