쌓고 쌓다

[JAVA] 명품 자바 프로그래밍 5장 실습문제 본문

프로그래밍/java

[JAVA] 명품 자바 프로그래밍 5장 실습문제

승민아 2022. 5. 18. 22:00

1번 문제

import java.util.Scanner;

class TV{
	private int size;
	public TV(int size) { this.size = size; }
	protected int getSize() { return size; }
}

class ColorTV extends TV
{
	int color;
	
	public ColorTV(int size, int color)
	{
		super(size);
		this.color=color;
	}
	
	public void printProperty()
	{
		System.out.println(getSize()+"인치 "+color+"컬러");
	}
	
}

public class Extest{
	
    public static void main(String[] args) {
    	ColorTV myTV = new ColorTV(32,1024);
    	myTV.printProperty();
    }
	
}

 

2번 문제

import java.util.Scanner;

class TV{
	private int size;
	public TV(int size) { this.size = size; }
	protected int getSize() { return size; }
}

class ColorTV extends TV
{
	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
{
	String address;
	public IPTV(String address, int size, int color)
	{
		super(size,color);
		this.address=address;
	}
	
	public void printProperty()
	{
		System.out.print("나의 IPTV는 "+address+" 주소의 ");
		super.printProperty();
		
	}
}


public class Extest{
	
    public static void main(String[] args) {
    	IPTV iptv = new IPTV("192.1.1.2",32,2048);
    	iptv.printProperty();
    	
    }
	
}

 

3번 문제

추상클래스는 private로 선언 불가

추상클래스는 상속 받아 사용하게 만드는것인데 private가 말이 될까요!?!?

import java.util.Scanner;

abstract class Converter {
	protected double ratio;
	abstract protected double convert(double src);
	abstract protected String getSrcString();
	abstract protected String getDestString();
	
	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{
	
	int won;
	
	Won2Dollar(double ratio)
	{
		this.ratio=ratio;
	}
	
	protected String getSrcString()
	{
		return "원";
	}
	
	protected String getDestString()
	{
		return "달러";
	}
	
	protected double convert(double src)
	{
		return src/ratio;
	}
	
}

public class Extest{
	
    public static void main(String[] args) {
    	Won2Dollar toDollar = new Won2Dollar(1200);
    	toDollar.run();
    }
	
}

 

4번 문제

import java.util.Scanner;

abstract class Converter {
	protected double ratio;
	abstract protected double convert(double src);
	abstract protected String getSrcString();
	abstract protected String getDestString();
	
	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{
	
	int won;
	
	Won2Dollar(double ratio)
	{
		this.ratio=ratio;
	}
	
	protected String getSrcString()
	{
		return "원";
	}
	
	protected String getDestString()
	{
		return "달러";
	}
	
	protected double convert(double src)
	{
		return src/ratio;
	}
	
}

class Km2Mile extends Converter
{
	
	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 Extest{
	
    public static void main(String[] args) {
    	Km2Mile toMile = new Km2Mile(1.6);
    	toMile.run();
    }
	
}

 

5번 문제

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{
	
	String color;
	
	ColorPoint(int x, int y, String color)
	{
		super(x,y);
		this.color=color;
	}
	
	void setXY(int x, int y)
	{
		move(x,y);
	}
	
	void setColor(String color)
	{
		this.color=color;
	}
	
	public String toString()
	{
		return color+"색의 ("+getX()+","+getY()+")의 점";
	}
}

public class Extest{
	
    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번 문제

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{
	
	String color;
	
	ColorPoint(int x, int y, String color)
	{
		super(x,y);
		this.color=color;
	}
	
	ColorPoint()
	{
		super(0,0);
		this.color="BLACK";
	}
	
	void setXY(int x, int y)
	{
		move(x,y);
	}
	
	void setColor(String color)
	{
		this.color=color;
	}
	
	public String toString()
	{
		return color+"색의 ("+getX()+","+getY()+")의 점";
	}
}

public class Extest{
	
