728x90

1.Ex01)

package days11;
/**
 * @author kimjieun
 * @date 2023. 2. 9.-오후 12:28:29
 * @subject
 * @content
 */
public class Ex01 {

	public static void main(String[] args) {
		//10진수 정수(int)를 입력받아서 10
		//2바이트 2진 형태로 출력해라 00000000 000010[1][0]

		int n=10;
		//System.out.println(Integer.toBinaryString(n)); //00000000 0000+"1010"?
		String binaryN=Integer.toBinaryString(n);
		int zeroCount = 16-binaryN.length();
		//String.repeat(갯수)
		//System.out.printf("%s%s", "0".repeat(zeroCount),binaryN);
		System.err.printf("%s%s", "000000000000",binaryN);
		/*
		 * System.out.println(Integer.toBinaryString(n));
		 * System.out.println(Integer.toOctalString(n));
		 * System.out.println(Integer.toHexString(n));
		 */

		//방법2
		//String binaryN = Integer.toBinaryString(n);
		//System.out.printf("%016d\n",n);	%d 10진수 정수값만 출
		//System.out.printf("%016d\n", Integer.parseInt(binaryN));

		//방법3
		// int []  m =  [][][][][][][][][][][1][0][1][0]
		// 방법 3  - 제어문 사용..
		int 몫, 나머지;

		String b = "";
		while( n != 0) {
			몫 = n/2;
			나머지 = n%2;
			// System.out.printf("[%d]", 나머지);
			b += 나머지;
			n =  몫;
		} // 
		// [0][1][0][1]        => 1010
		System.out.println(b);  // "0101"  문자열 reverse 기능

		String rb = "";
		for (int i = b.length()-1; i >=0 ; i--) {
			System.out.println(  b.charAt(i));
			rb += b.charAt(i);
		}
		System.out.println( rb );
		/*
	      int 몫 = n/2;       // 5
	      int 나머지 = n%2;  // 0

	      n = 몫;            // 5
	      몫 = n/2;        // 2
	      나머지 = n%2;  // 1

	      n = 몫;            // 2
	      몫 = n/2;        // 1
	      나머지 = n%2;  // 0

	      n = 몫;            // 1
	      몫 = n/2;        // 0
	      나머지 = n%2;  // 1

	      중단~
		 */

	}

}

 

2.Ex02)

package days11;

public class Ex02 {

	public static void main(String[] args) {
		String bn="0101";
		StringBuffer sb = new StringBuffer(bn);
		System.out.println(sb.reverse() );
		

	}

}

 

3.Ex03)

package days11;
/* 
 * @author kimjieun
 * @date 2023. 2. 9.-오후 2:05:51
 * @subject
 * @content
 */
public class Ex03 {

	public static void main(String[] args) {
		int money=125760;
		int share; //
		int rest;//나머

		share=money/50000; //몫
		rest=money%50000; //나머지
		System.out.printf("5만원: %d 개 \n", share);
		
		money=rest;
		share=money/10000; //몫
		rest=money%10000; //나머지
		System.out.printf("1만원: %d 개 \n", share);
		
		money=rest;
		share=money/5000; //몫
		rest=money%5000; //나머지
		System.out.printf("5천원: %d 개 \n", share);
		
		money=rest;
		share=money/1000; //몫
		rest=money%1000; //나머지
		System.out.printf("1천원: %d 개 \n", share);
		
		money=rest;
		share=money/500; //몫
		rest=money%500; //나머지
		System.out.printf("5백원: %d 개 \n", share);
		
		money=rest;
		share=money/100; //몫
		rest=money%100; //나머지
		System.out.printf("1백원: %d 개 \n", share);
		
		money=rest;
		share=money/10; //몫
		rest=money%10; //나머지
		System.out.printf("십원: %d 개 \n", share);
		
		money=rest;
		share=money/1; //몫
		rest=money%1; //나머지
		System.out.printf("1원: %d 개 \n", share);

	}//end of main

}

 

4.Ex03_02)

package days11;

public class Ex03_02 {

	public static void main(String[] args) {
		int money=125760;
		int share; //
		int rest;//나머
		
		int [] unit = {50000,10000,5000,1000,500,100,50,10,1};//화폐단위 배열 선언
		String [] s_unit = { "5만원","1만원","5천원", "1천원", "5백원", "1백원","5십원","1십원","5원","1원" };
		for (int i = 0; i<unit.length; i++) {
			share=money/unit[i]; 
			rest=money%unit[i];
			System.out.printf("%s: %d개 \n", s_unit[i],share);
			money=rest;
		}
		/*
		 * share=money/50000; //몫 rest=money%50000; //나머지
		 * System.out.printf("5만원: %d 개 \n", share);
		 * 
		 * money=rest; share=money/10000; //몫 rest=money%10000; //나머지
		 * System.out.printf("1만원: %d 개 \n", share);
		 * 
		 * money=rest; share=money/5000; //몫 rest=money%5000; //나머지
		 * System.out.printf("5천원: %d 개 \n", share);
		 * 
		 * money=rest; share=money/1000; //몫 rest=money%1000; //나머지
		 * System.out.printf("1천원: %d 개 \n", share);
		 * 
		 * money=rest; share=money/500; //몫 rest=money%500; //나머지
		 * System.out.printf("5백원: %d 개 \n", share);
		 * 
		 * money=rest; share=money/100; //몫 rest=money%100; //나머지
		 * System.out.printf("1백원: %d 개 \n", share);
		 * 
		 * money=rest; share=money/10; //몫 rest=money%10; //나머지
		 * System.out.printf("십원: %d 개 \n", share);
		 * 
		 * money=rest; share=money/1; //몫 rest=money%1; //나머지
		 * System.out.printf("1원: %d 개 \n", share);
		 */

	}

}

 

