KU COSE102 2019중간고사 복습

3 minute read

되냐 되냐?


Scanner 클래스

JAVA 04

콘솔에 값을 입력하는 클래스

클래스 import

import java.util.Scanner;

Scanner 생성

Scanner sc = new Scanner(System.in);

값 입력

int a = sc.nextInt();
double b = sc.nextDouble();
String c = sc.next();
String d = sc.nextLine();
//next와 nextLine의 차이는?

identifier

JAVA 02

다음 중 identifier로서 사용할 수 없는 것은?

miles, Test, a++, –a, 4#R, $4, #44, apps, class, public, int, x, y, radius

정답
  사용가능 : miles, Test, x, y, radius
  
  불가능 : a++, --a, 4#R, #44, class, public, int
  

data type

JAVA 02

Name Range Storage Size
byte $-2^7$ to $2^7-1$ 8-bit signed
short $-2^{15}$ to $2^{15}-1$ 16-bit signed
int $-2^{31}$ to $2^{31}-1$ 32-bit signed
long $-2^{63}$ to $2^{63}-1$ 64-bit signed
float $-3.4028235E+38$ to $-1.4E-45$ and $1.4E-45$ to $3.4028235E$ 32-bit IEEE 754
double $-1.7976931348623157E+308$ to $-4.9E-324$ and $4.9E-324$ to $1.7976931348623157E+308$ 64-bit IEEE 754

IEEE 754 : Institute of Electrical and Eletronics Engineers가 만든 부동 소수점 기준


arithmetic operator

JAVA 04

add subtract multiply divise remainder


Math.pow(a,b)

  • Math 클래스에 정의된 pow 메소드
  • double형 변환

다음 출력을 작성하시오

System.out.println(Math.pow(2, 3));
System.out.println(Math.pow(4, 0.5));
System.out.println(Math.pow(2.5, 2));
정답
  8.0
  2.0
  6.25
  

current time

System.currentTimeMillis()

UNIX epoch 1970년 1월 1일 0시 0분 0초 GMT로부터 현재까지 소요된 시간을 밀리초로 나타낸 숫자를 리턴하는 함수


type casting

JAVA 03

다음 출력을 작성하시오

System.out.println((int)1.7);
System.out.println((double)1 / 2);
System.out.println(1 / 2);

int sum = 0;
sum += 4.5;
System.out.println(sum);
sum += 4.5;
System.out.println(sum);

char ch = (char)0XAB0041;
System.out.println(ch);
ch = (char)65.25;
System.out.println(ch);
정답
  1
  0.5
  0
  4
  8
  A
  A
  

boolean

boolean 데이터 타입은 true또는 false를 리턴함


Math.random()

JAVA 20

Math.random()0.0 이상 1.0 미만의 double 값을 리턴함

이를 활용하여 정수형을 리턴하게 할 수 있음

(int)(Math.random()*10)

을 하면 얼마부터 얼마까지 반환할까요?

정답
  0~9
  

if-else

JAVA 05

굳이 따로 설명할 게 없음

if-else if문 안에는 무조건 조건식만 들어갈 수 있음

C와는 다르게

int c=1;
if(c)

이렇게 쓰면 에러 발생

int c = 1;
if(c == 1)

이런거는 에러 발생하지 않음


logical operator

JAVA 04

논리 연산자 참고


switch-case

JAVA 05

주의할 점

case문에는 문자, 정수, 문자열 리터럴만 넣을 수 있음


ASCII & Unicode

ASCII : 0000 ~ 007F / 1바이트 차지함 / ASCII Code (아스키 코드)

Unicode : 0000 ~ FFFF / 2바이트 차지함


Math class

JAVA 20

Math 클래스에는 수학에 관련된 메소드들이 들어있음.

삼각함수

sin(radians), cos(radians), tan(radians), toRadians(degree), toDegree(radians), asin(a), acos(a), atan(a)

지수로그

exp(x), log(x), log10(x), pow(a, b), sqrt(x)

올림내림반올림

ceil(x), floor(x), rint(x), round(x)

rint(x) : 가장 가까운 정수로 올림. 가까운 값이 2개인 경우, 짝수 값으로 리턴. double형 반환

round(x) : x가 float인 경우, (int)Math.floor(x+0.5)를 반환. x가 double인 경우, (long)Math.floor(x+0.5)를 반환.

기타

max(a, b), min(a, b), abs(a), random()


String class

JAVA 20

기본 메소드

length(), charAt(index), concat(s1), toUpperCase(), toLowerCase(), trim()

비교 메소드

equals(s1), equalsIgnoreCase(s1), compareTo(s1), compareToIgnoreCase(s1), startsWith(prefix), endsWith(suffix), contains(s1)

뒤에 IgnoreCase가 붙은거는 it is case insensitive 한다고 함.

부분 문자열

substring(beginIndex), substring(beginIndex, endIndex)

검색 메소드

index(ch), indexOf(ch, fromIndex), indexOf(s), indexOf(s, fromIndex)

똑같이 앞에 last붙은 메소드들이 있음

없으면 -1


printf

C언어 printf 포맷

C에서 하듯 하면 됨


for, while, do-while

JAVA 06

실습1 : GCD를 구하는 코드를 작성하시오

실습2 : 10진수에서 16진수로 변환하는 코드를 작성하시오.


for-each

JAVA 07

for(변수:배리굿나이스) {
  작업문
}

break & continue

JAVA 06

break를 쓰는 곳이 반복문 말고 어디서 또 쓰죠? 정답


method

JAVA 11

Methods can be used to define reusable code and organize and simplify coding.

A method definition consists of its method name, parameters, return value type, and body.

Calling a method executes the code in the method.

A void method does not return a value.

The arguments are passed by value to parameters when invoking a method.

실습3 : 16진수에서 10진수로 변환하는 메소드를 작성하시오.


method overloading

JAVA 11

Overloading methods enables you to define the methods with the same name as long as their signatures are different.

이런게 메소드 오버로딩이다.

public static int max(int num1, int num2) {
  if (num1 > num2)
    return num1;
  else
    return num2;
}

public static double max(double num1, double num2) {
  if (num1 > num2)
    return num1;
  else
    return num2;
}

public static double max(double num1, double num2, double num3) {
  return max(max(num1, num2), num3);
}

Categories: ,

Updated:

Comments