## 과제_1 : 클래스 구조 설계해보기
목표 : 클래스를 만들어보며 클래스의 구조를 이해한다
요구사항 :
- `Human`, `Lion`, `Elephant`, `Dolphin`, `Turtle` 5개의 클래스를 만든다.
- 5개 클래스는 모두 `age`, `weight` 필드를 가지고 있다.
- 생성자를 통해 `age`, `weight`를 초기화한다.
- 각 필드는 `private` 접근 제한자를 가지고 있다.
- 각 필드 정보를 수정할 수 있는 `getter`, `setter` 메서드를 구현한다.
- 5개 클래스는 모두 `public void grow()` 메서드를 가지고 있으며, 해당 메서드를 호출 시 `age`가 `1` 증가한다.
- 5개 클래스는 모두 `public void eat()` 메서드를 가지고 있으며, 해당 메서드를 호출 시 `weight`가 `1` 증가한다.
- `Human`, `Lion`, `Elephant` 클래스는 `walk()` 메서드를 가지고 있다.
- `walk()` 메서드는 `두 발로 걷는다` 또는 `네 발로 걷는다` 라는 메시지를 출력한다.
- `Dolphin`, `Turtle` 클래스는 `swim()` 메서드를 가지고 있다. `swim()`메서드를 호출 시 `헤엄!`이라는 메시지를 출력한다.
## 과제_2 : 배열 정렬하기
목표 : 배열을 오름차순으로 정렬하는 코드를 작성한다.
요구사항 :
- 표준 입출력으로 배열의 크기를 입력받는다.
- 입력받은 크기만큼 정수형 배열을 생성한다.
- 배열의 각 요소에 1~100 사이의 난수를 생성하여 저장한다.
- **배열의 요소를 오름차순으로 정렬한다.**
- 배열의 요소를 출력한다.
---
## 과제_1 : 클래스 구조 설계해보기 모범답안
```java
class Human {
private int age;
private int weight;
public Human(int age, int weight) {
this.age = age;
this.weight = weight;
}
public void grow() {
this.age++;
}
public void eat() {
this.weight++;
}
public int getAge() {
return this.age;
}
public int getWeight() {
return this.weight;
}
public void setAge(int age) {
this.age = age;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void walk() {
System.out.println("두 발로 걷는다");
}
}
class Lion {
private int age;
private int weight;
public Lion(int age, int weight) {
this.age = age;
this.weight = weight;
}
public void grow() {
this.age++;
}
public void eat() {
this.weight++;
}
public int getAge() {
return this.age;
}
public int getWeight() {
return this.weight;
}
public void setAge(int age) {
this.age = age;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void walk() {
System.out.println("네 발로 걷는다");
}
}
class Elephant {
private int age;
private int weight;
public Elephant(int age, int weight) {
this.age = age;
this.weight = weight;
}
public void grow() {
this.age++;
}
public void eat() {
this.weight++;
}
public int getAge() {
return this.age;
}
public int getWeight() {
return this.weight;
}
public void setAge(int age) {
this.age = age;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void walk() {
System.out.println("네 발로 걷는다");
}
}
class Dolphin {
private int age;
private int weight;
public Dolphin(int age, int weight) {
this.age = age;
this.weight = weight;
}
public void grow() {
this.age++;
}
public void eat() {
this.weight++;
}
public int getAge() {
return this.age;
}
public int getWeight() {
return this.weight;
}
public void setAge(int age) {
this.age = age;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void swim() {
System.out.println("헤엄!");
}
}
class Turtle {
private int age;
private int weight;
public Turtle(int age, int weight) {
this.age = age;
this.weight = weight;
}
public void grow() {
this.age++;
}
public void eat() {
this.weight++;
}
public int getAge() {
return this.age;
}
public int getWeight() {
return this.weight;
}
public void setAge(int age) {
this.age = age;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void swim() {
System.out.println("헤엄!");
}
}
```
## 과제_2 : 배열 정렬하기 모범답안
```java
import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
int[] numbers = { 3, 1, 9, 7, 5 };
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = 0; j < numbers.length - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(numbers));
}
}
```
> 사용된 알고리즘 : 버블 정렬
---
### 버블 정렬
```java
// bubbleSort 메서드
public static void bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
```