๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ป Extracurricular/๋ฉ‹์Ÿ์ด์‚ฌ์ž์ฒ˜๋Ÿผ11๊ธฐ

[3์ฃผ์ฐจ] Java ๊ณผ์ œ (1)

Java ๋ž€ ๋ฌด์—‡์ธ๊ฐ€

์ž๋ฐ”๋Š” ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ž…๋‹ˆ๋‹ค.

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ž€, ํ”„๋กœ๊ทธ๋žจ์„ ์„ค๊ณ„ํ•˜๋Š” ๊ฐœ๋…์ด์ž ๋ฐฉ๋ฒ•๋ก ์ด๋ฉฐ ์ค„์—ฌ์„œ OOP๋ผ ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

 

๐Ÿ’ก ์ž๋ฐ”์— ๊ณต๋ถ€ํ•˜๊ธฐ ์•ž์„œ,

  • ๊ฐ์ฒด์ง€ํ–ฅํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ž€ ๋ฌด์—‡์ผ์ง€
  • ๊ฐ์ฒด์ง€ํ–ฅ๊ณผ ์ ˆ์ฐจ์ง€ํ–ฅ์˜ ์ฐจ์ด
  • ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ 4๊ฐ€์ง€ ํŠน์ง•
  • ๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„๋ฅผ ์œ„ํ•œ 5๊ฐ€์ง€ ์›์น™

๋“ฑ์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด๋ฉฐ ๊ฐ์ฒด์ง€ํ–ฅ์— ๋Œ€ํ•ด ํŒŒ์•…ํ•ด ๋ณด๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”?

 

๊ณผ์ œ๋ฅผ ์œ„ํ•ด ์•Œ์•„๋‘ฌ์•ผ ํ•  ์ง€์‹

  • ์ž๋ฐ” ์†Œ์Šค์™€ ์ปดํŒŒ์ผ
  • ๋ณ€์ˆ˜์™€ ์ž๋ฃŒํ˜•
  • ์ฃผ์„
  • ์ž๋ฃŒํ˜•
  • ์ œ์–ด๋ฌธ (์กฐ๊ฑด๋ฌธ , ๋ฐ˜๋ณต๋ฌธ)

์ž๋ฐ”๋ฅผ ๊ณต๋ถ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•Œ์•„๋‘๋ฉด ์ข‹์„ ์‚ฌ์ดํŠธ

  1. java api

Java Platform SE 7

 

Java Platform SE 7

 

docs.oracle.com

  1. ์„œ์  ๋บจ์น˜๋Š” ์˜จ๋ผ์ธ ์ •๋ฆฌ

02์žฅ ์ž๋ฐ” ์‹œ์ž‘ํ•˜๊ธฐ

 

02์žฅ ์ž๋ฐ” ์‹œ์ž‘ํ•˜๊ธฐ

02์žฅ์—์„œ๋Š” ์ž๋ฐ”์— ๋Œ€ํ•ด์„œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๊ณต๋ถ€ํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ์•Œ์•„๋‘์–ด์•ผ ํ•  ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฒƒ๋“ค์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

wikidocs.net

์ฝ”๋”ฉ๊ต์œก ํ‹ฐ์”จํ”ผ์Šค์ฟจ

๐Ÿ’ก ์œ ์šฉํ•œ ํŽธ์ง‘๊ธฐ vscode๋ฅผ ์‚ฌ์šฉํ•ด ๋ณผ๊นŒ์š”? ์œ ์šฉํ•œ ํ™•์žฅํŒฉ๋“ค์ด ๋งŽ์ด ์žˆ์–ด์š” !

๐Ÿ’ก ์›น ํด๋ผ์ด์–ธํŠธ๋Š” chrome์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

โš ๏ธ ์ œ์ถœํ•˜๋Š” ๋ชจ๋“  html ํŒŒ์ผ๋“ค์€ HTML validator ๋ฅผ ํ†ต๊ณผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


๊ณผ์ œ

ex00 ์ถœ๋ ฅ

์ œ์ถœ ๋””๋ ‰ํ† ๋ฆฌ : ex00/

์ œ์ถœ ํŒŒ์ผ : hw_1.java

HelloWorld! ๋ฅผ ์ถœ๋ ฅํ•ด๋ณด์„ธ์š”.

 

์ฝ”๋“œ

package ex00;

