Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 어노테이션
- spring
- 의존주입
- PointCut
- @Valid
- 비즈니스레이어
- SpringBoot
- Java
- application.properties
- 서비스레이어
- gradle
- AOP
- @RequestMapping
- 생성자주입
- frontController
- @ResponseBody
- 바인딩변수
- @RequestParam
- 스프링
- MVC
- jointpoint
- after-throwing
- produces
- springmvc
- c:if
- .xml
- springjdbc
- @
- Model
- 유효성검사
Archives
- Today
- Total
메모장
[실습] 자동차 class를 코딩하기 본문
728x90
반응형
자동차 코딩해주세요 !
자동차.printInfo() // 출력 메서드
-> ㅁㅁ님의 차는 [__/___]입니다.
현재속도/최대속도
자동차.speedUp() // 속력을 내는 메서드
0 -> 100
120 -> 과속! -> 120
최대속도를 못넘어가게 코딩
자동차.speedDown() // 속력을 줄이는 메서드
55 -> 45
5 -> 정지... -> 0
0보다 작아질수없게 코딩
// 생성자 3개 생성하기
Car car=new Car();
무명 / 0 / 120
Car car=new Car("홍길동");
홍길동 / 0 / 120
Car car=new Car("아무무",200);
아무무 / 0 / 200
190 -> 200
200 -> 과속! -> 200
주의 깊에 볼것 !
1- 생성자 오버로딩
2- 멤버변수,메서드
3- this
+++) 자동차.speedUp(23) 속력을 올려주는 메서드에 23을 입력 하였을때 속력을 올려주는 메서드
0 -> 23
package class04;
class Car2{
String name; // 차주 이름
int speed; // 속력
int max; // 최대 속력
Car2(){ // 생성자
this.name="무명";
this.speed=0;
this.max=120;
}
Car2(String name){
this.name=name;
this.speed=0;
this.max=120;
}
Car2(String name,int max){
this.name=name;
this.speed=0;
this.max=max;
}
void printInfo() { // 출력해주는 메서드
System.out.println(this.name+"님의 차는 ["+this.speed+"/"+this.max+"]입니다.");
}
void speedUp() {
this.speed+=100; // 속력이 +=100 씩 증가한다
if(this.speed>this.max) { // 최대속력을 넘었을시
this.speed=this.max; // 현재속력을 최대속력으로 정해놓은 값과 같다고 정의
System.out.println("과속!");
}
}
void speedUp(int speed) { // 오버로딩 //메소드를 두개 씀
this.speed+=speed; // 속력을 입력하였을때 그만큼 속력이 올라간다.
if(this.speed>this.max) {
this.speed=this.max;
System.out.println("과속!");
}
}
void speedDown() {
this.speed-=5; // 속력은 -5 씩 줄어든다라는 가정
if(this.speed<=0) {
this.speed=0; // 0 밑으로 떨어질수 없게
System.out.println("정지...");
}
}
}
public class Test07 {
public static void main(String[] args) {
Car2 c1=new Car2();
Car2 c2=new Car2("홍길동");
Car2 c3=new Car2("김아무개",200);
c1.printInfo();
c2.speedDown();
c2.printInfo();
c3.speedUp(50);
c3.speedUp(30);
c3.printInfo();
c3.speedUp(20);
c3.printInfo();
}
}
728x90
반응형
'JAVA > [실습문제]' 카테고리의 다른 글
| [실습 문제] 상속 (포켓몬) (0) | 2024.04.08 |
|---|---|
| [자판기 프로그램] (0) | 2024.04.08 |
| 포켓몬 문제 ( class 만들기 ) (0) | 2024.04.08 |
| [과제] Class (0) | 2024.04.08 |
| 3,6,9 게임 (0) | 2024.04.08 |