✅ 답안과 비교하여 스스로 코드 개선점 짚어보기 완료(2022.01.06)
BASIC1. 상속 구조를 통한 학생-직원 관리 프로그램
name=홍길동, age=20, height=178.2, weight=70.0, grade=1, major=정보시스템공학과
name=김말똥, age=21, height=187.3, weight=80.0, grade=2, major=경영학과
name=강개순, age=23, height=167.0, weight=45.0, grade=4, major=정보통신학과
이름 : 박
나이 : 26
키 : 180
몸무게 : 71
급여 : 10000000
부서 : 기획부
계속 입력하시겠습니까? (y/n) : n
name=박, age=26, height=180.0, weight=71.0, salary=10000000, dept=기획부
Person.class
package com.reminder.inheritance_practice.extend;
public class Person {
protected String name;
private int age;
private double height;
private double weight;
public Person() {}
public Person(int age, double height, double weight) {
this.age = age;
this.height = height;
this.weight = 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 double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getInformation() {
return "name=" + name + ", age=" + age + ", height=" + height + ", weight=" + weight;
}
}
Student.class
package com.reminder.inheritance_practice.extend;
public class Student extends Person {
private int grade;
private String major;
public Student() {}
public Student(String name, int age, double height, double weight, int grade, String major) {
super(age, height, weight);
super.name = name;
this.grade = grade;
this.major = major;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getInformation() {
return super.getInformation() + ", grade=" + grade + ", major=" + major;
}
}
Employee.class
package com.reminder.inheritance_practice.extend;
public class Employee extends Person {
private int salary;
private String dept;
public Employee() {}
public Employee(String name, int age, double height, double weight, int salary, String dept) {
super(age, height, weight);
super.name = name;
this.salary = salary;
this.dept = dept;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getInformation() {
return super.getInformation() + ", salary=" + salary + ", dept=" + dept;
}
}
Application.class
package com.reminder.inheritance_practice.extend;
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
/* A. Student */
/* 객체배열 할당 */
Student[] student = new Student[3];
/* 객체 생성 */
student[0] = new Student("홍길동", 20, 178.2, 70.0, 1, "정보시스템공학과");
student[1] = new Student("김말똥", 21, 187.3, 80.0, 2, "경영학과");
student[2] = new Student("강개순", 23, 167.0, 45.0, 4, "정보통신학과");
for(Student students : student) {
System.out.println(students.getInformation());
}
/* B. Employee */
Employee[] employee = new Employee[10];
Scanner scanner = new Scanner(System.in);
int count=0;
while(true) {
System.out.print("이름 : ");
String name = scanner.nextLine();
System.out.print("나이 : ");
int age = scanner.nextInt();
System.out.print("키 : ");
double height = scanner.nextDouble();
System.out.print("몸무게 : ");
double weight = scanner.nextDouble();
System.out.print("급여 : ");
int salary = scanner.nextInt();
System.out.print("부서 : ");
scanner.nextLine();
String dept = scanner.nextLine();
System.out.print("계속 입력하시겠습니까? (y/n) : ");
String answer = scanner.nextLine();
employee[count++] = new Employee(name, age, height, weight, salary, dept);
if(!(answer.equalsIgnoreCase("y"))) {
break;
}
}
for(int i=0; i < count; i++) {
System.out.println(employee[i].getInformation());
}
}
}
NORMAL1. 도형의 x, y 좌표 값 및 원-사각형의 면적과 둘레 계산하기
수업시간에 printf 구문을 처음 다뤘다. 소수점 첫째자리까지 출력하는 방법으로 예를 들어 주셨다.
앞으로는 향상된 for문 사용법에 익숙해져야 할 것 같다.
===== circle =====
(x, y) : (1, 2)
면적 : 28.3
둘레 : 18.8
(x, y) : (3, 3)
면적 : 50.3
둘레 : 25.1
===== rectangle =====
(x, y) : (-1, -2)
면적 : 10
둘레 : 14
(x, y) : (-2, 5)
면적 : 16
둘레 : 20
Application.class
package com.reminder.inheritance_practice.extend2;
public class Application {
public static void main(String[] args) {
/* 크기가 2인 Circle 배열 할당 */
Circle[] circ = new Circle[2];
/* 객체 데이터 초기화 */
circ[0] = new Circle(1, 2, 3);
circ[1] = new Circle(3, 3, 4);
/* 크기가 2인 Rectangle 배열 할당 */
Rectangle[] rect = new Rectangle[2];
/* 객체 데이터 초기화 */
rect[0] = new Rectangle(-1, -2, 5, 2);
rect[1] = new Rectangle(-2, 5, 2, 8);
System.out.println("===== circle =====");
for(Circle circle : circ) {
circle.draw();
}
System.out.println("===== rectangle =====");
for(Rectangle rectangle : rect) {
rectangle.draw();
}
}
}
Circle.class
package com.reminder.inheritance_practice.extend2;
public class Circle extends Point {
private int radius;
public Circle() {}
public Circle(int x, int y, int radius) {
super(x, y);
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public void draw() {
super.draw();
System.out.printf("면적 : %.1f%n", (float)Math.PI * radius * radius);
System.out.printf("둘레 : %.1f%n", (float)Math.PI * radius * 2);
}
}
Rectangle.class
package com.reminder.inheritance_practice.extend2;
public class Rectangle extends Point {
private int width;
private int height;
public Rectangle() {}
public Rectangle(int x, int y, int width, int height) {
super(x, y);
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public void draw() {
super.draw();
System.out.printf("면적 : %d%n", width * height);
System.out.printf("둘레 : %d%n", (width + height) * 2);
}
}
Point.class
package com.reminder.inheritance_practice.extend2;
public class Point {
private int x;
private int y;
public Point() {}
public Point(int x, int y) {
this.x = x;
this.y = 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 void draw() {
System.out.println("(x, y) : " + "(" + x + ", " + y + ")");
}
}
'Java' 카테고리의 다른 글
[JAVA] 10-1. API | Object | String | StringBuilder (0) | 2022.01.06 |
---|---|
[JAVA/수업 과제 practice] 다형성 Lv. 1~2 (0) | 2022.01.06 |
[JAVA] 9. 다형성 | 추상클래스 | 인터페이스 (1) | 2022.01.05 |
[JAVA] 8. 상속 | super | 오버라이딩 (0) | 2022.01.04 |
[JAVA/수업 과제 practice] 객체배열 Lv. 1~2 (0) | 2022.01.03 |