Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved: Practice 3~4 #15

Merged
merged 14 commits into from
Jul 12, 2023
15 changes: 15 additions & 0 deletions src/main/java/Practice/stopmin/Coffeetype.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package Practice.stopmin;

public enum Coffeetype {
AMERICANO,
ICE_AMERICANO,
CAFE_LATTE;
public static void main(String[] args) {
System.out.println(AMERICANO);
for (Coffeetype type : Coffeetype.values()) {
System.out.println(type);
}
}
};


14 changes: 14 additions & 0 deletions src/main/java/Practice/stopmin/FinalPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package Practice.stopmin;

import java.util.ArrayList;

public class FinalPractice {

public static void main(String[] args) {
final int n = 10;

final ArrayList<String> a = new ArrayList<>();

a.add("hello~");
}
}
13 changes: 13 additions & 0 deletions src/main/java/Practice/stopmin/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package Practice.stopmin;

public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello World!"); // 표준 출력
System.err.println("Error!"); // 표준 에러 출력
System.out.println("숫자" + 3 + "~~~");

Sample sample = new Sample("EE");
sample.myPublic();
}
}
21 changes: 21 additions & 0 deletions src/main/java/Practice/stopmin/ListPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Practice.stopmin;

import java.util.ArrayList;
import java.util.Comparator;

public class ListPractice {

public static void main(String[] args) {
ArrayList pitches = new ArrayList();
pitches.add("123");
pitches.add("312");
pitches.add("213");
System.out.println(pitches);

System.out.println("pitches.get(1) = " + pitches.get(1));
System.out.println("pitches.size() = " + pitches.size());

pitches.sort(Comparator.naturalOrder());
System.out.println(pitches);
}
}
15 changes: 15 additions & 0 deletions src/main/java/Practice/stopmin/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package Practice.stopmin;

public class NumberPractice {

public static void main(String[] args) {
int age = 21;
long count = 102938901283012093L;

age += 10;

boolean isTest = true; // is~, has~, can~

}

}
31 changes: 31 additions & 0 deletions src/main/java/Practice/stopmin/Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Practice.stopmin;

public class Sample {


private String message; // 속성, 필드

public Sample(String message) {
this.message = message;
}

public static void main(String[] args) {
Sample sample = new Sample("ABDERERER");
}

public void myPublic() {
System.out.println(message);
}

protected void myP() {

}

private void myP2() {

}

void myD() {

}
}
26 changes: 26 additions & 0 deletions src/main/java/Practice/stopmin/StringPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package Practice.stopmin;

public class StringPractice {

public static void main(String[] args) {
String a = "Happy"; // 리터럴 방식
String b = new String("Java"); // 객체 생성

System.out.println("a.eqauls(b) = " + a.equals(b));

boolean c = true; // 프리미티브 -> Null을 담을 수 없다. 대신 string은 안된다!!
Boolean e = true; // 레퍼클래스

e = null;

int f = 10;
Integer g = 10;

g = null;

a.equals(b);

// indexOf
// contains
}
}
4 changes: 3 additions & 1 deletion src/main/java/Problem/stopmin/Chapter3/Q01.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public static void main(String[] args) {
int english = 75;
int math = 55;

// TODO: 평균 점수를 구해서 출력하기
// TODO: 평균 점수를 구해서 출력하기
System.out.println((korean + english + math) / 3);

}
}
14 changes: 13 additions & 1 deletion src/main/java/Problem/stopmin/Chapter3/Q02.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ public class Q02 {
/**
* 홀수 짝수 판별
*/
public static boolean isOdd(int a) {
if (a % 2 == 1) {
return true;
} else {
return false;
}
}

public static void main(String[] args) {
int num = 13;

// TODO: num이 홀수라면 odd를, 짝수라면 even을 출력하기
if (isOdd(num)) {
System.out.println("Odd");
} else {
System.out.println("even");
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/Problem/stopmin/Chapter3/Q03.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public static void main(String[] args) {
String personNumber = "881120-1068234";

// TODO: 주민등록번호를 연월일(YYYYMMDD) 부분과 그 뒤의 숫자 부분으로 나누어 출력하기
System.out.println(personNumber.substring(0, 6));
System.out.println(personNumber.substring(7, 14));
}
}
2 changes: 2 additions & 0 deletions src/main/java/Problem/stopmin/Chapter3/Q05.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public static void main(String[] args) {
String a = "a:b:c:d";

// TODO: replace 함수를 사용하여 a#b#c#d로 바꿔서 출력해 보자.
a = a.replaceAll(":", "#");
System.out.println(a);
}
}
4 changes: 4 additions & 0 deletions src/main/java/Problem/stopmin/Chapter3/Q06.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class Q06 {

Expand All @@ -13,5 +15,7 @@ public static void main(String[] args) {
System.out.println(myList); // [1, 3, 5, 4, 2]가 출력됨.

// TODO: 리스트를 [5, 4, 3, 2, 1]로 만들어서 출력하기
Collections.sort(myList, Collections.reverseOrder());
System.out.println(myList);
}
}
2 changes: 2 additions & 0 deletions src/main/java/Problem/stopmin/Chapter3/Q07.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public static void main(String[] args) {
System.out.println(myList); // [Life, is, too, short] 출력됨.

// TODO: 리스트를 "Life is too short" 문자열로 만들어 출력해 보자.
String string = String.join(" ", myList);
System.out.println(string);
}
}
3 changes: 3 additions & 0 deletions src/main/java/Problem/stopmin/Chapter3/Q08.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Problem.stopmin.Chapter3;

