[JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 3일차 예제 풀이

2021. 1. 15. 20:30·자바 공부

※꼭 제 코드가 맞는 것도 아니고 정해진 답이 있는 것도 아닙니다. 문제가 어려워 접근하지 못하시는 분들을 위한 풀이이니 잘못된 점이 있으면 댓글로 남겨주시면 감사하겠습니다. 예제의 출력은 이 예제를 눈으로만 보고 계시는 분들이 있을 것 같아 직접 해보고 출력 값이 어떨까를 알고 가셨으면 하는 마음에 출력 값은 제공하지 않습니다. 궁금하신 분들은 코드를 복사해서 컴파일해보세요!

 

예제 1. 세 점을 사용자에게서 입력받아 세 점 사이의 중점을 구하는 프로그램.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Ex1 {
    private int x; 
    private int y;
    
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    
    public Ex1(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public Ex1 getCenter(Ex1 other1, Ex1 other2) { //입력받는 매개변수 좌표를 2개로 늘렸습니다.
        return new Ex1((this.x + other1.getX() + other2.getX()) / 3 ,(this.y + other1.getY() + other2.getY()) / 3);
    }
    
}
Colored by Color Scripter
cs
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
 
public class Ex1Main {
    public static void main(String[] args) {
        
        Scanner s = new Scanner(System.in);
        System.out.print("첫번째 점의 x좌표와 y좌표를 입력하시오. :");
        Ex1 one = new Ex1(s.nextInt(),s.nextInt());
        System.out.print("두번째 점의 x좌표와 y좌표를 입력하시오. :");
        Ex1 two = new Ex1(s.nextInt(),s.nextInt());
        System.out.print("세번째 점의 x좌표와 y좌표를 입력하시오. :");
        Ex1 three = new Ex1(s.nextInt(),s.nextInt());
        Ex1 result = one.getCenter(two, three);
        
        System.out.println("x : " + result.getX() + ", y : " + result.getY());
    }
 
}
Colored by Color Scripter
cs

 

예제 2. Person과 Student를 응용해서 몇 명의 학생의 정보를 입력받을 것인지 배열로 입력받고 각 줄마다 그 정보를 입력받아 인원수만큼의 입력이 끝나면 마지막에 한꺼번에 출력하는 프로그램

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Ex2Person {
    
    private String name;
    private int age;
    private int height;
    private int weight;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    public Ex2Person(String name, int age, int height, int weight) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.weight = weight;
    }
 
}
Colored by Color Scripter
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class Ex2Student extends Ex2Person {
    
    private String studentID;
    private int grade;
    private double credit;
    
    public String getStudentID() {
        return studentID;
    }
    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }
    public int getGrade() {
        return grade;
    }
    public void setGrade(int grade) {
        this.grade = grade;
    }
    public double getCredit() {
        return credit;
    }
    public void setCredit(double credit) {
        this.credit = credit;
    }
    //개발의 편의성을 위해서 자신이 상속받은 클래스의 변수도 초기화.
    public Ex2Student(String name, int age, int height, int weight, String studentID, int grade, double credit) {
        super(name, age, height, weight); //자신의 부모가 가지고있는 생성자를 실행.
        this.studentID = studentID;
        this.grade = grade;
        this.credit = credit;
    }
    
    public void show() {
        System.out.println("------------------------");
        System.out.println("학생 이름 : " + getName());
        System.out.println("학생 나이 : " + getAge());
        System.out.println("학생 키 : " + getHeight());
        System.out.println("학생 몸무게 : " + getWeight());
        System.out.println("학생 학번 : " + getStudentID());
        System.out.println("학생 학년 : " + getGrade());
        System.out.println("학생 학점 : " + getCredit());
        System.out.println();
    }
}
Colored by Color Scripter
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
 
