My footsteps

code 실습 / 2️⃣8️⃣ 본문

국비수업/CODE

code 실습 / 2️⃣8️⃣

밀김 2023. 4. 16. 15:49
728x90

 

 

 

 

 

 

 

 

 

 

<main>

package ex01;

import java.util.Arrays;
import java.util.Comparator;

public class Ex01 {
	public static void main(String[] args) {
		

	   // int 배열의 최댓값을 리턴하는 메서드를 작성하세요
	   Ex01class array = new Ex01class();

	   int[] nums = {1, 3, 7, 13, 3, 22, 5, 1};
	   
//     int maxIntNum = array.getMaxNum(nums);

       
       // long 배열의 최댓값을 리턴하는 오버로딩 메서드 nums를 작성하세요
//	   long[] nums = {1, 3, 7, 13, 3, 22, 5, 1};
//       long maxLongNum = array.getMaxNum(nums);		
//       System.out.println("max num : " + maxLongNum);
		

    // length를 이용하지 말고 배열의 길이를 리턴하는 메서드를 작성하세요
//	   int count = array.getMaxNum(nums);
//	   System.out.println(count);
	   
	   
	// 정수형 배열의 평균값을 리턴하는 메서드를 작성하세요
//	   int maxIntNum = array.getMaxNum(nums);
//	   System.out.println(maxIntNum/nums.length);
       
	   // 정수형 배열의 합을 리턴하는 메서드를 작성하세요
//	   int maxIntNum = array.getMaxNum(nums);
//	   System.out.println(maxIntNum);
	   
	// 지정된 인덱스 사이의 합을 구하는 메서드를 작성하세요
//	   int maxIntNum = array.getSum(nums,0,2);
//       getSum(nums, 0, 2); // 배열 nums의 인덱스 1부터 2까지의 합
//	   System.out.println(maxIntNum);
	   
	   
       //???????정수형 배열에서 중복 값을 제거한 새로운 정수형 배열을 리턴하는 메서드를 작성하세요
       // 결과: {7, 13, 22, 5};
//	   int[] maxIntNum = array.getSum(nums);
//	   for(int i=0; i<maxIntNum.length; i++) {
//		   System.out.println(maxIntNum[i]);
//	   }
//	   int[] nums = {1,3,7,13,3,22,5,1};
	   
	   //혜인이 풀이
//	      int[] tmp = new int[nums.length];
//	      
//	      int index=0;
//	      
//	      for(int i=0; i<nums.length; i++) {
//	         boolean isDuplicate = false;
//	         for(int j=0; j<tmp.length; j++) {
//	            if(i!=j && nums[i]==nums[j]) {
//	               isDuplicate = true;
//	            }
//	         }if(!isDuplicate) {
//	            tmp[index++] = nums[i];
//	         }
//	      }
//	      int zeroNums = 0;
//	      for(int i=0;i<nums.length;i++) {
//	         if(tmp[i] == 0)
//	            zeroNums++;
//	      }
//	      
//	      int[] answer = new int[tmp.length-zeroNums];
//	      
//	      for(int i=0; i<answer.length; i++) {
//	         answer[i] = tmp[i];
//	      }
//	      
//	      
//	      for(int i = 0; i<answer.length; i++) {
//	         System.out.printf("%d", tmp[i]);
//	         if(i!=answer.length-1)
//	         System.out.print(", ");
//	         
//	      }
	      /*
	      // 둘 다 Arrays의 메서드인데 알아두면 유용해!
	      tmp = Arrays.copyOfRange(tmp, 0, tmp.length-zeroNums);
	      //copeOfRange는 (배열,from,to) 라서 from부터 to까지 복사해서 넘겨줌.
	      System.out.println(Arrays.toString(tmp));
	      //배열을 바로 모두 출력하는 방법.
	      */
	   
	   

	   //도연언니가 알려준 답
//	   HashSet<Integer> set = new HashSet<>();
//
//	   for (int num : nums) {
//	       if (!set.add(num)) { // 중복되는 경우
//	           while (set.contains(num)) {
//	               set.remove(num);
//	           }
//	       }
//	   }
//
//	   int[] result = new int[set.size()];
//	   int i = 0;
//	   for (int num : set) {
//	       result[i++] = num;
//	   }
//
//	   System.out.println(Arrays.toString(result));
	   
	   

	//???????정수형 배열에서 중복되지 않은 수들의 합을 리턴하는 메서드를 작성하세요 결과: 47
//	   int maxIntNum = array.getSum(nums);
//	   System.out.println(maxIntNum);
	   
	   
	   
       //???????정수형 두 배열을 합친 새로운 배열을 리턴하는 메서드를 작성하세요
//       int[] nums2 = {99, 1};
//       concat(nums, nums2); // 결과: {1, 3, 7, 13, 3, 22, 5, 1, 99, 1}

	   
	   // 정수형 배열에서 입력한 값의 개수를 리턴하는 메서드를 작성하세요
//       System.out.println(array.count(nums, 3));
	   // 결과: 2
	   
	   // 정수형 배열 nums에 입력한 인덱스에 입력한 값을 삽입한 배열을 리턴하는 메서드를 작성하세요
//	   int idx = 2;
//	   int value = 100;
//	   int[] newArray = array.insertValueIntoArray(nums, idx, value);
//	   System.out.println(Arrays.toString(newArray));
	   
	   
	     // 정수형 배열에서 특정 값의 첫번째 인덱스를 찾아 리턴하는 메서드를 작성하세요
       // 이때, 값이 없다면 -1을 리턴하세요
//       array.findIndexByValue(nums, 7); // 결과: 2
//       System.out.println(array.findIndexByValue(nums, 7));
//       array.findIndexByValue(nums, 999); // 결과: -1
//       System.out.println(array.findIndexByValue(nums, 999));
       
       

       // 정수형 배열에서 특정 값을 모두 삭제한 새로운 배열을 리턴하는 메서드를 작성하세요?????
       // 결과: {3, 7, 13, 3, 22, 5};
//	   System.out.println(array.removeAll(nums, 1));
	   
       
	   // 정수형 배열의 값을 오름차순으로 정렬한 새로운 배열을 리턴하는 메서드를 작성하세요
//	   Arrays.sort(nums);
//       for (int i : nums) {
//           System.out.print(i + " ");
//       }
       
       // 정수형 배열의 값을 내림차순으로 정렬한 새로운 배열을 리턴하는 메서드를 작성하세요
//	   Integer[] nums2 = {1, 3, 7, 13, 3, 22, 5, 1};
//       Arrays.sort(nums2, Comparator.reverseOrder()); // 내림차순
//       for (int i : nums2) {
//           System.out.print(i + " ");
//       }
       
	   
	   
	   
       //--------------★★★문자열 문제★★★------------------------------------
	    // 문자열 배열에서 가장 긴 공통 문자열을 찾아 리턴하는 메서드를 작성하세요
//       String[] strings = {"feedback", "flower", "fight", "flight"};
//       System.out.println(array.findLongestString(strings));
// 

	   	// 두 문자열을 합친 새로운 문자열을 리턴하는 메서드를 작성하세요
       // 결과: "hello newlec"
//       String string1 = "hello ";
//       String string2 = "newlec";
//       System.out.println(string1.concat(string2));
       
//       String concatenatedString = array.concatenateStrings("hello ", "newlec");
//       System.out.println(concatenatedString);
       
       
       // 문자열의 모든 영소문자를 대문자로 바꾼 문자열을 리턴하는 메서드를 작성하세요
//       String upper = array.upper("hello","newlec");
//       System.out.println(upper);
	   
	   // 문자열의 모든 영대문자를 소문자로 바꾼 문자열을 리턴하는 메서드를 작성하세요
//	   String lower = array.lower("HELLO","NEWLEC");
//	   System.out.println(lower);
	   
	   
	// 문자열이 숫자로만 이루어졌는지 판별하는 메서드를 작성하세요???????
//       String string1 = "12345"; // true
//       String string2 = "1a223b"; // false
//       boolean lower = array.lower(string1, string2);
//       System.out.print(lower);
	   
	// 문자열이 영문자 + 숫자 조합으로 이루어졌는지 판별하는 메서드를 작성하세요??????????
//	   String string1 = "12345"; // false
//	   String string2 = "abcd"; // false
//	   String password = "q1w2e3r4"; // true
//	   System.out.println(array.isAlphaNumeric(string1));

       // 000000-0000000 형태의 문자열을 -을 제외한 문자열을 리턴하는 메서드를 작성하세요
       // 결과: {"1234561234567", "0000001111111"}
//       String[] RRNs = {"123456-1234567", "000000-1111111"};
//       String result = array.removeDash(RRNs);
//       System.out.println(result);

	   
	   
	    // 문자열에서 특정 문자열을 찾아 다른 문자열로 대체하여 리턴하는 메서드를 작성하세요???????
       // 입력 : "my name is", "name", "age"
       // 출력 : "my age is"
//	   String[] strings = {"my name is", "name", "age"};
//	   boolean result = array.num(strings);
//	   
//	   System.out.println(result);
	   
	   
	   


       // 문자열에서 모든 공백을 제거한 문자열을 리턴하는 메서드를 작성하세요
       // 입력 : "my name is"
       // 출력 : "mynameis"
//	   String line = "my name is";
//	   String newStr = array.str(line);
//	   System.out.println(newStr);
	   


       // 다음 url 문자열에서 access_token의 값을 추출하는 메서드를 작성하세요??????
       // 결과 : "a123bd24"
//       String requestUrl = "https://quiz4.io?access_token=a123bd24&expires=1000";
//       String result = requestUrl.substring("=");
       

       // 문자열에서 가장 많이 나온 단어를 찾아 리턴하는 메서드를 작성하세요???????
       // 입력 : "cat dog name student quiz cat"
       // 결과 : "cat"
	   String line = "cat dog name student quiz cat";
	   int result = array.aCount(line);
	   System.out.println(result);
	   
	   
	   
	   
	}
}

 

 

 

 

