๐Ÿ“š Java ์—ฐ์Šต๋ฌธ์ œ ๋ชจ์Œ

์•™์ง€๋™ยท2025๋…„ 2์›” 27์ผ
1

JAVA

๋ชฉ๋ก ๋ณด๊ธฐ
1/6
post-thumbnail

๐Ÿ”น 1. ๋ฌธ์ž์—ด ๋น„๊ต ์—ฐ์Šต๋ฌธ์ œ

โœ” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋‘ ๋ฌธ์ž์—ด์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•˜์„ธ์š”.

import java.util.Scanner;

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

        System.out.print("์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š”: ");
        String str1 = scanner.nextLine();

        System.out.print("๋‘ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š”: ");
        String str2 = scanner.nextLine();

        boolean result = str1.equals(str2);
        System.out.println("๋‘ ๋ฌธ์ž์—ด์ด ๊ฐ™์€๊ฐ€์š”? " + result);
    }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ

์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š”: hello
๋‘ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š”: world
๋‘ ๋ฌธ์ž์—ด์ด ๊ฐ™์€๊ฐ€์š”? false

๐Ÿ”น 2. ๋น„๊ต ์—ฐ์‚ฐ ์—ฐ์Šต๋ฌธ์ œ

โœ” ๋‘ ์ˆซ์ž์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.


public class Main {
    public static void main(String[] args) {
        int x = 10;
        int y = 20;

        System.out.println("x๊ฐ€ y๋ณด๋‹ค ํฐ๊ฐ€? " + (x > y));
        System.out.println("x๊ฐ€ y๋ณด๋‹ค ์ž‘์€๊ฐ€? " + (x < y));
        System.out.println("x์™€ y๊ฐ€ ๊ฐ™์€๊ฐ€? " + (x == y));
        System.out.println("x์™€ y๊ฐ€ ๋‹ค๋ฅธ๊ฐ€? " + (x != y));
    }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ

x๊ฐ€ y๋ณด๋‹ค ์ž‘์€๊ฐ€? true
x์™€ y๊ฐ€ ๊ฐ™์€๊ฐ€? false
x์™€ y๊ฐ€ ๋‹ค๋ฅธ๊ฐ€? true

๐Ÿ”น 3. for ๋ฐ˜๋ณต๋ฌธ & break ์—ฐ์Šต๋ฌธ์ œ

โœ” ์†๋‹˜ 5๋ช…์„ ๋งž์ดํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, 4๋ฒˆ์งธ ์†๋‹˜์—์„œ ์ค‘๋‹จํ•˜์„ธ์š”.

public class Robot {
    public static void main(String[] args) {
        int customers = 5;


        for (int i = 1; i <= customers; i++) {
            if (i == 4) {
                System.out.println("4๋ฒˆ์งธ ์†๋‹˜ ๋„์ฐฉ! ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ.");
                break;
            }
            System.out.println(i + "๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!");
        }
    }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ

1๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!
2๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!
3๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!
4๋ฒˆ์งธ ์†๋‹˜ ๋„์ฐฉ! ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ.

๐Ÿ”น 4. ๊ตฌ๊ตฌ๋‹จ ์ถœ๋ ฅ ์—ฐ์Šต๋ฌธ์ œ

โœ” ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ˆซ์ž์˜ ๊ตฌ๊ตฌ๋‹จ์„ ์ถœ๋ ฅํ•˜์„ธ์š”.

import java.util.Scanner;

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

        System.out.print("์ถœ๋ ฅํ•  ๊ตฌ๊ตฌ๋‹จ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” (2~9): ");
        int dan = scanner.nextInt();

        if (dan < 2 || dan > 9) {
            System.out.println("โš ๏ธ 2์—์„œ 9 ์‚ฌ์ด์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”!");
        } else {
            System.out.println("\n==== " + dan + "๋‹จ ====");
            for (int i = 1; i <= 9; i++) {
                System.out.println(dan + " x " + i + " = " + (dan * i));
            }
        }
    }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ


์ถœ๋ ฅํ•  ๊ตฌ๊ตฌ๋‹จ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” (2~9): 3

==== 3๋‹จ ====
3 x 1 = 3
3 x 2 = 6
...
3 x 9 = 27

๐Ÿ”น 5. ๊ฐ์ฒด ์ƒ์„ฑ ์—ฐ์Šต๋ฌธ์ œ

โœ” "์ž๋™์ฐจ(Car)" ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ž๋™์ฐจ๊ฐ€ ์ฃผํ–‰ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜์„ธ์š”.

package chapter2.clazz;

public class Car {
    String name;
    String carNumber;

