본문 바로가기
BackEnd/Backend 공부 정리

Java-12

by Brilliant_Graphite 2024. 8. 22.

 오버로딩

오버라이딩

 

package kr.co.hanuledu.oop1;

public class StringEx0 {

	public static void main(String[] args) {
		String str = "apple";
		
		String str1 = new String("APPLE");
		
		System.out.println(str);
		System.out.println(str1);
		
		// 문자열 길이
		System.out.println(str1.length());
		
		// 대문자 변경
		System.out.println(str1.toUpperCase());
		
		// 소문자 변경
		System.out.println(str1.toLowerCase());
		
		// 특정 문자 추출
		System.out.println(str1.substring(1));
		System.out.println(str1.substring(0,3));
		
		// 특정 문자열의 존재여부
		int succ = str1.indexOf("ap");
		
		if(succ == -1) {
			System.out.println("검색 실패");
		} else {
			System.out.println("검색 성공");
		}
		
		String str3 = "대 : 100@중 : 70@소 : 50";
		String[] sp = str3.split("@");
		
		for(int i = 0; i < sp.length; i++) { 
			System.out.println(sp[i]);
		}
		
		// 문자열에서 특정 문자 한글자만 출력
		System.out.println(str1.charAt(3));
		System.out.println();
		
		// 문자열을 한 글자씩 출력(str1)
		for(int i = 0; i < str1.length(); i++) {
			System.out.print(str1.charAt(i));
		}
		
		System.out.println();
		
		
		String str4 = null;
		
		// 문자열이 같은지 다른지를 판단하는 메서드
		if("ㅇ".equals(str4)) {
			System.out.println(str + "과" + str1 +"는 같다.");
		} else {
			System.out.println(str+"과" + str1 + "는 같지 않습니다.");
		}
		
		// 사과@바나나@오렌지@귤@포도의 데이터를 @를 기준으로 문자열을 분리하여 출력(Array.toString)
		// 하고 과일 이름을 기준으로 내림차순 정렬(descSort())하여 출력(print())하시오.
		
		
		
	}

}

 

package kr.co.hanuledu.oop1;

public class StringTest {

	public static void main(String[] args) {

		String str = "사과@바나나@오렌지@귤@포도";
		String[] arr = str.split("@");

		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}

		for (int i = 0; i < arr.length; i++) {
			for (int j = i + 1; j < arr.length; j++) {
				// person[i]의 이름이 person[j]의 이름보다 크다면(양수라면)
				if (arr[i].compareTo(arr[j]) < 0) {
					// 데이터를 교환
					String temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}

		}
		
		System.out.println();
		
		for(int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
	}
}

 

package kr.co.hanuledu.oop1;

import java.util.Arrays;

public class TestStringDAO {
	
	public void descSort(String[] sp) {
		for(int i = 0; i < sp.length; i++) {
			for(int j = 0; j < sp.length; j++) {
				if(sp[i].compareTo(sp[j]) > 0) {
					String temp = sp[i];
					sp[i] = sp[j];
					sp[j] = temp;
				}
			}
		}
	}
	
	public void print(String[] sp) {
		System.out.println(Arrays.toString(sp));
	}
	
	
}

 

package kr.co.hanuledu.oop1;

import java.util.Arrays;

public class TestStringMain {

	public static void main(String[] args) {
		String pr = new String("사과@바나나@오렌지@귤@포도");
		String[] sp = pr.split("@");
		
		TestStringDAO testStringDao = new TestStringDAO();
		
		for(int i = 0; i < sp.length; i++) {
			System.out.print(sp[i] + " ");
		}
		
		System.out.println(Arrays.toString(sp));
		
		testStringDao.descSort(sp);
		testStringDao.print(sp);

	}

}

 

package kr.co.hanuledu.oop1;

public class tryCatchEx {

	public static void main(String[] args) {
		
		/*
		 * try (~~) catch (~~) : Exception(예외처리)
		 * 정상적인 구간 / 예외적인 구간
		 */
		try {
		System.out.println(args[0]);
		System.out.println(args[1]);
		System.out.println(args[0] + args[1]);
		
		int num1 = Integer.parseInt(args[0]);
		int num2 = Integer.parseInt(args[1]);
		
		// 래퍼클래스 문자형 숫자 -> 숫자형 숫자 변환
		System.out.println(Integer.parseInt(args[0]) + Integer.parseInt(args[1]));
		
		// 숫자형 숫자 -> 문자형 숫자 변환
		System.out.println(String.valueOf(num1) + String.valueOf(num2));
		} catch (Exception e) {
			 e.printStackTrace();
			 System.out.println(e);
			// System.out.println("값을 입력해 주세요");
		}
	}

}

'BackEnd > Backend 공부 정리' 카테고리의 다른 글

Java-13  (0) 2024.08.23
sqldeveloper-12  (0) 2024.08.22
sqldeveloper-11  (0) 2024.08.21
Java-11  (2) 2024.08.21
sqldeveloper-10  (0) 2024.08.20