public class hw_1 {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

 

๊ฒฐ๊ณผ


ex01 ๋ณ€์ˆ˜

์ œ์ถœ ๋””๋ ‰ํ† ๋ฆฌ : ex01/

 

ex01-1

์ œ์ถœ ํŒŒ์ผ : hw_1.md

  • ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ์ง€์„๋•Œ ์ง€์ผœ์•ผ ํ•˜๋Š” ๊ทœ์น™์€?
  • ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ์ž๋ฐ”์˜ ํ‚ค์›Œ๋“œ์—๋Š” ๋ฌด์—‡์ด ์žˆ์„๊นŒ์š” ?
# 1. ๋ณ€์ˆ˜
## **๐Ÿ’ก ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ์ง€์„ ๋•Œ ์ง€์ผœ์•ผ ํ•˜๋Š” ๊ทœ์น™์€?**
1. ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ ์˜๋ฌธ์ž(๋Œ€์†Œ๋ฌธ์ž), ์ˆซ์ž, ์–ธ๋”์Šค์ฝ”์–ด(_), ๋‹ฌ๋Ÿฌ($)๋กœ๋งŒ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ <U>์ˆซ์ž๋กœ ์‹œ์ž‘ํ•  ์ˆ˜ ์—†์Œ</U>

3. ๋ณ€์ˆ˜์˜ ์ด๋ฆ„ ์‚ฌ์ด์—๋Š” <U>๊ณต๋ฐฑ์„ ํฌํ•จ ๋ถˆ๊ฐ€๋Šฅ</U>

4. ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์œผ๋กœ ์ž๋ฐ”์—์„œ <U>๋ฏธ๋ฆฌ ์ •์˜๋œ ํ‚ค์›Œ๋“œ(keyword)๋Š” ์‚ฌ์šฉ๋ถˆ๊ฐ€</U>
<br/> <br/>

## **๐Ÿ’ก ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ์ž๋ฐ”์˜ ํ‚ค์›Œ๋“œ์—๋Š” ๋ฌด์—‡์ด ์žˆ์„๊นŒ์š”?**
1. abstract
2. assert
3. boolean
4. int
5. char
6. String
7. while
8. finally

<br>
์ด ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์˜ˆ์•ฝ์–ด๋Š” ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜

 

 

๊ฒฐ๊ณผ

 

 

ex01-2

์ œ์ถœ ํŒŒ์ผ : hw_2.java

๋‘ ๊ฐœ์˜ intํ˜• ๋ณ€์ˆ˜ a์™€ b๋ฅผ ์„ ์–ธํ•˜๊ณ , a์— 10์„ ํ• ๋‹นํ•˜๊ณ  b์— 5๋ฅผ ํ• ๋‹นํ•œ ํ›„ a์™€ b๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex01;

public class hw_2 {
    public static void main(String[]args){
        int a = 10, b = 5;

        System.out.println("a="+a);
        System.out.println("b="+b);
    }
}

 

๊ฒฐ๊ณผ

 

 

ex01-3

์ œ์ถœ ํŒŒ์ผ : hw_3.java

Stringํ˜• ๋ณ€์ˆ˜ name์„ ์„ ์–ธํ•˜๊ณ , ๊ฐ’ "John"์„ ํ• ๋‹นํ•œ ํ›„ "Hello, John!"์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex01;

import org.w3c.dom.ls.LSOutput;

import java.sql.SQLOutput;

public class hw_3 {
    public static void main(String[] args) {
        String name = "John";
        System.out.println("Hello, "+ name+"!");
    }
}

 

 

๊ฒฐ๊ณผ


ex02 ์ž๋ฃŒํ˜•

์ œ์ถœ ๋””๋ ‰ํ† ๋ฆฌ : ex02/

 

ex02-1

์ œ์ถœ ํŒŒ์ผ : hw_1.md