    public Car(String name, String carNumber) {
        this.name = name;
        this.carNumber = carNumber;
    }

    public void drive() {
        System.out.println(name + " (" + carNumber + ") ์ฃผํ–‰ ์ค‘!");
    }
}
package chapter2.clazz;

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Tesla Model Y", "1234-ABCD");
        myCar.drive();
    }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ

Tesla Model Y (1234-ABCD) ์ฃผํ–‰ ์ค‘!

๐Ÿ”น 6. ๋ž˜ํผํด๋ž˜์Šค ๊ธฐ๋ณธ์„ฑ๋Šฅ ๋น„๊ต

public class MyDouble {
    private double value;

    public MyDouble(double value) {
        this.value = value;
    }

    public MyDouble add(MyDouble other) {
        return new MyDouble(this.value + other.value);
    }

    public double getValue() {
        return value;
    }
}

public class DoubleWrapperPerformance {
    public static void main(String[] args) {
        int iteration = 10_000_000; // 1000๋งŒ ๋ฒˆ ๋ฐ˜๋ณต

        // 1. ๊ธฐ๋ณธํ˜• double ์—ฐ์‚ฐ
        long startTime1 = System.nanoTime();
        double sum1 = 0.0;
        for (int i = 0; i < iteration; i++) {
            sum1 += i * 1.1;
        }
        long endTime1 = System.nanoTime();
        long primitiveTime = endTime1 - startTime1;

        // 2. MyDouble ์—ฐ์‚ฐ
        long startTime3 = System.nanoTime();
        MyDouble sum3 = new MyDouble(0.0);
        for (int i = 0; i < iteration; i++) {
            sum3 = sum3.add(new MyDouble(i * 1.1));
        }
        long endTime3 = System.nanoTime();
        long myDoubleTime = endTime3 - startTime3;

        // ๊ฒฐ๊ณผ ์ถœ๋ ฅ
        System.out.println("๊ธฐ๋ณธํ˜•(double) ์—ฐ์‚ฐ ์‹œ๊ฐ„: " + primitiveTime + " ns");
        System.out.println("MyDouble ํด๋ž˜์Šค ์—ฐ์‚ฐ ์‹œ๊ฐ„: " + myDoubleTime + " ns");
        System.out.println("MyDouble vs double ์„ฑ๋Šฅ ์ฐจ์ด: " + (double) myDoubleTime / primitiveTime);
    }
}

class MyDouble {
    private double value;

    public MyDouble(double value) {
        this.value = value;
    }

    public MyDouble add(MyDouble other) {
        return new MyDouble(this.value + other.value);
    }

    public double getValue() {
        return value;
    }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ

๊ธฐ๋ณธํ˜•(double) ์—ฐ์‚ฐ ์‹œ๊ฐ„: 650000000 ns
MyDouble ํด๋ž˜์Šค ์—ฐ์‚ฐ ์‹œ๊ฐ„: 1800000000 ns
MyDouble vs double ์„ฑ๋Šฅ ์ฐจ์ด: 2.769230769230769

๐Ÿ”น 7. ์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋Šฅ์ •์˜

โœ” ๋‹ค์–‘ํ•œ ๊ฐ€์ „์ œํ’ˆ์„ ์ผ๊ด€๋œ ๋ฐฉ์‹์œผ๋กœ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์„ค๊ณ„ํ•˜์„ธ์š”.

// ๊ฐ€์ „์ œํ’ˆ ์ •์˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค
interface ElectronicDevice {
    void turnOn();  // ์ „์› ์ผœ๊ธฐ ๊ธฐ๋Šฅ
    void turnOff(); // ์ „์› ๋„๊ธฐ ๊ธฐ๋Šฅ
}


//  TV ํด๋ž˜์Šค๊ตฌํ˜„
class TV implements ElectronicDevice {

