반응형
1. 다음 main()메소드와 실행 결과를 참고하여 상속받은 ColorTV 클래스를 작성하라. |
package chapter5;
class TV {
private int size;
public TV(int size) {this.size = size;}
protected int getSize() {return size;}
}
public class ColorTV extends TV{
private int nColors;
public ColorTV(int size, int nColors) {
super(size);
this.nColors = nColors;
}
public void printProperty() {
System.out.println(getSize() + "인치 " + nColors + "컬러");
}
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
}
}
2. 다음 main()메소드와 실행 결과를 참고하여 문제 1의 ColorTV를 상속받는 IPTV 클래스를 작성하라. |
package chapter5;
class TV2 {
private int size;
public TV2(int size) {this.size = size;}
protected int getSize() {return size;}
}
class ColorTV2 extends TV2{
private int nColors;
public ColorTV2(int size, int nColors) {
super(size);
this.nColors = nColors;
}
protected int getnColors() {return nColors;}
public void printProperty() {
System.out.println(getSize() + "인치 " + nColors + "컬러");
}
}
public class IPTV extends ColorTV2 {
private String ip;
public IPTV(String ip, int size, int nColors) {
super(size, nColors);
this.ip = ip;
}
public void printProperty() {
System.out.println("나의 IPTV는 " + ip + " 주소의 " + getSize() + "인치 " + getnColors() + "컬러");
}
public static void main(String[] args) {
IPTV myTV = new IPTV("192.1.1.2", 32, 2048);
myTV.printProperty();
}
}
3. Coverter 클래스를 상속받아 원화를 달러로 변환하는 Won2Dollar 클래스를 작성하라. |
package chapter5;
import java.util.Scanner;
abstract class Converter {
abstract protected double convert(double src);
abstract protected String getSrcString();
abstract protected String getDestString();
protected double ratio;
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println(getSrcString() + "을 " + getDestString() + "로 바꿉니다.");
System.out.print(getSrcString() + "을 입력하세요>> ");
double val = scanner.nextDouble();
double res = convert(val);
System.out.println("변환 결과: " + res + getDestString() + "입니다.");
scanner.close();
}
}
public class Won2Dollar extends Converter{
public Won2Dollar(double ratio) {
this.ratio = ratio;
}
@Override
protected double convert(double src) {
return src / ratio;
}
@Override
protected String getSrcString() {
return "원";
}
@Override
protected String getDestString() {
return "달러";
}
public static void main(String[] args) {
Won2Dollar toDollar = new Won2Dollar(1200);
toDollar.run();
}
}
4. Converter클래스를 상속받아 Km를 mile(마일)로 변환하는 km2Mile클래스를 작성하라. main() 메소드와 실행 결과는 다음과 같다. |
package chapter5;
import java.util.Scanner;
abstract class Converter2 {
abstract protected double convert(double src);
abstract protected String getSrcString();
abstract protected String getDestString();
protected double ratio;
public void run2() {
Scanner scanner = new Scanner(System.in);
System.out.println(getSrcString() + "을 " + getDestString() + "로 바꿉니다.");
System.out.print(getSrcString() + "을 입력하세요>> ");
double val = scanner.nextDouble();
double res = convert(val);
System.out.println("변환 결과: " + res + getDestString() + "입니다.");
scanner.close();
}
}
public class Km2Mile extends Converter{
public Km2Mile(double ratio) {
this.ratio = ratio;
}
@Override
protected double convert(double src) {
return src / ratio;
}
@Override
protected String getSrcString() {
return "Km";
}
@Override
protected String getDestString() {
return "Mile";
}
public static void main(String[] args) {
Km2Mile toMile = new Km2Mile(1.6);
toMile.run();
}
}
5. Poin를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라. |
package chapter5;
class Point {
private int x, y;
public Point(int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
public class ColorPoint extends Point{
private String color;
public ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}
public void setXY(int x, int y) {move(x,y);}
public void setColor(String color) {this.color = color;}
public String toString() {
return color + "색의 " + "(" + getX() + "," + getY() + ")의 점";
}
public static void main(String[] args) {
ColorPoint cp = new ColorPoint(5, 5, "YELLOW");
cp.setXY(10, 20);
cp.setColor("RED");
String str = cp.toString();
System.out.println(str + "입니다.");
}
}
6. Point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라. 다음 main() 메소드를 포함하고 실행 결과와 같이 출력되게 하라. |
package chapter5;
class Point2 {
private int x, y;
public Point2() {this.x = 0; this.y = 0;} //기본 생성자 추가
public Point2(int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
public class ColorPoint2 extends Point2{
private String color;
public ColorPoint2() { //기본 생성자 추가
this.color = "Black";
}
public ColorPoint2(int x, int y) {
super(x, y);
}
public void setXY(int x, int y) {move(x,y);}
public void setColor(String color) {this.color = color;}
public String toString() {
return color + "색의 " + "(" + getX() + "," + getY() + ")의 점";
}
public static void main(String[] args) {
ColorPoint2 zeroPoint = new ColorPoint2();
System.out.println(zeroPoint.toString() + "입니다.");
ColorPoint2 cp = new ColorPoint2(10, 10);
cp.setXY(5, 5);
cp.setColor("RED");
System.out.println(cp.toString() + "입니다.");
}
}
7. Point를 상속받아 3차원의 점을 나타내는 Point3D 클래스를 작성하라. |
package chapter5;
class Point3 {
private int x, y;
public Point3(int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
public class Point3D extends Point3{
private int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
public void moveUp() {z += 1;}
public void moveDown() {z -= 1;}
public void move(int x, int y, int z) {
move(x, y); this.z = z;
}
public String toString() {
return "(" + getX() + "," + getY() + "," + z + ")의 점";
}
public static void main(String[] args) {
Point3D p = new Point3D(1, 2, 3);
System.out.println(p.toString() + "입니다.");
p.moveUp();
System.out.println(p.toString() + "입니다.");
p.moveDown();
p.move(10, 10);
System.out.println(p.toString() + "입니다.");
p.move(100, 200, 300);
System.out.println(p.toString() + "입니다.");
}
}
8. Point를 상속받아 양수의 공간에서만 점을 나타내는 PositivePoint 클래스를 작성하라. |
package chapter5;
class Point4 {
private int x, y;
public Point4() {this.x = 0; this.y = 0;}
public Point4(int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x, int y) {this.x = x; this.y = y;}
}
public class PositivePoint extends Point4{
public PositivePoint(int x, int y) {
super(x, y);
if(x < 0 || y < 0) move(0, 0);
}
public PositivePoint() {
}
@Override
protected void move(int x, int y) {
if(x >= 0 && y >= 0) super.move(x, y);
}
public String toString() {
return "(" + getX() + "," + getY() + ")의 점";
}
public static void main(String[] args) {
PositivePoint p = new PositivePoint();
p.move(10, 10);
System.out.println(p.toString() + "입니다.");
p.move(-5, 5);
System.out.println(p.toString() + "입니다.");
PositivePoint p2 = new PositivePoint(-10, -10);
System.out.println(p2.toString() + "입니다.");
}
}
반응형
'프로그래밍 > JAVA' 카테고리의 다른 글
<명품 JAVA Programming> - Chapter6 실습문제 (0) | 2020.11.17 |
---|---|
<명품 JAVA Programming> - Chapter5 연습문제(9, 10, 11, 12, 13, 14) (0) | 2020.11.09 |
<명품 JAVA Programming> - Chapter4 연습문제(1, 2, 3, 4, 5, 6) (0) | 2020.10.26 |
<명품 JAVA Programming> - Chapter3 연습문제(13, 14, 15, 16) (0) | 2020.10.25 |
<명품 JAVA Programming> - Chapter3 연습문제(7, 8, 9, 10, 11, 12) (0) | 2020.10.25 |