18-2: JAVA BigInteger, token, Random, overflow

jk·2024년 1월 24일
0

kdt 풀스택

목록 보기
34/127



1. BigInteger 클래스에 대하여 설명하시오.

  • class to deal bigger integer than limitations of primitive integers.
  • String is allowed to initialize the integer as parameter.



2.아래의 문자열을 ":" 구분자로 하여, PM 08 45 를 차례로 출력하는 프로그램을 작성 하시오.

"PM:08:45"
//
//code
//
import java.util.*;
class Const {
    static final String STR_INPUT = "PM:08:45";
    static final String DELIMITER = ":";
}
class Print {
    private static StringBuilder print = new StringBuilder();
    private static void printAndReset() {
        System.out.print(print);
        print.setLength(0);
    }
    static void stringAndSpace(String str) {
        print.append(str);
        print.append("\s");
        printAndReset();
    }
}
class Functions {
    static void removeColons() {
        try {
            StringTokenizer stringTokenizer = new StringTokenizer(Const.STR_INPUT, Const.DELIMITER);
            while (stringTokenizer.hasMoreTokens()) {
                Print.stringAndSpace(stringTokenizer.nextToken());
            };
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
        };
    }
}
class RemoveColons {
    public static void main(String[] args) {
        Functions.removeColons();
    }
}
//
//print
//
PM 08 45 



3.Random rand = new Random(); 과 Random rand2 = new Random(1); 차이를 설명하시오.

  • new Random(): Random seed results.
  • new Random(1): The only result from seed "1".



4 아래를 @로 구분하시오.

String str = "inpa@tistory@com@super@power";
//
//code
//
import java.util.*;
class Const {
    static final String STR_INPUT = "inpa@tistory@com@super@power";
    static final String DELIMITER = "@";
}
class Print {
    private static StringBuilder print = new StringBuilder();
    private static void printAndReset() {
        System.out.print(print);
        print.setLength(0);
    }
    static void stringAndSpace(String str) {
        print.append(str);
        print.append("\s");
        printAndReset();
    }
}
class Functions {
    static void removeColons() {
        try {
            StringTokenizer stringTokenizer = new StringTokenizer(Const.STR_INPUT, Const.DELIMITER);
            while (stringTokenizer.hasMoreTokens()) {
                Print.stringAndSpace(stringTokenizer.nextToken());
            };
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
        };
    }
}
class RemoveColons2 {
    public static void main(String[] args) {
        Functions.removeColons();
    }
}
//
//print
//
inpa tistory com super power



5. 아래가 long 의 min 값이 되는 원리를 설명하시오.

System.out.println(Long.MAX_VALUE + 1);
//
//code
//
class Print {
    private static StringBuilder print = new StringBuilder();
    private static void printAndReset() {
        System.out.print(print);
        print.setLength(0);
    }
    private static void printlnAndReset() {
        System.out.println(print);
        print.setLength(0);
    }
    static void longToBinary(Long lo) {
        print.append(Long.toBinaryString(lo));
        printlnAndReset();
    }
}
class Overflow {
    public static void main(String[] args) {
        Print.longToBinary(Long.MAX_VALUE);
        Print.longToBinary(Long.MIN_VALUE);
        Print.longToBinary(Long.MAX_VALUE + (long)1);
    }
}
/*print
111111111111111111111111111111111111111111111111111111111111111
1000000000000000000000000000000000000000000000000000000000000000
1000000000000000000000000000000000000000000000000000000000000000
*/
  • It is easier for binary computer language. So overflow occurs.
profile
Brave but clumsy

0개의 댓글