1.
package Chapter5;
class TV {
private int size;
public TV(int size) { this.size = size; }
protected int getSize() { return size; }
}
class ColorTV extends TV{
private int Color;
public ColorTV(int size, int Color) {
super(size);
this.Color = Color;
}
public void printProperty() {
System.out.println(super.getSize()+"인치 " + Color+"컬러");
}
}
public class Exercise {
public static void main(String [] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
}
}
2.
package Chapter5;
class TV {
private int size;
public TV(int size) { this.size = size; }
protected int getSize() { return size; }
}
class ColorTV extends TV{
private int Color;
public ColorTV(int size, int Color) {
super(size);
this.Color = Color;
}
public void printProperty() {
System.out.println(super.getSize()+"인치 " + Color+"컬러");
}
}
class IPTV extends ColorTV{
private String address;
public IPTV(String address, int size, int Color) {
super(size, Color);
this.address = address;
}
public void printProperty() {
System.out.print("나의 IPTV는 " + this.address +" 주소의 ");
super.printProperty();
}
}
public class Exercise {
public static void main(String [] args) {
IPTV iptv = new IPTV("192.1.1.2", 32, 2048);
iptv.printProperty();
}
}
3.
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();
}
}
class Won2Dollar extends Converter {
public Won2Dollar(double ratio) {
this.ratio = ratio;
}
protected double convert(double src) {
return src/ratio;
}
protected String getSrcString() {
return "원";
}
protected String getDestString() {
return "달러";
}
}
public class Exercise {
public static void main(String args[]) {
Won2Dollar toDollar = new Won2Dollar(1200);
toDollar.run();
}
}
4.
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();
}
}
class Km2Mile extends Converter {
public Km2Mile(double ratio) {
this.ratio = ratio;
}
protected double convert(double src) {
return src/ratio;
}
protected String getSrcString() {
return "Km";
}
protected String getDestString() {
return "mile";
}
}
public class Exercise {
public static void main(String args[]) {
Km2Mile toMile = new Km2Mile(1.6);
toMile.run();
}
}
5.
package Chapter5;
import java.util.Scanner;
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; }
}
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 this.color + "색의 (" + getX() + "," + getY() + ")의 점";
}
}
public class Exercise {
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.
package Chapter5;
import java.util.Scanner;
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; }
}
class ColorPoint extends Point {
private String color;
public ColorPoint() {
super(0,0);
this.color = "BLACK";
}
public ColorPoint(int x, int y) {
super(x, y);
this.color = "BLACK";
}
public void setXY (int x, int y) {
move(x, y);
}
public void setColor (String color) {
this.color = color;
}
public String toString() {
return this.color + "색의 (" + getX() + "," + getY() + ")의 점";
}
}
public class Exercise {
public static void main(String args[]) {
ColorPoint zeroPoint = new ColorPoint();
System.out.println(zeroPoint.toString() + "입니다.");
ColorPoint cp = new ColorPoint(10, 10);
cp.setXY(5, 5);
cp.setColor("RED");
System.out.println(cp.toString() + "입니다.");
}
}
7.
package Chapter5;
import java.util.Scanner;
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; }
}
class Point3D extends Point {
private int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
public int getZ() { return z; }
public String toString () {
return "(" + getX() + "," + getY() + "," + getZ() + ")의 점";
}
protected void moveUp() { this.z += 1; }
protected void moveDown() { this.z -= 1; }
protected void move(int x, int y, int z) { move(x, y); this.z = z;}
}
public class Exercise {
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.
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; }
}
class PositivePoint extends Point {
public PositivePoint() { super(0,0); }
public PositivePoint(int x, int y) {
super(x, y);
if(x < 0 || y < 0)
super.move(0, 0);
}
protected void move(int x, int y) {
if(x > 0 && y > 0) {
super.move(x, y);
}
}
public String toString() {
return "(" + getX() + "," + getY() + ")의 점";
}
}
public class Exercise {
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() + "입니다.");
}
}
9.
package Chapter5;
import java.util.Scanner;
interface Stack {
int length();
int capacity();
String pop();
boolean push(String val);
}
class StringStack implements Stack{
private int size;
private int index; // 배열의 인덱스 번호
private String [] StackArray;
public StringStack(int size) {
this.size = size;
StackArray = new String [size];
this.index = -1;
}
public int length() {
return index+1;
}
public int capacity() {
return StackArray.length;
}
public String pop() {
String popStr;
if(index == -1)
return null;
popStr = StackArray[index];
index--;
return popStr;
}
public boolean push(String val) {
if(index+1 < size) {
index++;
StackArray[index] = val;
return true;
}
else {
return false;
}
}
}
public class Exercise {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("총 스택 저장 공간의 크기 입력 >> ");
int size = sc.nextInt();
StringStack Stack = new StringStack(size);
while(true) {
System.out.print("문자열 입력 >> ");
String val = sc.next();
if(val.equals("그만"))
break;
if(!Stack.push(val))
System.out.println("스택이 꽉 차서 푸시 불가!");
}
System.out.print("스택에 저장된 모든 문자열 팝 : ");
for(int i=0; i<Stack.capacity(); i++) {
System.out.print(Stack.pop()+" ");
}
}
}
10.
package Chapter5;
abstract class PairMap{
protected String keyArray [];
protected String valueArray [];
abstract String get(String key);
abstract void put(String key, String value);
abstract String delete(String key);
abstract int length();
}
class Dictionary extends PairMap{
private int index = 0;
public Dictionary(int size) {
keyArray = new String [size];
valueArray = new String [size];
}
public String get(String key) {
for(int i=0; i<index; i++) {
if(key.equals(keyArray[i]))
return valueArray[i];
}
return null;
}
public void put(String key, String value) {
// 이미 있는 key라면
for(int i=0; i<index; i++) {
if(key.equals(keyArray[i])) {
valueArray[i] = value;
return;
}
}
// 새로운 key라면
keyArray[index] = key;
valueArray[index] = value;
index++;
}
public String delete(String key) {
for(int i=0; i<index; i++) {
if(key.equals(keyArray[i])) {
valueArray[i] = "null";
return "null";
}
}
// 배열에 없는 key라면
return "동일한 key가 없습니다.";
}
public int length() {
return index;
}
}
public class DictionaryApp {
public static void main(String [] args) {
Dictionary dic = new Dictionary(10);
dic.put("황기태", "자바");
dic.put("이재문", "파이썬");
dic.put("이재문", "C++");
System.out.println("이재문의 값은 " + dic.get("이재문"));
System.out.println("황기태의 값은 " + dic.get("황기태"));
dic.delete("황기태");
System.out.println("황기태의 값은 " + dic.get("황기태"));
}
}
11.
package Chapter5;
import java.util.Scanner;
abstract class Calc {
protected int a;
protected int b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
abstract int calculate();
}
class Add extends Calc{
public int calculate() {
return a+b;
}
}
class Sub extends Calc{
public int calculate() {
return a-b;
}
}
class Mul extends Calc{
public int calculate() {
return a*b;
}
}
class Div extends Calc{
public int calculate() {
return a/b;
}
}
public class javaCalc {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.print("두 정수와 연산자를 입력하시오 >> ");
int a = sc.nextInt();
int b = sc.nextInt();
char op = sc.next().charAt(0);
switch(op) {
case '+':
Add ad = new Add();
ad.setValue(a, b);
System.out.println(ad.calculate());
break;
case '-':
Sub sb = new Sub();
sb.setValue(a, b);
System.out.println(sb.calculate());
break;
case '*':
Mul ml = new Mul();
ml.setValue(a, b);
System.out.println(ml.calculate());
break;
case '/':
Div dv = new Div();
dv.setValue(a, b);
System.out.println(dv.calculate());
break;
}
}
}
12.
13.
package Chapter5;
interface Shape{
final double PI = 3.14;
void draw();
double getArea();
default public void redraw() {
System.out.print("--- 다시 그립니다. ");
draw();
}
}
class Circle implements Shape{
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public void draw() {
System.out.println("반지름이 " + this.radius + "인 원입니다.");
}
public double getArea() {
return PI * radius * radius;
}
}
public class Exercise {
public static void main(String args[]) {
Shape donut = new Circle(10);
donut.redraw();
System.out.println("면적은 " + donut.getArea());
}
}
14.
package Chapter5;
interface Shape{
final double PI = 3.14;
void draw();
double getArea();
default public void redraw() {
System.out.print("--- 다시 그립니다. ");
draw();
}
}
class Circle implements Shape{
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public void draw() {
System.out.println("반지름이 " + this.radius + "인 원입니다.");
}
public double getArea() {
return PI * radius * radius;
}
}
class Oval implements Shape{
private int a, b;
public Oval(int a, int b) {
this.a = a;
this.b = b;
}
public void draw() {
System.out.println(this.a+"x"+this.b+"에 내접하는 타원입니다.");
}
public double getArea() {
return PI * a * b;
}
}
class Rect implements Shape{
private int a, b;
public Rect(int a, int b) {
this.a = a;
this.b = b;
}
public void draw() {
System.out.println(this.a+"x"+this.b+"크기의 사각형입니다.");
}
public double getArea() {
return a*b;
}
}
public class Exercise {
public static void main(String args[]) {
Shape [] list = new Shape [3];
list[0] = new Circle(10);
list[1] = new Oval(20, 30);
list[2] = new Rect(10, 40);
for(int i=0; i<list.length; i++) list[i].redraw();
for(int i=0; i<list.length; i++) System.out.println("면적은 " + list[i].getArea());
}
}