    @Override
    void turnOn() {
        System.out.println("TV ์ „์›์ด ์ผœ์กŒ์Šต๋‹ˆ๋‹ค.");
    }

    @Override
    void turnOff() {
        System.out.println("TV ์ „์›์ด ๊บผ์กŒ์Šต๋‹ˆ๋‹ค.");
    }
    
    // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ 
    void changeChannel() {
        System.out.println("์ฑ„๋„์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.");
    }
}

// ์—์–ด์ปจ ํด๋ž˜์Šค (ElectronicDevice ๊ตฌํ˜„)
class AirConditioner implements ElectronicDevice {

    @Override
    void turnOn() {
        System.out.println("์—์–ด์ปจ์ด ๊ฐ€๋™๋ฉ๋‹ˆ๋‹ค.");
    }

    @Override
    void turnOff() {
        System.out.println("์—์–ด์ปจ์ด ๊บผ์กŒ์Šต๋‹ˆ๋‹ค.");
    }
    
    // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ (์ธํ„ฐํŽ˜์ด์Šค์—๋Š” ์—†๋Š” ๊ธฐ๋Šฅ)
    void setTemperature() {
        System.out.println("์˜จ๋„๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.");
    }
}

// ์„ธํƒ๊ธฐ ํด๋ž˜์Šค (ElectronicDevice ๊ตฌํ˜„)
class WashingMachine implements ElectronicDevice {

    @Override
    void turnOn() {
        System.out.println("์„ธํƒ๊ธฐ๊ฐ€ ์ž‘๋™์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.");
    }

    @Override
    void turnOff() {
        System.out.println("์„ธํƒ๊ธฐ๊ฐ€ ์ž‘๋™์„ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.");
    }
    
    // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ (์ธํ„ฐํŽ˜์ด์Šค์—๋Š” ์—†๋Š” ๊ธฐ๋Šฅ)
    void setTime() {
        System.out.println("์„ธํƒ ์‹œ๊ฐ„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.");
    }
}
public class Main {
    public static void main(String[] args) {
        // โœ… ๊ฐ€์ „์ œํ’ˆ ๊ฐ์ฒด ์ƒ์„ฑ
        TV tv = new TV();
        AirConditioner airConditioner = new AirConditioner();
        WashingMachine washingMachine = new WashingMachine();

        // โœ… ๊ฐœ๋ณ„์ ์œผ๋กœ ์ „์› ์ผœ๊ธฐ
        System.out.println(" ๊ฐ€์ „์ œํ’ˆ์„ ์ผญ๋‹ˆ๋‹ค.");
        tv.turnOn();
        airConditioner.turnOn();
        washingMachine.turnOn();

        System.out.println(); // ์ค„๋ฐ”๊ฟˆ

        // โœ… ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ์‚ฌ์šฉ
        tv.changeChannel();
        airConditioner.setTemperature();
        washingMachine.setTime();

        System.out.println(); // ์ค„๋ฐ”๊ฟˆ

        // โœ… ๊ฐœ๋ณ„์ ์œผ๋กœ ์ „์› ๋„๊ธฐ
        System.out.println(" ๊ฐ€์ „์ œํ’ˆ์„ ๋•๋‹ˆ๋‹ค.");
        tv.turnOff();
        airConditioner.turnOff();
        washingMachine.turnOff();
    }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ

๊ฐ€์ „์ œํ’ˆ์„ ์ผญ๋‹ˆ๋‹ค.
TV ์ „์›์ด ์ผœ์กŒ์Šต๋‹ˆ๋‹ค.
์—์–ด์ปจ์ด ๊ฐ€๋™๋ฉ๋‹ˆ๋‹ค.
์„ธํƒ๊ธฐ๊ฐ€ ์ž‘๋™์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

์ฑ„๋„์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
์˜จ๋„๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
์„ธํƒ ์‹œ๊ฐ„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ€์ „์ œํ’ˆ์„ ๋•๋‹ˆ๋‹ค.
TV ์ „์›์ด ๊บผ์กŒ์Šต๋‹ˆ๋‹ค.
์—์–ด์ปจ์ด ๊บผ์กŒ์Šต๋‹ˆ๋‹ค.
์„ธํƒ๊ธฐ๊ฐ€ ์ž‘๋™์„ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.

๐Ÿ”น 8.๊ฐ์ฒด์˜ ๋‹คํ˜•์„ฑ ๊ตฌํ˜„

โœ” ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•ด์„œ ์œ„ ๋‚ด์šฉ์˜ 3๋‹จ๊ณ„ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ์„ค๊ณ„ํ•˜์„ธ์š”.

// LifeForm ์ธํ„ฐํŽ˜์ด์Šค
package chapter2.polymorphism;

public interface LifeForm {
    void exist();
}

// Animal ์ธํ„ฐํŽ˜์ด์Šค LifeForm ์ƒ์†
package chapter2.polymorphism;

public interface Animal extends LifeForm {
    void makeSound();
}

// Cat ํด๋ž˜์Šค 
package chapter2.polymorphism;

public class Cat implements Animal {

