반응형
Q1. 예외처리 기본
package com.dinfree;
import java.io.File;
import java.util.Scanner;
public class ExceptionTest1001 {
public static void main(String[] args) {
File file=new File("test.txt");
Scanner scan;
scan = new Scanner(file);
while(scan.hasNext()) {
System.out.println(scan.next());
}
}
}
package com.dinfree;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ExceptionTest1001 {
public static void main(String[] args) {
File file=new File("test.txt");
Scanner scan;
try {
scan = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Q2. 사용자 예외 클래스 구현
package com.dinfree;
public class ExceptionTest2_1001 extends Exception{
String exMsg;
public ExceptionTest2_1001(String msg)
{
exMsg = "ExceptionTest : "+msg;
}
@Override
public String getMessage() {
return exMsg;
}
}
package com.dinfree;
public class ExceptionTest3_1001 {
int num;
public void doException() throws ExceptionTest2_1001{
if(num==1)
System.out.println("OK");
else
throw new ExceptionTest2_1001("doException");
}
public static void main(String[] args) {
ExceptionTest3_1001 app = new ExceptionTest3_1001();
app.num=2;
try {
app.doException();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Q3. 자바 문자열 생성과 비교
package com.dinfree_1001;
public class StringTest1 {
public static void main(String[] args) {
String s1=new String("hello");
String s2="hello";
System.out.printf("%s, %s\n", s1.hashCode(), s2.hashCode());
String s3=new String("hello");
String s4="hello";
System.out.printf("%s, %s\n", s3.hashCode(), s4.hashCode());
System.out.printf("s1==s2: %s\n", s1==s2);
System.out.printf("s1==s3: %s\n", s1==s3);
System.out.printf("s2==s4: %s\n", s2==s4);
System.out.printf("s1.equals(s2): %s\n", s1.equals(s2));
s2 = s2+ " world";
String s5 = "hello world";
System.out.printf("s2==s5:%s\n", s2==s5);
}
}
Q4. 문자열 처리
package com.dinfree_1001;
import java.util.Arrays;
import java.util.StringJoiner;
public class StringTest2 {
public static void main(String[] args) {
StringJoiner sj = new StringJoiner(",", "[", "]");
String[] carArr= { "hyunadi", "mercedes", "bmw"};
for(String s : carArr)
{
sj.add(s.toUpperCase());
}
System.out.println(sj.toString());
String s1 = " Hello World ";
String s2 = s1.trim();
System.out.printf("#%s#\n", s1);
System.out.printf("#%s#\n", s2);
String s3=s1.substring(1,3);
System.out.println(s3);
System.out.println(s1.replace('l', 'k'));
char[] carr=s2.toCharArray();
System.out.println(Arrays.toString(carr));
}
}
package com.dinfree_1001;
public class StringTest3 {
public static void main(String[] args) {
String s1="Hello World";
System.out.println(s1);
System.out.printf("indexOf('l'): %s\n", s1.indexOf('l'));
System.out.printf("lastIndexOf('l'): %s\n", s1.lastIndexOf('l'));
System.out.printf("charAt(6) : %s\n", s1.charAt(6));
System.out.printf("startWith(\"He\"): %s\n", s1.startsWith("He"));
System.out.printf("length(): %s\n", s1.length());
StringBuffer sb = new StringBuffer();
sb.append("Hello");
sb.append(" World");
String str=sb.toString();
System.out.println(str);
}
}
Q5. 날짜 시간 구하기 기본
package com.dinfree_1001;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class DateTimeTest1 {
public static void main(String[] args) {
LocalDate d1 = LocalDate.now();
LocalDate d2 = LocalDate.of(2019, 10, 10);
LocalTime t1=LocalTime.now();
LocalTime t2=LocalTime.of(7, 20, 20);
System.out.printf("LocalDate.now() : %s\n", d1);
System.out.printf("LocalDate.of(2019, 10, 10): %s\n", d2);
System.out.printf("LocalTime t1=LocalTime.now() : %s\n", t1);
System.out.printf("LocalTime.of(7, 20, 20) : %s\n", t2);
LocalDateTime dt1=LocalDate.now().atTime(LocalTime.MIDNIGHT);
LocalDateTime dt2=LocalDate.now().atTime(LocalTime.MAX);
System.out.printf("LocalDate.now().atTime(LocalTime.MIDNIGHT) : %s\n", dt1);
System.out.printf("LocalDate.now().atTime(LocalTime.MAX) : %s\n", dt2);
}
}
Q6. 날짜 형식 지정 및 시간차이 계산
package com.dinfree_1001;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class DateTimeTest2 {
public static void main(String[] args) {
LocalDateTime dt1=LocalDateTime.now();
DateTimeFormatter dtf=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
System.out.println(dtf.format(dt1));
System.out.println(dt1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println(dt1.plusDays(2));
System.out.println(LocalTime.now().minusHours(3));
System.out.println(Duration.ofMinutes(10).getSeconds());
LocalTime start=LocalTime.of(11, 40, 50);
LocalTime end = LocalTime.of(11, 42, 50);
Duration duration = Duration.between(start, end);
System.out.println("Seconds : "+duration.getSeconds());
LocalDate startDate = LocalDate.of(1950, 9, 1);
LocalDate endDate = LocalDate.of(2010, 9, 2);
Period period = Period.between(startDate, endDate);
System.out.println("Years : "+period.getYears());
System.out.println("Months : "+period.getMonths());
System.out.println("Days : "+period.getDays());
}
}
Q7. 기본적인 제네릭 클래스 생성과 사용
package com.dinfree_1001;
class StorageT1<T> {
T item;
public T getItem() {
return item;
}
public void setItem(T item) {
this.item=item;
}
}
package com.dinfree_1001;
public class GenericsTest1 {
public static void main(String[] args) {
StorageT1<String> storage1=new StorageT1<>();
storage1.setItem("MyItem");
System.out.println(storage1.getItem());
StorageT1<Integer> storage2=new StorageT1<>();
storage2.setItem(20211001);
System.out.println(storage2.getItem());
}
}
Q8. 제네릭 메서드 및 와일드 카드 사용 예제
package com.dinfree_1001;
import java.util.ArrayList;
import java.util.List;
public class GenericsTest2 {
public <T> List<Character> convert(StorageT1<T> storage){
ArrayList<Character> list = new ArrayList<>();
String s = String.valueOf(storage.getItem());
int size= s.length();
for(int i=0; i<size; i++) {
list.add(s.charAt(i));
}
return list;
}
public static void main(String[] args) {
StorageT1<String> s1=new StorageT1<>();
s1.setItem("MyItem");
StorageT1<Integer> s2=new StorageT1<>();
s2.setItem(20211001);
GenericsTest2 gt2=new GenericsTest2();
System.out.println(gt2.convert(s1));
System.out.println(gt2.convert(s2));
}
}
반응형
'Java > Java 2' 카테고리의 다른 글
[Java 실습] 6. 자료구조와 컬랙션프레임워크 (0) | 2021.10.01 |
---|---|
[Java] 6. 자료구조와 컬렉션프레임워크 (0) | 2021.10.01 |
[Java] 5. 자바 중급 활용 (0) | 2021.07.16 |
[Java 실습] 4. 객체지향 개념과 자바 (0) | 2021.07.15 |
[Java] 4. 객체지향 개념과 자바 (0) | 2021.07.15 |