-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourse - Chapter 2 .txt
310 lines (169 loc) · 8.51 KB
/
Course - Chapter 2 .txt
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
The Basic Java Application
프로그래밍 언어의 의미는 해당 언어로 작성된 프로그램의 의미를 결정하는 집합입니다.
의미상 올바른 프로그램은 원하는 것을 수행하는 프로그램입니다.
Syntax = 문장이 언어의 문법 Grammer 에 따라 유효성을 체크
Semantics = 문장이 타당한 의미를 지니는지 판별
서브루틴 = 이름이 부여된 프로그램 명령 세트
public static void main(String []args){
System.out.println("1234"); 이것도 서브루틴
}
System Class안에 out이라는 객체한테 println 이라는 메서드 실행
이런 것을 built - in SubRoutine (내장서브루틴)이라 함
Variables and the Primitive Types
identifiers = 클래스 , 변수 , 서브루틴의 이름을 지정하는데 사용 함
int number = 5;
number - indentifier
reserved words = 예약어
Class , Public , Static , If , Else , While
등 이미 용도를 규정지어놓은 예약어 존재함
Literals = 변수 그 자체가 가진 값 And 변경 가능
Constance = 상수 = 변수 그 자체가 가진 값 But 변경 불가능
String str , String str2
str==str2 false 지만
str="abc" str2"abc" (Literal 상수 값)
str= str2 하면 true가 나온다 .
그냥 값 정의해 놓으면 그게 Literal 임
int a = 5 Literal
boolean flag= true , flas ( Literal )
Expression = 6 + 9 + 8 상태변화 x
Statement = int num = 6+9+8 상태변화 O
int num; (변수 선언 , 메모리에만 올려놧다)
int num = 5; ( 변수 정의 , 값까지 넣어놈)
SubRoutine 안에있는 Variable (변수)를 Local Vriable 이라 함
Strings, Classes, Objects, and Subroutines
SubRoutine 는 Class나 Object 에 귀속됨
String 의 경우에는 내장함수를 가짐
즉 Built - in SubRoutine 를 가짐
우리는 이것이 어떻게 구현됐는지 몰라도 갖다가 쓸 수 있음
str.substr() , str.erase() ...
이거 SubRountine 의 핵심임 , 내부에 구현체를 모르지만 쓸수있는 , 즉 "Black Box"라 함
클래스에 일부인 서브루틴 = Static Member , 정적 맴버라 함
public static void main() 메서드는 클래스의 Static Member임
모든 서브루틴은 특정 작업을 수행하지만 일부 서브루틴의 경우 작업은
일부 데이터 값을 계산하거나 검색하는 것입니다.
이런걸 = Function = Method 라고 함
정적맴버는 정적변수 , 정적메서드 모든 걸 포함
즉 Static 이다 -> 클래스의 일부다 라고 생각해
그러니 객체 안만들어도 클래스.메서드명 or 클래스.변수명 하면 찍어낼수 있어
클래스의 일부니까
클래스는 정적 변수 + 서브루틴의 컨테이너
클래스를 만든다 = 타입을 정의한다 라고 보면됨
System. = 클래스
out = static 변수 (PrintStream Type)
println = out의 타입 (PrintStream)이 갖고있는 서브루틴
Enum Class
열거형값은 항상 상수임 , 열거형 상수라 부름
따로 Class 안만들어도 enum Day{Monday,Tusday} 식으로 만들기 가능
또 출력 값은 항상 열거형 상수의 이름임
Text Input and Output
Text Input and Output
Basic Outpur , Formatted Output
System.out.println(a); // 기본 출력
System.our.printf("%d",num) // 포멧 출력
printf 함수 = 출력 형식을 지정하는 문자열 = 매개변수 형식 문자열
입력을 받는 도구
Scanner sc = new Scanner(System.in);
Details of Expressions
Expression = 문
상태변화가 없는 것
6+9+8 문
int num = 6+9+8 statement
37.4 + 10 -> 실수형과 정수형의 결합임
따라서 이련 경우에 연산 수행을 위해서 Type Conversion 을 해줘야 함 (타입 변환)
자바에서는 자동으로 형변환을 해주기도 함 (묵시적 형 변환 , Auto Casting)
더 큰 타입으로는 자동으로 형변환을 지원한다
Widening Casting
byte -> short -> char -> int -> long -> float -> double
이건 Auto Casting
Narrowing Casting
더 작은 형으로 변환할 경우 Type Casting 을 해줘야 한다 .
이를 Narrowing Casting 라고 함
double num = 123.0;
int num2 = (int)num;
num2 = 123;
1번 문제
Briefly explain what is meant by the syntax and the semantics of a programming language.
Give an example to illustrate the difference between a syntax error and a semantics error.
Syntax란 프로그램 언어 문법상의 오류를 말합니다.
Symantics는 프로그램 언어 문법상으로는 오류가 없지만
효용성이 없는 것을 의미합니다.
while(true){
} // 무한 루프 문법상 에러 없지만 효용성이 없음
2번 문제
What does the computer do when it executes a variable declaration statement. Give an example.
변수를 특정한 메모리번지에 올리고 값을 할당합니다.
3번 문제
What is a type, as this term relates to programming?
타입이란 특정한 목적을 가진 연산들의 집합을 의미합니다.
4번 문제
One of the primitive types in Java is boolean. What is the boolean type?
Where are boolean values used? What are its possible values?
Boolean은 논리 자료형으로 특정한 값이나 연산이 참인지 거짓인지를 판별합니다.
true , false 두 개의 값만 가집니다.
5번 문제
Give the meaning of each of the following Java operators:
a) ++
증감연산 +1
b) &&
논리연산 And
c) !=
논리연산 같지않으면
6번 문제
Explain what is meant by an assignment statement, and give an example.
What are assignment statements used for?
statement란 상태변화가 있는 식을 의미합니다.
예를들어 int num = 6+9+8;
는 num의 상태가 변화하는 하나의 "식"으로 statement라고 할 수 있습니다.
7번 문제
What is meant by precedence of operators?
(논리연산 , 산술연산 , 대입연산 , 증감연산)
연산자 우선순위란 논리연산 , 증감연산 등등 다양한 연산 들이 존재할떄
어느 연산을 먼저 실행할지를 나타냅니다.
단적으로 = 연산자는 가장 우선순위가 낮습니다.
따라서 int num = num + 1 ;
이라는 식을 실행시킬때 + 산술연산자가 먼저 실행되고 = 대입연산자 가 나중에 실행됩니다.
8번 문제
What is a literal?
literal이란 값 그 자체를 의미합니다.
Constant(상수)와는 다른 개념으로
값의 재정의또한 가능하며 , 변수가 가진 값 그 자체를 의미합니다.
9번 문제
In Java, classes have two fundamentally different purposes. What are they?
1. type 정의
2. 연산 및 필드변수
10번 문제
What is the difference between the statement "x = TextIO.getDouble();"
and the statement "x = TextIO.getlnDouble();"
123"개행문자"
getln 한줄읽기
get 개행문자 전까지 한줄 읽기
11번 문제
Explain why the value of the expression 2 + 3 + "test" is the string "5test"
while the value of the expression "test" + 2 + 3 is the string "test23".
What is the value of "test" + 2 * 3 ?
앞에있는 2+3은 int형 변수이며
뒤에있는 2+3은 String 형 입니다.
"test" + 2 * 3 는 text6이 됩니다.
12번 문제
Integrated Development Environments such as Eclipse often use syntax coloring,
which assigns various colors to the characters in a program to reflect the syntax of the language.
A student notices that Eclipse colors the word String differently from int, double, and boolean.
The student asks why String should be a different color, since all these words are names of types.
What's the answer to the student's question?
String은 double , boolean 과 다르게 , primitive type이 아닌 , reference type입니다.
13번 문제
What is the purpose of an import directive, such as import textio.TextIO or import java.util.Scanner?
textio.TextIO 라는 Class를 사용하겠다는 의미입니다.
java.util.Scanner 또한
java util안에 있는 Scanner Class를 사용하겠다는 의미입니다.
14번 문제
Write a complete program that asks the user to enter the number of "widgets"
they want to buy and the cost per widget. The program should then output the total cost for all the widgets.
Use System.out.printf to print the cost, with two digits after the decimal point.
You do not need to include any comments in the program.
widgets = in
widgets Cost = in
double result=0;
for(int i=0; i<widgets.size();i++)
result+=widgets[i];
printf("%2f",result);