romworld

Chepter 06. 객체지향 프로그래밍 클래스 문제 (Circle, Time, Plane, NewCar) 본문

JAVA/문제풀이

Chepter 06. 객체지향 프로그래밍 클래스 문제 (Circle, Time, Plane, NewCar)

inderrom 2022. 9. 23. 02:43

문제 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으로 접근제한을 해놓았기 때문이다!

Comments