  • Java์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•(primitive data type)์—๋Š” ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ํฌ๊ธฐ๋Š” ์–ผ๋งˆ์ธ๊ฐ€์š”?
# 2. ์ž๋ฃŒํ˜•
## **๐Ÿ’ก Java์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•(primitive data type)์—๋Š” ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ํฌ๊ธฐ๋Š” ์–ผ๋งˆ์ธ๊ฐ€์š”?**
<br>

### **๐Ÿ’ฅ์ •์ˆ˜ ์ž๋ฃŒํ˜•**
1. byte - 1byte
2. short - 2byte
3. int - 4byte
4. long - 8byte

### **๐Ÿ’ฅ์‹ค์ˆ˜ ์ž๋ฃŒํ˜•**
1. float - 4byte
2. double - 8byte

### **๐Ÿ’ฅ๋…ผ๋ฆฌํ˜• ์ž๋ฃŒํ˜•**
1. boolean - 1byte

### **๐Ÿ’ฅ๋ฌธ์ž ์ž๋ฃŒํ˜•**
1. char - 2byte

 

 

๊ฒฐ๊ณผ

 

 

ex02-2

์ œ์ถœ ํŒŒ์ผ : hw_2.java

  • ์ •์ˆ˜ํ˜• ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ถœ๋ ฅํ•˜์‹œ์˜ค.
  • ์‹ค์ˆ˜ํ˜• ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ถœ๋ ฅํ•˜์‹œ์˜ค.
  • ๋ฌธ์ž์—ด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ถœ๋ ฅํ•˜์‹œ์˜ค.
  • ๋…ผ๋ฆฌํ˜• ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ถœ๋ ฅํ•˜์‹œ์˜ค.

 

์ฝ”๋“œ

package ex02;

public class hw_2 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);

        float b = 1;
        System.out.println(b);

        String c = "hyunjin";
        System.out.println(c);

        boolean d = true;
        System.out.println(d);
    }
}

 

 

๊ฒฐ๊ณผ

 

ex02-3

์ œ์ถœ ํŒŒ์ผ : hw_3.java

๋‹ค์Œ์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ๋’ค,

short s1= 1, short s2= 2, short s3 = s1+s2

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ . ์ด์œ ๋ฅผ ์„œ์ˆ ํ•˜์‹œ์˜ค

 

์ฝ”๋“œ

package ex02;

public class hw_3 {
    public static void main(String[] args) {
        /*
        ๋ณ€์ˆ˜๋ฅผ short๋กœ ์„ ์–ธํ•ด๋„ ๋ง์…ˆ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•  ๋•Œ
        intํ˜•์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ  ๊ณ„์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์—
        short ํ˜•์ธ s3์— ๋„ฃ์œผ๋ ค๊ณ  ํ•˜๋ฉด overflow๊ฐ€ ๋ฐœ์ƒํ•จ.
        ๋”ฐ๋ผ์„œ s3์„ intํ˜•์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•จ.
        */
        short s1 = 1, s2 = 2;
        int s3 = s1 + s2;
    }
}

 

๊ฒฐ๊ณผ

๋ณ€์ˆ˜๋ฅผ short๋กœ ์„ ์–ธํ•ด๋„ ๋ง์…ˆ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•  ๋•Œ
intํ˜•์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ  ๊ณ„์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์—
short ํ˜•์ธ s3์— ๋„ฃ์œผ๋ ค๊ณ  ํ•˜๋ฉด overflow๊ฐ€ ๋ฐœ์ƒํ•จ.
๋”ฐ๋ผ์„œ s3์„ intํ˜•์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•จ.

ex03 ํ•จ์ˆ˜

์ œ์ถœ ๋””๋ ‰ํ† ๋ฆฌ : ex03/

 

ex03-1

์ œ์ถœ ํŒŒ์ผ : hw_1.md

  • Java์—์„œ ํ•จ์ˆ˜(function)๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ํ‚ค์›Œ๋“œ(keyword)๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”?
# 3. ํ•จ์ˆ˜
## **๐Ÿ’ก Java์—์„œ ํ•จ์ˆ˜(function)๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ํ‚ค์›Œ๋“œ(keyword)๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”?**
<br>

1. ๋ฐ˜ํ™˜ ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ<br>
-> 'function + ํ•จ์ˆ˜๋ช…'

2. ๋ฐ˜ํ™˜ ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ<br>
-> '๋ฐ˜ํ™˜ํ˜• + ํ•จ์ˆ˜๋ช…'

 

๊ฒฐ๊ณผ

 

 

ex03-2

์ œ์ถœ ํŒŒ์ผ : hw_2.java

๋‘ ๊ฐœ์˜ intํ˜• ๋งค๊ฐœ๋ณ€์ˆ˜ a์™€ b๋ฅผ ๋ฐ›์•„์„œ ๋”ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ add๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex03;
public class hw_2 {
    public static int add(int a, int b){
        return a+b;
    }

