hyndb
명품 JAVA 5장 실습문제 본문
prac 5_1
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(getSize() + "인치 " + color + "컬러");
}
}
public class Practice {
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
}
}
prac 5_2
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(getSize() + "인치 " + color + "컬러");
}
}
class IPTV extends ColorTV {
private String id;
public IPTV(String id,int size, int color ) {
super(size, color);
this.id = id;
}
public void printProperty() {
System.out.print("나의 IPTV는 " + id + "주소의 " );
super.printProperty();
}
}
public class Practice {
public static void main(String[] args) {
IPTV iptv = new IPTV("192.1.1.2", 32, 2048);
iptv.printProperty();
}
}
prac 5_3
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 Practice {
public static void main(String[] args) {
Won2Dollar toDollar = new Won2Dollar(1200);
toDollar.run();
}
}
prac 5_4
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 Practice {
public static void main(String[] args) {
Km2Mile toMile = new Km2Mile(1.6);
toMile.run();
}
}
prac 5_5
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 {
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 class Practice {
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 + "입니다.");
}
}
prac 5_6
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 {
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(color + "색의 " + "(" + getX() + "," + getY() + ")의 점");
}
}
public class Practice {
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() + "입니다.");
}
}
prac 5_7
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 {
int z;
public Point3D(int x, int y, int z) {
super(x,y);
this.z = z;
}
public int getZ() {
return z;
}
public void move(int x, int y, int z) {
move(x,y);
this.z = z;
}
public void moveUp() {
z++;
}
public void moveDown() {
z--;
}
public String toString() {
return("(" + getX() + "," + getY() + "," + getZ() + ")의 점");
}
}
public class Practice {
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() + "입니다.");
}
}
prac 5_8
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) { //둘 다 음수일 경우 0,0 으로 move
super.move(0, 0);
}
}
@Override
public void move(int x, int y) {//둘 다 양수일 경우 move
if(x > 0 && y > 0 ) {
super.move(x, y);
}
}
public String toString() {
return("(" + getX() + "," + getY() + ")의 점");
}
}
public class Practice {
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() + "입니다");
}
}
prac 5_9
import java.util.Scanner;
interface Stack {
int length();
int capacity();
String pop();
boolean push(String val);
}
class StringStack implements Stack {
private String[] stack;
private int top;
public StringStack(int capacity) {
stack = new String[capacity];
top = -1;
}
@Override
public int length() {
return top+1;
}
@Override
public int capacity() {
return stack.length;
}
@Override
public String pop() {
if(top==-1) {
return null;
}
String s = stack[top];
top--;
return s;
}
@Override
public boolean push(String val) {
if(top == stack.length-1) {
return false;
}
else {
top++;
stack[top]= val;
return true;
}
}
}
public class pr5_9 { //stackapp 클래스
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("총 스택 저장 공간의 크기 입력 >> ");
int n = scanner.nextInt();
StringStack ss = new StringStack(n);
while(true) {
System.out.print("문자열 입력 >> ");
String str = scanner.next();
if(str.equals("그만"))
break;
boolean res = ss.push(str);
if(res == false)
System.out.println("스택이 꽉 차서 푸시 불가!");
}
System.out.print("스택에 저장된 모든 문자열 팝: ");
int len = ss.length();
for(int i = 0; i<len; i++) {
System.out.print(ss.pop() + " ");
}
scanner.close();
}
}
prac 5_10은 사진으로 넣기
prac 5_11
import java.util.Scanner;
abstract class Calc {
protected int a, b;
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 Mul extends Calc {
public int calculate() {
return a * b;
}
}
class Sub extends Calc {
public int calculate() {
return a - b;
}
}
class Div extends Calc {
public int calculate() {
return a / b;
}
}
public class Practice {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("두 정수와 연산자를 입력하시오>> ");
int a = scanner.nextInt();
int b = scanner.nextInt();
char op = scanner.next().charAt(0);
Calc exp;
switch (op) {
case '+':
exp = new Add();
break;
case '-':
exp = new Sub();
break;
case '*':
exp = new Mul();
break;
case '/':
exp = new Div();
break;
default:
System.out.println("잘못된 입력");
scanner.close();
return;
}
exp.setValue(a, b);
System.out.println(exp.calculate());
scanner.close();
}
}
prac 5_12
import java.util.Scanner;
abstract class Shape { //추상 클래스 Shape
private Shape next;
public Shape() { next = null; }
public void setNext(Shape obj) { next = obj; } //링크 연결
public Shape getNext() { return next; }
public abstract void draw(); //추상메소드
}
class Line extends Shape {
public Line() {}
public void draw() {
System.out.println("Line");
}
}
class Rect extends Shape {
public Rect() {}
public void draw() {
System.out.println("Rect");
}
}
class Circle extends Shape {
public Circle() {}
public void draw() {
System.out.println("Circle");
}
}
class GraphicEditor {
private Shape head, tail;
private Scanner sc;
public GraphicEditor() {
head = null;
tail = null;
sc = new Scanner(System.in);
}
void run() { //실행 메소드
System.out.println("그래픽 에디터 beauty을 실행합니다.");
while (true) {
System.out.print("삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>");
int num = sc.nextInt();
if(num == 1) {
insert(); //
}
else if(num == 2) {
System.out.print("삭제할 도형의 위치");
int dInput = sc.nextInt();
delete(dInput); //
}
else if( num == 3) {
print();
}
else if( num == 4) {
System.out.println("beauty를 종료합니다.");
break;
}
}
}
void insert() { //삽입 메소드
Shape s;
System.out.print("Line(1), Rect(2), Circle(3)>> " );
int num2 = sc.nextInt();
if(num2 == 1) {
s = new Line(); //shape 객체 s에 저장
} else if(num2 == 2) {
s = new Rect();
} else {
s = new Circle();
}
if(head ==null) {
head = s;
tail = s;
} else {
tail.setNext(s);
tail = s;
}
}
void delete(int num) { //삭제 메소드
Shape c = head;
Shape t = head;
int i;
if (num == 1) {
if (head == tail) {
head = null;
tail = null;
return;
} else {
head = head.getNext();
return;
}
}
for(i = 1; i<num; i++) {
t = c;
c = c.getNext();
if(c == null) {
System.out.println("삭제할 수 없습니다.");
return;
}
}
t.setNext(c.getNext());
}
void print() { //출력
Shape s = head;
while(s != null) {
s.draw();
s = s.getNext();
}
}
}
public class Practice {
public static void main(String[] args) {
GraphicEditor g = new GraphicEditor();
g.run();
}
}
prac 5_13
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; //반지름 변수 radius 선언
public Circle(int radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("반지름이 " + radius + "인 원입니다.");
}
public double getArea() {
return radius*radius*PI;
}
}
public class Practice {
public static void main(String[] args) {
Shape donut = new Circle(10);
donut.redraw();
System.out.println("면적은 " + donut.getArea());
}
}
prac 5_14
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; //반지름 변수 radius 선언
public Circle(int radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("반지름이 " + radius + "인 원입니다.");
}
public double getArea() {
return radius*radius*PI;
}
}
class Oval implements Shape {
private int a, b;
public Oval(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public void draw() {
System.out.println(a + " x " + b + "에 내접하는 타원입니다.");
}
public double getArea() {
return a*b*PI;
}
}
class Rect implements Shape {
private int a, b;
public Rect(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public void draw() {
System.out.println(a + " x " + b + "크기의 사각형입니다.");
}
public double getArea() {
return a*b;
}
}
public class Practice {
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());
}
}
'JAVA > 명품 JAVA 실습문제' 카테고리의 다른 글
명품 JAVA 2장 실습문제 (0) | 2023.07.25 |
---|---|
명품 JAVA 9장 실습문제 (0) | 2023.07.09 |
명품 JAVA 8장 실습문제 (0) | 2023.06.10 |
명품 JAVA 7장 실습문제 (0) | 2023.06.09 |
명품 JAVA 6장 실습문제 (0) | 2023.06.06 |