반응형
1. 다음 표와 실행 결과를 참고해 Circle 및 자식인 ColoredCircle 클래스를 작성하라. 그리고 Circle과 ColoredCircle 객체의 show() 메서드를 호출하는 테스트 프로그램도 작성하라.
package Java;
class Circle0 {
int radius;
Circle0(int radius) {
this.radius = radius;
}
void show() {
System.out.println("반지름이 " + radius + "인 원이다.");
}
}
class ColoredCircle extends Circle0 {
String color;
ColoredCircle(int radius, String color) {
super(radius);
this.color=color;
}
void show() {
System.out.println("반지름이 " + radius + "인 " + color + " 원이다.");
}
}
public class Java {
public static void main(String[] args) {
Circle0 c1 = new Circle0(5);
ColoredCircle c2 = new ColoredCircle(10,"빨간색");
c1.show();
c2.show();
}
}
2. 다음 표와 실행 결과를 참고해서 답하라. show() 메서드는 객체의 정보를 문자열로 반환한다.
package Java;
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
String getName() {
return name;
}
int getAge() {
return age;
}
void setName(String name) {
this.name=name;
}
void setAge(int age) {
this.age=age;
}
String show() {
return ("사람[이름 : " + name + ", 나이 : " + age + "]");
}
}
class Student extends Person {
int class_num;
Student(String name, int age, int class_num) {
super(name, age);
this.class_num = class_num;
}
int getClass_num() {
return class_num;
}
void setClass_num(int class_num) {
this.class_num=class_num;
}
String show() {
return ("사람[이름 : " + name + ", 나이 : " + age + ", 학번 : " + class_num + "]");
}
}
class ForeignStudent extends Student {
String country;
ForeignStudent(String name, int age, int class_num, String country) {
super(name, age, class_num);
this.country=country;
}
String getCountry() {
return country;
}
void setCountry(String country) {
this.country = country;
}
String show() {
return ("사람[이름 : " + name + ", 나이 : " + age + ", 학번 : " + class_num +", 국적 : " + country + "]");
}
}
public class Java {
public static void main(String[] args) {
Person[] p = {new Person("길동이",22),new Student("황진이",23,100),new ForeignStudent("Amy",30,200,"U.S.A")};
for (Person k : p) {
System.out.println(k.show());
}
}
}
3. 다음 표를 참고해 MovablePoint의 부모 클래스인 Point를 작성하라. Point 클래스의 toString() 메서드는 좌표를 나타내는 문자열이며, MovablePoint 클래스의 toString() 메서드는 좌표와 이동 속도를 나타내는 문자열을 반환한다.
package Java;
class Point {
private int x,y;
Point(int x, int y) {
this.x=x;
this.y=y;
}
int getX() {
return x;
}
int getY() {
return x;
}
void setX(int x) {
this.x = x;
}
void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}
class MovablePoint extends Point {
private int xSpeed, ySpeed;
MovablePoint(int x, int y,int xSpeed, int ySpeed) {
super(x,y);
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
int getxSpeed() {
return xSpeed;
}
int getySpeed() {
return ySpeed;
}
void setxSpeed(int xSpeed) {
this.xSpeed = xSpeed;
}
void setySpeed(int ySpeed) {
this.ySpeed = ySpeed;
}
@Override
public String toString() {
return "MovablePoint [x=" + getX() + ", y =" +getY() + " xSpeed=" + xSpeed + ", ySpeed =" + ySpeed + "]";
}
}
4. 메서드는 자식 클래스가 오버라이딩할 수 있지만, 필드는 자식 클래스가 오버라이딩할 수 없다. 다음 표를 참고해 Parent의 자식인 Child를 클래스로 작성하고, 이를 아래에 있는 OvershadowTest 프로그램으로 테스트하라.
package Java;
class Parent {
String name = "영조";
void print() {
System.out.println("나는 영조이다.");
}
}
class Child extends Parent {
String name = "사도세자";
void print() {
System.out.println("나는 사도세자이다.");
}
}
public class Java {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.name);
p.print();
}
}
5. 다음 표를 참고해 Phone, Phone의 자식 클래스 Telephone, Telephone의 자식 클래스 Smartphone을 작성하고, 테스트 프로그램도 작성하라.
package Java;
class Phone {
protected String owner;
Phone(String owner) {
this.owner = owner;
}
void talk() {
System.out.println(owner + "가 통화 중이다.");
}
}
class Telephone extends Phone {
private String when;
Telephone(String owner, String when) {
super(owner);
this.when = when;
}
void autoAnswering() {
System.out.println(owner + "가 없다. " + when + "전화 줄래.");
}
}
class Smartphone extends Telephone {
private String game;
Smartphone(String owner, String game) {
super(owner,"when");
this.game = game;
}
void playGame() {
System.out.println(owner + "가 " + game + " 게임을 하는 중이다.");
}
}
public class Java {
public static void main(String[] args) {
Phone[] phones = { new Phone("황진이"), new Telephone("길동이", "내일"), new Smartphone("민국이", "갤러그") };
for (Phone phone : phones) {
if (phone instanceof Smartphone) {
((Smartphone) phone).playGame();
} else if (phone instanceof Telephone) {
((Telephone) phone).autoAnswering();
} else if (phone instanceof Phone) {
phone.talk();
}
}
}
}
6. 운송 수단과 운송 수단의 하나인 자동차를 다음과 같이 모델링하려고 한다. 각 클래스의 show() 메서드는 필드 값을 출력한다. 두 클래스를 작성하고 아래 테스트 프로그램 OverrideTest를 실행해서 오버라이딩된 메서드와 다형성 관계를 살펴보자.
package Java;
class Vehicle {
static String color;
static int speed;
Vehicle(String color, int speed) {
this.color = color;
this.speed = speed;
}
static void show() {
System.out.println("색상 : " +color);
System.out.println("속도 : " +speed);
}
}
class Car extends Vehicle {
static int displacement;
static int gears;
Car(String color, int speed, int displacement, int gears) {
super(color, speed);
this.displacement = displacement;
this.gears = gears;
}
static void show() {
System.out.println("색상 : " +color);
System.out.println("속도 : " +speed);
System.out.println("배기량 : " +displacement);
System.out.println("기어 단수 : " +gears);
}
}
public class Java {
public static void main(String[] args) {
Car c = new Car("파랑", 200, 1000, 5);
c.show();
System.out.println();
Vehicle v = c;
v.show();
}
}
반응형
'프로그래밍 언어' 카테고리의 다른 글
명품 C++ Programming_제3장 실습문제 (0) | 2021.04.21 |
---|---|
명품 C++ Programming_제2장 실습문제 (0) | 2021.04.20 |
명품 C++ Programming_제1장 실습문제 (0) | 2021.04.20 |
두근두근파이썬_제3장 연습문제 (0) | 2020.10.03 |
두근두근파이썬_제2장 연습문제 (0) | 2020.10.02 |
댓글