public class Ex2Main {
    public static void main(String[] args) {
        
        Scanner s = new Scanner(System.in);
        System.out.print("학생 몇 명의 인적사항을 작성하시겠습니까? :");
        int n = s.nextInt();
        Ex2Student[] students = new Ex2Student[n];
        
        for(int i = 0; i < n; i++) {
            String name;
            int age;
            int height;
            int weight;
            String studentID;
            int grade;
            double credit;
            
            System.out.print("학생의 이름을 입력하시오. :");
            name = s.next();
            System.out.print("학생의 나이를 입력하시오. :");
            age = s.nextInt();
            System.out.print("학생의 키를 입력하시오. :");
            height = s.nextInt();
            System.out.print("학생의 몸무게 입력하시오. :");
            weight = s.nextInt();
            System.out.print("학생의 힉번 입력하시오. :");
            studentID = s.next();
            System.out.print("학생의 학년을 입력하시오. :");
            grade = s.nextInt();
            System.out.print("학생의 학점을 입력하시오. :");
            credit = s.nextDouble();
            students[i] = new Ex2Student(name, age, height, weight, studentID, grade, credit);
        }
        for(int i = 0; i < n; i ++) {
            students[i].show();
        }
    }
 
}
Colored by Color Scripter
cs

 

예제 3. 추상의 개념을 이용해서 철수와 영희가 우는 프로그램 구현.

코드

1
2
3
4
abstract class Ex3Crying {
    abstract void crying();
}
 
cs
1
2
3
4
5
6
7
public class Ex3ChulSu extends Ex3Crying {
    
    void crying() {
        System.out.println("철수 : 엉엉엉엉엉");
    }
 
}
Colored by Color Scripter
cs
1
2
3
4
5
6
7
public class Ex3YoungHee extends Ex3Crying {
    
    void crying() {
        System.out.println("영희 : 흑흑흑흑흑");
    }
 
}
Colored by Color Scripter
cs
1
2
3
4
5
6
7
8
9
10
11
public class Ex3Main {
    public static void main(String[] args) {
        
        Ex3ChulSu chulsu = new Ex3ChulSu();
        Ex3YoungHee younghee = new Ex3YoungHee();
        
        chulsu.crying();
        younghee.crying();
    }
 
}
Colored by Color Scripter
cs

 

저도 제 포스팅을 보면서 풀었네요. 아직 머리랑 손에 잘 익지 않은 것 같습니다. 복습하는 시간을 정해서 복습도 해야겠어요. 오늘도 봐주셔서 감사합니다.

'자바 공부' 카테고리의 다른 글

[JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 4일차 예제 풀이  (0) 2021.01.16
[JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 4일차 (인터페이스(interface), 다형성(Polymorphism), 객체(Object)클래스)  (0) 2021.01.16
[JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 3일차 (클래스(class), 상속(inheritance), 추상(abstract), Final)  (0) 2021.01.15
[JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 2일차 예제 풀이  (0) 2021.01.15
[JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 2일차 (객체지향, 메소드, 반복, 재귀, 배열, 다차원 배열)  (0) 2021.01.14
'자바 공부' 카테고리의 다른 글
  • [JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 4일차 예제 풀이
  • [JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 4일차 (인터페이스(interface), 다형성(Polymorphism), 객체(Object)클래스)
  • [JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 3일차 (클래스(class), 상속(inheritance), 추상(abstract), Final)
  • [JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 2일차 예제 풀이
김줘
김줘
김줘와 같이 데이터, 컴퓨터, IT 공부
  • 김줘
    초보개발자 김줘의 코딩일기
    김줘
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 데이터 엔지니어링 데브코스
      • 데이터
        • Airflow
        • Spark
        • Kafka
        • dbt
      • TroubleShooting
      • Docker
      • AWS
      • 크롤링, 스크래핑, 시각화
        • Selenium
        • 시각화
      • 코딩테스트
        • 프로그래머스
        • 입출력과 사칙연산
        • 정렬
      • Django
      • 자바 공부
      • 끄적끄적
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    데이터 엔지니어
    오블완
    자바
    데이터 엔지니어링 데브코스
    데이터 엔지니어링 데브코스 4기
    부트캠프
    티스토리챌린지
    TiL
    cloud
    프로그래머스
    Airflow
    Python
    데브코스
    Java
    aws
    파이썬
    Azure
    초보개발자
    에어플로우
    프로그래밍
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
김줘
[JAVA]초보 개발자 김주호와 기초부터 시작하는 자바공부 3일차 예제 풀이
상단으로

티스토리툴바