<Class>

package ex01;
import java.util.Arrays;
import java.util.stream.IntStream;

public class Ex01class {
	//클래스에 접근하려면 객체를 생성해야한다.

	// int 배열의 최댓값을 리턴하는 메서드를 작성하세요
//	public int getMaxNum(int[] nums) {
//		int maxNum = 0;
//		
//		for(int i=0; i<nums.length; i++) {
//			if(nums[i]>maxNum){
//				maxNum = nums[i];
//			}
//		}
//		return maxNum;
//	}
	
	 // long 배열의 최댓값을 리턴하는 오버로딩 메서드 nums를 작성하세요
//	public long getMaxNum(long[] nums) {
//		long maxNum = 0;
//		
//		for(int i=0; i<nums.length; i++) {
//			if(nums[i]>maxNum){
//				maxNum = nums[i];
//			}
//		}
//		return maxNum;
//	}
	
	
	// length를 이용하지 말고 배열의 길이를 리턴하는 메서드를 작성하세요
//	public int getMaxNum(int[] nums) {
//		int count = 0;
//	    for (int i : nums) { 
//		   count++;
//		   }
//	   return count;
//	}
	
	// 정수형 배열의 평균값을 리턴하는 메서드를 작성하세요
//	public int getMaxNum(int[] nums) {
//		int avg = 0;
//		for(int i=0; i<nums.length; i++) {
//			avg += nums[i];
//		}
//		return avg;
//	}
	