    public static void main(String[] args) {
        System.out.println(add(1,2));
    }
}

 

๊ฒฐ๊ณผ

 

 

ex03-3

์ œ์ถœ ํŒŒ์ผ : hw_3.java

doubleํ˜• ๋งค๊ฐœ๋ณ€์ˆ˜ radius๋ฅผ ๋ฐ›์•„์„œ ์›์˜ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ calcArea๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex03;

public class hw_3 {
    public static double calcArea(double radius){
        return radius*radius*3.14;
    }
    public static void main(String[] args) {
        System.out.println(calcArea(2.0));
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex03-4

์ œ์ถœ ํŒŒ์ผ : hw_4.java

intํ˜• ๋ฐฐ์—ด arr์„ ๋ฐ›์•„์„œ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ sum์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex03;

public class hw_4 {
    public static int sum(int[] arr) {
        int sum=0;
        for(int i = 0; i<arr.length; i++){
            sum += arr[i];
        }
        return sum;
    }
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9,10};
        System.out.println(sum(array));
    }
}

 

 

๊ฒฐ๊ณผ


ex04 ์—ฐ์‚ฐ์ž

์ œ์ถœ ๋””๋ ‰ํ† ๋ฆฌ : ex04/

 

ex04-1

์ œ์ถœ ํŒŒ์ผ : hw_1.md

  • Java์—์„œ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž(arithmetic operator)๋Š” ๋ฌด์—‡์ด ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ์šฐ์„ ์ˆœ์œ„๋Š” ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?
  • Java์—์„œ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž(logical operator)๋Š” ๋ฌด์—‡์ด ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ์šฐ์„ ์ˆœ์œ„๋Š” ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?
  • Java์—์„œ ๋น„๊ต ์—ฐ์‚ฐ์ž(comparison operator)๋Š” ๋ฌด์—‡์ด ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ์šฐ์„ ์ˆœ์œ„๋Š” ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?
# 4. ์—ฐ์‚ฐ์ž
## **๐Ÿ’กJava์—์„œ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž(arithmetic operator)๋Š” ๋ฌด์—‡์ด ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ์šฐ์„ ์ˆœ์œ„๋Š” ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?**

### **๐Ÿ’ฅ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž**
1. \+ (๋ง์…ˆ)
2. \- (๋บ„์…ˆ)
3. \* (๊ณฑ์…ˆ)
4. / (๋‚˜๋ˆ—์…ˆ)
5. % (๋‚˜๋จธ์ง€)

<br>

### **๐Ÿ’ฅ์šฐ์„ ์ˆœ์œ„**
1. \*, /, %
2. \+, -


## **๐Ÿ’กJava์—์„œ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž(logical operator)๋Š” ๋ฌด์—‡์ด ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ์šฐ์„ ์ˆœ์œ„๋Š” ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?**
### **๐Ÿ’ฅ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž**
1. AND(๋…ผ๋ฆฌ๊ณฑ)
- && 
- &

2. OR(๋…ผ๋ฆฌํ•ฉ)
- ||
- |

3. XOR(๋ฐฐํƒ€์  ๋…ผ๋ฆฌํ•ฉ)
- ^

4. NOT(๋…ผ๋ฆฌ ๋ถ€์ •)
- !

<br>

### **๐Ÿ’ฅ์šฐ์„ ์ˆœ์œ„**
1. !
2. &
3. ^
4. |
5. &&
6. ||

## **๐Ÿ’กJava์—์„œ ๋น„๊ต ์—ฐ์‚ฐ์ž(comparison operator)๋Š” ๋ฌด์—‡์ด ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ์šฐ์„ ์ˆœ์œ„๋Š” ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?**
### **๐Ÿ’ฅ๋น„๊ต์—ฐ์‚ฐ์ž**
1. ๋™๋“ฑ ๋น„๊ต
- == : ๋‘ ํ”ผ์—ฐ์‚ฐ์ž์˜ ๊ฐ’์ด ๊ฐ™์€๊ฐ€?
- != : ๋‘ ํ”ผ์—ฐ์‚ฐ์ž์˜ ๊ฐ’์ด ๋‹ค๋ฅธ๊ฐ€?

2. ํฌ๊ธฐ ๋น„๊ต
- \>
- \>=
- <
- <=

<br>

### **๐Ÿ’ฅ์šฐ์„ ์ˆœ์œ„**
1. ํฌ๊ธฐ ๋น„๊ต
2. ๋™๋“ฑ ๋น„๊ต

 

 

 

