티스토리 뷰
반응형
✏️ 5장 실습문제 - 1번
package ex;
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()+"인치 "+this.Color+"컬러");
}
}
public class SolveProblem {
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32,1024);
myTV.printProperty();
}
}
✏️ 5장 실습문제 - 2번
package ex;
import java.util.Scanner;
class TV{
private int size;
public TV(int size) {
this.size = size;
}
protected int getSize(){
return size;
}
}
class IPTV extends TV{
private int color;
private String IP;
public IPTV(String IP, int size, int color){
super(size);
this.IP=IP;
this.color=color;
}
public void printProperty() {
System.out.println("나의 IPTV는 "+this.IP+" 주소의 "+getSize()+"인치 "+this.color+"컬러");
}
}
public class SolveProblem {
public static void main(String[] args) {
IPTV iptv = new IPTV("192.1.1.2",32,2048);
iptv.printProperty();
}
}
✏️ 5장 실습문제 - 3번
package ex;
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 dollar){
ratio=dollar;
}
@Override
protected String getSrcString() {
return "원";
}
@Override
protected double convert(double src) {
return src/ratio;
}
@Override
protected String getDestString() {
return "달러";
}
}
public class SolveProblem {
public static void main(String[] args) {
Won2Dollar toDollar = new Won2Dollar(1200);
toDollar.run();
}
}
✏️ 5장 실습문제 - 4번
package ex;
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 a){
ratio=a;
}
@Override
protected double convert(double src) {
return src/ratio;
}
@Override
protected String getSrcString() {
return "km";
}
@Override
protected String getDestString() {
return "mile";
}
}
public class SolveProblem {
public static void main(String[] args) {
km2Mile toMile = new km2Mile(1.6);
toMile.run();
}
}
✏️ 5장 실습문제 - 5번
package ex;
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(){
String str = this.color+"색의 ("+getX()+","+getY()+")의 점";
return str;
}
}
public class SolveProblem {
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+"입니다.");
}
}
✏️ 5장 실습문제 - 6번
package ex;
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);
}
public void setXY(int x, int y) {
move(x, y);
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
String str = this.color + "색의 (" + getX() + "," + getY() + ")의 점";
return str;
}
}
public class SolveProblem {
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() + "입니다.");
}
}
✏️ 5장 실습문제 - 7번
package ex;
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 String toString(){
String str = "("+getX()+","+getY()+","+z+")의 점";
return str;
}
public void moveUp() {
z++;
}
public void moveDown() {
z--;
}
public void move(int x,int y,int z){
move(x,y);
this.z=z;
}
}
public class SolveProblem {
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()+"입니다");
}
}
✏️ 5장 실습문제 - 8번
package ex;
import java.util.Scanner;
class Point {
private int x, y;
public Point(){
this.x=0;
this.y=0;
}
public Point(int x, int y) {
if(x<0||y<0){
this.x=0;
this.y=0;
}
else {
this.x = x;
this.y = y;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
protected void move(int x, int y) {
if(x<0||y<0) {
this.x = 0;
this.y = 0;
}
else {
this.x = x;
this.y = y;
}
}
}
class PositiviePoint extends Point {
public PositiviePoint(){
super();
}
public PositiviePoint(int x, int y) {
super(x, y);
}
public String toString() {
String str = "(" + getX() + "," + getY() + ")의 점";
return str;
}
@Override
protected void move(int x, int y) {
if(x<0||y<0){
return;
}
else
super.move(x,y);
}
}
public class SolveProblem {
public static void main(String[] args) {
PositiviePoint p = new PositiviePoint();
p.move(10, 10);
System.out.println(p.toString() + "입니다.");
p.move(-5, 5);
System.out.println(p.toString() + "입니다.");
PositiviePoint p2 = new PositiviePoint(-10, -10);
System.out.println(p2.toString() + "입니다.");
}
}
✏️ 5장 실습문제 - 9번
package ex;
import java.util.Scanner;
interface Stack {
int length();
int capacity();
String pop();
boolean push(String val);
}
class StringStack implements Stack {
String [] str;
int size;
int top;
public StringStack(int size) {
this.size=size;
str = new String[this.size];
top=this.size;
}
@Override
public int length() {
return size-top;
}
@Override
public int capacity() {
return top;
}
@Override
public String pop() {
int tmp=top;
top++;
return str[tmp];
}
@Override
public boolean push(String val) {
if(top>0){
str[top-1]=val;
top--;
return true;
}
else{
return false;
}
}
}
public class SolveProblem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("총 스택 저장 공간의 크기 입력 >> ");
int size = scanner.nextInt();
StringStack stringStack = new StringStack(size);
while (true) {
System.out.print("문자열 입력 >> ");
String str = scanner.next();
if(str.equals("그만")) {
int len = stringStack.length();
if(len==0){
System.out.println("현재 스택에 저장된 문자열이 없습니다.");
}
else {
System.out.print("스택에 저장된 모든 문자열팝 : ");
for (int i = 0; i < len; i++)
System.out.print(stringStack.pop() + " ");
break;
}
}
boolean isCheck = stringStack.push(str);
if(!isCheck)
System.out.println("스택이 꽉 차서 푸시 불가!");
}
}
}
✏️ 5장 실습문제 - 10번
package ex;
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{
private int size;
private int index=0;
public Dictionary(int size){
this.size=size;
keyArray = new String[this.size];
valueArray = new String[this.size];
}
@Override
String get(String key) {
String str="";
for(int i=0;i< keyArray.length;i++){
if(key.equals(keyArray[i])){
str=valueArray[i];
break;
}
}
return str;
}
@Override
void put(String key, String value) {
for(int i=0;i< keyArray.length;i++){
if(key.equals(keyArray[i])){
valueArray[i]=value;
return;
}
}
keyArray[index] = key;
valueArray[index] = value;
index++;
}
@Override
String delete(String key) {
String str="";
for(int i=0;i< keyArray.length;i++){
if(key.equals(keyArray[i])){
str=valueArray[i];
valueArray[i]=null;
break;
}
}
return str;
}
@Override
int length() {
return index;
}
}
public class SolveProblem {
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("황기태"));
}
}
✏️ 5장 실습문제 - 11번
package ex;
import java.util.Scanner;
abstract class Calc{
abstract public void setValue(int a,int b);
abstract public int calculate(int a,int b);
}
class Add extends Calc {
int a, b;
@Override
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int calculate(int a, int b) {
return a+b;
}
}
class Sub extends Calc {
int a, b;
@Override
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int calculate(int a, int b) {
return a-b;
}
}
class Mul extends Calc{
int a, b;
@Override
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int calculate(int a, int b) {
return a*b;
}
}
class Div extends Calc {
int a, b;
@Override
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int calculate(int a, int b) {
return a/b;
}
}
public class SolveProblem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Add add = new Add();
Sub sub = new Sub();
Mul mul = new Mul();
Div div = new Div();
while (true) {
System.out.print("두 정수와 연산자를 입력하시오(종료: 숫자에 0 입력) >> ");
int a = sc.nextInt();
int b = sc.nextInt();
char c = sc.next().charAt(0);
if(a==0 && b==0 && c=='0') {
System.out.println("프로그램을 종료합니다.");
sc.close();
break;
}
switch (c) {
case '+' : add.setValue(a, b); System.out.println(add.calculate(a,b)); break;
case '-' : sub.setValue(a, b); System.out.println(sub.calculate(a,b)); break;
case '*' : mul.setValue(a, b); System.out.println(mul.calculate(a,b)); break;
case '/' : div.setValue(a, b); System.out.println(div.calculate(a,b)); break;
default : System.out.println("연산자를 잘못 입력하셨습니다.");
}
}
}
}
✏️ 5장 실습문제 - 12번
✏️ 5장 실습문제 - 13번
package ex;
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 r;
public Circle(int r){
this.r = r;
}
@Override
public void draw() {
System.out.println("반지름이 "+this.r+"인 원입니다.");
}
@Override
public double getArea() {
return r*r*PI;
}
@Override
public void redraw() {
System.out.print(" --- 다시 그립니다.");
draw();
}
}
public class SolveProblem{
public static void main(String[] args) {
Shape donut = new Circle(10);
donut.redraw();
System.out.println("면적은 "+donut.getArea());
}
}
✏️ 5장 실습문제 - 14번
package ex;
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 r;
public Circle(int r){
this.r = r;
}
@Override
public void draw() {
System.out.println("반지름이 "+this.r+"인 원입니다.");
}
@Override
public double getArea() {
return r*r*PI;
}
@Override
public void redraw() {
Shape.super.redraw();
}
}
class Oval implements Shape{
private int x,y;
public Oval(int x,int y){
this.x=x;
this.y=y;
}
@Override
public void draw() {
System.out.println(x+"x"+y+"에 내접하는 타원입니다.");
}
@Override
public double getArea() {
return x*y*PI;
}
@Override
public void redraw() {
Shape.super.redraw();
}
}
class Rect implements Shape{
private int x,y;
public Rect(int x,int y){
this.x=x;
this.y=y;
}
@Override
public void draw() {
System.out.println(x+"x"+y+"크기의 사각형 입니다.");
}
@Override
public double getArea() {
return x*y;
}
@Override
public void redraw() {
Shape.super.redraw();
}
}
public class SolveProblem{
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());
}
}
반응형
'PL > JAVA' 카테고리의 다른 글
명품 자바 프로그래밍 7장 실습 문제 (0) | 2024.06.20 |
---|---|
명품 자바 프로그래밍 6장 실습 문제 (0) | 2024.06.20 |
명품 자바 프로그래밍 4장 실습 문제 (0) | 2024.06.18 |
명품 자바 프로그래밍 3장 실습 문제 (0) | 2024.06.18 |
명품 자바 프로그래밍 2장 실습 문제 (0) | 2024.06.18 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 우선순위 큐
- 자료구조
- 알고리즘 공부
- 에라토스테네스의 체
- Do it!
- C++
- 유클리드 호제법
- CSS
- 자바
- 백준
- DP
- HTML5
- C++ Stack
- 유니온 파인드
- 백준 풀이
- 카운팅 정렬
- BFS
- html
- 반복문
- 세그먼트 트리
- js
- DFS
- 자바스크립트
- 이분 매칭
- c++ string
- java
- 투 포인터
- 스택
- 스프링 부트 crud 게시판 구현
- 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함