	 // 정수형 배열의 합을 리턴하는 메서드를 작성하세요
//	public int getMaxNum(int[] nums) {
//	int sum = 0;
//	for(int i=0; i<nums.length; i++) {
//		sum += nums[i];
//	}
//	return sum;
//} 
	
	// 지정된 인덱스 사이의 합을 구하는 메서드를 작성하세요
//	public int getSum(int[] nums,int a,int b) {
//	int idx = 0;
//	
//	for(int i=0; i<3; i++) {
//		idx += nums[i];
//	}
//
//	return idx;
//} 
	
	
	// 정수형 배열에서 중복 값을 제거한 새로운 정수형 배열을 리턴하는 메서드를 작성하세요
//	public int[] getSum(int[] nums) {
//	   int count = 0;
//	   int[] tmp = new int[6];
//	   
//		for(int i=0; i<nums.length; i++) {
//			boolean flag = false;
//			for(int j=0; j<tmp.length; j++) {
//				if(nums[i] == tmp[j]) {
//					flag = true;
//				}
//			}	
//			if(!flag) { 
//				tmp[count++] = nums[i];
//			}
//		}
//		return tmp;
//} 

	
	
	// 정수형 배열에서 중복되지 않은 수들의 합을 리턴하는 메서드를 작성하세요 결과: 47
//	public int getSum(int[] nums) {
//	   int count = 0;
//	   int[] tmp = new int[6];
//	   
//		for(int i=0; i<nums.length; i++) {
//			boolean flag = false;
//			for(int j=0; j<tmp.length; j++) {
//				if(nums[i] == tmp[j]) {
//					flag = true;
//				}
//			}	
//			if(!flag) { 
//				tmp[count++] = nums[i];
//			}
//		}
//		
//		int avg = 0;
//		for(int i=0; i<tmp.length; i++) {
//			avg += tmp[i];
//		}
//		
//		return avg;
//} 
	
	
	// 정수형 두 배열을 합친 새로운 배열을 리턴하는 메서드를 작성하세요

	
	