    public static void main(String[] args) {
    	ColorPoint zeroPoint = new ColorPoint();
    	System.out.println(zeroPoint.toString()+"입니다.");
    	
    	ColorPoint cp = new ColorPoint(5, 5,"YELLOW");
    	cp.setXY(10, 20);
    	cp.setColor("RED");
    	System.out.println(cp.toString()+"입니다.");
    	
    }
	
}

 

7번 문제

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
{
	
	int z;
	Point3D(int x, int y, int z)
	{
		super(x,y);
		this.z=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 void moveUp()
	{
		z+=1;
	}
	
	public void moveDown()
	{
		z-=1;
	}

}

public class Extest{
	
    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번 문제

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 PositivePoint extends Point
{
	
	PositivePoint(int x, int y)
	{
		super(x,y);
		if(x<0||y<0)
			move(0,0);
	}
	
	PositivePoint()
	{
		super(0,0);
	}
	
	protected void move(int x,int y)
	{
		if(x<0||y<0)
			return;
		else
			super.move(x, y);
	}
	
	public String toString()
	{
		return "("+getX()+","+getY()+")"+"의 점";
	}
	
}

public class Extest{
	
    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번 문제

import java.util.Scanner;

interface Stack{
	
	int length();
	int capacity();
	String pop();
	boolean push(String val);
}

class StringStack implements Stack
{
	String str[];
	int len;
	int cap;
	
	StringStack(int size)
	{
		len=0;
		cap=size;
		str = new String[size];
	}
	
	public int length()
	{
		return len;
	}
	
	public int capacity()
	{
		return cap;
	}
	
	public String pop() {
		return str[--len];
	}
	public boolean push(String val)
	{
		if(len==cap)
			return false;
		
		str[len++] = val;
		return true;
	}
}

public class StackApp
{
	public static void main(String[] args) {
	    
    	System.out.print("총 스택 크기 입력>>");
    	Scanner scanner = new Scanner(System.in);
    	int size = scanner.nextInt();
    	StringStack stringstack = new StringStack(size);
    	while(true)
    	{
    		System.out.print("문자열 입력 >>");
    		String str = scanner.next();
    		if(str.equals("그만"))
    			break;
    		if(!stringstack.push(str))
    			System.out.println("스택이 꽉차서 불가능");		
    	}
    	System.out.print("스택에 저장된 모든 문자열 팝 :");
    	int cnt=stringstack.len;
    	for(int i=0;i<cnt;i++)
    		System.out.print(stringstack.pop()+" ");
    }
	
	
}

/*public class Extest{
    
}*/

 

10번 문제

import java.util.Scanner;

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
{
	protected String keyArray[];
	protected String valueArray[];
	int len;
	Dictionary(int size)
	{
		len=0;
		keyArray = new String[size];
		valueArray = new String[size];
	}
	String get(String key)
	{
		
		for(int i=0;i<len;i++)
		{
			if(keyArray[i].equals(key))
				return valueArray[i];
		}
		return null;
	}
	void put(String key, String value)
	{
		for(int i=0;i<len;i++)
		{
			if(keyArray[i].equals(key))
				valueArray[i]=value;
		}
		
		keyArray[len]=key;
		valueArray[len++]=value;
	}
	String delete(String key)
	{
		for(int i=0;i<len;i++)
		{
			if(keyArray[i].equals(key)) {
				String str = valueArray[i];
				valueArray[i]=null;
				return str;
			}
		}
		return null;
	}
	int length()
	{
		return len;
	}
}

public class DictionaryApp
{
	
	public static void main(String[] args)
	{
		Dictionary dic = new Dictionary(10);
		dic.put("황기태", "자바");
		dic.put("이재문", "파이썬");
		dic.put("이재문", "C++"); // 이재문의 값을 C++로 수정
		System.out.println("이재문의 값은 "+dic.get("이재문"));
		System.out.println("황기태의 값은 "+dic.get("황기태"));
		dic.delete("황기태"); // 황기태 아이템 삭제
		System.out.println("황기태의 값은 "+dic.get("황기태"));
	}
	
}

/*public class Extest{
    
}*/

 

11번 문제

import java.util.Scanner;

abstract class Calc{
	

	int a,b;
	int res;
	abstract void setValue(int a, int b);
	
	int calculate()
	{
		return res;
	}	
}

class Add extends Calc{
	
	void setValue(int a, int b)
	{
		res=a+b;
	}

}

class Sub extends Calc{
	void setValue(int a, int b)
	{
		res=a-b;
	}

}
class Mul extends Calc{
	
	void setValue(int a, int b)
	{
		res=a*b;
	}
	
}
class Div extends Calc{
	int a,b;
	int res;
	
	void setValue(int a, int b)
	{
		res=a/b;
	}

}

public class Test {
		public static void main(String[] args) {	
			Scanner scanner = new Scanner(System.in);
			System.out.print("두 정수와 연산자를 입력하시오>>");
			int a=scanner.nextInt();
			int b=scanner.nextInt();
			char c=scanner.next().charAt(0);
			if(c=='+') {
				Add add = new Add();
				add.setValue(a, b);
				System.out.println(add.calculate());
			}
			else if(c=='-') {
				Sub sub = new Sub();
				sub.setValue(a, b);
				System.out.println(sub.calculate());
			}
			else if(c=='*')
			{
				Mul mul = new Mul();
				mul.setValue(a, b);
				System.out.println(mul.calculate());
			}
			else if(c=='/')
			{

				Div div = new Div();
				div.setValue(a, b);
				System.out.println(div.calculate());
			}
	}
}

 

13번 문제

import java.util.Scanner;

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 double getArea()
	{
		return PI*radius*radius;
	}
	
	public void draw()
	{
		System.out.println("반지름이"+radius+"인 원입니다.");
	}
	
}

public class Extest{
    
	public static void main(String[] args) {
	Shape donut = new Circle(10);
	donut.redraw();
	System.out.println("면적은 "+donut.getArea());
	}
}

 

14번 문제

import java.util.Scanner;

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 double getArea()
	{
		return PI*radius*radius;
	}
	
	public void draw()
	{
		System.out.println("반지름이"+radius+"인 원입니다.");
	}
	
}

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 Extest{
    
	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());
	}
}

Comments