..열심히 공부하세../JAVA 문법

[15] Math 클래스 및 Wrapper 클래스

댄스댄스 2012. 4. 24. 15:04

 

* Math 클래스

파일명 : MathTest.java

package function;
//import java.util.Date;
//import java.util.Calendar;
import java.util.*;
//import java.lang.*;
//java.lang패키지는 이미 선언되어 있음

//메이커가 제공하는 클래스 사용방법


public class MathTest {

    public static void main(String[] args) {
        Date today=new Date();
        System.out.println(today.getYear());
        System.out.println(today.getMonth());
        System.out.println(today.getDate());
        System.out.println(today.getDay());
       
        Calendar cal=new Calendar();
       
        //Math math=new Math();
        //Math 클래스는 수학관련
        //static 객체를 별도로 생성하지 말고
        //클래스명으로 직접 사용가능하다
        System.out.println("절대값 abs:"+Math.abs(-5));
        System.out.println("올림ceil:" + Math.ceil(1.1));
        System.out.println("버림floor:" + Math.floor(1.9));
        System.out.println("반올림round(10.6):" + Math.round(10.6));
       
        /*
         * 난수:컴퓨터가 발생시키는 값
         * 난수범위 : 0.0 <= n < 1.0
         * */
        System.out.println(Math.random());
        System.out.println(Math.random()*3);

        //0~2 정수값 발생
        System.out.println((int)(Math.random()*3));

        //0~44 정수값 발생
        System.out.println((int)(Math.random()*45));

        //1~45 정수값 발생
        System.out.println((int)(Math.random()*45)+1);
        System.out.println((int)(Math.random()*45)+1);
        System.out.println((int)(Math.random()*45)+1);
        System.out.println((int)(Math.random()*45)+1);
        System.out.println((int)(Math.random()*45)+1);
        System.out.println((int)(Math.random()*45)+1);
    }

}

 

 

* 로또번호 발생

 

파일명 : Lotto.java

package oop;

public class Lotto {

    public static void main(String[] args) {
        int[] lotto=new int[6];
        int p, q, su;
       
        for(p=0; p<lotto.length; p++)
        {
            su=(int)(Math.random()*45+1);
            lotto[p]=su;
           
            for(q=0; q<p; q++)
            {
                if(lotto[p]==lotto[q])
                {
                     p--;//p=4                 
                }
            }           
        }       
       
        for(p=0; p<lotto.length; p++)
        {
            System.out.print(lotto[p]+" ");
        }

        
       
/*
숙제)
int[] lotto=new int[6];
1~45사이의 난수를 6개 발생
(단, 중복되는 수가 나오면 안됨) -> 등수구하는 알고리즘 참고
lotto배열에 대입시키기

10 3 6 13 4 8
*
**
***
****
*****
*/    
  

    }

}

 

* Wrapper 클래스

 

   - 기본 자료형(int, long, float, char, double...)의 데이터 타입을 객체화할 수 클래스들
   - 첫자를 대문자로 시작하며 Boolean, Byte, Character, Double, Integer, Long...
   - int는 하나의 정수 값을 저장할 수 있다.
      Integer는 정수를 저장 할 수 있을 뿐만 아니라 그 정수를 편집할 수 있는 메소드를
     포함하고 있다.
   - 객체지향의 속성중 하나인 캡슐화를 지원한다.
   - 단순한 값의 처리는 int를 사용한다.

 

 

파일명 : WrapTest.java

 

package oop;

public class WrapTest {

    public static void main(String[] args) {

/*
 * Wrapper Class
 * 최대장점은  편집기능
 * */
/*       
        int a=10;
        System.out.println(a);
       
        Integer b=new Integer(20);
        System.out.println(b);
*/
       
        String jumin="881225-123456";
        String myyear=jumin.substring(0,2); //"88"
        int yy=Integer.parseInt(myyear);  //88
        System.out.println(1900+yy);//1900+"88"
        // 88-->"88"
        System.out.println(Integer.toString(88)+10);
       

    }

}

 

 

파일명 : WrapTest2.java

 

package oop;

public class WrapTest2 {

    public static void main(String[] args) {
             
       String jumin="101225-3123456";
      
       //jumin에서 "88" 두글자만 추출
       String y=jumin.substring(0, 2); //"88"
      
       //static 접근자는 객체를 생성하지 않고
       //클래스명을 직접 사용한다. (new연산자사용안함)
       //int myyear=(int)y; 에러
       //문자열->정수로 변환
       int myyear=Integer.parseInt(y); //88
      
       //문제)성별코드를 이용해서 1900 또는 2000을 더하시오
       //"happy".charAt(3)
       char code=jumin.charAt(7); //  '3'
       switch(code)
       {
       case '1':
       case '2': myyear=myyear+1900; break;
       case '3':
       case '4': myyear=myyear+2000; break;
       }
      
       System.out.print(myyear);

    }

}