
Book package Prac08; public class Book { public String title; public String author; public String publisher; public int price; public Book() { } public Book(String title, String author, String publisher, int price) { this.title = title; this.author = author; this.publisher = publisher; this.price = price; } public void setTitle(String title) { this.title = title; } public void setAuthor(String a..

오버로딩이란? 메서드도 변수와 마찬가지로 같은 클래스 내에서 서로 구별될 수 있어야 하기 때문에 각기 다른 이름을 가져야 한다. 자바에서는 한 클래스 내에 이미 사용하려는 이름과 같은 이름을 가진 메서드가 있더라도 매개변수의 개수 또는 타입이 다르면, 같은 이름을 사용해서 메서드를 정의할 수 있다. 한 클래스 내에 같은 이름의 메서드를 여러개 정의하는 것을 ‘오버로딩’ 이라고 한다. 오버로딩의 조건 메서드 이름이 같아야 한다. 매개변수의 개수 또는 타입이 달라야 한다. 참고) 오버로딩된 메서드들은 매개변수에 의해서만 구별될 수 있으므로 반환 타입은 오버로딩을 구현하는데 아무런 영향을 주지 못한다. 오버라이딩(overriding) 부모(조상)클래스로부터 상속받은 메서드의 내용을 변경하는 것을 오버라이딩이라..

오버로딩, 오버라이딩, 다형성 public class MathTest { public int add (int a, int b) { System.out.println("int add(int a, int b) - " ); return a+b; } public int add (int a, int b, int c) { System.out.println("int add(int a, int b, int c) - " ); return a+b+c; } public long add (long a, int b) { System.out.println("int add(long a, int b) - " ); return a+b; } public long add (long a, long b) { System.out.println..
Getter / Setter / 상속 개념설명 getter/setter 클래스에는 개인정보와 같이 민감하고 중요한 정보가 많이 있기 때문에 함부로 접근하고 값을 변경 하지 않도록 최소한의 허들을 두어야 하는데요. 지난시간에 배웠던 접근제어자 를 통해 해당 클래스의 접근 범위를 설정할 수 있었습니다. 일반적으로 멤버변수는 private로 설정이 되고, 해당 클래스의 인스턴스 생성시 값을 변경하거나 가져올때에는 클래스의 해당메서드를 통해서만 그 기능을 할 수 있도록 하고 있습니다. 따라서 접근제어자가 private 인 경우, 어떻게 다른 클래스에서 인스턴스를 생성하고 객체의 값을 넣고 가져오는지 getter/ setter 문법을 복습해 보도록 하는 시간입니다. // setter // 메서드 (public) ..
public class Practice { public static void main(String[] args) { boolean run = true; int studentNum = 0; int[] scores = null; Scanner sc = new Scanner(System.in); while (run) { System.out.println("--------------------------------------------------------"); System.out.println(" 1. 학생수 | 2. 점수입력 | 3. 점수리스트 |4. 분석 | 5. 종료"); System.out.println("------------------------------------------------------..

풀이 코드 public class quiz { public static void main(String[] args) { Random random = new Random(); // int num = (int) (Math.random() * 100); int num = random.nextInt(100)+1; // 100개의 숫자를 임의로 발생시킨다. Scanner scanner = new Scanner(System.in); int count = 0; // 총 횟수가 필요하므로 초기화 while (true) { // 숫자를 맞출때까지 프로그램을 무한반복한다. 그래서 while을 true로 적었다. System.out.println("숫자를 입력하세요."); // cpu가 생성한 Random숫자 콘솔에서 확인..

풀이 코드 public class quiz { public static void main(String[] args) { Random random = new Random(); int cpu = random.nextInt(3)+1; // System.out.println("CPU: " + cpu); Scanner sc = new Scanner(System.in); System.out.println("가위(1), 바위(2), 보(3): "); int user = sc.nextInt(); if(cpu == 1 && user == 2 || cpu ==2 && user ==3 || cpu ==3 && user ==1) { System.out.println("이겼습니다."); } else if (cpu == user..

풀이 public class Quiz { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = new int[100]; System.out.println("숫자를 입력하세요: "); // 숫자를 100개까지 입력받는 부분 for (int i = 0; i = 0; i--) { if (arr[i] != 0) System.out.print(arr[i..