import java.util.HashMap;
import java.util.Map;

public class Q08 {

Expand All @@ -14,5 +15,7 @@ public static void main(String[] args) {
grade.put("C", 70);

// TODO: 다음의 맵 grade에서 "B'에 해당되는 값을 추출해 보자. (추출하고 나면 grade에는 "B"에 해당하는 아이템이 사라져야 한다.)
grade.remove("B");
System.out.println(grade);
}
}
7 changes: 7 additions & 0 deletions src/main/java/Problem/stopmin/Chapter3/Q09.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.TreeSet;

public class Q09 {

Expand All @@ -13,5 +14,11 @@ public static void main(String[] args) {
System.out.println(numbers); // [1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5] 출력됨.

// TODO: numbers 리스트에서 중복 숫자를 제거해 보자.
TreeSet<Integer> treeSet = new TreeSet<>();
for (Integer item : numbers) {
treeSet.add(item);
}

System.out.println(treeSet);
}
}
9 changes: 7 additions & 2 deletions src/main/java/Problem/stopmin/Chapter3/Q10.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ static void printCoffeePrice(int type) {
priceMap.put(1, 3000); // 1: 아메리카노
priceMap.put(2, 4000); // 2: 아이스 아메리카노
priceMap.put(3, 5000); // 3: 카페라떼
int price = priceMap.get(type);
System.out.println(String.format("가격은 %d원 입니다.", price));
Integer price = priceMap.get(type);

if (price != null) {
System.out.println(String.format("가격은 %d원 입니다.", price));
} else {
System.out.println("해당하는 커피 종류가 없습니다.");
}
}

public static void main(String[] args) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/Problem/stopmin/Chapter4/Q02.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,16 @@ public class Q02 {
*/
public static void main(String[] args) {
// TODO: while 문을 사용해서 1부터 1000까지의 자연수 중 3의 배수의 합을 구하기
int num = 1;
int sum = 0;

while (num < 1000) {
if (num % 3 == 0) {
sum += num;
}
num++;
}

System.out.println(sum);
}
}
8 changes: 8 additions & 0 deletions src/main/java/Problem/stopmin/Chapter4/Q03.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@ public static void main(String[] args) {
//***
//****
//*****
int num = 1;
while (num <= 5) {
for (int i = 0; i < num; i++) {
System.out.print("*");
}
System.out.print("\n");
num++;
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/Problem/stopmin/Chapter4/Q04.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ public class Q04 {
*/
public static void main(String[] args) {
// TODO: for문을 사용해 1부터 100까지의 숫자를 출력해 보자.
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/Problem/stopmin/Chapter4/Q05.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ public static void main(String[] args) {
int[] marks = {70, 60, 55, 75, 95, 90, 80, 80, 85, 100};

// TODO: for each 문을 사용하여 A 학급의 평균 점수를 구해 보자.
int average = 0;

for (int entry : marks) {
average += entry;
}
System.out.println(average / marks.length);
}
}
1 change: 1 addition & 0 deletions src/main/java/Save/Chapter3/Q01.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public static void main(String[] args) {
int math = 55;

// TODO: 평균 점수를 구해서 출력하기
System.out.println((korean + english + math) / 3);
}
}
1 change: 1 addition & 0 deletions src/main/java/Save/Chapter3/Q03.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static void main(String[] args) {
String personNumber = "881120-1068234";

// TODO: 주민등록번호를 연월일(YYYYMMDD) 부분과 그 뒤의 숫자 부분으로 나누어 출력하기
System.out.println(personNumber.substring(0, 6) + "\n" + personNumber.substring(8, 14));
System.out.println(personNumber.substring(0, 5));
System.out.println(personNumber.substring(7, 13));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 코드 패치에는 주민등록번호를 처리하는 부분이 포함되어 있습니다. 리뷰 결과와 개선 제안을 아래에서 안내합니다:

  1. substring()을 사용하여 주민등록번호를 원하는 형식으로 분리하는 점은 효과적입니다.

  2. 그러나 코드 패치에는 오류가 있습니다. personNumber.substring(8, 14) 라인에서 인덱스 범위가 잘못되었습니다. 주민등록번호의 뒷자리 숫자는 인덱스 6부터 시작하는데, 해당 값을 출력하기 위해 personNumber.substring(8, 14) 대신에 personNumber.substring(7)을 사용해야 합니다.

개선된 코드 프래그먼트는 다음과 같습니다:

System.out.println(personNumber.substring(0, 6) + "\n" + personNumber.substring(7));
System.out.println(personNumber.substring(0, 5));
System.out.println(personNumber.substring(7, 13));
  1. 추가 개선 사항은 코드에 존재하지 않습니다. 주어진 작업을 수행하기에 충분히 간단하고 명확한 방법을 사용하고 있습니다.

이제 주어진 주민등록번호를 연월일과 뒷자리 숫자로 올바르게 나눈 후 출력할 수 있습니다.

Expand Down