즐거운프로그래밍

[자바] 노트패드++ 예제 (자전거 클래스)

수수께끼 고양이 2023. 10. 16. 16:51
728x90
반응형

 

삼천리 자전거 회사에서 자전거 관리 프로그램을 요청했습니다.
1. 자전거 클래스를 적당한 영어로 정의하시오.
2. 여러분들이 관심있는 자전거 속성 2가지를 추출하여 private로 추가합니다.
3. 2가지 속성에 대한 getter, setter 함수를 클래스에 추가합니다.
4. 생성자 함수를 추가하시오. 
5. 자전거 객체를 하나 생성한 후, 속성 정보를 출력하시오.

 

public class Samcheonri {
	public static void main(String args[]) {
		// Bicycle BicycleParts = new Bicycle();
		Bicycle BicycleParts = new Bicycle("iron","plastic");
		// BicycleParts.setChain("iron");
		System.out.println(BicycleParts.getChain());
		// BicycleParts.setPedal("plastic");
		System.out.println(BicycleParts.getPedal());
	}
}

 

 

class Bicycle {
	private String chain;
	void setChain(String chain) {
		this.chain=chain;
	}
	String getChain() {
		return this.chain;
	}
	private String pedal;
	void setPedal(String pedal) {
		this.pedal=pedal;
	}
	String getPedal() {
		return this.pedal;
	}
	Bicycle(String chain, String pedal) {
		this.chain=chain;
		this.pedal=pedal;
	}
}

전체 코드

 

public class Samcheonri {
	public static void main(String args[]) {
		// Bicycle BicycleParts = new Bicycle();
		Bicycle BicycleParts = new Bicycle("iron","plastic");
		// BicycleParts.setChain("iron");
		System.out.println(BicycleParts.getChain());
		// BicycleParts.setPedal("plastic");
		System.out.println(BicycleParts.getPedal());
		
	}
}
class Bicycle {
	private String chain;
	void setChain(String chain) {
		this.chain=chain;
	}
	String getChain() {
		return this.chain;
	}
	private String pedal;
	void setPedal(String pedal) {
		this.pedal=pedal;
	}
	String getPedal() {
		return this.pedal;
	}
	Bicycle(String chain, String pedal) {
		this.chain=chain;
		this.pedal=pedal;
	}
}

 

728x90
반응형