일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- pyqt
- 대덕인재개발원
- crud
- 자바문제
- 자바
- 맥
- 컬렉션프레임워크
- ibatis
- Java
- Homebrew
- 반복문
- 생활코딩
- Mac
- Oracle
- FastAPI
- API
- ddit
- nodejs
- Error
- html
- JDBC
- 이클립스
- 배열
- 객체지향
- servlet
- Android
- spring
- python
- jsp
- 단축키
Archives
- Today
- Total
romworld
Chepter 06. 객체지향 프로그래밍 클래스 문제 (Circle, Time, Plane, NewCar) 본문
문제 1
Circle
package z_homework.work6.circle;
public class Circle {
double radius;
double x;
double y;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
// if(radius <0) {
// radius = 0;
// }
this.radius = radius;
if (this.radius < 0) {
this.radius = 0;
}
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
Circle() {
}
public double getArea() {
return Math.PI * Math.pow(this.radius, 2);
}
}
문제 2
Time
package z_homework.work6.time;
public class Time {
private int hour;
private int minute;
private int second;
public Time() {
this.hour = 0;
this.minute = 0;
this.second = 0;
}
public Time(int hour, int minute, int second) {
this.hour = hour;
if (this.hour < 0 || this.hour > 23) {
this.hour = 0;
}
this.minute = minute;
if (this.minute < 0 || this.minute > 59) {
this.minute = 0;
}
this.second = second;
if (this.second < 0 || this.second > 59) {
this.second = 0;
}
}
public String toString() {
return String.format("%02d:%02d:%02d", this.hour, this.minute, this.second);
}
}
문제 3
Plane
package z_homework.work6.plane;
public class Plane {
String manufacture; // 제작
String model; // 비행기모델
int maxNumberOfPassengers; // 최대승객
static int NumberOfPlanes; // 지금까지 생성된 비행기 객체의 수,
//정적 변수로 선언 객체가 생성될때마다 생성자에서 증가시킴
public Plane() {
manufacture = "";
model = "";
maxNumberOfPassengers = 0;
NumberOfPlanes++;
}
public static int getNumberOfPlanes() {
return NumberOfPlanes;
}
public Plane(String manufacturer, String model, int maxNumberOfPassengers) {
this.manufacture = manufacturer;
this.model = model;
this.maxNumberOfPassengers = maxNumberOfPassengers;
NumberOfPlanes++;
}
public String getManufacture() {
return manufacture;
}
public void setManufacture(String manufacture) {
this.manufacture = manufacture;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getMaxNumberOfPassengers() {
return maxNumberOfPassengers;
}
public void setMaxNumberOfPassengers(int maxNumberOfPassengers) {
this.maxNumberOfPassengers = maxNumberOfPassengers;
if (this.maxNumberOfPassengers < 0) {
this.maxNumberOfPassengers = 0;
}
}
}
문제 4
Car
package z_homework.work6.car;
public class Car {
private double speed; // 현재속
private String color; // 자동차 색
final private static double MAX_SPEED = 200; // 최대 속력
public Car() {
}
public Car(String color) {
this.color = color;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public static double getMaxSpeed() {
return MAX_SPEED;
}
public boolean speedUp(double speed) {
double targetSpeed = this.speed + speed;
if (speed < 0 || speed > MAX_SPEED) {
return false;
} else {
this.speed = speed;
return true;
}
}
@Override
public String toString() {
return "Car [speed=" + speed + ", color=" + color + "]";
}
}
getter, setter를 활용한다.
보안을 위해 private으로 접근제한을 해놓았기 때문이다!
'JAVA > 문제풀이' 카테고리의 다른 글
컬렉션 프레임워크 문제 (List, Map) (0) | 2022.10.10 |
---|---|
Chapter 07 상속 문제 풀이 ( Human, 도형클래스, Bank) (0) | 2022.10.10 |
배열 middleTest (1) | 2022.09.23 |
chapter 05-4. 다차원배열 문제 (학생들의 평균 구하기) (1) | 2022.09.23 |
Chapter 05-2. 배열 문제 (거스름돈, *그래프, 중복값 제거,로또번호, 가위바위보, 주사위 출력) (0) | 2022.09.18 |
Comments