-
반응형
1번.
class TV{ private int size; public TV(int size) { this.size=size;} protected int getSize() { return size;} } class ColorTV extends TV{ private int colorSize; public ColorTV(int size,int colorSize) { super(size); this.colorSize=colorSize; } public void printProperty() { System.out.println(getSize()+"인치 "+colorSize+"컬러"); } } public class p0501 { public static void main(String[] args) { // TODO Auto-generated method stub ColorTV myTV= new ColorTV(32,1024); myTV.printProperty(); } }
2번.
class TV{ private int size; public TV(int size) { this.size=size;} protected int getSize() { return size;} } class ColorTV extends TV{ private int colorSize; public ColorTV(int size,int colorSize) { super(size); this.colorSize=colorSize; } public void printProperty() { System.out.println(getSize()+"인치 "+colorSize+"컬러"); } } class IPTV extends ColorTV{ String ipAddress; public IPTV(String ip,int size,int colorSize) { super(size,colorSize); ipAddress=ip; } @Override public void printProperty() { System.out.print("나의 IPTV는 "+ipAddress+" 주소의 "); super.printProperty(); } } public class p0502 { public static void main(String[] args) { // TODO Auto-generated method stub IPTV iptv= new IPTV("192.1.1.2",32,2048); iptv.printProperty(); } }
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 p0503 { public static void main(String[] args) { // TODO Auto-generated method stub Won2Dollar toDollar=new Won2Dollar(1200); toDollar.run(); } }
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 p0504 { public static void main(String[] args) { // TODO Auto-generated method stub Km2Mile toMile=new Km2Mile(1.6); toMile.run(); } }
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 p0505 { 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번.
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); color="BLACK"; } public ColorPoint(int x,int y) { super(x,y); color="BLACK"; } 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 p0506 { public static void main(String[] args) { // TODO Auto-generated method stub 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번.
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 void moveUp() { z++; } public void moveDown() { z--; } public String toString() { return "("+getX()+","+getY()+","+z+")의 점"; } public void move(int x,int y,int z) { super.move(x, y); this.z=z; } } public class p0507 { public static void main(String[] args) { // TODO Auto-generated method stub 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번.
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(getX()<0||getY()<0) { move(0,0); } } public String toString() { return "("+getX()+","+getY()+")"+"의 점"; } protected void move(int x1,int y1) { if(x1<0||y1<0) { return; } else { super.move(x1, y1); } } } public class p0508 { public static void main(String[] args) { // TODO Auto-generated method stub 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번.
import java.util.Scanner; interface Stack{ int length(); int capacity(); String pop(); boolean push(String val); } class StringStack implements Stack{ String[] arr; int len,cap; public StringStack(int n) { arr=new String[n]; len=0; cap=n; } public int length() { return len; } public int capacity() { return cap; } public String pop() { return arr[--len]; } public boolean push(String val) { if(len==cap) { System.out.println("스택이 꽉 차서 푸시 불가!"); return false; } else { arr[len++]=val; return true; } } public boolean isEmpty() { if(len==0) { return true; } else { return false; } } public void print() { System.out.print("스택에 저장된 모든 문자열 팝 :"); while(!isEmpty()) { System.out.print(" "+pop()); } System.out.println(); } } public class p0509 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); System.out.print("총 스택 저장 공간의 크기 입력 >>"); int n=sc.nextInt(); StringStack ss=new StringStack(n); String op; while(true) { System.out.print("문자열 입력 >> "); op=sc.next(); if(op.equals("그만")) { break; } ss.push(op); } ss.print(); } }
10번.
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 num; //생성자로 배열을 초기화 public Dictionary(int n) { num=0; keyArray=new String[n]; valueArray=new String[n]; } String get(String key) { for(int i=0;i<num;i++) { //입력된 키값과 같은게 있으면 리턴 if(keyArray[i].equals(key)){ return valueArray[i]; } } return null; } void put(String key,String value) { for(int i=0;i<num;i++) { //만약에 키값이 같은게 있으면 벨류값 수정 if(keyArray[i].equals(key)) { valueArray[i]=value; return; } } //아니면 추가 valueArray[num]=value; keyArray[num++]=key; return; } String delete(String key) { String deleteData=null; for(int i=0;i<num;i++) { //삭제하려는 키값이 존재하면 저장 후 삭제 if(keyArray[i].equals(key)){ deleteData=valueArray[i]; for(int j=i;j<num-1;j++) { valueArray[j]=valueArray[j+1]; keyArray[j]=keyArray[j+1]; } num--; } } //저장한 값을 반환 return deleteData; } int length() { return num; } } public class p0510 { public static void main(String[] args) { // TODO Auto-generated method stub 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번.
import java.util.Scanner; abstract class Calc{ int a,b; abstract void setValue(int a,int b); abstract int calculate(); } class Add extends Calc{ void setValue(int a,int b) { this.a=a; this.b=b; } int calculate() { return a+b; } } class Sub extends Calc{ void setValue(int a,int b) { this.a=a; this.b=b; } int calculate() { return a-b; } } class Mul extends Calc{ void setValue(int a,int b) { this.a=a; this.b=b; } int calculate() { return a*b; } } class Div extends Calc{ void setValue(int a,int b) { this.a=a; this.b=b; } int calculate() { if(b==0) { System.out.println("0으로 나눌 수 없어요"); } else { System.out.println(a/b); } } } public class p0511 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); System.out.print("두 정수와 연산자를 입력하시오>>"); int a=sc.nextInt(); int b=sc.nextInt(); String op=sc.next(); Calc sp; switch(op.charAt(0)) { case '+': sp=new Add(); break; case '-': sp=new Sub(); break; case '*': sp=new Mul(); break; case '/': sp=new Div(); break; default: return ; } sp.setValue(a, b); System.out.println(sp.calculate()); } }
12번.
import java.util.Scanner; abstract class 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() { super(); } //오버라이딩 public void draw() { System.out.println("Line"); } } class Rect extends Shape{ public Rect() { super(); } //오버라이딩 public void draw() { System.out.println("Rect"); } } class Circle extends Shape{ public Circle() { super(); } //오버라이딩 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 runEditor() { int choice; System.out.println("그래픽 에디터 beauty을 실행합니다."); do { System.out.print("삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>"); choice=sc.nextInt(); switch(choice) { case 1: System.out.print("Line(1), Rect(2), Circle(3)>>"); int option=sc.nextInt(); put(option); break; case 2: System.out.print("삭제할 도형의 위치>>"); int index=sc.nextInt(); delete(index); break; case 3: print(); break; case 4: break; default: System.out.println("다시 입력해주세요"); break; } }while(choice!=4); System.out.println("beauty을 종료합니다."); } //리스트에 넣는 메서드 void put(int num) { Shape g; //입력번호에 따라 Line,Rect,Circle객체 생성 switch(num) { case 1: g=new Line(); break; case 2: g=new Rect(); break; case 3: g=new Circle(); break; default: System.out.println("다시입력해주세요"); return; } //리스트에 연결 if(head==null) { head=g; tail=g; } else { tail.setNext(g); tail=g; } return; } void delete(int index) { Shape cur=head; Shape temp=head; if(head==null) { System.out.println("삭제할 수 없습니다."); return; } //리스트에서 해당 인덱스의 노드 삭제 if(index==1) { if(head==tail) { tail=head=null; return; } else { head=head.getNext(); return; } } else { int i; for(i=1;i<index;i++) { temp=cur; cur=cur.getNext(); if(cur==null) { System.out.println("삭제할 수 없습니다."); return; } } if(cur==tail) { tail=temp; tail.setNext(null); } else { temp.setNext(cur.getNext()); } } } void print() { Shape d=head; while(d!=null) { d.draw(); d=d.getNext(); } } } public class p0512 { public static void main(String[] args) { // TODO Auto-generated method stub GraphicEditor ge=new GraphicEditor(); ge.runEditor(); } }
13번.
interface Shape{ final double PI=3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r=r; } public void draw() { System.out.println("반지름이 "+r+"인 원 입니다."); } public double getArea() { return r*r*PI; } } public class p0513 { public static void main(String[] args) { // TODO Auto-generated method stub Shape donut=new Circle(10); donut.redraw(); System.out.println("면적은 "+donut.getArea()); } }
14번.
interface Shape{ final double PI=3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r=r; } public void draw() { System.out.println("반지름이 "+r+"인 원 입니다."); } public double getArea() { return r*r*PI; } } class Oval implements Shape{ int x,y; public Oval(int x,int y) { this.x=x; this.y=y; } public double getArea() { return x*y*PI; } public void draw() { System.out.println(x+"x"+y+"에 내접하는 타원입니다."); } } class Rect implements Shape{ int x,y; public Rect(int x,int y) { this.x=x; this.y=y; } public double getArea() { return x*y; } public void draw() { System.out.println(x+"x"+y+"크기의 사각형 입니다."); } } public class p0514 { public static void main(String[] args) { // TODO Auto-generated method stub 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()); } }
반응형'문제풀이 > 명품 자바 프로그래밍(개정4판)' 카테고리의 다른 글
명품 자바 프로그래밍(개정4판) 제 7장 실습문제 (0) 2020.12.07 명품 자바 프로그래밍(개정4판) 제 6장 실습문제 (0) 2020.11.22 명품 자바 프로그래밍(개정4판) 제 4장 실습문제 (0) 2020.10.12 명품 자바 프로그래밍(개정4판) 제 3장 실습문제 (0) 2020.10.08 명품 자바 프로그래밍(개정4판) 제 2장 실습문제 (0) 2020.10.08 댓글