	// 정수형 배열에서 입력한 값의 개수를 리턴하는 메서드를 작성하세요
//	public int count(int[] nums,int a) {
//		int count = 0;
//		for(int i=0; i<nums.length; i++) {
//			if(nums[i]==a) {
//				count++;
//			}
//		}
//		return count;
//	}
	
	
	// 정수형 배열 nums에 입력한 인덱스에 입력한 값을 삽입한 배열을 리턴하는 메서드를 작성하세요
//	public static int[] insertValueIntoArray(int[] nums, int idx, int value) {
//	    int[] newArray = new int[nums.length + 1]; // 새로운 배열 생성
//	    int j = 0;
//	    for (int i = 0; i < newArray.length; i++) {
//	        if (i == idx) { // 삽입할 위치에 새로운 값을 삽입
//	            newArray[i] = value;
//	        } else { // 새로운 값을 삽입하지 않을 위치에 이전 배열의 값을 복사
//	            newArray[i] = nums[j];
//	            j++;
//	        }
//	    }
//	    return newArray;
//	}
	
	 // 정수형 배열에서 특정 값의 첫번째 인덱스를 찾아 리턴하는 메서드를 작성하세요
//	public int findIndexByValue(int[] nums,int a) {
//		int index = 0;
//		for(int i=0; i<nums.length; i++) {
//			if(a==nums[i]) {
//				index = i;
//				return index;
//			}
//		}
//		return -1;
//	}
	
	
	
	 // 정수형 배열에서 특정 값을 모두 삭제한 새로운 배열을 리턴하는 메서드를 작성하세요
//	public int[] removeAll(int[] nums,int a) {
//		int[] newNums = new int[nums.length - 1];
//		int index = 0;
//
//		for (int i = 0; i < nums.length; i++) {
//		    if (nums[i] != 1) { 
//		        newNums[index] = nums[i];
//		        index++;
//		    }
//		}
//
//		// 삭제된 숫자 1이 없는 새로운 배열을 출력합니다.
//		for (int i = 0; i < newNums.length; i++) {
//		    System.out.print(newNums[i] + " ");
//		}
//		return newNums;
//	}
	
	
	// 정수형 배열의 값을 오름차순으로 정렬한 새로운 배열을 리턴하는 메서드를 작성하세요

	
	
	// 정수형 배열의 값을 내림차순으로 정렬한 새로운 배열을 리턴하는 메서드를 작성하세요
	
	
	
