diff --git a/src/choyoungjun/SOLID/DIP.java b/src/choyoungjun/SOLID/DIP.java new file mode 100644 index 0000000..a368e1c --- /dev/null +++ b/src/choyoungjun/SOLID/DIP.java @@ -0,0 +1,119 @@ +package choyoungjun.SOLID; +//원칙 적용 방법 +//고수준 모듈의 변화되는 부분을 추상화합니다. +//저수준 모듈을 추상화에 의존시킵니다. +//계산기 클래스에 추상화된 부모 클래스를 포함시킵니다. ok +//연산 클래스는 추상화된 부모 클래스를 상속받아 기능별로 구현합니다. ok + +//문제 +//계산기(고수준 모듈)가 개별 연산 클래스(저수준 모듈)을 포함하며 계산 기능을 구현할 때 연산 클래스에 의존하고 있습니다. +//저수준 모듈에서 변경이 발생되면 고수준 모듈에 수정사항이 발생합니다. + +import java.util.HashMap; +import java.util.Map; + +//클라이언트는 calculator를 통해 통신해야 한다. +public class DIP { + public static void main(String[] args) throws InstantiationException, IllegalAccessException { + Calculator calculator = new Calculator( + new AddOperation(), + new SubstractOperation(), + new MultiplyOperation(), + new DivideOperation() + ); + + int firNum = 140; + int secNum = 60; + + String operator = "+"; + int answer = calculator.calculate(operator, firNum, secNum); + System.out.println(operator + " answer = " + answer); + + operator = "-"; + answer = calculator.calculate(operator, firNum, secNum); + System.out.println(operator + " answer = " + answer); + + operator = "*"; + answer = calculator.calculate(operator, firNum, secNum); + System.out.println(operator + " answer = " + answer); + + operator = "/"; + answer = calculator.calculate(operator, firNum, secNum); + System.out.println(operator + " answer = " + answer); + } +} +class Calculator { + + private AbstractOperation abstractOperation; + + Map abstractOperationMap = new HashMap<>(); + public Calculator(AbstractOperation...abstractOperations) throws InstantiationException, IllegalAccessException { + for (AbstractOperation abstractOperation : abstractOperations){ + abstractOperationMap.put(abstractOperation.getOperator(), abstractOperation.getClass().newInstance()); + } + } + + public void setAbstractOperation(AbstractOperation abstractOperation) { + this.abstractOperation = abstractOperation; + } + + public int calculate(String operator, int firstNumber, int secondNumber) { + int answer = 0; // 문제 : operator라는 string과 그 주인장을 연결시켜야 함 + abstractOperation = abstractOperationMap.get(operator); + answer = abstractOperation.operate(firstNumber, secondNumber); + return answer; + } +} +abstract class AbstractOperation{ + private String operator; + + public abstract int operate(int firstNumber, int secondNumber); + + public String getOperator() { + return operator; + } + + public void setOperator(String operator) { + this.operator = operator; + } + +} + +class AddOperation extends AbstractOperation{ + AddOperation(){ + this.setOperator("+"); + } + @Override + public int operate(int firstNumber, int secondNumber){ + return firstNumber + secondNumber; + } +} +class SubstractOperation extends AbstractOperation { + SubstractOperation(){ + this.setOperator("-"); + } + @Override + public int operate(int firstNumber, int secondNumber){ + return firstNumber - secondNumber; + } +} + +class MultiplyOperation extends AbstractOperation{ + MultiplyOperation(){ + this.setOperator("*"); + } + @Override + public int operate(int firstNumber, int secondNumber){ + return firstNumber * secondNumber; + } +} + +class DivideOperation extends AbstractOperation{ + DivideOperation(){ + this.setOperator("/"); + } + @Override + public int operate(int firstNumber, int secondNumber){ + return firstNumber / secondNumber; + } +} \ No newline at end of file diff --git a/src/choyoungjun/SOLID/ISP.java b/src/choyoungjun/SOLID/ISP.java new file mode 100644 index 0000000..7d0fb43 --- /dev/null +++ b/src/choyoungjun/SOLID/ISP.java @@ -0,0 +1,104 @@ +package choyoungjun.SOLID; + +public class ISP { + public static void main(String[] args) { + int firNum = 140; + int secNum = 60; + + // 연산 결과만 출력 + DisplayTypeA displayTypeA = new DisplayTypeA(); + displayTypeA.displayResult(new AddOperation(), firNum, secNum); + + // 연산 과정까지 출력 + DisplayTypeB displayTypeB = new DisplayTypeB(); + displayTypeB.displayResultWithOperator(new AddOperation(), firNum, secNum); + } +} +class Calculator { + public int calculate(AbstractOperation operation, int firstNumber, int secondNumber){ + return operation.operate(firstNumber, secondNumber); + } +} + +abstract class AbstractOperation { + public abstract int operate(int firstNumber, int secondNumber); + public abstract String getOperator(); +} + +interface DisplayResult { + void displayResult(AbstractOperation operation, int firstNumber, int secondNumber) throws Exception; +} + +interface DisplayWithOperator{ + void displayResultWithOperator(AbstractOperation operation, int firstNumber, int secondNumber) throws Exception; +} + +// 연산 결과만 출력 +class DisplayTypeA extends Calculator implements DisplayResult { + + @Override + public void displayResult(AbstractOperation operation, int firstNumber, int secondNumber) { + int answer = operation.operate(firstNumber, secondNumber); + System.out.println(answer); + } + +} + +// 연산 과정을 포함한 출력 +class DisplayTypeB extends Calculator implements DisplayWithOperator { + + @Override + public void displayResultWithOperator(AbstractOperation operation, int firstNumber, int secondNumber) { + int answer = operation.operate(firstNumber, secondNumber); + String operator = operation.getOperator(); + System.out.println(firstNumber + " " + operator + " " + secondNumber + " = " + answer); + } +} + +class AddOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber + secondNumber; + } + + @Override + public String getOperator() { + return "+"; + } +} + +class SubstractOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber - secondNumber; + } + + @Override + public String getOperator() { + return "-"; + } +} + +class MultiplyOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber * secondNumber; + } + + @Override + public String getOperator() { + return "*"; + } +} + +class DivideOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber / secondNumber; + } + + @Override + public String getOperator() { + return "/"; + } +} \ No newline at end of file diff --git a/src/choyoungjun/SOLID/LSP.java b/src/choyoungjun/SOLID/LSP.java new file mode 100644 index 0000000..6c1a8cb --- /dev/null +++ b/src/choyoungjun/SOLID/LSP.java @@ -0,0 +1,74 @@ +package choyoungjun.SOLID; + +//원칙 적용 방법 +//부모 와 자식 클래스 사이의 행위가 일관성이 있도록 추상화를 좀 더 정교하게 구현합니다. +//연산 기능을 추상화한 부모 클래스에 피연산자 값의 유효성 검사를 진행하는 메서드를 추가해 줍니다. +//계산기 클래스에서는 이 메서드를 사용하여 유효성 검사를 진행하고 이 유효성 검사가 필요한 자식 클래스에서는 이 추가된 유효성 검사 조건을 구체화 합니다. +//isInvalid : 추상 메서드 + +public class LSP { + public static void main(String[] args) { + Calculator calculator = new Calculator(); + + int firNum = 140; + int secNum = 0; + + int answer = calculator.calculate(new AddOperation(), firNum, secNum); + System.out.println(" + answer = " + answer); + + answer = calculator.calculate(new SubstractOperation(), firNum, secNum); + System.out.println(" - answer = " + answer); + + answer = calculator.calculate(new MultiplyOperation(), firNum, secNum); + System.out.println(" * answer = " + answer); + + answer = calculator.calculate(new DivideOperation(), firNum, secNum); + System.out.println(" / answer = " + answer); + } +} + +class Calculator { + // 연산 기능을 추상화된 부모클래스에 의존하여 처리한다. + public int calculate(AbstractOperation operation, int firstNumber, int secondNumber) { + if (!operation.isValid(firstNumber, secondNumber)) return -99999; + return operation.operate(firstNumber, secondNumber); + } +} + +abstract class AbstractOperation { + public boolean isValid(int firstNumber, int secondNumber){return true;} + public abstract int operate(int firstNumber, int secondNumber); +} + +class AddOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber + secondNumber; + } +} + +class SubstractOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber - secondNumber; + } +} + +class MultiplyOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber * secondNumber; + } +} + +class DivideOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber/secondNumber; + } + @Override + public boolean isValid(int firstNumber, int secondNumber) { + if (secondNumber==0) return false; + return true; + } +} \ No newline at end of file diff --git a/src/choyoungjun/SOLID/OCP.java b/src/choyoungjun/SOLID/OCP.java new file mode 100644 index 0000000..7f7e6f6 --- /dev/null +++ b/src/choyoungjun/SOLID/OCP.java @@ -0,0 +1,104 @@ +package choyoungjun.SOLID; +//변화되는 부분을 추상화해서 변화를 고정 시킵니다. +//기능이 추가될 때 클래스의 상속을 통해서 하위 클래스에서 기능을 구현 하도록 합니다. +//기존코드를 수정하지 않아도 객체 상속의 다형성 원리에 의해 기능이 확장 되도록 합니다. +//연산 클래스는 추상화된 부모 클래스를 상속받아 기능별로 구현합니다. +//AbstractOperation : 추상 클래스 +//operate : 추상 메서드 + +//고려사항 : 계산기는 늘 2개의 숫자를 가지고 움직인다. +//안되는거 : 위에 new 연산자로 iv로 주입해버리고싶은데 어떻게 안되나요? + +public class OCP { + public static void main(String[] args) { + Calculator calculator = new Calculator( +// new AddOperation(), +// new SubstractOperation() +// new MultiplyOperation() +// new DivideOperation() + ); + + int firNum = 140; + int secNum = 60; + + int answer = calculator.add(firNum,secNum); + System.out.println(" + answer = " + answer); + + answer = calculator.substract(firNum,secNum); + System.out.println(" - answer = " + answer); + + answer = calculator.multiply(firNum, secNum); + System.out.println(" * answer = " + answer); + + answer = calculator.divide(firNum, secNum); + System.out.println(" / answer = " + answer); + } +} +class Calculator { + private AbstractOperation abstractOperation; + + public void setAbstractOperation(AbstractOperation abstractOperation) { + this.abstractOperation = abstractOperation; + } + + public Calculator(AbstractOperation...abstractOperations) { + for (AbstractOperation abstractOperation : abstractOperations){ + this.setAbstractOperation(abstractOperation); + } + } // 가변인자 for문돌려서 자동으로 추가함 + + + public int add(int firstNumber,int secondNumber){ + setAbstractOperation(new AddOperation()); + return abstractOperation.operate(firstNumber,secondNumber); + } + + public int substract(int firstNumber, int secondNumber){ + setAbstractOperation(new SubstractOperation()); + return abstractOperation.operate(firstNumber, secondNumber); + } + + public int multiply(int firstNumber, int secondNumber){ + setAbstractOperation(new MultiplyOperation()); + return abstractOperation.operate(firstNumber, secondNumber); + } + + public int divide(int firstNumber, int secondNumber){ + setAbstractOperation(new DivideOperation()); + return abstractOperation.operate(firstNumber, secondNumber); + } + + +} + +class AddOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber+secondNumber; + } +} + +class SubstractOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber-secondNumber; + } +} + +class MultiplyOperation extends AbstractOperation{ + @Override + public int operate(int firstNumber, int secondNumber){ + return firstNumber * secondNumber; + } +} + +class DivideOperation extends AbstractOperation{ + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber/secondNumber; + } +} + +abstract class AbstractOperation{ + abstract public int operate(int firstNumber, int secondNumber); +} \ No newline at end of file diff --git a/src/choyoungjun/SOLID/SRP.java b/src/choyoungjun/SOLID/SRP.java new file mode 100644 index 0000000..96db897 --- /dev/null +++ b/src/choyoungjun/SOLID/SRP.java @@ -0,0 +1,158 @@ +package choyoungjun.SOLID; + +import static java.lang.System.exit; + +public class SRP { + public static void main(String[] args) { + Calculator calculator = new Calculator(); + + calculator.setFirstNumber(140); + calculator.setSecondNumber(0); + + + int answer = calculator.addOperation(); + System.out.println("answer = " + answer); + + answer = calculator.subtractOperation(); + System.out.println("answer = " + answer); + + answer = calculator.multiplyOperation(); + System.out.println("answer = " + answer); + + answer = (int)calculator.divideOperation(); + System.out.println("answer = " + answer); + } +} +class Calculator { + private int firstNumber; + private int secondNumber; + public Calculator(){ + this.firstNumber = 0; + this.secondNumber = 0; + } + + public Calculator(int firstNumber, int secondNumber) { + this.firstNumber = firstNumber; + this.secondNumber = secondNumber; + } + + public int addOperation() { + AddOperation addOperation = new AddOperation(firstNumber,secondNumber); + return addOperation.operate(); + } + public int subtractOperation(){ + SubstractOperation substractOperation = new SubstractOperation(firstNumber, secondNumber); + return substractOperation.operate(); + } + + public int multiplyOperation(){ + MultiplyOperation multiplyOperation = new MultiplyOperation(firstNumber,secondNumber); + return multiplyOperation.operate(); + } + public double divideOperation() { + DivideOperation divideOperation = new DivideOperation(firstNumber, secondNumber); + return divideOperation.operate(); + } + + public void setFirstNumber(int firstNumber) { + this.firstNumber = firstNumber; + } + + public void setSecondNumber(int secondNumber) { + this.secondNumber = secondNumber; + } +} + +class AddOperation{ + private int firstNumber; + private int secondNumber; + + public AddOperation(int firstNumber, int secondNumber) { + this.firstNumber = firstNumber; + this.secondNumber = secondNumber; + } + + public void setFirstNumber(int firstNumber) { + this.firstNumber = firstNumber; + } + + public void setSecondNumber(int secondNumber) { + this.secondNumber = secondNumber; + } + + public int operate(){ + return firstNumber+secondNumber; + } +} + +class SubstractOperation{ + private int firstNumber; + private int secondNumber; + + public SubstractOperation(int firstNumber, int secondNumber) { + this.firstNumber = firstNumber; + this.secondNumber = secondNumber; + } + + public void setFirstNumber(int firstNumber) { + this.firstNumber = firstNumber; + } + + public void setSecondNumber(int secondNumber) { + this.secondNumber = secondNumber; + } + + public int operate(){ + return firstNumber-secondNumber; + } +} + +class MultiplyOperation{ + private int firstNumber; + private int secondNumber; + + public MultiplyOperation(int firstNumber, int secondNumber) { + this.firstNumber = firstNumber; + this.secondNumber = secondNumber; + } + + public void setFirstNumber(int firstNumber) { + this.firstNumber = firstNumber; + } + + public void setSecondNumber(int secondNumber) { + this.secondNumber = secondNumber; + } + + public int operate(){ + return firstNumber*secondNumber; + } +} + +class DivideOperation{ + private int firstNumber; + private int secondNumber; + + public DivideOperation(int firstNumber, int secondNumber) { + this.firstNumber = firstNumber; + this.secondNumber = secondNumber; + } + + public void setFirstNumber(int firstNumber) { + this.firstNumber = firstNumber; + } + + public void setSecondNumber(int secondNumber) { + this.secondNumber = secondNumber; + } + + public double operate(){ + try{ + return firstNumber/secondNumber; + } catch(ArithmeticException e){ + System.out.println("0으로 나눌수 없습니다. 다시 계산해주세요"); + exit(0); + } + return firstNumber/secondNumber; + } +} \ No newline at end of file diff --git a/src/choyoungjun/report1/Report1_1 b/src/choyoungjun/report1/Report1_1 new file mode 100644 index 0000000..9736b82 --- /dev/null +++ b/src/choyoungjun/report1/Report1_1 @@ -0,0 +1,4 @@ +2-4번 문제 + +정답 : +4. float f = 3.14f로 초기화 diff --git a/src/choyoungjun/report1/Report1_2 b/src/choyoungjun/report1/Report1_2 new file mode 100644 index 0000000..42e8ee3 --- /dev/null +++ b/src/choyoungjun/report1/Report1_2 @@ -0,0 +1,17 @@ +2-7번 문제 + +정답 : +System.out.println("1" + "2"); + => 12 +System.out.println(true+""); + => true +System.out.println('A' + 'B'); + => 131 (ASKII코드 참조) +System.out.println('1' + 2); + => 51 +System.out.println('1' + '2'); + => 99 +System.out.println('J' +"ava"); + => Java +System.out.println(true + null); + => Error \ No newline at end of file diff --git a/src/choyoungjun/report1/Report1_3.java b/src/choyoungjun/report1/Report1_3.java new file mode 100644 index 0000000..d3d256a --- /dev/null +++ b/src/choyoungjun/report1/Report1_3.java @@ -0,0 +1,18 @@ +package choyoungjun.report1; + +// 2-8번 문제 +public class Report1_3 { + public static void main(String[] args) { + int x = 1; + int y = 2; + int z = 3; + x = 2; + y = 3; + z = 1; + + System.out.println("x="+x); + System.out.println("y="+y); + System.out.println("z="+z); + } +} +//예상 결과 : x=2, y=3, z=1 \ No newline at end of file diff --git a/src/choyoungjun/report2/Report2_1 b/src/choyoungjun/report2/Report2_1 new file mode 100644 index 0000000..98e4830 --- /dev/null +++ b/src/choyoungjun/report2/Report2_1 @@ -0,0 +1,17 @@ +byte b = 10; +char ch = 'A'; +int i = 100; +long l = 1000L; + +//3-1. 다음 중 형변환을 생략할 수 있는 것은? (모두 고르시오) +b = (byte)i; +ch = (char)b; +short s = (short)ch; +float f = (float)l; +i = (int)ch; + + +해설 : 형변환 연산자는 타입간 형변환이나 큰 타입으로의 변환시 사용한다.(값 손실 방지를 위해) +4번과 5번은 생략할수 있다. +4번 : 값 손실없다. +5번 : 값 손실없다. \ No newline at end of file diff --git a/src/choyoungjun/report2/Report2_2.java b/src/choyoungjun/report2/Report2_2.java new file mode 100644 index 0000000..21f7e8a --- /dev/null +++ b/src/choyoungjun/report2/Report2_2.java @@ -0,0 +1,30 @@ +package choyoungjun.report2; +// 3-2번 문제 +public class Report2_2 { + public static void main(String[] args) { + int x = 2; + int y = 5; + char c = 'A'; // 'A'의 문자코드는 65 + + System.out.println(y >= 5 || x < 0 && x > 2); + // true or (false and false) = true or false = true + System.out.println(y += 10 - x++); + // y += 10 => 15, y-x = 13 , x++ => x의 값을 3으로 변화시킨다.(print에 반영안됨) + System.out.println(x += 2); + // x += 2 => x에다가 2를 더해준다 => 3+2 = 5 + System.out.println(!('A' <= c && c <= 'Z')); + // !(true and true) = false + System.out.println('C' - c); + // C의 아스키코드는 67, 67-65 = 2 + System.out.println('5' - '0'); + // 5 + System.out.println(c + 1); + // char + int = int => 66 + System.out.println(++c); + // c의 ASKII코드를 66으로 변화시킨다. 따라서 B됨 + System.out.println(c++); + // print해서 B를 찍고 char c = 'C'형태로 변화시킨다. + System.out.println(c); + // char c = 'C' 출력된다. + } +} diff --git a/src/choyoungjun/report2/Report2_3.java b/src/choyoungjun/report2/Report2_3.java new file mode 100644 index 0000000..2dfcb0a --- /dev/null +++ b/src/choyoungjun/report2/Report2_3.java @@ -0,0 +1,14 @@ +package choyoungjun.report2; +//3-3. 아래는 변수의 num 값 중에서 백의 자리 이하를 버리는 코드이다. +//만일 변수 num의 값이 '456'이라면 '400'이 되고, '111'이라면 '100'이 된다. +//알맞은 코드를 넣으시오. +import java.util.Scanner; + +public class Report2_3 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int num = sc.nextInt(); + num = num/100*100; + System.out.println(num); + } +} diff --git a/src/choyoungjun/report2/Report2_4.java b/src/choyoungjun/report2/Report2_4.java new file mode 100644 index 0000000..d076f74 --- /dev/null +++ b/src/choyoungjun/report2/Report2_4.java @@ -0,0 +1,14 @@ +package choyoungjun.report2; +//3-4. 아래의 코드는 사과를 담는데 필요한 바구니(버켓)의 수를 구하는 코드이다. +//만일 사과의 수가 123개이고 하나의 바구니에는 10개의 사과를 담을 수 있다면, 13개의 바구니가 필요할 것이다. +//알맞은 코드를 넣으시오. + +public class Report2_4 { + public static void main(String[] args) { + int numOfApples = 131; // 사과의 개수 + int sizeOfBucket = 10; // 바구니의 크기(바구니에 담을 수 있는 사과의 개수) + int numOfBucket = (int)(Math.ceil((double) numOfApples/sizeOfBucket)); // 모든 사과를 담는데 필요한 바구니의 수 + + System.out.println("필요한 바구니의 수 :"+numOfBucket); + } +} diff --git a/src/choyoungjun/report2/Report2_5.java b/src/choyoungjun/report2/Report2_5.java new file mode 100644 index 0000000..0fa0908 --- /dev/null +++ b/src/choyoungjun/report2/Report2_5.java @@ -0,0 +1,14 @@ +package choyoungjun.report2; +//3-5. 아래는 변수 num의 값에 따라 '양수', '음수', '0'을 출력하는 코드이다. +//삼항연산자를 이용해서 빈칸에 알맞은 코드를 넣으시오. +//Hint : 삼항 연산자를 두 번 사용할 것! + +import java.util.Scanner; + +public class Report2_5 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int num = sc.nextInt(); + System.out.println(num>0? "양수" : (num==0? "0" : "음수")); + } +} diff --git a/src/choyoungjun/report2/Report2_6.java b/src/choyoungjun/report2/Report2_6.java new file mode 100644 index 0000000..692d90a --- /dev/null +++ b/src/choyoungjun/report2/Report2_6.java @@ -0,0 +1,17 @@ +package choyoungjun.report2; + +import java.util.Scanner; + +//3-6. 아래는 화씨(Fahrenheit)를 섭씨(Celcius)로 변환하는 코드이다. +//변환 공식이 'C = 5/9*(F-32)'라고 할 때, 빈 칸에 알맞은 코드를 넣으시오. +// 단, 변환값은 소수점 셋째자리에서 반올림하며, Math.round() 함수를 사용하지 않고 처리할 것! +public class Report2_6 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int fahrenheit = sc.nextInt(); + float celcius = ((int)((5f/9*(fahrenheit-32)*100)+0.5f))/100f; + + System.out.println("Fahrenheit:"+fahrenheit); + System.out.println("Celcius:"+celcius); + } +} diff --git a/src/choyoungjun/report3/Report3_1 b/src/choyoungjun/report3/Report3_1 new file mode 100644 index 0000000..2cedb4a --- /dev/null +++ b/src/choyoungjun/report3/Report3_1 @@ -0,0 +1,24 @@ +//int형 변수 x가 10보다 크고 20보다 작을 때 true인 조건식 + int x; + (10<=x && x<20) +//char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식 + char ch; + (ch!=' ' || char!= '\t') +//char형 변수 ch가 'x' 또는 'X'일 때 true인 조건식 + char ch; + (ch=='x' || ch=='X') +//char형 변수 ch가 숫자('0'~'9')일 때 true인 조건식 + char ch; + ('0'<=ch && ch<='9') +//char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식 + char ch; + (('A'<=ch && ch<='Z') || ('a'<=ch && ch<='z')) +//int형 변수 year가 400으로 나눠떨어지거나 또는 4로 나눠떨어지고 100으로 나눠떨어지지 않을때 true인 조건식 + int year + (year%400 == 0 || (year%4==0 && year%100 != 0)) +//boolean형 변수 powerOn이 false일 때 true인 조건식 + boolean powerOn; + (!powerOn) +//문자열 참조변수 str이 "yes"일 때 true인 조건식 + String str; + str.equals("yes") \ No newline at end of file diff --git a/src/choyoungjun/report3/Report3_10.java b/src/choyoungjun/report3/Report3_10.java new file mode 100644 index 0000000..fbe5c72 --- /dev/null +++ b/src/choyoungjun/report3/Report3_10.java @@ -0,0 +1,27 @@ +package choyoungjun.report3; + +public class Report3_10 { + public static void main(String[] args) { + // 1~100사이의 임의의 값을 얻어서 answer에 저장한다. + int answer = (int)(Math.random()*100+1); + int input = 0; //사용자입력을 저장할 공간 + int count = 0; //시도횟수를 세기위한 변수 + + // 화면으로 부터 사용자입력을 받기 위해서 Scanner클래스 사용 + java.util.Scanner s = new java.util.Scanner(System.in); + do { + count++; + System.out.print("1과 100사이의 값을 입력하세요 : "); + input = s.nextInt(); //입력받은 값을 변수 input에 저장한다. + if (input < answer) + System.out.println("더 큰 수를 입력하세요"); + else if (input > answer) + System.out.println("더 작은 수를 입력하세요"); + else{ + System.out.println("맞혔습니다."); + System.out.println("시도횟수는 "+count+"번입니다."); + break; + } + } while(true); //무한반복문 + } +} diff --git a/src/choyoungjun/report3/Report3_2.java b/src/choyoungjun/report3/Report3_2.java new file mode 100644 index 0000000..1d1e80a --- /dev/null +++ b/src/choyoungjun/report3/Report3_2.java @@ -0,0 +1,12 @@ +package choyoungjun.report3; + +public class Report3_2 { + //4-2. 1부터 20까지의 정수중에서 2 또는 3의 배수가 아닌 수의 총합을 구하세요. + public static void main(String[] args) { + int sum = 0; + for (int i=1; i<21; i++){ + if (i%2!=0 && i%3!=0) sum += i; + } + System.out.println(sum); + } +} diff --git a/src/choyoungjun/report3/Report3_3.java b/src/choyoungjun/report3/Report3_3.java new file mode 100644 index 0000000..07efab7 --- /dev/null +++ b/src/choyoungjun/report3/Report3_3.java @@ -0,0 +1,13 @@ +package choyoungjun.report3; + +public class Report3_3 { + public static void main(String[] args) { + int sum = 0; + int totalSum = 0; + for (int i=1; i<11; i++){ + sum += i; + totalSum += sum; + } + System.out.println("totalSum="+totalSum); + } +} diff --git a/src/choyoungjun/report3/Report3_4.java b/src/choyoungjun/report3/Report3_4.java new file mode 100644 index 0000000..692ddae --- /dev/null +++ b/src/choyoungjun/report3/Report3_4.java @@ -0,0 +1,16 @@ +package choyoungjun.report3; + +public class Report3_4 { + public static void main(String[] args) { + int sum = 0; // 총합을 저장할 변수 + int num = 1; + while (true){ + if (num%2==0) sum -= num; + else sum += num; + if (sum>=100) break; + num++; + } + System.out.println("num="+num); + System.out.println("sum="+sum); + } +} diff --git a/src/choyoungjun/report3/Report3_5.java b/src/choyoungjun/report3/Report3_5.java new file mode 100644 index 0000000..270e74f --- /dev/null +++ b/src/choyoungjun/report3/Report3_5.java @@ -0,0 +1,16 @@ +package choyoungjun.report3; + +public class Report3_5 { + public static void main(String[] args) { + int i=0; + while (i<10){ + int j=0; + while (j<=i){ + System.out.print("*"); + j++; + } + System.out.println(); + i++; + } + } +} diff --git a/src/choyoungjun/report3/Report3_6.java b/src/choyoungjun/report3/Report3_6.java new file mode 100644 index 0000000..83b262f --- /dev/null +++ b/src/choyoungjun/report3/Report3_6.java @@ -0,0 +1,9 @@ +package choyoungjun.report3; + +public class Report3_6 { + public static void main(String[] args) { + for (int i=1; i<6; i++){ + System.out.println(i+" "+(6-i)); + } + } +} diff --git a/src/choyoungjun/report3/Report3_7.java b/src/choyoungjun/report3/Report3_7.java new file mode 100644 index 0000000..b6f4449 --- /dev/null +++ b/src/choyoungjun/report3/Report3_7.java @@ -0,0 +1,15 @@ +package choyoungjun.report3; + +public class Report3_7 { + public static void main(String[] args) { + String str = "12345"; + int sum = 0; + + for (int i = 0; i < str.length(); i++) { + sum += str.charAt(i)-'0'; + } + + System.out.println("sum=" + sum); + + } +} diff --git a/src/choyoungjun/report3/Report3_8.java b/src/choyoungjun/report3/Report3_8.java new file mode 100644 index 0000000..6f9bb75 --- /dev/null +++ b/src/choyoungjun/report3/Report3_8.java @@ -0,0 +1,8 @@ +package choyoungjun.report3; + +public class Report3_8 { + public static void main(String[] args) { + int value = (int)(Math.random()*6+1); + System.out.println("value:"+value); + } +} diff --git a/src/choyoungjun/report3/Report3_9.java b/src/choyoungjun/report3/Report3_9.java new file mode 100644 index 0000000..a19418e --- /dev/null +++ b/src/choyoungjun/report3/Report3_9.java @@ -0,0 +1,13 @@ +package choyoungjun.report3; + +public class Report3_9 { + public static void main(String[] args) { + int num = 12345; + int sum = 0; + while (num > 0){ + sum += num%10; + num /= 10; + } + System.out.println("sum="+sum); + } +} diff --git a/src/choyoungjun/report4/Report4_1 b/src/choyoungjun/report4/Report4_1 new file mode 100644 index 0000000..faab506 --- /dev/null +++ b/src/choyoungjun/report4/Report4_1 @@ -0,0 +1,8 @@ +//5-1. 다음은 배열을 선언하거나 초기화한 것이다. 잘못된 것을 고르고 그 이유를 설명하세요. +int[] arr[]; +int[] arr = {1,2,3,}; +int[] arr = new int[5]; +int[] arr = new int[5]{1,2,3,4,5}; +int arr[5]; +int[] arr[] = new int[3][]; + diff --git a/src/choyoungjun/report4/Report4_2 b/src/choyoungjun/report4/Report4_2 new file mode 100644 index 0000000..59311c3 --- /dev/null +++ b/src/choyoungjun/report4/Report4_2 @@ -0,0 +1,10 @@ +//5-2. 다음과 같은 배열이 있을 때, arr[3].length의 값은? +int[][]arr ={ + {5,5,5,5,5}, + {10,10,10}, + {20,20,20,20}, + {30,30} +}; + +arr[3] = {30,30} +length = 2 \ No newline at end of file diff --git a/src/choyoungjun/report4/Report4_3.java b/src/choyoungjun/report4/Report4_3.java new file mode 100644 index 0000000..fe7e937 --- /dev/null +++ b/src/choyoungjun/report4/Report4_3.java @@ -0,0 +1,10 @@ +package choyoungjun.report4; + +public class Report4_3 { + public static void main(String[] args) { + int[] arr = {10, 20, 30, 40, 50}; + int sum = 0; + for (int i : arr) sum += i; + System.out.println("sum="+sum); + } +} diff --git a/src/choyoungjun/report4/Report4_4.java b/src/choyoungjun/report4/Report4_4.java new file mode 100644 index 0000000..e334d39 --- /dev/null +++ b/src/choyoungjun/report4/Report4_4.java @@ -0,0 +1,26 @@ +package choyoungjun.report4; + +public class Report4_4 { + public static void main(String[] args) { + int[][] arr = { + { 5, 5, 5, 5, 5 }, + { 10, 10, 10, 10, 10 }, + { 20, 20, 20, 20, 20 }, + { 30, 30, 30, 30, 30 } + }; + + int total = 0; + float average = 0; + float length = 0; + for (int[] intArr : arr){ + length += intArr.length; + for (int i=0; i", i + 1, new String(question)); + String answer = scanner.nextLine(); + + if (words[i].equals(answer.trim())) + System.out.printf("맞았습니다.%n%n"); + else + System.out.printf("틀렸습니다.%n%n"); + } + } +} diff --git a/src/choyoungjun/report4/Report4_7.png b/src/choyoungjun/report4/Report4_7.png new file mode 100644 index 0000000..346243a Binary files /dev/null and b/src/choyoungjun/report4/Report4_7.png differ diff --git a/src/choyoungjun/report5/Report5_1.java b/src/choyoungjun/report5/Report5_1.java new file mode 100644 index 0000000..ada6a96 --- /dev/null +++ b/src/choyoungjun/report5/Report5_1.java @@ -0,0 +1,21 @@ +package choyoungjun.report5; + +public class Report5_1 { + public static void main(String[] args) { + //6-1. 다음과 같은 멤버 변수를 갖는 Student 클래스를 정의하세요. + //타입 : String, 변수명 : name, 설명 : 학생 이름 + //타입 : int, 변수명 : ban, 설명 : 반 + //타입 : int, 변수명 : no, 설명 : 번호 + //타입 : int, 변수명 : kor, 설명 : 국어 점수 + //타입 : int, 변수명 : eng, 설명 : 영어 점수 + //타입 : int, 변수명 : math, 설명 : 수학 점수 + } +} +//class Student{ +// String name; +// int ban; +// int no; +// int kor; +// int eng; +// int math; +//} \ No newline at end of file diff --git a/src/choyoungjun/report5/Report5_2.java b/src/choyoungjun/report5/Report5_2.java new file mode 100644 index 0000000..f96017c --- /dev/null +++ b/src/choyoungjun/report5/Report5_2.java @@ -0,0 +1,36 @@ +package choyoungjun.report5; + +//6-2. 6-1에서 정의한 Student 클래스에 생성자와 info()를 추가해서 실행결과와 같은 결과를 얻도록 하세요. +public class Report5_2 { + public static void main(String[] args) { +// Student s = new Student("홍길동", 1, 1, 100, 60, 76); +// +// String str = s.info(); +// System.out.println(str); + + // 예상 결과 : 홍길동, 1, 1, 100, 60, 76, 236, 78.7 + } +} +//class Student{ +// String name; +// int ban; +// int no; +// int kor; +// int eng; +// int math; +// int total; +// double average; +// Student(String name, int ban, int no, int kor, int eng, int math){ +// this.name = name; +// this.ban = ban; +// this.no = no; +// this.kor = kor; +// this.eng = eng; +// this.math = math; +// this.total = kor+eng+math; +// this.average = (Math.round(this.total/3d*10))/10d; +// } +// public String info(){ +// return name+", "+ban+", "+no+", "+kor+", "+eng+", "+math+", "+total+", "+average; +// } +//} \ No newline at end of file diff --git a/src/choyoungjun/report5/Report5_3.java b/src/choyoungjun/report5/Report5_3.java new file mode 100644 index 0000000..a13c95e --- /dev/null +++ b/src/choyoungjun/report5/Report5_3.java @@ -0,0 +1,60 @@ +package choyoungjun.report5; +//6-3. 연습문제 6-1에서 정의한 Student 클래스에 다음과 같이 정의된 두 개의 메서드를 추가하세요. +//1. 메서드명 : getTotal +//기능 : 국어(kor), 영어(eng), 수학(math)의 점수를 모두 더해서 반환한다. +//반환타입 : int +//매개변수 : 없음 +//2. 메서드명 : getAverage +//기능 : 총점(국어점수+영어점수+수학점수)을 과목수로 나눈 평균을 구한다. +//소수점 둘째자리에서 반올림할 것. +//반환타입 : float +//매개변수 : 없음 + + +public class Report5_3 { + public static void main(String[] args) { + Student s = new Student(); + s.name = "홍길동"; + s.ban = 1; + s.no = 1; + s.kor = 100; + s.eng = 60; + s.math = 76; + System.out.println("이름 :"+s.name); + System.out.println("총점 :"+s.getTotal()); + System.out.println("평균 :"+s.getAverage()); + }//예상 결과 : 이름 : 홍길동, 총점 : 236, 평균 : 78.7 +} +class Student{ + String name; + int ban; + int no; + int kor; + int eng; + int math; + int total; + float average; + + public Student() { + } + + Student(String name, int ban, int no, int kor, int eng, int math){ + this.name = name; + this.ban = ban; + this.no = no; + this.kor = kor; + this.eng = eng; + this.math = math; + this.total = getTotal(); + this.average = getAverage(); + } + public int getTotal(){ + return kor+eng+math; + } + public float getAverage(){ + return (Math.round(this.getTotal()/3d*10))/10f; + } + public String info(){ + return name+", "+ban+", "+no+", "+kor+", "+eng+", "+math+", "+total+", "+average; + } +} \ No newline at end of file diff --git a/src/choyoungjun/report5/Report5_4.java b/src/choyoungjun/report5/Report5_4.java new file mode 100644 index 0000000..d7cecc6 --- /dev/null +++ b/src/choyoungjun/report5/Report5_4.java @@ -0,0 +1,4 @@ +package choyoungjun.report5; + +public class Report5_4 { +} diff --git a/src/choyoungjun/report5/Report5_5 b/src/choyoungjun/report5/Report5_5 new file mode 100644 index 0000000..1afb681 --- /dev/null +++ b/src/choyoungjun/report5/Report5_5 @@ -0,0 +1,19 @@ +//6-5. 다음의 코드에 정의된 변수들을 종류별(클래스 변수,인스턴스 변수, 지역변수)로 구분해서 적으세요. +class PlayingCard { + int kind; + int num; + static int width; + static int height; + PlayingCard(int k, int n) { + kind = k; + num = n; + } + public static void main(String args[]) { + PlayingCard card = new PlayingCard(1,1); + } +} + +해설 +cv : width, height +iv : kind, num +lv : k, n, card \ No newline at end of file diff --git a/src/choyoungjun/report5/Report5_6 b/src/choyoungjun/report5/Report5_6 new file mode 100644 index 0000000..42ad7e0 --- /dev/null +++ b/src/choyoungjun/report5/Report5_6 @@ -0,0 +1,23 @@ +//6-7. 다음은 컴퓨터 게임의 병사(marine)를 클래스로 정의한 것이다. +//이 클래스의 멤버 중에 static을 붙여야 하는 것은 어떤 것들이고 그 이유는 무엇인가? +//(단, 모든 병사의 공격력과 방어력은 같아야 한다.) +class Marine { + int x=0, y=0; //Marine의 위치좌표 (x,y) + int hp = 60; //현재 체력 + int weapon = 6; //공격력 + int armor = 0; //방어력 + void weaponUp() { + weapon++; + } + void armorUp() { + armor++; + } + void move(int x, int y) { + this.x = x; + this.y = y; + } +} + +해설 +weapon과 armor에 static을 붙여야 한다. 조건에 모든 병사의 공격력과 방어력은 같아야 한다고 기술되어있기 때문 +상식적으로 생각해도 Marine이 움직여야 하고, hp는 모든 병사마다 다르기 때문에 iv가 되어야 함 \ No newline at end of file diff --git a/src/choyoungjun/report6/Report6_1.md b/src/choyoungjun/report6/Report6_1.md new file mode 100644 index 0000000..c9cacd3 --- /dev/null +++ b/src/choyoungjun/report6/Report6_1.md @@ -0,0 +1,69 @@ +### 6-8. 다음 중 생성자에 대한 설명으로 옳지 않은 것은? (모두 고르시오) +- [ ] 모든 생성자의 이름은 클래스의 이름과 동일해야한다. +- [x] 생성자는 객체를 생성하기 위한 것이다. + - 생성하기 위한 것이 아니라, 생성될 때 호출되는 '초기화 메서드'이다. +- [ ] 클래스에는 생성자가 반드시 하나 이상 있어야 한다. +- [ ] 생성자가 없는 클래스는 컴파일러가 기본 생성자를 추가한다. +- [x] 생성자는 오버로딩 할 수 없다. + - overloading 가능하다. +--- +### 6-9. 다음 중 this에 대한 설명으로 맞지 않은 것은? (모두 고르시오) +- [ ] 객체 자신을 가리키는 참조변수이다. +- [x] 클래스 내에서라면 어디서든 사용할 수 있다. + - 객체 안에서만 사용가능하다. +- [ ] 지역변수와 인스턴스변수를 구별할 때 사용한다. +- [ ] 클래스 메서드 내에서는 사용할 수 없다. +--- +### 6-10. 다음 중 오버로딩이 성립하기 위한 조건이 아닌 것은? (모두 고르시오) +- [ ] 메서드의 이름이 같아야 한다. +- [ ] 매개변수의 개수나 타입이 달라야 한다. +- [x] 리턴타입이 달라야 한다. + - 반환타입은 상관없다. +- [x] 매개변수의 이름이 달라야 한다. + - 매개변수의 이름은 상관없다. +--- +### 6-11. 다음 중 아래의 add메서드를 올바르게 오버로딩 한 것은? (모두 고르시오) +> long add(int a, int b) { return a+b; } + +- [ ] long add(int x, int y) { return x+y; } + - 매개변수의 타입, 개수 같다. +- [x] long add(long a, long b) { return a+b; } +- [x] int add(byte a, byte b) { return a+b; } +- [x] int add(long a, int b) { return (int)(a+b); } +--- +### 6-12. 다음 중 초기화에 대한 설명으로 옳지 않은 것은? (모두 고르시오) +- [ ] 멤버변수는 자동 초기화되므로 초기화하지 않고도 값을 참고할 수 있다. +- [ ] 지역변수는 사용하기 전에 반드시 초기화해야 한다. +- [x] 초기화 블럭보다 생성자가 먼저 수행된다. + - 자 > 명 > 초 > 생 순서로 수행이다. +- [ ] 명시적 초기화를 제일 우선적으로 고려해야 한다. +- [x] 클래스변수보다 인스턴스변수가 먼저 초기화된다 + - cv -> iv 순서로 초기화된다. +--- +### 6-13. 다음 중 인스턴스변수의 초기화 순서가 올바른 것은? +- [x] 기본값-명시적초기화-초기화블럭-생성자 + - 자 > 명 > 초 > 생 +- [ ] 기본값-명시적초기화-생성자-초기화블럭 +- [ ] 기본값-초기화블럭-명시적초기화-생성자 +- [ ] 기본값-초기화블럭-생성자-명시적초기화 +--- +### 6-14. 다음 중 지역변수에 대한 설명으로 옳지 않은 것은? (모두 고르시오) +- [x] 자동 초기화되므로 별도의 초기화가 필요없다. + - 초기화 꼭 해줘야한다. 별도의 초기화가 필요없는것은 iv이다. + - 다만 메서드의 아규먼트로 붙은 lv는 자동초기화된다. +- [ ] 지역변수가 선언된 메서드가 종료되면 지역변수도 함께 소멸된다. +- [ ] 메서드의 매개변수로 선언된 변수도 지역변수이다. +- [ ] 클래스변수나 인스턴스변수보다 메모리 부담이 적다. +- [x] 힙(heap)영역에 생성되며 가비지 컬렉터에 의해 소멸된다. + - 힙에서는 iv를 다룸, lv는 콜스택에 생성된다. +--- +### 6-15. 호출스택이 다음과 같은 상황일 때 옳지 않은 설명은? (모두 고르시오) +![img](img.png) +- [ ] 제일 먼저 호출스택에 저장된 것은 main메서드이다. +- [x] println메서드를 제외한 나머지 메서드들은 모두 종료된 상태이다. + - 아직 실행 중임;; +- [ ] method2메서드를 호출한 것은 main메서드이다. +- [ ] println메서드가 종료되면 method1메서드가 수행을 재개한다. +- [ ] main-method2-method1-println의 순서로 호출되었다. +- [ ] 현재 실행중인 메서드는 println뿐이다. + - 실행은 println, 나머지 메서드는 대기 중이다. \ No newline at end of file diff --git a/src/choyoungjun/report6/img.png b/src/choyoungjun/report6/img.png new file mode 100644 index 0000000..d56f1a2 Binary files /dev/null and b/src/choyoungjun/report6/img.png differ diff --git a/src/choyoungjun/report7/Report7_1.java b/src/choyoungjun/report7/Report7_1.java new file mode 100644 index 0000000..7a0157d --- /dev/null +++ b/src/choyoungjun/report7/Report7_1.java @@ -0,0 +1,27 @@ +package choyoungjun.report7; +//6-17. 다음과 같이 정의된 메서드를 작성하고 테스트하세요. +// 메서드명 : shuffle +// 기능 +// 주어진 배열에 담긴 값의 위치를 바꾸는 작업을 반복하여 뒤섞이게 한다. +// 처리한 배열을 반환한다. +// 반환타입 : int[] +// 매개변수 : int[] arr - 정수값이 담긴 배열 +public class Report7_1 { + public static void main(String[] args) { + int[] original = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + System.out.println(java.util.Arrays.toString(original)); + + int[] result = shuffle(original); + System.out.println(java.util.Arrays.toString(result)); + } + static int[] shuffle(int[] arr){ + for (int i=0; i MIN_VOLUME) volume--; + } + void channelUp() { + if (channel == MAX_CHANNEL) channel = MIN_CHANNEL; + else channel++; + } + void channelDown() { + if (channel == MIN_CHANNEL) channel = MAX_CHANNEL; + else channel--; + } +} +public class Report7_3 { + public static void main(String args[]) { + MyTv t = new MyTv(); + t.channel = 100; + t.volume = 0; + System.out.println("CH:" + t.channel + ", VOL:" + t.volume); + t.channelDown(); + t.volumeDown(); + System.out.println("CH:" + t.channel + ", VOL:" + t.volume); + t.volume = 100; + t.channelUp(); + t.volumeUp(); + System.out.println("CH:" + t.channel + ", VOL:" + t.volume); + } +} + +//예상 결과 : CH: 100, VOL: 0 / CH: 99, VOL: 0 / CH: 100, VOL: 100 \ No newline at end of file diff --git a/src/choyoungjun/report7/Report7_4.java b/src/choyoungjun/report7/Report7_4.java new file mode 100644 index 0000000..a7a1c58 --- /dev/null +++ b/src/choyoungjun/report7/Report7_4.java @@ -0,0 +1,27 @@ +package choyoungjun.report7; + +import java.util.Arrays; + +//6-20. 다음과 같이 정의된 메서드를 작성하고 테스트하세요. +// 메서드명 : max +// 기능 +// 주어진 int형 배열의 값 중에서 제일 큰 값을 반환한다. +// 만일 주어진 배열이 null이거나 크기가 0인 경우, -999999를 반환한다. +// 반환타입 : int +// 매개변수 : int[] arr - 최대값을 구할 배열 +public class Report7_4 { + public static void main(String[] args) { + int[] data = {3,2,9,4,7}; + System.out.println(java.util.Arrays.toString(data)); + System.out.println("최대값 :"+max(data)); + System.out.println("최대값 :"+max(null)); + System.out.println("최대값 :"+max(new int[]{})); // 크기가 0인 배열 } + }//예상 결과 : 최대값: 9 / 최대값: -99999 최대값: -999999 + static int max(int[] arr){ + if (arr==null || arr.length==0) return -999999; + else { + Arrays.sort(arr); + return arr[arr.length-1]; + } + } +} \ No newline at end of file diff --git a/src/choyoungjun/report7/Report7_5.java b/src/choyoungjun/report7/Report7_5.java new file mode 100644 index 0000000..fbcbdbb --- /dev/null +++ b/src/choyoungjun/report7/Report7_5.java @@ -0,0 +1,18 @@ +package choyoungjun.report7; +//6-21. 다음과 같이 정의된 메서드를 작성하고 테스트하시오. +// 메서드명: abs +// 기능 : 주어진 값의 절대값을 반환한다. +// 반환타입 : int +// 매개변수 : int value +public class Report7_5 { + public static void main(String[] args) { + int value = 5; + System.out.println(value + "의 절대값 :" + abs(value)); + value = -10; + System.out.println(value + "의 절대값 :" + abs(value)); + } + static int abs(int value){ // Math.abs 안쓰고 해보자 + return value>=0? value : -value; + } +} +//예상 결과 : 5의 절대값 : 5 / -10의 절대값 : 10 diff --git a/src/choyoungjun/report8/Report8_1.java b/src/choyoungjun/report8/Report8_1.java new file mode 100644 index 0000000..45f4bde --- /dev/null +++ b/src/choyoungjun/report8/Report8_1.java @@ -0,0 +1,80 @@ +package choyoungjun.report8; +//7-1. +// 섯다카드 20장을 포함하는 섯다카드 한 벌(SutdaDeck클래스)을 정의한 것이다. +// 섯다카드 20장을 담는 SutdaCard배열을 초기화하시오. +// 단, 섯다카드는 1부터 10까지의 숫자 가 적힌 카드가 한 쌍씩 있고, 숫자가 1, 3, 8인 경우에는 둘 중의 한 장은 광(Kwang)이 어야 한다. +// 즉, SutdaCard의 인스턴스변수 isKwang의 값이 true이어야 한다. +class SutdaDeck { + final int CARD_NUM = 20; + SutdaCard[] cards = new SutdaCard[CARD_NUM]; + + SutdaDeck() { + for (int i=0; i MAX_VOLUME || volume < MIN_VOLUME) + return; + this.volume = volume; + } + + public int getVolume() { + return volume; + } + + public void setChannel(int channel) { + if (channel > MAX_CHANNEL || channel < MIN_CHANNEL) + return; + + prevChannel = this.channel; + this.channel = channel; + } + + public int getChannel() { + return channel; + } + + public int gotoPrevChannel(){ + setChannel(prevChannel); + return channel; + } +} \ No newline at end of file