실습문제
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 {
private int color;
public ColorTV(int size, int color) {
super(size);
this.color = color;
}
public void printProperty() {
System.out.println(getSize() + "인치 " + color + "컬러");
}
}
public class main {
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 {
private int color;
public ColorTV(int size, int color) {
super(size);
this.color = color;
}
protected int getColor() {
return 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.println("나의 IPTV는 " + address + " " + getSize() + "인치 " + getColor() + "컬러");
}
}
public class main {
public static void main(String[] args) {
IPTV myTV = new IPTV("192.1.1.2", 32, 1024);
myTV.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{
protected double ratio;
public Won2Dollar(int ratio){this.ratio = ratio;}
protected double convert(double src){ return src / ratio; }
protected String getSrcString() {return "원";}
protected String getDestString() {return "달러";}
}
public class main {
public static void main(String args[]) {
Won2Dollar toDollar = new Won2Dollar(1200); // 1달러는 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{
protected double ratio;
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 main {
public static void main(String args[]) {
Km2Mile toMile = new Km2Mile(1.6); // 1마일은 1.6km
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{
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() + ") 의 점";
}
}
class main {
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{
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 color + "색의 (" + getX() + "," + getY() + ") 의 점";
}
}
class main {
public static void main(String[] args) {
ColorPoint zeroPoint = new ColorPoint(); // (0,0) 위치의 BLACK 색 점
System.out.println(zeroPoint.toString() + "입니다.");
ColorPoint cp = new ColorPoint(10, 10); // (10,10) 위치의 BLACK 색 점
cp.setXY(5,5);
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{
private int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
public void move(int x, int y, int z){
move(x,y);
this.z = z;
}
public void moveUp(){
this.z = z + 1;
}
public void moveDown(){
this.z = z - 1;
}
public String toString(){
return "(" + getX() + ", " + getY() + ", " + z + ")의 점";
}
}
class main {
public static void main(String[] args) {
Point3D p = new Point3D(1,2,3); // 1,2,3은 각각 x, y, z축의 값.
System.out.println(p.toString()+"입니다.");
p.moveUp(); // z 축으로 위쪽 이동
System.out.println(p.toString()+"입니다.");
p.moveDown(); // z 축으로 아래쪽 이동
p.move(10, 10); // x, y 축으로 이동
System.out.println(p.toString()+"입니다.");
p.move(100, 200, 300); // x, y, z축으로 이동
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{
public PositivePoint() {
super(0, 0);
}
public PositivePoint(int x, int y) {
super(x, y);
if(x < 0 || y < 0){
super.move(0,0);
}
}
public void move(int x, int y){
if(x>0 && y>0) {
super.move(x, y);
}
}
public String toString(){
return "(" + getX() + ", " + getY() + ")의 점";
}
}
class main {
public static void main(String[] args) {
PositivePoint p = new PositivePoint();
p.move(10, 10);
System.out.println(p.toString()+"입니다.");
p.move(-5,5); // 객체 p는 음수 공간으로 이동되지 않음
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(); // 스택의 톱(top)에 실수 저장
boolean push(String val); // 스택의 톱(top)에 저장된 실수 리턴
}
class StackApp implements Stack{
private int size;
private int cnt;
private String[] stack;
public StackApp(int size){
cnt = 0;
this.size = size;
stack = new String[size];
}
public int length(){
return stack.length;
}
public int capacity(){
return size - cnt;
}
public String pop(){
return stack[--cnt];
}
public boolean push(String val){
if(capacity() <= 0){
return false;
}
else{
stack[cnt] = val;
cnt++;
return true;
}
}
}
class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("총 스택 저장 공간의 크기 입력 >> ");
int size = sc.nextInt();
StackApp sa = new StackApp(size);
while(true){
System.out.print("문자열 입력 >>");
String tmp = sc.next();
if(tmp.equals("그만")) break;
else{
boolean s = sa.push(tmp);
if(!s){
System.out.println("스택이 꽉 차서 푸시 불가!");
}
}
}
System.out.print("스택에 저장된 모든 문자열 팝:");
int cntTmp = sa.capacity();
for(int i = 0; i < sa.length() - cntTmp; i++){
System.out.print(sa.pop() + " ");
}
}
}
10번
import java.util.Scanner;
abstract class PairMap {
protected String keyArray[]; // key 들을 저장하는 배열
protected String valueArray[]; // value 들을 저장하는 배열
abstract String get(String key); // key 값을 가진 value 리턴, 없으면 null 리턴
abstract void put(String key, String value); // key와 value를 쌍으로 저장. 기존에 key가 있으면, 값을 value로 수정
abstract String delete(String key); // key 값을 가진 아이템 (value와 함꼐) 삭제, 삭제된 value 값 리턴
abstract int length(); // 현재 저장된 아이템의 개수 리턴
}
class Dictionary extends PairMap{
private int size;
private int cnt;
public Dictionary(int size) { this.size = size; keyArray = new String[size]; valueArray = new String[size]; cnt = 0;}
String get(String key){
for(int i = 0; i < cnt; i++){
if(keyArray[i].equals(key)){
return valueArray[i];
}
}
return "null";
}
void put(String key, String value){
int index = cnt;
for(int i = 0; i < cnt; i++){
if(key.equals(keyArray[i])){
index = i;
break;
}
}
keyArray[index] = key;
valueArray[index] = value;
cnt++;
}
String delete(String key){
String temp = get(key);
int index = cnt;
for(int i = 0; i < cnt; i++){
if(key.equals(keyArray[i])){
index = i;
break;
}
}
keyArray[index] = "";
valueArray[index] = "";
cnt--;
return temp;
}
int length(){
return cnt;
}
}
class main {
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("황기태")); //삭제된 아이템 접근
}
}
11번
import java.util.Scanner;
abstract class Calc{
protected int a;
protected int b;
void setValue(int a, int b) {this.a = a; this.b = b;}
abstract int calculate();
}
class Add extends Calc{
public Add(){}
int calculate() {return a + b;}
}
class Sub extends Calc{
public Sub(){}
int calculate() {return a - b;}
}
class Mul extends Calc{
public Mul(){}
int calculate() {return a * b;}
}
class Div extends Calc{
public Div(){}
int calculate() {return a / b;}
}
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("두 정수와 연산자를 입력하시오>> ");
int a = sc.nextInt();
int b = sc.nextInt();
char c = sc.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());
}
}
}
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(){}
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(){}
public void run(){
sc = new Scanner(System.in);
System.out.println("그래픽 에디터 beauty를 실행합니다.");
while(true){
System.out.print("삽입(1), 삭제(2), 모두 보기(3), 종료(4)>> ");
int input = sc.nextInt();
if(input == 1){
insert();
}
else if(input == 2){
System.out.print("삭제할 도형의 위치 >>");
int dInput = sc.nextInt();
delete(dInput);
}
else if(input == 3){
print();
}
else{
break;
}
}
}
void insert(){
Shape s;
System.out.print("Line(1), Rect(2), Circle(3)>> ");
int iInput = sc.nextInt();
if(iInput == 1){
s = new Line();
}
else if(iInput == 2){
s = new Rect();
}
else{
s = new Circle();
}
if(head == null){
head = s;
tail = s;
}
else{
tail.setNext(s);
tail = s;
}
}
public void delete(int num) {
Shape cur = head; // 현재 노드
Shape tmp = 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++) {
tmp = cur;
cur = cur.getNext();
if(cur == null) {
System.out.println("삭제할 수 없습니다.");
return;
}
}
tmp.setNext(cur.getNext());
}
public void print() {
Shape s = head;
while(s != null) {
s.draw();
s = s.getNext();
}
}
}
public class main {
public static void main(String[] args) {
GraphicEditor ge = new GraphicEditor();
ge.run();
}
}
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 void draw() {
System.out.println("반지름이 "+radius+"인 원입니다.");
}
public double getArea() {
return PI*radius*radius;
}
}
public class main {
public static void main(String[] args) {
Shape donut = new Circle(10); // 반지름이 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 void draw() {
System.out.println("반지름이 "+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(a + "x" + 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(a + "x" + b + "크기의 사각형.");
}
public double getArea() {
return a*b;
}
}
public class main {
public static void main(String[] args) {
Shape[] list = new Shape[3]; // Shape을 상속받은 클래스 객체의 레퍼런스 배열
list[0] = new Circle(10); // 반지름이 10인 원 객체
list[1] = new Oval(20, 30); // 20x30 사각형에 내접하는 타원
list[2] = new Rect(10, 40); // 10x40 크기의 사각형
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] 명품 자바 프로그래밍 7장 실습문제 (0) | 2022.06.14 |
---|---|
[JAVA] 명품 자바 프로그래밍 6장 실습문제 (0) | 2022.06.12 |
[JAVA] 명품 자바 프로그래밍 4장 실습문제 (0) | 2022.04.17 |
[JAVA] 명품 자바 프로그래밍 3장 실습문제 (0) | 2022.04.15 |
[JAVA] 명품 자바 프로그래밍 2장 실습문제 (0) | 2022.04.14 |