    @Override
    public void exist() {
        System.out.println("๊ณ ์–‘์ด๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค");
    }

    @Override
    public void makeSound() {
        System.out.println("์•ผ์˜น");
    }

    public void scratch() {
        System.out.println("์Šคํฌ๋ ˆ์น˜");
    }
}

// Dog ํด๋ž˜์Šค
package chapter2.polymorphism;

public class Dog implements Animal {

    @Override
    public void exist() {
        System.out.println("๊ฐ•์•„์ง€๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค");
    }

    @Override
    public void makeSound() {
        System.out.println("๋ฉ๋ฉ");
    }

    public void wag() {
        System.out.println("ํ”๋“คํ”๋“ค");
    }
}

// Main ํด๋ž˜์Šค
package chapter2.polymorphism;

public class Main {
    public static void main(String[] args) {
        // โœ… ๋‹ค์–‘์„ฑ ํ™œ์šฉ
        Animal animal1 = new Cat();
        Animal animal2 = new Dog();

        animal1.exist();
        animal1.makeSound();

        animal2.exist();
        animal2.makeSound();

        // โœ… ๋‹ค์šด์บ์ŠคํŒ…
        Cat cat = (Cat) animal1;
        cat.scratch();

        Dog dog = (Dog) animal2;
        dog.wag();

        // โœ… ๋‹ค์šด์บ์ŠคํŒ… instanceof ํ™œ์šฉ
        if (animal2 instanceof Cat) {
            Cat cat2 = (Cat) animal2;
            cat2.scratch();
        } else {
            System.out.println("๊ฐ์ฒด๊ฐ€ ๊ณ ์–‘์ด๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค");
        }
    }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ

๊ณ ์–‘์ด๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค
์•ผ์˜น
๊ฐ•์•„์ง€๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค
๋ฉ๋ฉ
์Šคํฌ๋ ˆ์น˜
ํ”๋“คํ”๋“ค
๊ฐ์ฒด๊ฐ€ ๊ณ ์–‘์ด๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค

๐Ÿ”น 9. ArrayList ์—ฐ์Šต๋ฌธ์ œ

โœ” ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๊ตฌํ˜„ํ•˜๊ธฐ

  • ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ƒํ’ˆ: ์–‘ํŒŒ, ์‚ฌ๊ณผ, ์ƒ์„ , ๋‘๋ถ€
  • ๊ธฐ๋Šฅ1: ์ƒํ’ˆ์ถ”๊ฐ€(addProduct)
  • ๊ธฐ๋Šฅ2: ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก ์ถœ๋ ฅ(printCart)
  • ๊ธฐ๋Šฅ3: ์ƒํ’ˆ ์‚ญ์ œ(removeProduct)
  • ๊ธฐ๋Šฅ4: ์ด ๊ฐ€๊ฒฉ ๊ณ„์‚ฐ(calculateTotalPrice )
public class Product {
    // ์†์„ฑ
    private String name;
    private int price;

    // ์ƒ์„ฑ์ž
    public Product(String name, int price) {
        this.name = name;
        this.price = price;
    }

    // ๊ธฐ๋Šฅ
    public String getName() {
        return this.name;
    }

    public int getPrice() {
        return this.price;
    }
}
import java.util.ArrayList;
import java.util.List;

public class Cart {

    // ์†์„ฑ
    private List<Product> cart = new ArrayList<>();

    // ์ƒ์„ฑ์ž

