7-21 다음과 같이 attack메서드가 정의되어 있을 때, 이 메서드의 매개변수로 가능한 것 두 가지를 적으시오.
interface Movable {
void move(int x, int y);
}
void attack(Movable f) {
}
null, Movable인터페이스를 구현한 클래스 또는 그 자손의 인스턴스
7-22 아래는 도형을 정의한 Shape클래스이다. 이 클래스를 조상으로 하는 Circle클래스와 Rectangle클래스를 작성하시오. 이때, 생성자도 각 클래스에 맞게 적절히 추가해야 한다.
(1) 클래스명 : Circle
조상클래스 : Shape
멤버변수 : double r - 반지름
(2) 클래스명 : Rectangle
조상클래스 : Shape
멤버변수 : double width - 폭
: double height - 높이
메서드 :
1. 메서드명 : isSquare
기 능 : 정사각형인지 아닌지를 알려준다.
반환타입 : boolean
매개변수 : 없음
abstract class Shape {
Point p;
Shape() {
this(new Point(0, 0));
}
Shape(Point p) {
this.p = p;
}
abstract double calcArea(); // 도형의 면적을 계산해서 반환하는 메서드
Point getPosition() {
return p;
}
void setPosition(Point p) {
this.p = p;
}
}
class Point {
int x;
int y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + "," + y + "]";
}
}
abstract class Shape {
Point p;
Shape() {
this(new Point(0, 0));
}
Shape(Point p) {
this.p = p;
}
abstract double calcArea(); // 도형의 면적을 계산해서 반환하는 메서드
Point getPosition() {
return p;
}
void setPosition(Point p) {
this.p = p;
}
}
class Rect extends Shape {
double width;
double height;
Rect(double width, double height) {
this(new Point(0, 0), width, height);
}
Rect(Point p, double width, double height) {
super(p); // . 조상의 멤버는 조상의 생성자가 초기화하도록 한다
this.width = width;
this.height = height;
}
boolean isSquare() {
// width height 0 width height true . 나 가 이 아니고 와 가 같으면 를 반환한다
return width * height != 0 && width == height;
}
double calcArea() {
return width * height;
}
}
class Circle extends Shape {
double r; // 반지름
Circle(double r) {
this(new Point(0, 0), r); // Circle(Point p, double r)를 호출
}
Circle(Point p, double r) {
super(p); // . 조상의 멤버는 조상의 생성자가 초기화하도록 한다
this.r = r;
}
double calcArea() {
return Math.PI * r * r;
}
}
class Point {
int x;
int y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + "," + y + "]";
}
}
7-23 문제7-22에서 정의한 클래스들의 면적을 구하는 메서드를 작성하고 테스트하시오.
class JavaExercise7_23 {
public static void main(String[] args) {
Shape[] arr = { new Circle(5.0), new Rect(3, 4), new Circle(1) };
System.out.println(" :" + sumArea(arr));
}
}
class Exercise7_23 {
static double sumArea(Shape[] arr) {
double sum = 0;
for (int i = 0; i < arr.length; i++)
sum += arr[i].calcArea();
return sum;
}
public static void main(String[] args) {
Shape[] arr = { new Circle(5.0), new Rect(3, 4), new Circle(1) };
System.out.println(" :" + sumArea(arr));
}
}
7-24 다음 중 인터페이스의 장점이 아닌 것은? e
a. 표준화를 가능하게 해 준다.
b. 서로 관계없는 클래스들에게 관계를 맺어줄 수 있다/
c. 독립적인 프로그래밍이 가능하다.
d. 다중 상속을 가능하게 해 준다.
e. 패키 지간의 연결을 도와준다
7-25 Outer클래스의 내부 클래스 Inner의 멤버 변수 iv의 값을 출력하시오.
class Outer { // 외부 클래스
class Inner { // ( ) 내부 클래스 인스턴스 클래스
int iv = 100;
}
}
class JavaExercise7_25 {
public static void main(String[] args) {
}
}
class Outer { // 외부 클래스
class Inner { // ( ) 내부 클래스 인스턴스 클래스
int iv = 100;
}
}
class JavaExercise7_25 {
public static void main(String[] args) {
Outer o = new Outer();
Outer.Inner ii = o.new Inner();
System.out.println(ii.iv);
}
}
100
7-26 Outer클래스의 내부 클래스 Inner의 멤버변수 iv의 값을 구하시오.
class Outer { // 외부 클래스
static class Inner { // (static ) 내부 클래스 클래스
int iv = 200;
}
}
class JavaExercise7_26 {
public static void main(String[] args) {
}
}
class Outer { // 외부 클래스
static class Inner { // (static ) 내부 클래스 클래스
int iv = 200;
}
}
class JavaExercise7_26 {
public static void main(String[] args) {
Outer.Inner ii = new Outer.Inner();
System.out.println(ii.iv);
}
}
7-27 다음과 같은 실행혈과를 얻도록 (1)~(4)의 코드를 완성하시오.
class Outer {
int value = 10;
class Inner {
int value = 20;
void method1() {
int value = 30;
System.out.println(/* (1) */);
System.out.println(/* (2) */);
System.out.println(/* (3) */);
}
} // Inner클래스의 끝
} // Outer클래스의 끝
class JavaExercise7_27 {
public static void main(String args[]) {
/*
* (4) . 알맞은 코드를 넣어 완성하시오
*/
inner.method1();
}
}
30
20
10
class Outer {
int value = 10; // Outer.this.value
class Inner { // (instance inner class) 인스턴스 클래스
int value = 20; // this.value
void method1() {
int value = 30; // value
System.out.println(value);
System.out.println(this.value);
System.out.println(Outer.this.value);
}
} // Inner클래스의 끝
} // Outer클래스의 끝
class JavaExercise7_27 {
public static void main(String args[]) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.method1();
}
}
30
20
10
7-28 아래의 EventHandler를 익명 클래스 (anonymous class)로 변경하시오.
import java.awt.*;
import java.awt.event.*;
class JavaExercise7_28 {
public static void main(String[] args) {
Frame f = new Frame();
f.addWindowListener(new EventHandler());
}
}
class EventHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
e.getWindow().dispose();
System.exit(0);
}
}
import java.awt.*;
import java.awt.event.*;
class JavaExercise7_28 {
public static void main(String[] args) {
Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
e.getWindow().dispose();
System.exit(0);
}
});
} // main
}
7-29 지역 클래스에서 외부 클래스의 인스턴스 멤버와 static멤버에 모두 접근할 수 있지만, 지역변수는 final이 붙은 상수만 접근할 수 있는 이유가 무엇인가?
메서드가 수행을 마쳐서 지역변수가 소멸된 시점에도, 지역 클래스의 인스턴스가 소멸된 지역변수를 참조하려는 경우가 발생할 수 있기 때문이다.
'07강 객체지향 프로그래밍2 > 객체지향 프로그래밍2 자바200제' 카테고리의 다른 글
JAVA07강 객체지향 프로그래밍2자바200제 11~20 (0) | 2021.07.31 |
---|---|
JAVA07강 객체지향 프로그래밍2자바200제 1~10 (0) | 2021.07.26 |