계속 이어서 Arithmetic Operations and Mathematical Functions, Calling Static Methods, Strings, Reading Input 에 대해 설명할께,,,

일단 Arithmetic Operations and Mathematical Functions 요놈,,,
Arithmetic Operations는 산술적인 연산자 + - * / ()설명인데,,, 수학하고 같으니 PASS!!
Mathematical Functions는
Math.sqrt(x)
Square root of x (≥0)
Math.pow(x, y)
xy (x > 0, or x = 0 and y > 0, or x < 0 and y is an integer)
Math.sin(x)
Sine of x (x in radians)
Math.cos(x)
Cosine of x
Math.tan(x)
Tangent of x
Math.asin(x)
Arc sine (sin-1x ε [-π/2, π/2], x ε [-1, 1])
Math.acos(x)
Arc cosine (cos-1x ε [0, π], x ε [-1, 1])
Math.atan(x)
Arc tangent (tan-1x ε [-π/2, π/2])
Math.atan2(y, x)
Arc tangent (tan-1y/x ε [-π, π]), x may be 0
Math.toRadians(x)
Convert x degrees to radians (i.e., returns x · π/180)
Math.toDegrees(x)
Convert x radians to degrees (i.e., returns x · 180/π)
Math.exp(x)
ex
Math.log(x)
Natural log (ln(x), x > 0)
Math.round(x)
Closest integer to x (as a long)
Math.ceil(x)
Smallest integer ≥x (as a double)
Math.floor(x)
Largest integer ≤x (as a double)
Math.abs(x)
Absolute value |x|
Math.max(x, y)
The larger of x and y
Math.min(x, y)
The smaller of x and y
                                                                            (출처 와일리,,,,,)
이거 있다고,, 그냥 그렇다고,,,

그다음은 Calling Static Methods
이건,,,,
int x=4;
double rootofx = x.sqrt();
이렇게 삽질하지 말고
double rootofx = Math.sqrt(x);
이렇게 하래,,,

Strings
String 클래스에는 substr및 각종함수들이 준비되어 있어서 문자열을 사용하기 편하다,,
라는,,,,,,,

이번엔 중요한 Reading Input!!!
C++은 인풋을 읽어오기가 쉬워서 숙제에도 가끔 인풋이 필요했지만,,,
자바는 없었지? 드디어 나왔어 인풋 받는법,,,

Scanner in = new Scanner(System.in);
이렇게 하면 인풋을 받는 스트림이 생긴거야.
그 다음엔
system.out.print("input number : ");
int quantity = in.nextInt();
이렇게 하면 input number : 뒤에 커서가 깜박인데,,, 거기에 숫자를 쓴다음 엔터를 치면quantity에 들어간다는데,,, 지금 컴을 포맷해서 아직 테스트는,,,,

그다음 in.nextInt() 대신 in.nextDouble();을 쓰면 double를 받아오고,,,in.nextLine(); 하면 한 줄(엔터까지) 받아서 string으로,,,in.next(); 까지만 해도 string으로 받는 다는데, 이건 아마 스페이스바가 구분자인가봐,,,
일단 예제,,(출처 와일리 교재 내부,,)

1 import java.util.Scanner;
2
3  /**
4    This program simulates a transaction in which a user pays for an item
5    and receives change.
6    */
7    public class CashRegisterSimulator
8    {
9       public static void main(String[] args)
10      {
11     Scanner in = new Scanner(System.in);
12
13     CashRegister register = new CashRegister();
14
15     System.out.print(“Enter price: ”);
16     double price = in.nextDouble();
17     register.recordPurchase(price);
18
19     System.out.print(“Enter dollars: ”);
20     int dollars = in.nextInt();
21     System.out.print(“Enter quarters: ”);
22     int quarters = in.nextInt();
23     System.out.print(“Enter dimes: ”);
24     int dimes = in.nextInt();
25     System.out.print(“Enter nickels: ”);
26     int nickels = in.nextInt();
27     System.out.print(“Enter pennies: ”);
28     int pennies = in.nextInt();
29     register.enterPayment(dollars, quarters, dimes, nickels, pennies);
30
31     System.out.print(“Your change: ”);
32     System.out.println(register.giveChange());
33      }
34 }

register클래스가 뭔진 몰라도 돈을 입력받을수 있는 클래스인가봐,,,
여하간 여기서 중요한건 register가 아니라 Scanner in 의 용법이니까
실행이 안되면 눈으로 라도 보고,,, 용법만이라도 익혀,,, 근데 아마 저거 다 클럽에 있던,,,
내가 블로그에 퍼놓은  헤더파일에 있는예제인듯

'대학생활 > JAVA' 카테고리의 다른 글

4주차 자바숙제 떳어요,,  (0) 2009.04.01
내맘대로 강좌 JAVA편 4.5편!!  (0) 2009.03.25
내맘대로 강좌 JAVA편 3  (0) 2009.03.25
3주차 JAVA숙제가 나왔습니다^^  (2) 2009.03.23
내맘대로 강좌 JAVA편 2  (0) 2009.03.16