    // ๊ธฐ๋Šฅ
    // ์ƒํ’ˆ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ
    public  void addProduct(Product product) {
        cart.add(product);
        System.out.println(product.getName() + " ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    }

    // ์ƒํ’ˆ ์ œ๊ฑฐ ๊ธฐ๋Šฅ
    public void removeProduct(String removeProductName) {
        boolean removed = false;

        for (Product product : cart) {
            String foundProductName = product.getName();
            if (foundProductName.equals(removeProductName)) {
                cart.remove(product);
                System.out.println(product.getName() + "๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์—์„œ ์ œ๊ฑฐ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                break;
            }
        }
        if (!removed) {
            System.out.println("ํ•ด๋‹น ์ƒํ’ˆ์ด ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์—†์Šต๋‹ˆ๋‹ค.");
        }
    }

    // ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก ์ถœ๋ ฅ ๊ธฐ๋Šฅ
    public void printCart() {
        if (cart.isEmpty()) {
            System.out.println("์žฅ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ๋น„์–ด ์žˆ์Šต๋‹ˆ๋‹ค.");
        } else {
            for (Product product : cart) {
                System.out.println(product.getName() + ": " + product.getPrice());
            }
        }
    }

    // ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ด ์ƒํ’ˆ ๊ฐ€๊ฒฉ ์กฐํšŒ ๊ธฐ๋Šฅ
    public void calculateTotalPrice() {
        int total = 0;
        for (Product product : cart) {
            total += product.getPrice();
        }
        System.out.println("์ด ๊ธˆ์•ก์€: " + total);
    }
}
public class Main {

    public static void main(String[] args) {

        Cart cart = new Cart();
        Product onion = new Product("์–‘ํŒŒ", 3000);
        Product apple = new Product("์‚ฌ๊ณผ", 10000);
        Product fish = new Product("์ƒ์„ ", 12000);
        Product tofu = new Product("๋‘๋ถ€", 2000);


        // ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ƒํ’ˆ ์ถ”๊ฐ€
        System.out.println("์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ƒํ’ˆ ์ถ”๊ฐ€: ");
        cart.addProduct(onion);
        cart.addProduct(apple);
        cart.addProduct(fish);
        cart.addProduct(tofu);
        System.out.println();

        //  ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ƒํ’ˆ ์กฐํšŒ
        System.out.println("์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ƒํ’ˆ ์กฐํšŒ: ");
        cart.printCart();
        System.out.println();

        // ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ด ๊ธˆ์•ก ์กฐํšŒ
        System.out.println("์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ด ๊ธˆ์•ก ์กฐํšŒ: ");
        cart.calculateTotalPrice();
        System.out.println();

        // ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ƒํ’ˆ ์ œ๊ฑฐ(์‚ฌ๊ณผ)
        System.out.println("์žฅ๋ฐ”๊ตฌ๋‹ˆ์—์„œ ์‚ฌ๊ณผ ์ œ๊ฑฐ: ");
        cart.removeProduct("์‚ฌ๊ณผ");
        System.out.println();

        //  ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ƒํ’ˆ ์กฐํšŒ
        System.out.println("์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ƒํ’ˆ ์กฐํšŒ: ");
        cart.printCart();
        System.out.println();

        // ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ด ๊ธˆ์•ก ์กฐํšŒ
        System.out.println("์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ด ๊ธˆ์•ก ์กฐํšŒ: ");
        cart.calculateTotalPrice();
        System.out.println();
    }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ

์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ƒํ’ˆ ์ถ”๊ฐ€: 
์–‘ํŒŒ ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
์‚ฌ๊ณผ ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
์ƒ์„  ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๋‘๋ถ€ ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ƒํ’ˆ ์กฐํšŒ: 
์–‘ํŒŒ: 3000
์‚ฌ๊ณผ: 10000
์ƒ์„ : 12000
๋‘๋ถ€: 2000

์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ด ๊ธˆ์•ก ์กฐํšŒ: 
์ด ๊ธˆ์•ก์€: 27000

์žฅ๋ฐ”๊ตฌ๋‹ˆ์—์„œ ์‚ฌ๊ณผ ์ œ๊ฑฐ: 
์‚ฌ๊ณผ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์—์„œ ์ œ๊ฑฐ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ƒํ’ˆ ์กฐํšŒ: 
์–‘ํŒŒ: 3000
์ƒ์„ : 12000
๋‘๋ถ€: 2000

์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ด ๊ธˆ์•ก ์กฐํšŒ: 
์ด ๊ธˆ์•ก์€: 15000

๐Ÿ”น 10. ์ •์ˆ˜ ์—ฐ์Šต๋ฌธ์ œ(+)

โœ”์ •์ˆ˜ num1๊ณผ num2๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, num1๊ณผ num2์˜ ํ•ฉ์„ returnํ•˜๋„๋ก soltuion ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

class Solution {
    public int solution(int num1, int num2) {   
        return num1 + num2; 
    }
}

class Main { 
    public static void main(String[] args) { 
        Solution sol = new Solution();
        System.out.println(sol.solution(2,3)); 
        System.out.println(sol.solution(100,2)); 
    }
}

๐Ÿ”น 11. ์ •์ˆ˜ ์—ฐ์Šต๋ฌธ์ œ(-)

โœ” ์ •์ˆ˜ num1๊ณผ num2๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, num1์—์„œ num2๋ฅผ ๋บ€ ๊ฐ’์„ returnํ•˜๋„๋ก soltuion ํ•จ์ˆ˜๋ฅผ ์™„์„ฑ ํ•˜๊ธฐ

class Solution {
    public int solution(int num1, int num2) {  
        return num1 - num2; // ๋‘ ์ •์ˆ˜๋ฅผ ๋ฐ›์•„ ๋นผ๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
    }
}

class main {
      public static void main(String[] args) {
        Solution sol = new Solution();  // ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
             System.out.println(sol.solution(2, 3)); //๋ฉ”์„œ๋“œ(solution)๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ ๊ฐ’ ์ถœ๋ ฅ
        System.out.println(sol.solution(100, 2)); 
 
      }
}

๐Ÿ“Œ ์‹คํ–‰ ๊ฒฐ๊ณผ

num1์ด 2์ด๊ณ  num2๊ฐ€ 3์ด๋ฏ€๋กœ 2 - 3 = -1์„ returnํ•ฉ๋‹ˆ๋‹ค.

num1์ด 100์ด๊ณ  num2๊ฐ€ 2์ด๋ฏ€๋กœ 100 - 2 = 98์„ returnํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”น 12. ์ •์ˆ˜ ์—ฐ์Šต๋ฌธ์ œ(*)

โœ” ์ •์ˆ˜ num1, num2๊ฐ€ ์ฃผ์–ด์งˆ๋•Œ num1๊ณผ num2๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ returnํ•˜๋„๋ก soltuion ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜๊ธฐ
โœ” static ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ์ƒ์„ฑ
โœ” void ๋ฐ˜ํ™˜๊ฐ’์ด ์—†์Œ (return ๊ฐ’์ด ํ•„์š” ์—†์Œ)

class Solution {
    public int solution(int num1, int num2) {
        return num1 * num2; // ๋‘ ์ •์ˆ˜๋ฅผ ๋ฐ›์•„ ๊ณฑํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
    }
}
class main {
      public static void main(String[] args) { // 
        Solution sol = new Solution();    // ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
             System.out.println(sol.solution(3, 4));  //๋ฉ”์„œ๋“œ(solution)๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ ๊ฐ’ ์ถœ๋ ฅ
        System.out.println(sol.solution(27, 19));
 
      }
}

๐Ÿ”น 13. ์ •์ˆ˜ ์—ฐ์Šต๋ฌธ์ œ(/)

โœ” ์ •์ˆ˜ num1, num2๊ฐ€ ์ฃผ์–ด์งˆ๋•Œ num1๊ณผ num2๋ฅผ ๋‚˜๋ˆˆ ๊ฐ’์„ returnํ•˜๋„๋ก soltuion ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜๊ธฐ

class Solution {
    public int solution(int num1, int num2) { 
        return num1 / num2; // ์ •์ˆ˜ ๋‚˜๋ˆ—์…ˆ 
    }
}

class Main { 
    public static void main(String[] args) { 
        Solution sol = new Solution(); // ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ
        System.out.println(sol.solution(10, 5));//๋ฉ”์„œ๋“œ(solution)๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ ๊ฐ’ 
        System.out.println(sol.solution(7, 2))
    }
}

๐Ÿ”น 14. ์ฆ๊ฐ€ return

โœ” ๋จธ์“ฑ์ด๋Š” ์„ ์ƒ๋‹˜์ด ๋ช‡ ๋…„๋„์— ํƒœ์–ด๋‚ฌ๋Š”์ง€ ๊ถ๊ธˆํ•ด์กŒ์Šต๋‹ˆ๋‹ค. 2022๋…„ ๊ธฐ์ค€ ์„ ์ƒ๋‹˜์˜ ๋‚˜์ด age๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์„ ์ƒ๋‹˜์˜ ์ถœ์ƒ ์—ฐ๋„๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”
โœ” 2022๋…„ ํƒœ์–ด๋‚œ ์ˆœ๊ฐ„๋ถ€ํ„ฐ 1์‚ด์ด๋ฏ€๋กœ +1

class Solution {
    public int solution(int age) {
         return 2022 - age + 1; // 2022๋…„ ๊ธฐ์ค€ ์œผ๋กœ  (ํ˜„์žฌ ์—ฐ๋„ - ๋‚˜์ด) + 1
}
}

class Main { 
    public static void main(String[] args) { 
        Solution sol = new Solution(); // ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
        System.out.println(sol.solution(40));  //๋ฉ”์„œ๋“œ(solution)๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ ๊ฐ’ 
        System.out.println(sol.solution(25)); 
    }
}

๐Ÿ”น 15.๋น„๊ต์—ฐ์‚ฐ

โœ” ์ •์ˆ˜ num1๊ณผ num2๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋‘ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด 1 ๋‹ค๋ฅด๋ฉด -1์„ retrunํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
โœ” if ๋งŒ์•ฝ์— ~ํ•˜๋ฉด ๊ฐ™์€ ์กฐ๊ฑด์œผ๋กœ ํ™•์ธ
โœ” == ๋‘์ˆ˜๊ฐ€ ๊ฐ™์€์ง€

class Solution {
    public int solution(int num1, int num2) { 
        if (num1 == num2) { // num1๊ณผ num2๊ฐ€ ๊ฐ™์œผ๋ฉด
            return 1;  //1์„ ๋ฐ˜ํ™˜
        } else {  //๊ฐ™์ง€์•Š์œผ๋ฉด 
            return -1; //-1์„ ๋ฐ˜ํ™˜
        }
    }
}

๐Ÿ”น 16.๋‘์ˆ˜์˜ ๋‚˜๋ˆ—์…ˆ

โœ” ์ •์ˆ˜ num1๊ณผ num2๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, num1์„ num2๋กœ ๋‚˜๋ˆˆ ๊ฐ’์— 1,000์„ ๊ณฑํ•œ ํ›„ ์ •์ˆ˜ ๋ถ€๋ถ„์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
โœ” ์‹ค์ˆ˜ ๋‚˜๋ˆ—์…ˆ ๋จผ์ €์ˆ˜ํ–‰ํ›„ 1000์„ ๊ณฑํ•จ
โœ” ๊ตฌํ•œ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ฐ˜ํ™˜

class Solution {
    public int solution(int num1, int num2) { 
        return (int) (((double) num1 / num2) * 1000); // ์‹ค์ˆ˜ ๋‚˜๋ˆ—์…ˆ์„ ((๋จผ์ €์ˆ˜ํ–‰ 1000์„ ๊ณฑํ•ด์„œ ๊ฐ’ ๊ตฌํ•˜๊ธฐ, ๊ตฌํ•œ๊ฐ’์„ ์ •์ˆ˜๊ฐ’ ๋ฐ˜ํ™˜
    }
}

class Main { 
    public static void main(String[] args) { 
        Solution sol = new Solution();  // ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
        System.out.println(sol.solution(3, 2)); ////๋ฉ”์„œ๋“œ(solution)๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ ๊ฐ’ 
        System.out.println(sol.solution(7,3));
        System.out.println(sol.solution(1,16));
    }
}
profile
์šฐ๋‹นํƒ•๊ฐœ๋ฐœ์ผ์ง€

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด