-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/practicequestions/oopquiz05/AverageValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package practicequestions.oopquiz05; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
// 평균값 구하는 메서드 | ||
// 정수 배열 또는 정수 리스트로 평균값 구해 리턴하는 Calculator 클래스 작성 | ||
class Calculator2 { // nn | ||
int avg(int[] data) { | ||
int total = 0; | ||
for (int num : data) { | ||
total += num; | ||
} | ||
return total/data.length; | ||
} | ||
|
||
int avg(ArrayList<Integer> data) { | ||
int total = 0; | ||
for (int num : data) { | ||
total += num; | ||
} | ||
return total/data.size(); | ||
} | ||
} | ||
public class AverageValue { | ||
public static void main(String[] args) { | ||
Calculator2 cal = new Calculator2(); | ||
|
||
int[] data = {1, 3, 5, 7, 9}; | ||
int result = cal.avg(data); | ||
System.out.println(result); | ||
|
||
ArrayList<Integer> datas = new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9)); | ||
int results = cal.avg(datas); | ||
System.out.println(results); // 5 출력 | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/practicequestions/oopquiz05/ConstructorAndIntialValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package practicequestions.oopquiz05; | ||
|
||
import javax.swing.*; | ||
|
||
// 아래 코드 오류 발생하는데 코드 오류를 수정해라 | ||
class Calculator3 { | ||
Integer value; | ||
|
||
// Integer 자료형은 값을 대입 하지 않으면 null이기 때문에 | ||
// 아래와 같이 생성자 만들고 초깃값 설정해야 한다. | ||
Calculator3() { | ||
this.value = 0; | ||
} | ||
void add(int val) { | ||
this.value += val; | ||
} | ||
|
||
public int getValue() { | ||
return this.value; | ||
} | ||
} | ||
|
||
public class ConstructorAndIntialValue { | ||
public static void main(String[] args) { | ||
Calculator3 cal = new Calculator3(); // 여기서 NullPointerException 이 발생한다. | ||
cal.add(3); | ||
System.out.println(cal.getValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package practicequestions.oopquiz05; | ||
|
||
// 오류 발생하는 문장 찾고 오류 원인 설명 | ||
interface Predator { | ||
} | ||
|
||
class Animal { | ||
} | ||
|
||
class Dog extends Animal { | ||
} | ||
|
||
class Lion extends Animal implements Predator { | ||
} | ||
public class FindError1 { | ||
public static void main(String[] args) { | ||
Animal a = new Animal(); // Animal is a Animal | ||
Animal b = new Dog(); // Dog is a Animal | ||
Animal c = new Lion(); // Lion is a Animal | ||
// Dog d = new Animal(); // Animal is a Dog | ||
Predator e = new Lion(); // Lion is a Predator | ||
} | ||
// 나머지는 다 Animal에 포함되는 것을 가르키는 문장인데 | ||
// Dog d = new Animal();는 동물이 개에 포함된다고 하고 있으므로 오류이다. | ||
// 즉, d를 제외한 나머지 문장은 IS-A 관계가 성립한다. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package practicequestions.oopquiz05; | ||
|
||
interface Predator1 { | ||
String bark(); | ||
} | ||
|
||
abstract class Animal1 { | ||
public String hello() { | ||
return "hello"; | ||
} | ||
} | ||
|
||
class Dog1 extends Animal1 { | ||
} | ||
|
||
class Lion1 extends Animal1 implements Predator1 { | ||
public String bark() { | ||
return "Bark bark!!"; | ||
} | ||
} | ||
|
||
public class FindError2 { | ||
public static void main(String[] args) { | ||
Animal1 a = new Lion1(); | ||
Lion1 b = new Lion1(); | ||
Predator1 c = new Lion1(); | ||
|
||
|
||
System.out.println(a.hello()); | ||
// System.out.println(a.bark()); | ||
// Animal은 hello만 가능 | ||
|
||
System.out.println(b.hello()); | ||
System.out.println(b.bark()); | ||
// System.out.println(c.hello()); | ||
// Predator은 bark만 가능 | ||
|
||
System.out.println(c.bark()); | ||
|
||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/practicequestions/oopquiz05/InterfaceUse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package practicequestions.oopquiz05; | ||
|
||
// 광물 가치 계산기 | ||
// 금 100, 은 90, 구리 80 더하기 기능 (add 메소드) | ||
// 인터페이스 만들어 구현하기 | ||
|
||
interface Calculation { | ||
int getMineralValue(); | ||
|
||
} | ||
|
||
class Gold implements Calculation { | ||
public int getMineralValue() { | ||
return 100; | ||
} | ||
} | ||
|
||
class Silver implements Calculation { | ||
public int getMineralValue() { | ||
return 90; | ||
} | ||
} | ||
|
||
class Bronze implements Calculation { | ||
public int getMineralValue() { | ||
return 80; | ||
} | ||
} | ||
|
||
class MineralCalculator { | ||
int value = 0; | ||
|
||
public void add(Calculation calculation) { | ||
this.value += calculation.getMineralValue(); | ||
} | ||
|
||
public int getValue() { | ||
return this.value; | ||
} | ||
} | ||
|
||
public class InterfaceUse { | ||
public static void main(String[] args) { | ||
MineralCalculator cal = new MineralCalculator(); | ||
cal.add(new Gold()); | ||
cal.add(new Silver()); | ||
cal.add(new Bronze()); | ||
System.out.println(cal.getValue()); // 270 출력 | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/practicequestions/oopquiz05/ListAndObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package practicequestions.oopquiz05; | ||
|
||
// 출력 결과 예측하기 | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
public class ListAndObject { | ||
public static void main(String[] args) { | ||
ArrayList<Integer> a = new ArrayList<>(Arrays.asList(1, 2, 3)); | ||
// ArrayList<Integer> b = new ArrayList<>(a); | ||
// -> b가 a와 동일한 값을 가지지만 독립적으로 생성하고 싶을 때 | ||
// 이렇게 하게되면 아래와 같은 경우 a에 추가해서 size가 늘어났지만 | ||
// b는 변화가 없고 a==b도 false를 반환한다. | ||
|
||
ArrayList<Integer> b = a; | ||
a.add(4); | ||
System.out.println(b.size()); | ||
// a와 b는 모두 동일한 list 객체를 가르키고 있기 떄문에 a = b이다. | ||
// 그러므로 a.add(4)를해서 a의 size가 4로 늘어났다면 b의 size도 4이다. | ||
System.out.println(a==b); // true | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/practicequestions/oopquiz05/MaxLimitCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package practicequestions.oopquiz05; | ||
|
||
// 객체변수 value 100보다 큰 값 가질 수 없도록 제한하는 계산기 | ||
class Calculators { | ||
int value; | ||
|
||
Calculators() { | ||
this.value = 0; | ||
} | ||
|
||
void add(int val) { | ||
this.value += val; | ||
} | ||
|
||
int getValue() { | ||
return this.value; | ||
} | ||
} | ||
|
||
class MaxLimitCalculators extends Calculator { // ㅜㅜ | ||
void add(int val) { | ||
this.value += val; | ||
if (this.value > 100) { | ||
this.value = 100; | ||
} | ||
} | ||
} | ||
|
||
public class MaxLimitCalculator { | ||
public static void main(String[] args) { | ||
MaxLimitCalculators cal = new MaxLimitCalculators(); | ||
cal.add(50); // 50 더하기 | ||
cal.add(60); // 60 더하기 | ||
System.out.println(cal.getValue()); // 100 출력 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package practicequestions.oopquiz05; | ||
|
||
// 홀수 짝수 판별하기 | ||
// 주어진 정수가 홀수인지 짝수인지 판별해주는 Calculator | ||
class Calculator1 { | ||
boolean isOdd (int val) { | ||
return val % 2 == 1; | ||
// if (val % 2 == 1) { | ||
// return true; | ||
// } | ||
// return false; | ||
} | ||
|
||
} | ||
|
||
public class OddOrEven { | ||
public static void main(String[] args) { | ||
Calculator1 cal = new Calculator1(); | ||
System.out.println(cal.isOdd(3)); // 3은 홀수이므로 true 출력 | ||
System.out.println(cal.isOdd(4)); // 4는 짝수이므로 false 출력 | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/practicequestions/oopquiz05/UpgradeCalculators.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package practicequestions.oopquiz05; | ||
|
||
class Calculator { | ||
int value; | ||
|
||
Calculator() { | ||
this.value = 0; | ||
} | ||
|
||
void add(int val) { | ||
this.value += val; | ||
} | ||
|
||
|
||
int getValue() { | ||
return this.value; | ||
} | ||
|
||
} | ||
class UpgradeCalculator extends Calculator{ | ||
void minus(int val) { | ||
this.value -= val; | ||
} | ||
} | ||
|
||
public class UpgradeCalculators { | ||
public static void main(String[] args) { | ||
// Calculator cal = new Calculator(); | ||
// cal.add(10); | ||
// System.out.println(cal.getValue()); // 10 출력 | ||
UpgradeCalculator cal = new UpgradeCalculator(); | ||
cal.add(10); | ||
cal.minus(3); | ||
System.out.println(cal.getValue()); | ||
} | ||
} |