5.Ex03_03)

package days11;

public class Ex03_03 {

	public static void main(String[] args) {
		//[정보처리기사 실기]
		int money=125760;
		int unit=50000;
		boolean sw = false;//스위치 변수
		int count = 0; //화폐의 수량
		while(unit>=1) { //while문은 참일때 돌리니
			count=money/unit;//갯수
			System.out.printf("%d원:%d 개 \n", unit, count);
			money %=unit;
			
			unit /= (!sw?5:2 ); //unit=unit/(!sw?5:2 )
		}
		
		
//		/5 10000
//		/2 5000
//		/5 1000
//		/2 500
//		/5 100
//		/2 50
//		/5 10
//		/2 5
//		/5 1
		
		
		
	}//end of main

}//end of class

 

6.Ex04)

package days11;

import java.lang.reflect.Array;
import java.util.Scanner;

/**
 *
 * @author kimjieun
 * @date 2023. 2. 9.-오후 2:37:15
 * @subject 년도와 월을 입력받아서 달력을 출력하기.
 * @content
 */

public class Ex04 {

	   public static void main(String[] args) {
	      // [일정관리/근태관리] - 자바 달력, 오라클 SQL, javascript 달력, JSP 달력
	      // [년도와 월]을 입력받아서 달력을 출력.
	      // 3:05 수업 시작~
	      // 1) 1일 무슨 요일 ?
	      // 2) 마지막날짜 ? O
	      Scanner scanner = new Scanner(System.in);
	      int year, month;

	      System.out.printf("> year, month input ? ");
	      year = scanner.nextInt();
	      month = scanner.nextInt();

	      /*
	       * for (int i = 1; i <=12; i++) { int lastDay = getLastDay( year, i );
	       * System.out.printf("%d.%d = %d일\n", year, i , lastDay); }
	       */ 
	      printCalendar(year, month);
	   } // end of main
	 
	   private static void printCalendar(int year, int month) {
		  //(1)
	      int lastDay = getLastDay(year, month);
	      //(2)
	      int dayOfWeek = getDayOfWeek(year, month, 1); // 0(일), 1(월)~ 6(토)
	      System.out.printf("\t[%d년 %d월]\n", year, month );
	      days08.Ex03_02.drawLine(30,'-');
	      String week="일월화수목금토";
	      for(int i=0; i< week.length(); i++) System.out.printf("\t%c", week.charAt(i));
	      System.out.println(); //개행
	      System.out.println( "-".repeat(30));
	       // 4:03 수업 시작 [ 달력 출력 ]
	      for(int i=0; i<dayOfWeek; i++) {
	    	  System.out.print("\t");
	      }
	      //1~마지막날짜 for
	      for(int i=1; i<=lastDay; i++) {
	    	  System.out.printf("\t%d", i);
	    	  if((i+dayOfWeek)%7==0) {
	    		  System.out.println();
	    	  }
	      }
	      
	      System.out.println(); //개행
	      System.out.println( "-".repeat(30));
	   }

	   private static int getDayOfWeek(int year, int month, int day) {
	      // 1.1.1 ~ year.month.day 총날짜수
	      int totalDays = getTotalDays(year, month, day);
	      //System.out.println( "총 날짜수 : " + totalDays  );
	      int dayOfWeek = totalDays % 7;
	      //System.out.println(  "일월화수목금토".charAt(dayOfWeek) + "요일" );
	      return dayOfWeek; // 0~6 요일에 해당되는 정수 반환하는 함수.
	   }

