1.
interface Movable
{
void move(int dx, int dy);
}
class Shape implements Movable {
protected int x, y;
public void draw() {
System.out.println("Shape Draw");
}
public void move(int dx, int dy) {
x = dx;
y = dy;
}
};
class Rectangle extends Shape {
private int width, height;
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
public void draw() {
System.out.println("Rectangle Draw");
}
};
class Triangle extends Shape {
private int base, height;
public void draw() {
System.out.println("Triangle Draw");
}
@Override
public void move(int dx, int dy) {
x = dx;
y = dy;
}
};
class Circle extends Shape {
private int radius;
public void draw() {
System.out.println("Circle Draw");
}
};
public class ShapeTest {
private static Movable arrayOfShapes[];
public static void main(String arg[]) {
init();
moveAll();
}
public static void init() {
arrayOfShapes = new Shape[3];
arrayOfShapes[0] = new Rectangle();
arrayOfShapes[1] = new Triangle();
arrayOfShapes[2] = new Circle();
}
public static void moveAll() {
for (int i = 0; i < arrayOfShapes.length; i++) {
arrayOfShapes[i].move(10, 10);
}
}
};
2.
interface Drawable {
void draw();
}
class Shape implements Drawable {
protected int x, y;
public void draw() {
System.out.println("Shape Draw");
}
};
class Rectangle extends Shape {
private int width, height;
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
public void draw() {
System.out.println("Rectangle Draw");
}
};
class Triangle extends Shape {
private int base, height;
public void draw() {
System.out.println("Triangle Draw");
}
};
class Circle extends Shape {
private int radius;
public void draw() {
System.out.println("Circle Draw");
}
};
public class ShapeTest {
private static Drawable arrayOfShapes[];
public static void main(String arg[]) {
init();
drawAll();
}
public static void init() {
arrayOfShapes = new Shape[3];
arrayOfShapes[0] = new Rectangle();
arrayOfShapes[1] = new Triangle();
arrayOfShapes[2] = new Circle();
}
public static void drawAll() {
for (int i = 0; i < arrayOfShapes.length; i++) {
arrayOfShapes[i].draw();
}
}
};
3.
interface controllable {
void play();
void stop();
}
public class Test {
public static void main(String arg[]) {
controllable c = new controllable() {
public void play() {
System.out.println("PLAY");
}
public void stop() {
System.out.println("STOP");
}
};
c.play();
c.stop();
}
};
4.
interface Comparable {
// 이 객체가 다른 객체보다 크면 1, 같으면 0, 작으면 -1을 반환한다.
int compareTo(Object other);
}
class Person implements Comparable {
@Override
public String toString() {
return "Person [name=" + name + ", height=" + height + "]";
}
public Person(String name, double height) {
super();
this.name = name;
this.height = height;
}
String name;
double height;
@Override
public int compareTo(Object other) {
if ( this.height > ((Person)other).height )
return 1;
else if ( this.height == ((Person)other).height )
return 0;
else
return -1;
}
}
public class Test {
public static Person getMaximum(Person[] array)
{
Person max=array[0];
for(int i=1;i<array.length;i++){
if( array[i].height > max.height )
max = array[i];
}
return max;
}
public static void main(String arg[]) {
Person[] array;
array = new Person[3];
array[0] = new Person("홍길동1", 190);
array[1] = new Person("홍길동2", 180);
array[2] = new Person("홍길동3", 185);
System.out.println(getMaximum(array));
}
}
'솔루션모음 > 파워자바 프로그래밍' 카테고리의 다른 글
[파워자바] 14장 프로그래밍 솔루션 답지 (0) | 2023.02.15 |
---|---|
[파워자바] 13장 프로그래밍 솔루션 답지 (0) | 2023.02.15 |
[파워자바] 11장 프로그래밍 솔루션 답지 (0) | 2023.02.15 |
[파워자바] 10장 프로그래밍 솔루션 답지 (0) | 2023.02.15 |
[파워자바] 9장 프로그래밍 솔루션 답지 (0) | 2023.02.15 |
댓글