	// 문자열 배열에서 가장 긴 공통 문자열을 찾아 리턴하는 메서드를 작성하세요
//	public String findLongestString(String[] strings) {
//		String longestString;
//		longestString = "";
//	    for (String s : strings) {
//	        if (s.length() > longestString.length()) {
//	            longestString = s;
//	        }
//	    }
//	    return longestString;
//	}
	
	// 두 문자열을 합친 새로운 문자열을 리턴하는 메서드를 작성하세요
//	public static String concatenateStrings(String string1, String string2) {
//	    return string1.concat(string2);
//	}
	
	// 문자열의 모든 영소문자를 대문자로 바꾼 문자열을 리턴하는 메서드를 작성하세요
//	public String upper(String string1, String string2) {
//		return string1.toUpperCase() + " " + string2.toUpperCase();
//	}
	
	// 문자열의 모든 영대문자를 소문자로 바꾼 문자열을 리턴하는 메서드를 작성하세요
//	public String lower(String string1, String string2) {
//		return string1.toLowerCase() + " " + string2.toLowerCase();
//	}
	
	// 문자열이 숫자로만 이루어졌는지 판별하는 메서드를 작성하세요???????
//	public boolean lower(String string1, String string2) {
//		return string1.matches("12345") + " " + string2.matches("1a223b") != null;
//	}
	
	
	// 문자열이 영문자 + 숫자 조합으로 이루어졌는지 판별하는 메서드를 작성하세요
//	public boolean isAlphaNumeric(String str) {
//	    // 문자열이 null이거나 비어있다면 false를 반환합니다.
//	    if (str == null || str.isEmpty()) {
//	        return false;
//	    }
//
//	    boolean containsAlpha = false;
//	    boolean containsNumeric = false;
//
//	    // 문자열을 순회하면서 영어와 숫자를 검사합니다.
//	    for (int i = 0; i < str.length(); i++) {
//	        char c = str.charAt(i);
//	        if (Character.isLetter(c)) {
//	            containsAlpha = true;
//	        } else if (Character.isDigit(c)) {
//	            containsNumeric = true;
//	        }
//
//	        // 문자열에 영어와 숫자가 모두 포함되어 있으면 true를 반환합니다.
//	        if (containsAlpha && containsNumeric) {
//	            return true;
//	        }
//	    }
//
//	    return false;
//	}
	
	
	
	
	// 000000-0000000 형태의 문자열을 -을 제외한 문자열을 리턴하는 메서드를 작성하세요
//	public static String removeDash(String[] RRNs) {
//	    String result = "";
//	    for(String rrn : RRNs) {
//	        result += rrn.replace("-", "");
//	    }
//	    return result;
//	}
	
	
	// 문자열에서 특정 문자열을 찾아 다른 문자열로 대체하여 리턴하는 메서드를 작성하세요???????
//	public boolean num(String[] strings) {
//		boolean containsName = Arrays.asList(strings).contains("name");
//		return containsName;
//	}
	
	
	// 문자열에서 모든 공백을 제거한 문자열을 리턴하는 메서드를 작성하세요
//	public String str(String line) {
//		String newStr = line.replaceAll(" ", "");
//		return newStr;
//	}
	
	
	// 다음 url 문자열에서 access_token의 값을 추출하는 메서드를 작성하세요?????
	
	
	
	// 문자열에서 가장 많이 나온 단어를 찾아 리턴하는 메서드를 작성하세요??????
	public int aCount(String line) {
		int num = line.length() - line.replace("cat", "").length();
		return num;
	}
	
	
}

 

 

 

 

 

 

 

 

728x90

'국비수업 > CODE' 카테고리의 다른 글

code 실습 / 2️⃣7️⃣  (0) 2023.04.09
code 실습 / 2️⃣6️⃣  (0) 2023.03.31
code 실습 / 2️⃣5️⃣  (0) 2023.03.31
code 실습 / 2️⃣4️⃣  (0) 2023.03.29
code 실습 / 2️⃣3️⃣  (0) 2023.03.21