Tue/Jan/7 [Java] day6
Tue/Jan/7/2020
[Java]
국비교육 day6
좋은 코드란
⇒ 한번에 하려고 하기 보다는 1번부터 지키려고 하자
예제)
도서 할인률로 할인가 입력하는 ArrayList
⇒ 검색용 method를 모은 class 만들기
ArrayList<String>으로 return 하기
<way 1>
<way 2> new ArrayList에 별도 저장
<way 3> New Class Design ; 불필요한 var 제거
1. ResultClass 만들고
2.변수명 변경
<String's method 총정리>
- 문자열에 관련된 method들 아주많이 쓰이므로 잘 알아두도록!
* 프로젝트 복제 : 어제 한 거 닫고, 오늘은 새로 복제해서 사용
⇒ 가공된 data는 Method Class에 저장하고 - Main Class에서는 method만 호출
* 프로젝트 인원 구성
- team leader
- developer : project 관련 code 찾아놓기
- designer : 화면 design
- db 관리자 : database design
→ db 초기구성을 엉망으로하면 개발자가 힘들어진다
⇒ 초기설계가 중요한 이유!
<기사를 활용한 문제>
< 문제 1> 기사 데이터 중 특정 단어의 노출 횟수를 배열명으로 저장하여 출력
- String split[] = dc.newsList.get(0).news.split(" ");
< 문제 2> 위의 기사 데이터를 모두 역순(space기준)으로 변수저장하여 출력
<Answer>
dataclass에 method 다 정리해보려고 하다가
dc를 main method 밖에 넣어놔서 계속 오류 뜨는 바람에 헤맴..
재도전..
<The End>
[Java]
국비교육 day6
좋은 코드란
- 타인이 보기 좋은 코드 : 주석, 변수명 적극 활용,
- 짧은 코드 : class 분리, method 화
- 빠른 실행 가능한 코드 : memory, local variable 적절한 활용이 중요
- 재사용 가능한 코드 : library, SW architecture 공부를 해야..(design pattern 연구)
⇒ 한번에 하려고 하기 보다는 1번부터 지키려고 하자
예제)
도서 할인률로 할인가 입력하는 ArrayList
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class BookClass {
String title = "", author = "", press = "", picture = "";
int price = 0, dc_price = 0, dc = 0;
public BookClass() {
}
public BookClass(String t, String a, String p, int pr, String pic, int d) {
this.title = t;
this.author = a;
this.press = p;
this.price = pr;
this.picture = pic;
this.dc = d;
this.dc_price = this.makeDcPrice(d);
}
// makeDcPrice method
// 3가지 방법으로 만들 수 있음 : 값을 받지 않는 method, 값을 받는 method, 값 받고 return하는 method
private int makeDcPrice(int dp) { // 이 method는 이 class안에서만 사용하면 되기 때문에 private으로
double d = dp * 0.01;
return (int) (this.price * (1 - d));
}
}
| cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
DataClass dc = new DataClass();
// data 연산확인 코드
for (int i = 0; i < dc.arrBook.size(); i++) {
System.out.println("No " + (i + 1) + ".\t" + dc.arrBook.get(i).title);
System.out.println("Original Price \t:\t" + dc.arrBook.get(i).price + "원");
System.out.println("Discount Rate \t:\t" + dc.arrBook.get(i).dc + "%");
System.out.println("Sales Price \t:\t" + dc.arrBook.get(i).dc_price + "원\n");
}
System.out.println("\n-----------------\n");
// 책 제목 검색
String searchTitle = "HTML";
System.out.println("Books containing " + searchTitle + " are :\n");
for (int i = 0; i < dc.arrBook.size(); i++) {
// 책제목 : dc.arrBook.get(i).title
// 포함하는지? : String의 contains method →
// dc.arrBook.get(i).title.contains("")
if (dc.arrBook.get(i).title.contains(searchTitle)) {
System.out.println(dc.arrBook.get(i).title);
}
}
System.out.println("\n-----------------\n");
// 저자 검색 + 책제목 출력
String searchAuthor = "제프";
System.out.println("The author " + searchAuthor + "'s books are :\n");
for (int i = 0; i < dc.arrBook.size(); i++) {
if (dc.arrBook.get(i).author.contains(searchAuthor)) {
System.out.println(dc.arrBook.get(i).title);
}
}
System.out.println("\n-----------------\n");
// 출판사 검색 + 저자&제목 출력
String searchPress = "한빛";
System.out.println("The Press " + searchPress + "'s books and authors are \n");
for (int i = 0; i < dc.arrBook.size(); i++) {
if (dc.arrBook.get(i).press.contains(searchPress)) {
System.out.println(dc.arrBook.get(i).author + "\t\t\t" + dc.arrBook.get(i).title);
}
}
| cs |
⇒ 검색용 method를 모은 class 만들기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public class MethodClass {
public MethodClass() {
}
public void dataTest(ArrayList<BookClass> arrBook) {
// data 연산확인 코드
for (int i = 0; i < arrBook.size(); i++) {
System.out.println("No " + (i + 1) + ".\t" + arrBook.get(i).title);
System.out.println("Original Price \t:\t" + arrBook.get(i).price + "원");
System.out.println("Discount Rate \t:\t" + arrBook.get(i).dc + "%");
System.out.println("Sales Price \t:\t" + arrBook.get(i).dc_price + "원\n");
}
}
public void searchTitle(ArrayList<BookClass> arrBook, String keyword) {
System.out.println("Books containing " + keyword + " are :\n");
for (int i = 0; i < arrBook.size(); i++) {
if (arrBook.get(i).title.contains(keyword)) {
System.out.println(arrBook.get(i).title);
}
}
}
public void authorSearch(ArrayList<BookClass> arrBook, String key) {
System.out.println("The author " + key + "'s books are :\n");
for (int i = 0; i < arrBook.size(); i++) {
if (arrBook.get(i).author.contains(key)) {
System.out.println(arrBook.get(i).title);
}
}
}
public void pressSearch(ArrayList<BookClass> arrBook, String key) {
System.out.println("The Press " + key + "'s books and authors are \n");
for (int i = 0; i < arrBook.size(); i++) {
if (arrBook.get(i).press.contains(key)) {
System.out.println(arrBook.get(i).author + "\n" + arrBook.get(i).title + "\n");
}
}
}
}
| cs |
1
2
3
4
5
6
7
8
|
//1
private DataClass dc = new DataClass();
ArrayList<BookClass> books = dc.arrBook;
//2
private ArrayList<BookClass> books = new DataClass().arrBook;
// 1과 2는 동일한 코드, 상황에 따라 다양하게 변수설정할 수 있다
| cs |
ArrayList<String>으로 return 하기
<way 1>
1
2
3
4
5
6
7
8
9
10
11
|
public ArrayList<String> pressSearch(String key) {
ArrayList<String> r= new ArrayList<String>();
for (int i = 0; i < books.size(); i++) {
if (books.get(i).press.contains(key)) {
r.add(books.get(i).title + "\t" + books.get(i).author);
}
}
return r;
}
| cs |
1
2
3
4
5
6
7
8
|
String press = "제이";
ArrayList<String> result = mc.pressSearch(press);
System.out.println("The books of press "+ press +" are : \n");
for(int i=0; i<result.size(); i++){
String[] s = result.get(i).split("\t");
System.out.println("title \t: " + s[0]);
System.out.println("author \t: " + s[1] + "\n");
}
| cs |
<way 2> new ArrayList에 별도 저장
1
2
3
4
5
6
7
|
public ArrayList<BookClass> pressSearch(String key) {
ArrayList<BookClass> r = new ArrayList<BookClass>();
for (int i = 0; i < r.size(); i++) {
r.add(new BookClass(books.get(i).title, books.get(i).author));
}
return r;
}
| cs |
<way 3> New Class Design ; 불필요한 var 제거
1. ResultClass 만들고
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class ResultClass {
String title, author;
public ResultClass() {
}
public ResultClass(String t, String a) {
this.title = t;
this.author = a;
}
}
| cs |
2.변수명 변경
1
2
3
4
5
6
7
8
9
10
11
|
public ArrayList<ResultClass> pressSearch(String key) {
ArrayList<ResultClass> r = new ArrayList<ResultClass>();
for (int i = 0; i < books.size(); i++) {
if(books.get(i).press.contains(key)){
r.add(new ResultClass(books.get(i).title, books.get(i).author));
}
}
return r;
}
| cs |
<String's method 총정리>
- 문자열에 관련된 method들 아주많이 쓰이므로 잘 알아두도록!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
String str1 = "Java";
System.out.println(str1.toLowerCase()); // java
System.out.println(str1.toUpperCase()); //JAVA
System.out.println(str1.indexOf('v')); // 2
System.out.println(str1.charAt(2)); // v
System.out.println(str1.startsWith("J")); // true
System.out.println(str1.endsWith("a")); // true
System.out.println(str1.contains("a")); // true
String str2 = " Ja va";
System.out.println(str2.trim()); // Ja va
String s[] = str2.split(" ");
System.out.println(s.length); // 3
if(str1.equals(str2)){
System.out.println(true);
} else System.out.println(false); // false
System.out.println(str2.substring(2)); // a va
System.out.println(str2.substring(1, 4)); // Ja
String str3;
// System.out.println(str3.isEmpty()); // method 사용 불가
String str4 = "";
System.out.println(str4.isEmpty()); // true
String str5 = null;
// System.out.println(str5.isEmpty()); // 'NullPointerException'
| cs |
* 프로젝트 복제 : 어제 한 거 닫고, 오늘은 새로 복제해서 사용
⇒ 가공된 data는 Method Class에 저장하고 - Main Class에서는 method만 호출
* 프로젝트 인원 구성
- team leader
- developer : project 관련 code 찾아놓기
- designer : 화면 design
- db 관리자 : database design
→ db 초기구성을 엉망으로하면 개발자가 힘들어진다
⇒ 초기설계가 중요한 이유!
<기사를 활용한 문제>
< 문제 1> 기사 데이터 중 특정 단어의 노출 횟수를 배열명으로 저장하여 출력
- String split[] = dc.newsList.get(0).news.split(" ");
< 문제 2> 위의 기사 데이터를 모두 역순(space기준)으로 변수저장하여 출력
<Answer>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public class MainClass {
public static void main(String[] args) {
DataClass dc = new DataClass();
String[] s = dc.newsList.get(0).news.split(" ");
String key1 = "";
String key2 = "";
int[] resultNum = { 0, 0 };
for (int i = 0; i < s.length; i++) {
if (s[i].contains(key1)) {
resultNum[0]++;
}
}
for (int i = 0; i < s.length; i++) {
if (s[i].contains(key2)) {
resultNum[1]++;
}
}
System.out.println(key1 + "\t\t: " + resultNum[0]);
System.out.println(key2 + "\t: " + resultNum[1]);
String reverse[] = new String[s.length];
int index = s.length - 1;
for (int i = 0; i < s.length; i++) {
reverse[i] = s[index];
index--;
}
for (int i = 0; i < reverse.length; i++) {
System.out.println("no." + i + " is " + reverse[i]);
}
}
}
| cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class NewsClass {
String news = "";
public NewsClass() {
}
public NewsClass(String n) {
this.news = n;
}
}
| cs |
1
2
3
4
5
6
7
8
9
10
|
public class DataClass {
ArrayList<NewsClass> newsList = new ArrayList<NewsClass>();
public DataClass() {
String str = "";
newsList.add(new NewsClass(str));
}
}
| cs |
dataclass에 method 다 정리해보려고 하다가
dc를 main method 밖에 넣어놔서 계속 오류 뜨는 바람에 헤맴..
재도전..
1
2
3
4
5
6
7
8
9
10
11
|
public class MainClass {
public static void main(String[] args) {
DataClass dc = new DataClass();
System.out.println(dc.wordsNumber(dc.split())[0]);
System.out.println(dc.wordsNumber(dc.split())[1]);
}
}
| cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class NewsClass {
String news = "";
public NewsClass() {
}
public NewsClass(String n) {
this.news = n;
}
}
| cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package com.wjh.string;
import java.util.ArrayList;
public class DataClass {
ArrayList<NewsClass> newsList = new ArrayList<NewsClass>();
public DataClass() {
String str = "";
newsList.add(new NewsClass(str));
}
public String[] split() {
String[] x = newsList.get(0).news.split(" ");
return x;
}
public int[] wordsNumber(String[] s) {
int[] resultNum = { 0, 0 };
for (int i = 0; i < s.length; i++) {
if (s[i].contains("HTML5")) {
resultNum[0]++;
}
}
for (int i = 0; i < s.length; i++) {
if (s[i].contains("하이브리드앱")) {
resultNum[1]++;
}
}
return resultNum;
}
public String[] reNews(String[] s) {
String reverse[] = new String[s.length];
int index = s.length - 1;
for (int i = 0; i < s.length; i++) {
reverse[i] = s[index];
}
return reverse;
}
}
| cs |
<The End>
댓글
댓글 쓰기