	   // 총날짜수를  구합니까? 
	   // 1.1.1 ~ year.month.day 총날짜수
	   private static int getTotalDays(int year, int month, int day) {
	      int totalDays = 0; 
	      totalDays = ( year - 1 ) * 365 + ( year - 1 )/4 - ( year - 1 )/100 + ( year - 1 )/400 ;
	      int[] months = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };      
	      for (int i = 0; i < month - 1; i++)           totalDays += months[i];
	      if( month >= 3 && days08.Ex04.isLeapYear(year) ) totalDays++;
	      totalDays += day;      
	      return totalDays;
	   }
	   /*
	   private static int getTotalDays(int year, int month, int day) {
	      // 2000.12.1 총날짜수
	      // (2000-1) * 365 + 1월(31) + 2월(29) + 3월(31) + 4월(30) ... + 11월(30) + 1 (고민)
	      int totalDays = 0; // 총날짜수

	      for (int i = 1; i < year; i++) {
	         if (days08.Ex04.isLeapYear(i))
	            totalDays += 366;
	         else
	            totalDays += 365;
	      }
	      // 이전년도 까지의 총 날짜수 1999.12.31 까지의 총날짜수 ...

	      // month = 1
	      totalDays += 1;
	      // month =2
	      totalDays += 31 + 1;
	      // month =3
	      totalDays += 31 + 28/29+ 1;
	      // month =4
	      totalDays += 31 + 28/29+ 31+  1;
	      // month =5
	      totalDays += 31 + 28/29+ 31+  30+  1;
	      // month =6
	      totalDays += 31 + 28/29+31+30+31+ 1;
	      // month =7
	      totalDays += 31 + 28/29+31+30+31+30+ 1;
	      // month =8
	      totalDays += 31 + 28/29+31+30+31+30+31+ 1;
	      // month =9
	      totalDays += 31 + 28/29+31+30+31+30+31+31+ 1;
	      // month =10
	      totalDays += 31 + 28/29+31+30+31+30+31+31+30+ 1;
	      // month =11
	      totalDays += 31 + 28/29+31+30+31+30+31+31+30+31+ 1;
	      // month =12
	      totalDays += 31 + 28/29+31+30+31+30+31+31+30+31+30+ 1;

	      return totalDays;
	   }
	   */

	   private static int getLastDay(int year, int month) { // 5
	      // [0]=1월달 [11] = 12월달
	      int[] months = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	      if (month == 2)
	         return days08.Ex04.isLeapYear(year) ? 29 : 28;
	      return months[month - 1];
	   }

	   /*
	    * private static int getLastDay(int year, int month) { int lastDay = 0;
	    * 
	    * switch (month) { case 4:case 6:case 9:case 11: lastDay = 30; break; case
	    * 1:case 3:case 5:case 7:case 8: case 12: lastDay = 31; break; case 2: // 윤년
	    * 29, 평년 28 lastDay = days08.Ex04.isLeapYear(year) ? 29 : 28; break; // default
	    * : } return lastDay; }
	    */

	} // end of class

 

7.Ex05)

package days11;

public class Ex05 {

	public static void main(String[] args) {
		// 배열 + 제어문
		/*
	       * 1. 자바의 자료형 
	       *    기본형  8가지 
	       *    참조형  - 배열, 클래스, 인터페이스
	       * 2.  [] 인덱스(index) 연산자 
	       * 3. 배열. 
	       *       한 학생의 국어 점수 :   int kor
	       *       5만 학생의 국어 점수 : int kor00001 ~ kor50000
	       *     1) 정의 ? 동일한 자료형을 메모리상에 연속적으로 놓이게 한 것. 
	       *     2) 선언 형식 -  변수, 참조변수, 배열명
	       *        자료형 [] 배열명 = new 자료형[배열크기];
	       *        예) 
	       *        int kors[] = new int[4];
	       *        
	       *          kors[4] =90        ArrayIndexOutOfBoundException      index =4 X
	       *        
	       *          kors[0]~kors[3]
	       *          [ 90 ][ 0 ][ 0 ]                    [0x100]          
	       *                                            kors
	       *           0x100
	       *           
	       *      3) 0번째 요소, 1번째 요소
	       *           배열명[0]   배열명[1]
	       *           kors[0] = 90
	       *           syso( kors[0] )
	       *     4)  모든 요소 추ㄹ
	       *     syso( kors[0] )
	       *     syso( kors[1] )
	       *     syso( kors[2] )  
	       *     syso( kors[3] )  
	       *     
	       *      for( int i=0;   i< kors.length ; i++)
	       *          syso( kors[i] );   
	       *      5) 가장 인덱스값 == 첨자값 =      kors.length -1
	       *          가장 작은 인덱스값          = 0 
	       * */
		

	}//end of class

}//end of class

 

8.Ex05_02)

package days11;
/**
 * @author kimjieun
 * @date 2023. 2. 9.-오후 4:46:39
 * @subject
 * @content
 */
public class Ex05_02 {

	public static void main(String[] args) {
		int [] m = new int [3];
		m[0]=100;
		m[1]=50;
		m=null; //주소값에 null을 주면 참조를 못하고 값이 떨어지게 되고, disp()에는 예외에러가 발생하게 
		
		m = new int[10];
		
		disp(m);//배열을 매개변수값으로 주는 것을 call by reference
		
		//[][][]
		//가비지(쓰레기)
		//자바:GC - 자동으로 메모리 관리(자바 특징)
		//			동적영역(Heap)의 가비지(쓰레기가 가득차면->힘 메모리를 제거.)
		

	}//end of main

	private static void disp(int[] m) {
		for (int i = 0; i < m.length; i++) {
			System.out.printf("m[%d]=%d\n", i, m[i]);
		}
		
	}

}//end of class
728x90