ex04-2

์ œ์ถœ ํŒŒ์ผ : hw_2.java

intํ˜• ๋ณ€์ˆ˜ x์™€ y๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ฐ’์„ 5์™€ 3์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ ํ›„, x์™€ y์˜ ํ•ฉ๊ณผ ์ฐจ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex04;

public class hw_2 {
    public static void main(String[] args) {
        int x=5, y=3;
        System.out.println(x+y);
        System.out.println(x-y);
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex04-3

์ œ์ถœ ํŒŒ์ผ : hw_3.java

intํ˜• ๋ณ€์ˆ˜ num1๊ณผ num2๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ฐ’์„ 15์™€ 8๋กœ ์ดˆ๊ธฐํ™” ํ•œ ํ›„ num1์„ 3์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๊ณ , num2๋ฅผ 4๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ธ์ง€ ํ™•์ธํ•˜๋Š” ์กฐ๊ฑด๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (์ดˆ๊ธฐํ™” ๊ฐ’์€ ๋˜‘๊ฐ™์ด ํ•˜์ง€ ์•Š์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค)

 

์ฝ”๋“œ

package ex04;

import java.sql.SQLOutput;

public class hw_3 {
    public static void main(String[] args) {
        int num1 = 3, num2= 4;
        if((num1 % 3 == 0) && (num2 % 4 == 0)){
            System.out.println("num1์€ 3์˜ ๋ฐฐ์ˆ˜, num2๋Š” 4์˜ ๋ฐฐ์ˆ˜์ž…๋‹ˆ๋‹ค.");
        }
    }
}

 

๊ฒฐ๊ณผ

 

 

ex04-4

์ œ์ถœ ํŒŒ์ผ : hw_4.java

intํ˜• ๋ณ€์ˆ˜ x์™€ y๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ฐ’์„ 5์™€ 3์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ ํ›„, x์™€ y์˜ ๊ณฑ๊ณผ ๋ชซ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, ๋ชซ์€ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ์ •์ˆ˜๊ฐ’์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์ฝ”๋“œ

package ex04;

public class hw_4 {
    public static void main(String[] args) {
        int x=5, y=3;
        System.out.println(x*y);
        System.out.println(x/y);
    }
}

 

๊ฒฐ๊ณผ

 

 

ex04-5

์ œ์ถœ ํŒŒ์ผ : hw_5.java

intํ˜• ๋ณ€์ˆ˜ age๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ฐ’ 25๋ฅผ ํ• ๋‹นํ•œ ํ›„ age๊ฐ€ 18๋ณด๋‹ค ํฌ๋ฉด "์„ฑ์ธ์ž…๋‹ˆ๋‹ค."๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex04;

public class hw_5 {
    public static void main(String[] args) {
        int age = 25;
        if(age > 18) System.out.println("์„ฑ์ธ์ž…๋‹ˆ๋‹ค.");
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex04-6

์ œ์ถœ ํŒŒ์ผ : hw_4.java

intํ˜• ๋ฐฐ์—ด arr์„ ์„ ์–ธํ•˜๊ณ , ๊ฐ’ {1, 2, 3, 4, 5}๋ฅผ ํ• ๋‹นํ•œ ํ›„ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex04;

public class hw_6 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        for(int i = 0;i<arr.length; i++){
            System.out.print(arr[i]+" ");
        }
    }
}

 

 

๊ฒฐ๊ณผ


ex05 ์กฐ๊ฑด๋ฌธ

์ œ์ถœ ๋””๋ ‰ํ† ๋ฆฌ : ex05/

 

ex05-1

์ œ์ถœ ํŒŒ์ผ : hw_1.java

intํ˜• ๋ณ€์ˆ˜ age๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ฐ’์ด 20 ์ด์ƒ์ธ ๊ฒฝ์šฐ "์„ฑ์ธ์ž…๋‹ˆ๋‹ค."๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex05;
import java.util.Scanner;

public class hw_1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int age;
        age = sc.nextInt();

        if(age >= 20) System.out.println("์„ฑ์ธ์ž…๋‹ˆ๋‹ค.");
    }
}

 

๊ฒฐ๊ณผ

 

 

ex05-2

์ œ์ถœ ํŒŒ์ผ : hw_2.java

