Phone
// 멤버 변수 선언 String color; // 색상 String company; // 제조 float size; // 사이즈 int price; // 가격 // 생성자 오버로딩 // 매개변수가 없는 생성자 public Phone() { } // 매개변수가 있는 생성자 public Phone(String company) { this.company = company; } public Phone(String color, String company, float size, int price) { this.color = color; this.company = company; this.size = size; this.price = price; } public Phone(String company, int price) { super(); this.company = company; this.price = price; }
PhoneMain
Phone ph = new Phone(); ph.color = "Silver"; ph.company = "Apple"; ph.price = 999; ph.size = 6.1f; // 매개변수 없는 생성자 호출 Phone ph2 = new Phone(); // 매개변수가 있는 생성자 호출 Phone ph3 = new Phone("Samsung"); Phone ph4 = new Phone("Violet", "Samsung", 6.5f, 999);
Car
/* * 필드 선언 * 색상, 최고속력, 모델명 */ String color; String modelName; int maxSpeed; public Car(String color, String modelName, int maxSpeed) { this.color = color; this.modelName = modelName; this.maxSpeed = maxSpeed; } // 모든 필드값을 화면에 출력하는 기능 // 메소드 명명규칙은 동사가 먼저 나와야 한다. public void showInfo() { System.out.println("Color : " + this.color); System.out.println("Model : " + >this.modelName); System.out.println("Max Speed : " + >this.maxSpeed); }
CarMain
Car tesla = new Car("Pearl White", "Model S", 322); car.showInfo(); // 함수로 정보 불러오기
Tv
int channel; // TV 채널 int volume; // TV 볼륨 String company; // 제조사 // 모든 필드값 출력 메소드 public void showInfo() { System.out.println("현재 채널 : " + channel); System.out.println("현재 볼륨 : " + volume); System.out.println("제조사 : " + company); } public void turnOn() { System.out.println("TV가 켜졌습니다."); } public void turnOff() { System.out.println("TV가 꺼졌습니다."); } public void upChannel() { ++this.channel; } public void downChannel() { --this.channel; }
TvMain
// TV 클래스의 객체 생성 Tv tv = new Tv(); tv.channel = 11; tv.volume = 50; tv.company = "LG"; tv.showInfo(); tv.turnOn(); tv.turnOff(); // 채널 1만큼 올리기 tv.upChannel(); tv.showInfo(); // 채널 1만큼 내리기 tv.downChannel(); tv.showInfo();
Calculator
Scanner sc = new Scanner(System.in); public void showMenu() { System.out.println("=============================================="); System.out.println("| 1. 덧셈 | 2. 뺄셈 | 3. 곱셈 | 4. 나눗셈 | 5. 종료"); System.out.println("=============================================="); } public int[] inputNum() { int[] arr = new int[2]; for (int i = 0; i < 2; i++) { System.out.print((i + 1) + "번째 숫자 입력 : "); arr[i] = sc.nextInt(); } return arr; } public int plus(int[] arr) { return arr[0] + arr[1]; } public int minus(int[] arr) { return arr[0] - arr[1]; } public int multiple(int[] arr) { return arr[0] * arr[1]; } public int divide(int[] arr) { return arr[0] / arr[1]; }
CalculatorMain
Scanner sc = new Scanner(System.in); Calculator calc = new Calculator(); boolean run = true; while (run) { int[] arr = new int[2]; int result = 0; calc.showMenu(); System.out.print("메뉴 선택 : "); int input = sc.nextInt(); if ((input >= 1 && input <= 4) && run) { arr = calc.inputNum(); } switch (input) { case 1: result = calc.plus(arr); System.out.println(arr[0] + " + " + arr[1] + " = " + result); break; case 2: result = calc.minus(arr); System.out.println(arr[0] + " - " + arr[1] + " = " + result); break; case 3: result = calc.multiple(arr); System.out.println(arr[0] + " * " + arr[1] + " = " + result); break; case 4: result = calc.multiple(arr); System.out.println(arr[0] + " / " + arr[1] + " = " + result); break; case 5: run = false; break; } } System.out.println("프로그램 종료.");
Car2
private int gas; public void setGas(int gas) { if (gas < 0) gas = 0; this.gas = gas; } public boolean isLeftGas() { if(gas == 0) { System.out.println("gas가 없습니다."); return false; } else { System.out.println("gas가 있습니다."); return true; } } public int getGas() { return this.gas; } public void run() { while(true) { if (gas > 0) { System.out.println("달립니다. (gas 잔량 : " + gas + ")"); gas -= 1; } else { System.out.println("멈춥니다. (gas 잔략 : " + gas + ")"); break; } } }
Car2Main
Car2 car = new Car2(); car.setGas(10); boolean gasState = car.isLeftGas(); if (gasState) { System.out.println("출발"); } car.run();