POWER JAVA Exercise

2400·2023년 12월 8일

POWER_JAVA

목록 보기
3/4
post-thumbnail

CH 05

String a
static void sub2() { a = 0 }
정적 메소드는 인스턴수 변수 사용불가
100,200
이유: class point의 인스턴스를 생성했고 메소드sub를 실행하여 obj.x에 100을 obj.y에 200을 부여함(?) 이유를 뭐라해 이걸
package Point;

class Point {
    int x, y;

    public void sub(Point P) {
        P.x = 100;
        P.y = 200;
    }

    public boolean equalPoint(Point p1, Point p2) {
        return p1.x == p2.x && p1.y == p2.y;
    }
}

public class Test {
    public static void main(String[] args) {
        Point p1 = new Point();
        p1.sub(p1);

        Point p2 = new Point();
        p2.sub(p2);

        if (p1.equalPoint(p1, p2)) {
            System.out.println("동일함");
        } else {
            System.out.println("동일하지 않음");
        }
    }
}

package abfuke;

class MyMath {
    int multiply(int a, int b) {
        return a * b;
    }

    static int add(int a, int b) {
        return a + b;
    }
}

public class Test {
    public static void main(String[] args) {
        MyMath obj = new MyMath();
        System.out.println("2 * 2 = " + obj.multiply(2, 2));
        System.out.println("2 * 2 = " + obj.add(2, 2)); 
        System.out.println("2 * 3 = " + obj.multiply(2, 3));
        System.out.println("2 * 3 = " + obj.add(2, 3)); 
    }
}

MyMath.multiply가 아니라 obj.multiply 로 수정해야한다. 객체가 아닌 클래스명으로 메소드를 호출해야됨
9
javaMovie[] list = new Movie[10];
list[0] = new Movie();        
list[0].print();
for (int i = 0; i < values.length; i++) {
	values[i] = 0.0;
}
int[] a = {1, 2, 3, 4, 5};
int[] b = new int[5];
System.arraycopy(a, 0, b, 0, a.length);

CH 06

1-1. student,graduatestudent
1-2.
1-3.

package abfuke;

class Student {
    private int number;
    protected String name;

    // number 필드의 접근자 (getter)
    public int getNumber() {
        return number;
    }

    // number 필드의 설정자 (setter)
    public void setNumber(int number) {
        this.number = number;
    }

    // name 필드의 접근자 (getter)
    public String getName() {
        return name;
    }

    // name 필드의 설정자 (setter)
    public void setName(String name) {
        this.name = name;
    }
}

public class Test extends Student {
    public String lab;

    // lab 필드의 접근자 (getter)
    public String getLab() {
        return lab;
    }

    // lab 필드의 설정자 (setter)
    public void setLab(String lab) {
        this.lab = lab;
    }
}

1-4.

public Test(int number, String name, String lab) {
        // 부모 클래스인 Student의 생성자 호출
        super(number, name);
        this.lab = lab;
    }

1-5. name,lab

1-6. number,name

  1. 클래스 B 생성자

  2. private은 상속불가함

  3. 아니다. 자식 클래스의 메소드는 부모 클래스의 메소드보다 제한적일 수 없다

  4. 부모 클래스 test()
    BaseClass 라면 , 자식클래스 test()

  5. 6-1. b,c,d,e
    6-2. 2번 a는 b의 부모클래스이다

  6. class a에 기본생성자를 생성

  7. 8-1. B
    8-2. B
    8-3. super.print()

CH 07

  1. 1-1 O
    1-2 O
    1-3 O
    1-4 X
    1-5 O
    1-6 X

  2. 추상메서드인 print()를 재정의해야한다

  3. 추상메서드를 써야됨
    public interface Test {
    void myMethod(int a);
    }

  4. 인터페이스 메소드는 public으로 구현해야된다
    public void sub()

  5. class Rectangle implements calArea{
    	public void getArea(int length, int breadth);
        }
  1. class C implements A,B {
    	public int add(int a, int b) {}
        public int sub(int a, int b) {}
        }
  2. 2

CH 08

  1. 1-1. package library;
    1-2. library.Rectangle obj = new library.Rectangle();
    1-3. import library

2-1.

import java.util.Random;

public class RandomExample {
    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(101);
        System.out.println("Generated Random Number: " + randomNumber);
    }
}

2-2.

public class PhoneNumberExample {
    public static void main(String[] args) {
        String internationalNumber = "082-2-777-5566";
        String[] parts = internationalNumber.split("-");
        String countryNumber = parts[0];
        String cityCode = parts[1];
        String subscriberNumber = parts[2] + parts[3] + parts[4];
        System.out.println("국가번호: " + countryNumber);
        System.out.println("도시 식별 번호: " + cityCode);
        System.out.println("가입자 번호: " + subscriberNumber);
    }
}

2-3.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class CurrentDateExample {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        System.out.println("현재 날짜: " + formattedDate);
    }
}

2-4.

public class Test {
    public static void main(String[] args) {
        Test testObject = new Test();
        String className = testObject.getClass().getName();
        System.out.println("객체를 생성한 클래스의 이름: " + className);
    }
}

2-5.

public class SineValues {
    public static void main(String[] args) {
        for (int degree = 0; degree <= 90; degree += 5) {
            double radians = Math.toRadians(degree);
            double sineValue = Math.sin(radians);

            System.out.println("각도: " + degree + "도, 사인값: " + sineValue);
        }
    }
}
  1. ==은실제값을 가르키지 않는다. 실제값을 가르키려면 equals()를 사용해야한다. s1.equals(s2)

  2. int obj = 42;
    Integer obj2 = Integer.valueOf(obj);
    Integer obj2 = 42;
    int obj = obj2.intValue(); 
  1. 기초자료형을 객체로 다뤄야 할 때가 있기때문에

  2. 6-1. 범위를 벗어난 인덱스에 접근하게 되어 ArrayIndexOutOfBoundsException이 발생

6-2.

public class Test {
    public static void main(String[] args) {
        try {
            sub();
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("배열 인덱스 예외가 발생했습니다: " + e.getMessage());
        }
    }

    public static void sub() {
        int[] array = new int[10];

        try {
            int i = array[10];
            System.out.println("인덱스 10의 값: " + i);
        } catch (ArrayIndexOutOfBoundsException e) {
            throw e;
        }
    }
}

6-3.

public class Test {
    public static void main(String[] args) throws ArrayIndexOutOfBoundsException {
        sub();
    }

    public static void sub() throws ArrayIndexOutOfBoundsException {
        int[] array = new int[10];
        
        int i = array[10];
        System.out.println("인덱스 10의 값: " + i);
    }
}
  • Exception은 모든 오류를 처리하기에 모든 오류를 예외처리기로 잡을 수 있다.
  • 올바른 예외 처리 구문은 가장 구체적인 예외부터 가장 일반적인 예외 순으로 나열하여야 한다.
    그렇기에 ArithmeticException을 먼저 처리하고, 그 다음에 일반적인 Exception을 처리한다.
숫자형식오류
finally
profile
시즌 2의 공부기록 - Artificial Intelligence & AeroSpace

0개의 댓글