intํ˜• ๋ณ€์ˆ˜ num์„ ์„ ์–ธํ•˜๊ณ , ๊ฐ’์ด ์–‘์ˆ˜์ธ ๊ฒฝ์šฐ "์–‘์ˆ˜์ž…๋‹ˆ๋‹ค.", ์Œ์ˆ˜์ธ ๊ฒฝ์šฐ "์Œ์ˆ˜์ž…๋‹ˆ๋‹ค.", 0์ธ ๊ฒฝ์šฐ "0์ž…๋‹ˆ๋‹ค."๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex05;
import java.util.Scanner;

public class hw_2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int num;
        num = sc.nextInt();

        if(num>0) System.out.println("์–‘์ˆ˜์ž…๋‹ˆ๋‹ค.");
        else if(num<0) System.out.println("์Œ์ˆ˜์ž…๋‹ˆ๋‹ค.");
        else System.out.println("0์ž…๋‹ˆ๋‹ค.");
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex05-3

์ œ์ถœ ํŒŒ์ผ : hw_3.java

intํ˜• ๋ณ€์ˆ˜ score๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ฐ’์ด 90 ์ด์ƒ์ธ ๊ฒฝ์šฐ "A", 80 ์ด์ƒ์ธ ๊ฒฝ์šฐ "B", 70 ์ด์ƒ์ธ ๊ฒฝ์šฐ "C", ๊ทธ ์™ธ์—๋Š” "D"๋ผ๋Š” ํ•™์ ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex05;

import java.util.Scanner;

public class hw_3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int score;
        System.out.print("๋‹น์‹ ์˜ ์ ์ˆ˜๋Š”? ");
        score = sc.nextInt();

        if(score >= 90) System.out.println("A");
        else if(score >= 80) System.out.println("B");
        else if(score >= 70) System.out.println("C");
        else System.out.println("D");
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex05-4

์ œ์ถœ ํŒŒ์ผ : hw_4.java

intํ˜• ๋ณ€์ˆ˜ month์™€ day๋ฅผ ์„ ์–ธํ•˜๊ณ , month๊ฐ€ 2์ด๊ณ  day๊ฐ€ 29 ์ด์ƒ์ธ ๊ฒฝ์šฐ "์œค๋…„์ž…๋‹ˆ๋‹ค.", ๊ทธ ์™ธ์—๋Š” "์œค๋…„์ด ์•„๋‹™๋‹ˆ๋‹ค."๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex05;

import java.util.Scanner;

public class hw_4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int month, day;
        System.out.print("๋‚ ์งœ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        month = sc.nextInt();
        day = sc.nextInt();

        if(month == 2 && day >= 29){
            System.out.println("์œค๋…„์ž…๋‹ˆ๋‹ค.");
        }
        else System.out.println("์œค๋…„์ด ์•„๋‹™๋‹ˆ๋‹ค.");
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex05-5

์ œ์ถœ ํŒŒ์ผ : hw_5.java

intํ˜• ๋ณ€์ˆ˜ num1๊ณผ num2๋ฅผ ์„ ์–ธํ•˜๊ณ , ๋‘ ์ˆ˜ ์ค‘ ํฐ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, ์กฐ๊ฑด๋ฌธ(if-else)์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex05;

import java.util.Scanner;

public class hw_5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int num1, num2;
        System.out.print("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        num1 = sc.nextInt();
        num2 = sc.nextInt();

        System.out.println(num1>num2? num1:num2);
    }
}

 

 

๊ฒฐ๊ณผ


ex06 ๋ฐ˜๋ณต๋ฌธ

์ œ์ถœ ๋””๋ ‰ํ† ๋ฆฌ : ex06/

 

ex06-1

์ œ์ถœ ํŒŒ์ผ : hw_1.java

1๋ถ€ํ„ฐ 10๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ while๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex06;

public class hw_1 {
    public static void main(String[] args) {
        int n = 1, sum = 0;
        while(n <= 10){
            sum += n++;
        }
        System.out.println(sum);
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex06-2

์ œ์ถœ ํŒŒ์ผ : hw_2.java

1๋ถ€ํ„ฐ 10๊นŒ์ง€์˜ ์ง์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex06;

public class hw_2 {
    public static void main(String[] args) {
        for(int i = 1; i<=10; i++){
            if(i%2==0) System.out.print(i + " ");
        }
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex06-3

์ œ์ถœ ํŒŒ์ผ : hw_3.java

๊ตฌ๊ตฌ๋‹จ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ค‘์ฒฉ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex06;

public class hw_3 {
    public static void main(String[] args) {
        for(int i = 1; i<=9; i++){
            System.out.println(i+"๋‹จ");
            for(int j = 1; j<=9; j++){
                System.out.println(i+" X "+j+" = "+i*j);
            }
            System.out.println("");
        }
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex06-4

์ œ์ถœ ํŒŒ์ผ : hw_4.java

while๋ฌธ์„ ์ด์šฉํ•˜์—ฌ 1๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ์ˆ˜ ์ค‘ 3์˜ ๋ฐฐ์ˆ˜์ด๋ฉด์„œ 4์˜ ๋ฐฐ์ˆ˜์ธ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex06;

public class hw_4 {
    public static void main(String[] args) {
        int i = 1;
        while(i<=100){
            if(i%3==0 && i%4==0) System.out.println(i);
            i++;
        }
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex06-5

์ œ์ถœ ํŒŒ์ผ : hw_5.java

๋ณ„ ์ฐ๊ธฐ๋ฅผ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ด์ค‘ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”

 

์ฝ”๋“œ

package ex06;

public class hw_5 {
    public static void main(String[] args) {
        for(int i = 1; i<=5; i++){
            for(int j=5; j>=i;j--){
                System.out.print(" ");
            }
            for(int j=0; j<i*2-1;j++){
                System.out.print("*");
            }
            System.out.println("");
        }
    }
}

 

 

๊ฒฐ๊ณผ


ex07 ์—ฐ์‚ฐ์ž์™€ ํ•จ์ˆ˜์˜ ํ™œ์šฉ

์ œ์ถœ ๋””๋ ‰ํ† ๋ฆฌ : ex07/

์ œ์ถœ ํŒŒ์ผ : hw_1.java

  1. ์ „์œ„ ์ฆ๊ฐ€ ์—ฐ์‚ฐ์ž๋ž‘ ํ›„์œ„ ์ฆ๊ฐ€ ์—ฐ์‚ฐ์ž์™€ ๊ฐ™์€ “์—ฐ์‚ฐ์ž ํ•จ์ˆ˜” ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
  2. ์‹ค์ œ ์ „์œ„, ํ›„์œ„ ์—ฐ์‚ฐ์ž๋ž‘ ๋˜‘๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
class myInteger {
	int num = 0;
	...
}

public static int preIncrease(myInteger num) {
	...
}
public static int postIncrease(myInteger num) {
	...
}
// ์‹คํ–‰ ์ฝ”๋“œ
public static void main(String[] args) {
    int a = 0;
    myInteger b = new myInteger();

    System.out.println(a++);
    System.out.println(postIncrease(b));
    System.out.println("a: " + a + " b:" + b);
    System.out.println("-----");

    System.out.println(++a);
    System.out.println(preIncrease(b));
    System.out.println("a: " + a + " b:" + b);
    System.out.println("-----");

    System.out.println(a++ + a++);
    System.out.println(postIncrease(b) + postIncrease(b));
    System.out.println("a: " + a + " b:" + b);
    System.out.println("-----");

    System.out.println(++a + ++a);
    System.out.println(preIncrease(b) + preIncrease(b));
    System.out.println("a: " + a + " b:" + b);
    System.out.println("-----");
}
// ์‹คํ–‰ ๊ฒฐ๊ณผ
0
0
a: 1 b:1
-----
2
2
a: 2 b:2
-----
5
5
a: 4 b:4
-----
11
11
a: 6 b:6
-----

 

 

์ฝ”๋“œ

package ex07;

class myInteger {
    int num = 0;

    @Override
    public String toString() {
        return Integer.toString(num);
    }
}

public class hw_1 {
    // ์ „์œ„ ์—ฐ์‚ฐ์ž
    public static int preIncrease(myInteger num) {
        num.num += 1;
        return num.num;
    }

    // ํ›„์œ„ ์—ฐ์‚ฐ์ž
    public static int postIncrease(myInteger num) {
        int copy = num.num;
        num.num += 1;
        return copy;
    }
    // ์‹คํ–‰ ์ฝ”๋“œ
    public static void main(String[] args) {
        int a = 0;
        myInteger b = new myInteger();

        System.out.println(a++);
        System.out.println(postIncrease(b));
        System.out.println("a: " + a + " b:" + b);
        System.out.println("-----");

        System.out.println(++a);
        System.out.println(preIncrease(b));
        System.out.println("a: " + a + " b:" + b);
        System.out.println("-----");

        System.out.println(a++ + a++);
        System.out.println(postIncrease(b) + postIncrease(b));
        System.out.println("a: " + a + " b:" + b);
        System.out.println("-----");

        System.out.println(++a + ++a);
        System.out.println(preIncrease(b) + preIncrease(b));
        System.out.println("a: " + a + " b:" + b);
        System.out.println("-----");
    }
}

 

 

๊ฒฐ๊ณผ


ex08 ๋ฌธ์ž์—ด

์ œ์ถœ ๋””๋ ‰ํ† ๋ฆฌ : ex08/

 

ex08-1

์ œ์ถœ ํŒŒ์ผ : hw_1.java

์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ์ „๋ถ€ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex08;
import java.util.Scanner;

public class hw_1 {
    public static String toUpper(String str) {

        return str.toUpperCase();
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        String str;
        str = scan.nextLine();

        System.out.println(toUpper(str));
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex08-2

์ œ์ถœ ํŒŒ์ผ : hw_2.java

์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ์ „๋ถ€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex08;
import java.util.Scanner;

public class hw_2 {
    public static String toLower(String str) {
        return str.toLowerCase();
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        String str;
        str = scan.nextLine();

        System.out.println(toLower(str));
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex08-3

์ œ์ถœ ํŒŒ์ผ : hw_3.java

a → b, b → c, … , z → a, … , A → B, … , Z → A ๋กœ ๋ฌธ์ž์—ด์„ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex08;

import java.util.Scanner;

public class hw_3 {
    public static char changeAlpha(char word){
        if(word == 'Z') return 'A';
        else if(word == 'z') return 'a';
        else return (char)(word+1);
    }
    public static void main(String[] args) {
        for(char i = 'a'; i <= 'z'; i++){
            System.out.print(changeAlpha(i) + " ");
        }
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex08-4

์ œ์ถœ ํŒŒ์ผ : hw_4.java

๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex08;

public class hw_4 {
    public static int toInt(String str) {
        return Integer.parseInt(str);
    }

    public static void main(String[] args) {
        String num = "123456";
        System.out.println(num);
        System.out.println(toInt(num));
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex08-5

์ œ์ถœ ํŒŒ์ผ : hw_5.java

์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex08;

public class hw_5 {
    public static String toStr(int num) {
        return Integer.toString(num);
    }

    public static void main(String[] args) {
        int num = 123456;
        System.out.println(num);
        System.out.println(toStr(num));
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex08-6

์ œ์ถœ ํŒŒ์ผ : hw_6.java

๋ฌธ์ž์—ด์„ ํŠน์ • ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ String ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex08;

public class hw_6 {
    public static String[] spaceBar(String str){
        return str.split(" ");
    }

    public static void main(String[] args) {
        System.out.println(spaceBar("Hello World!")[0]);
        System.out.println(spaceBar("Hello World!")[1]);
    }
}

 

 

๊ฒฐ๊ณผ

 

 

ex08-7

์ œ์ถœ ํŒŒ์ผ : hw_7.java

์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๋ฅผ 2์ง„์ˆ˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ฝ”๋“œ

package ex08;

import java.util.Scanner;

public class hw_7 {
    public static String toBinary(int num){
        return Integer.toBinaryString(num);
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int num;
        num = scan.nextInt();

        System.out.println(toBinary(num));
    }
}

 

 

๊ฒฐ๊ณผ


Bonus01

์ œ์ถœ ํŒŒ์ผ : bonus01.java

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž๋ฅผ ์ฐพ์•„ ๊ฐ€์žฅ ๊ธด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด๋ณด์„ธ์š”.

 

์ฝ”๋“œ

import java.util.Scanner;

public class bonus01 {
    public static int longString(String str){
        int cnt = 1, length = 1;
        for(int i = 1; i<str.length(); i++){
            if(str.charAt(i) == str.charAt(i-1)) cnt++;
            else cnt = 1;
            length = Math.max(length, cnt);
        }
        return length;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        String str;
        str = scan.nextLine();

        System.out.println(longString(str));
    }
}

 

 

๊ฒฐ๊ณผ