1. 아래를 프로그래밍 하시오.
- 원객체 생성
//
- ArrayList 에 차례대로 반지름이 1인 원 부터 10인원을 입력.
//
- 차례대로 각각의 원 넓이 출력
//
- 10개의 원넓이 합산 하여 출력.
import java.util.*;
class Const {
static final int RADIUS_MIN = 1;
static final int RADIUS_MAX = 10;
}
class Print {
private static StringBuilder print = new StringBuilder();
private static void printlnAndReset() {
System.out.println(print);
print.setLength(0);
}
static void printlnDouble(double d) {
print.append(d);
printlnAndReset();
}
}
class Functions {
private static void printCircleListArea() {
List<Circle> list = new ArrayList<Circle>();
for (int i = Const.RADIUS_MIN; i <= Const.RADIUS_MAX; i++) {
list.add(new Circle((double)i));
};
double sum = 0;
for (Circle c : list) {
double area = c.getArea();
Print.printlnDouble(area);
sum += area;
};
Print.printlnDouble(sum);
}
static void run() {
while(true) {
try {
printCircleListArea();
break;
} catch (Exception e) {
e.printStackTrace();
};
};
}
}
class Circle {
private double radius;
Circle(double radius) {
this.radius = radius;
}
double getArea() {
double area = Math.PI * Math.pow(this.radius, 2);
return area;
}
}
class CircleMain {
public static void main(String[] args) {
Functions.run();
}
}
//
//print
//
3.141592653589793
12.566370614359172
28.274333882308138
50.26548245743669
78.53981633974483
113.09733552923255
153.93804002589985
201.06192982974676
254.46900494077323
314.1592653589793
1209.5131716320705
2. ArrayList 와 LinkedList 의 차이(장단점)는?
- ArrayList: It is based on Array. So it should be initialized whenever the length get chaged. So it can take more time to edit the size of ArrayList. But reading ArrayList is faster.
- LinkedList: It is based on connected list. Editing the size is easier but reading can take more time.
3. 5명의 별명(String)을 입력 받아 ArrayList에 저장하고 이들 중 별명의 길이가 제일 긴 별명을 출력하시오. (단, 각 별명의 길이는 모두 다르게 입력한다.)
import java.util.*;
class Const {
static final String[] NICKNAMES = {"jk", "John", "president", "dictator", "police"};
}
class Print {
private static StringBuilder print = new StringBuilder();
private static void printlnAndReset() {
System.out.println(print);
print.setLength(0);
}
static void printString(String str) {
print.append(str);
printlnAndReset();
}
}
class Functions {
private static List<String> list = new ArrayList<String>();
private static int length = 0;
private static String longestNickname = "";
static void run() {
while(true) {
try {
printLongestNickname();
break;
} catch (Exception e) {
e.printStackTrace();
};
};
}
private static void printLongestNickname() {
for (String nickname : Const.NICKNAMES) {
list.add(nickname);
};
for (String nickname : list) {
int nicknameLength = nickname.length();
if (nicknameLength > length) {
longestNickname = nickname;
length = nicknameLength;
};
};
Print.printString(longestNickname);
}
}
class LongestNickname {
public static void main(String[] args) {
Functions.run();
}
}
//
//print
//
president
4.Scanner 클래스로 -1이 입력될 때까지 양의 정수를 입력받아 List에 저장하고 List를 검색하여 가장 큰 수를 출력하는 프로그램을 작성하라.
정수(-1이 입력될 때까지)>> 10 6 22 6 88 77 -1
가장 큰 수는 88
import java.util.*;
class Print {
private static StringBuilder print = new StringBuilder();
private static void printlnAndReset() {
System.out.println(print);
print.setLength(0);
}
static void printlnInt(int num) {
print.append(num);
printlnAndReset();
}
}
class Functions {
private static Scanner scanner = new Scanner(System.in);
private static List<Integer> list = new LinkedList<Integer>();
private static int biggestNumber = Integer.MIN_VALUE;
static void run() {
while(true) {
try {
printBiggestNumberFromScanner();
scanner.close();
break;
} catch(Exception e) {
e.printStackTrace();
scanner = new Scanner(System.in);
}
};
}
private static void printBiggestNumberFromScanner() {
while(true) {
int inputNum = scanner.nextInt();
if (inputNum == -1) {
break;
} else {
list.add(inputNum);
};
};
for (int num : list) {
if (num > biggestNumber) {
biggestNumber = num;
};
};
Print.printlnInt(biggestNumber);
}
}
class BiggestNumber {
public static void main(String[] args) {
Functions.run();
}
}
//
//print
//
10 6 22 6 88 77 -1
88
5.Scanner 클래스를 사용하여 6개 학점('A', 'B', 'C', 'D', 'F')을 문자로 입력받아ArrayList에 저장하고, ArrayList를 검색하여 학점을 점수(A=4.0, B=3.0, C=2.0, D=1.0, F=0)로 변환하여 평균을 출력하는 프로그램을 작성하라.
6개의 학점을 빈 칸으로 분리 입력(A/B/C/D/F) >> A C A B F D
2.3333333333333335
import java.util.*;
class Const {
static final String[] CREDITS = {"A", "B", "C", "D", "F"};
static final int CREDITS_LENGTH = 6;
static final double SCORE_BEST = 4.;
static final double SCORE_DIF = 1.;
}
class Print {
private static StringBuilder print = new StringBuilder();
private static void printlnAndReset() {
System.out.println(print);
print.setLength(0);
}
static void printDouble(double d) {
print.append(d);
printlnAndReset();
}
}
class Functions {
private static Scanner scanner = new Scanner(System.in);
private static List<String> credits = new ArrayList<String>();
static void run() {
while(true) {
try {
printAverageCreditfromScanner();
scanner.close();
break;
} catch(Exception e) {
e.printStackTrace();
scanner = new Scanner(System.in);
};
}
}
private static void printAverageCreditfromScanner() {
double sum = 0.;
while(true) {
String input = scanner.next();
for (String credit : Const.CREDITS) {
if (input.equals(credit)) {
credits.add(input);
break;
};
};
if (credits.size() >= Const.CREDITS_LENGTH) {
break;
};
};
for (String credit : credits) {
for (int i = 0; i < Const.CREDITS.length; i++) {
if (credit.equals(Const.CREDITS[i])) {
double creditScore = (Const.SCORE_BEST - (double)i * Const.SCORE_DIF);
sum += creditScore;
break;
};
};
};
double average = sum / (double)Const.CREDITS_LENGTH;
Print.printDouble(average);
}
}
class AverageCredits {
public static void main(String[] args) {
Functions.run();
}
}
//
//print
//
A C A B F D
2.3333333333333335
6.5명의 사람 이름을 입력 받아서 ArrayList에 저장한 후에 이들 중 '김'씨 성을 가진 사람을 모두 출력하시오.
import java.util.*;
class Const {
static final String[] NAMES = {"김모씨", "김모양", "문모양", "안모씨", "오모씨"};
static final char KIM = '김';
}
class Print {
private static StringBuilder print = new StringBuilder();
private static void printlnAndReset() {
System.out.println(print);
print.setLength(0);
}
static void printlnString(String str) {
print.append(str);
printlnAndReset();
}
}
class Functions {
private static List<String> list = new ArrayList<String>();
static void run() {
while(true) {
try {
printKims();
break;
} catch(Exception e) {
e.printStackTrace();
};
};
}
private static void printKims() {
for(String name : Const.NAMES) {
list.add(name);
};
for(String name : list) {
if (name.charAt(0) == Const.KIM) {
Print.printlnString(name);
};
};
}
}
class WhereIsKim {
public static void main(String[] args) {
Functions.run();
}
}
//
//print
//
김모씨
김모양