bmi 구하기
강사님의 코드
public static void main(String[] args) {
BMIExample2 m=new BMIExample2();
float h=1.8f;
float w=77f;
float index=m.calculate(h,w);
System.out.println("비만지수:"+index);
String result=m.getResult(index);
System.out.println("비만분류:"+result);
}
public float calculate(float h, float w) {
// bmi 지수 공식처리
float hmulti=h*h;
float result=w/hmulti;
return result;
}
public String getResult(float val) {
String str="";
if(val>=35.0) str="고도비만";
else if(val>=30) str="1비만";
else if(val>=25) str="2비만";
else if(val>=23) str="위험";
else if(val>=18.5) str="정상";
else str="저체중";
return str;
}
결과
비만지수:23.765434
비만분류:위험
스캐너 이용 입력받기
// 강사님의 코드
public static void main(String[] args) {
BMIExample3 m=new BMIExample3();
// 스캐너 이용 입력받기
Scanner s=new Scanner(System.in);
System.out.println("height:");
float h=s.nextFloat();
System.out.println("weight:");
float w=s.nextFloat();
float index=m.calculate(h,w);
System.out.println("비만지수:"+index);
String result=m.getResult(index);
System.out.println("비만분류:"+result);
}
public float calculate(float h, float w) {
// bmi 지수 공식처리
float hmulti=h*h;
float result=w/hmulti;
return result;
}
public String getResult(float val) {
String str="";
if(val>=35.0) str="고도비만";
else if(val>=30) str="1비만";
else if(val>=25) str="2비만";
else if(val>=23) str="위험";
else if(val>=18.5) str="정상";
else str="저체중";
return str;
}
결과
height:
1.7
weight:
60
비만지수:20.761246
비만분류:정상
교재를 나눠줌
『이것이 자바다』개정판 한빛미디어 신용권,임경균 지음
책이 엄청 두껍다.
언제 다 공부하지...
소프트웨어를 개발할 때, 부품에 해당하는 객체들을 먼저 만들고, 이 객체들을 하나씩 조립해서 완성된 프로그램을 만드는 기법
속성과 동작으로 구성됨.
속성 : 필드(field)
ex) 이름, 나이, 색깔, 속도
동작 : 메소드(method)
ex) 웃다, 걷다, 달린다, 멈춘다
day006 project 생성
com.tech.gt001 package 생성
Person class 생성
public class Person {
public static void main(String[] args) {
String name; // 이름 field
String age; // 나이 field
Person m=new Person();
m.laugh();
m.eat();
}
public void laugh() { // 웃다 method
System.out.println("하하하");
}
public void eat() { // 먹다 method
System.out.println("냠냠");
}
}
결과
하하하
냠냠
캡슐화 (Encapsulation)
상속 (Inheritance)
다형성 (Polymorphism)
전역변수 : 전 영역에서 쓸 수 있는 변수
지역변수 : 메소드 안에서만 쓸 수 있는 변수
package com.tech.gt001;
public class Person {
String name; // 전역 변수
String name2;
String name3;
public static void main(String[] args) {
Person person=new Person(); // 객체 선언 방법
person.name="홍길동";
person.laugh();
person.name2="홍길순";
person.eat();
person.name3="유재석";
person.sleep();
}
public void laugh() { // 웃다 method
// 메소드 내용추가
System.out.println(name+"이 웃다");
}
public void eat() { // 먹다 method
System.out.println(name2+"이 맛있게 점심을 먹다");
}
public void sleep() {
System.out.println(name3+"이 곤히 잠을 자고 있다.");
}
}
결과
홍길동이 웃다
홍길순이 맛있게 점심을 먹다
유재석이 곤히 잠을 자고 있다.
field와 local 변수의 차이
local 변수는 생성자와 메소드 블록에서 선언되며, 생성자와 메소드 호출 시에만 생성되고 사용된다.
field는 클래스 블록에서 선언되며, 객체 내부에서 존재하고 객체 내/외부에서 사용 가능하다
field는 객체 생성 시 자동으로 기본값으로 초기화된다.
local 변수는 초기화를 명시해야한다.
public class Television {
// 필드
int channel;
int volume;
boolean onOff;
public static void main(String[] args) {
// 지역변수
int num;
boolean bool;
Television tv=new Television();
System.out.println(tv.channel);
System.out.println(tv.volume);
System.out.println(tv.onOff);
tv.channel=7;
tv.volume=30;
tv.onOff=true;
System.out.println("tv channel:"+tv.channel);
System.out.println("tv volume:"+tv.volume);
if(tv.onOff) System.out.println("켜진tv");
else System.out.println("꺼진tv");
}
}
결과
0
0
false
tv channel:7
tv volume:30
켜진tv
Car class 생성
Car car0=new Car();
기본 생성자 호출
public static void main(String[] args) {
// 생성자 (Constructor)
Car car0=new Car(); // 기본 생성자 호출
}
public Car() {
System.out.println("기본생성자 호출");
}
결과
기본 생성자는 생략할 수 있다.
단, 사용자생성자가 사용중이라면 생략할 수 없다. (호출이 없더라도)
사용자 생성자 호출 1
public static void main(String[] args) {
Car car1=new Car(15); // 사용자 생성자 호출
}
public Car(int i) {
System.out.println("사용자생성자 호출 "+i);
}
결과
사용자생성자 호출 15
사용자 생성자 호출 2
public static void main(String[] args) {
Car car2=new Car(10,20); // 사용자 생성자 호출
}
public Car(int i, int j) {
System.out.println("사용자생성자 호출 "+i+" : "+j);
}
결과
사용자생성자 호출 10 : 20
우리 강의에서 Java 는 Swing까지 배울 예정이며 (간단하게만)
미니 project 개념으로 3~5일 정도 기간을 주고 개인 project 진행할 예정
간단히 windows 화면 정도 구현하면 되고, DB 는 아주 조금만 들어갈 예정
Sample 주어질 것이고 응용해서 하면 됨.
VMware 실행
한국어 입력기 추가하기
시스템도구 - 설정
지역 및 언어 - '한국어' 삭제 (좌측 하단 - 버튼)
'영어(미국)' 추가 (좌측 하단 + 버튼)
shift+space 로 한영 전환 가능
Linux 미션
BMIExample2 file 을 terminal 로 typing 해서 출력해보기
Windows 미션
'Machine' 이라는 느낌을 살려서 자유롭게 Program을 짜보세요.
field : 3개 이상 종류 다르게 지정
method : 4개 이상 사용
메일로 제출
메일 제목 : 머신_홍길동
package com.tech.mission;
public class Machine {
boolean power;
boolean light;
int volume;
String button;
public static void main(String[] args) {
Machine machine=new Machine();
machine.power=true;
machine.powerOnOff();
machine.light=true;
machine.lightOnOff();
machine.volume=100;
machine.volumeControl();
machine.button="잘 눌림";
machine.buttonControl();
}
public void powerOnOff() {
if (power==true) System.out.println("전원 상태 : "+power);
else System.out.println("전원 상태 : "+!power);
}
public void lightOnOff() {
if (light==true) System.out.println("불 켜짐");
else System.out.println("불 꺼짐");
}
public void volumeControl() {
System.out.println("현재 음량 : "+volume);
}
public void buttonControl() {
System.out.println("버튼 상태 : "+button);
}
}
결과
전원 상태 : true
불 켜짐
현재 음량 : 100
버튼 상태 : 잘 눌림
오전엔 배가 아파서.. 오후엔 긴장이 풀려서 그런가 졸리기도 하고..
뭔가 오늘은 집중이 하나도 안되는 날이었다....
낼부터 다시 집중해서 공부해야지..!!!