728x90
package day13;
// 상속문제
/*
#1.
아래 main()메소드 #1번과 아래 실행결과를 참고하여 TVClass를 상속받은 ColorTV클래스를 작성하세요.
실행결과 : 32인치 1024컬러
#2. 위 1번 문제를 푼후, main()메소드 #2번과 아래 실행결과를 참고하여 ColorTV를 상속받는 IPTV클래스를 작성하세요.
실행결과 : 나의 IPTV는 192.1.1.2 주소의 32인치 2048컬러
*/
class TVClass {
private int size=0;
public TVClass(int size) { this.size = size; }
protected int getSize() { return size; }
}
class ColorTV extends TVClass{
int color;
ColorTV(int size, int color){
super(size); //부모한테 보
this.color = color;
}
public void printProperty() {
System.out.println(getSize()+"사이즈"+color+"컬러");
}
}
class IPTV extends ColorTV{
String ip;
IPTV(String ip, int size, int color){
super(size, color); //부모한테 보냄
this.ip = ip;
}
public void printProperty() {
System.out.print(ip+"주소의 "); super.printProperty();
}
}
public class ClassEx20 {
public static void main(String[] args) {
//#1.
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
//#2.
IPTV iptv = new IPTV("192.1.1.2 ", 32, 2048);
iptv.printProperty();
}
}
package day13;
import java.util.Scanner;
// 추상클래스
/*
#1. Converter클래스를 상속받아 원화를 달러로 변환하는 Won2Dollar 클래스를 작성하세요.
main()메소드의 #1번 코드와 아래 콘솔 출력예를 참고하세요.
콘솔출력예)
원을 달라로 바꿉니다.
원을 입력하세요>> 24000 (<-사용자가 값 입력)
변환결과: 20.0달러 입니다
#2. Converter 클래스를 상속받아 Km를 Mile(마일)로 변환하는 Km2Mile 클래스를 작성하세요.
main()메소드 #2번 코드와 아래 콘솔 출력예를 참고하세요.
콘솔출력예)
Km을 mile로 바꿉니다.
Km을 입력하세요>> 30(<-사용자가 깂 입력)
변환결과: 18.75mile입니다
*/
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) {
System.out.println(getSrcString() + "을 "+getDestString()+ "로 바꿉니다.");
System.out.print(getSrcString() + "을 입력하세요>> ");
double val = scanner.nextDouble();
double res = convert(val);
System.out.println("변환결과: "+res+getDestString()+"입니다");
}
}
class Won2Dollar extends Converter{ // ratio, run
public 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{
public Km2Mile(double ratio) {this.ratio = ratio; }
protected String getSrcString() { return "km"; }
protected String getDestString() { return "mile"; }
protected double convert(double src) { return src/ratio; }
}
public class ClassEx21 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//#1.
Won2Dollar toDollar = new Won2Dollar(1200); // 1달러는 1200원
toDollar.run(sc);
//#2.
Km2Mile toMile = new Km2Mile(1.6); // 1마일은 1.6km
toMile.run(sc);
sc.close();
}
}
package day13;
// 상속, 생성자
/*
Point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하세요.
main()메소드 적힌 코드들을 포함하고 아래와 같은 실행결과 출력되게 만드세요.
실행결과:
RED색의 (10,20)의 점입니다.
*/
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;
}
@Override
public String toString() {
return color+ "색의"+ getX()+","+getY()+"의 점";
}
}
public class ClassEx22 {
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+"입니다.");
}
}
package day13;
// 상속, 생성자
/*
Point1를 상속받아 색을 가진 점을 나타내는 ColorPoint1 클래스를 작성하세요.
main()메소드 적힌 코드들을 포함하고 아래와 같은 실행결과 출력되게 만드세요.
실행결과:
BLACK색의 (0,0)의 점입니다.
RED색의 (5,5)의 점입니다.
*/
class Point1 {
public int x;
protected int y;
public Point1(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 ColorPoint1 extends Point1{//x,y getx() gety() move()
String color;
public ColorPoint1(){
super(0,0);
color="BLACK";
}
ColorPoint1(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;
}
@Override
public String toString() {
return color+ "색의"+ getX()+","+getY()+"의 점";
}
}
public class ClassEx23 {
public static void main(String[] args) {
//ColorPoint1 zeroPoint = new ColorPoint1(); // (0,0) 위치의 BLACK 색 점
//System.out.println(zeroPoint.toString() + "입니다.");
ColorPoint1 cp = new ColorPoint1(10,10); // (10,10) 위치의 BLACK 색 점
cp.setXY(5,5);
cp.setColor("RED");
System.out.println(cp.toString() + "입니다.");
}
}
package day13;
// 상속, 생성자
/*
Point2를 상속받아 3차원의 점을 나타내는 Point3D 클래스를 작성하세요.
main()메소드 적힌 코드들을 포함하고 아래와 같은 실행결과 출력되게 만드세요.
실행결과:
(1,2,3)의 점입니다.
(1,2,4)의 점입니다.
(10,10,3)의 점입니다.
(100,200,300)의 점입니다.
*/
class Point2 {
private int x, y;
public Point2(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 Point2{
int z;
Point3D(int x, int y, int z){
super(x,y);
this.z=z;
}
@Override
public String toString() {
return "("+getX()+","+getY()+","+z+")의 점";
}
void moveUp() {
z++;
}
void moveDown() {
z--;
}
void move() {
move(10,10);
}
void move(int x, int y , int z) {
move(x,y);
this.z = z;
}
}
public class ClassEx24 {
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() + "입니다.");
}
}
package day13;
// 상속, 생성자
/*
Point3를 상속받아 양수의 공간에서만 점을 나타내는 PositivePoint 클래스를 작성하세요.
main()메소드 적힌 코드들을 포함하고 아래와 같은 실행결과 출력되게 만드세요.
실행결과:
(10,10)의 점입니다.
(10,10)의 점입니다.
(0,0)의 점입니다.
*/
class Point3 {
private int x, y;
public Point3(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 Point3{
PositivePoint(){
super(0,0);
}
public void move(int x, int y) {
move(x,y);
}
@Override
public String toString() {
return "("+getX()+","+getY()+")의 점입니다.";
}
PositivePoint(int x ,int y){
super(x,y);
if(x<0 || y<0) {
super.move(0, 0);
}
}
}
public class ClassEx25 {
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() + "입니다.");
}
}
package day13;
// 인터페이스
/*
Stack 인터페이스를 상속받아 문자열을 저장하는 StringStack 클래스를 구현하세요.
아래와 같은 실행결과가 나오도록 main()을 참고하여 작성하세요.
실행결과:
총 스택 저장 공간의 크기 입력 >> 3 (사용자가 입력)
문자열 입력 >> hello
문자열 입력 >> sunny
문자열 입력 >> smile
문자열 입력 >> happy
스택이 꽉 차서 푸시 불가!
문자열 입력 >> 그만
스택에 저장된 모든 문자열 팝 : smile sunny hello
*/
interface Stack {
int length(); // 현재 스택에 저장된 개수 리턴
int capacity(); // 스택의 전체 저장 가능한 개수 리턴
String pop(); // 스택의 톱(top)에 저장된 문자열 리턴하고 저장소에서 문자열 삭제
boolean push(String val); // 스택의 톱(top)에 저장소에 문자열 넣고 실행 결과 boolean 타입으로 리턴
}
public class ClassEx26 {
public static void main(String[] args) {
//StringStack s = new StringStack();
//s.run(); // 프로그램 구동 시작
}
}
package day13;
// 추상 클래스
/*
아래와 같이 4개의 멤버(변수와 메소드)를 가진, 4개의 클래스 Add,Sub,Mul,Div 를 작성하세요.
- int타입의 a, b 변수 : 2개의 피연산자 저장할 변수
- void setValue(int a, int b) : 피연산자 값을 객체 내에 저장
- int calculate() : 클래스의 목적에 맞는 연산을 실행하고 결과를 리턴한다.
그런데, 각각의 클래스마다 공통된 필드와 메소드가 존재하는 구조이므로,
Calc라는 이름의 추상클래스를 작성하여 Calc를 상속받아 각 4개의 클래스를 작성해보세요.
그리고, main()메소드에서 실행예시와 같이 2개의 정수와 연산자를 입력받은 후,
4개의 클래스중 적합한 연산을 처리할 수 있는 객체를 생성하고 메서드 호출하여 그 결과 값을 화면에 출력하게 작성 해보세요.
실행 예시 :
두 정수와 연산자를 입력하세요 >> 5 7 +
12
힌트 : 문자열 자르기 String의 기능중 split() 메서드 사용
String str = "5,7,+";
String[] arr = str.split(",");
for(String s : arr) {
System.out.println(s);
*/
public class ClassEx27 {
public static void main(String[] args) {
}
}
package day13;
// 추상클래스
/*
텍스트로 입출력하는 간단한 그래픽 편집기 만들기.
아래 추상클래스 ShapeAbst를 상속받은 Line, Rect, Circle 클래스를 만들고,
실행 예시처럼 "삽입", "삭제", "모두보기", "종료"의 4가지 그래픽 편집 기능을 가진
클래스 GraphicEditor를 작성하세요.
실행예시 :
그래픽 에디터를 실행합니다. 원하는 기능의 번호를 눌러주세요.
1.삽입 2.삭제 3.모두보기 4.종료 >> 1 (사용자 입력)
1.Line 2.Rect 3.Circle >> 2
1.삽입 2.삭제 3.모두보기 4.종료 >> 1
1.Line 2.Rect 3.Circle >> 3
1.삽입 2.삭제 3.모두보기 4.종료 >> 3
Rect
Circle
1.삽입 2.삭제 3.모두보기 4.종료 >> 2
삭제할 도형의 위치 >> 3
삭제할 수 없습니다.
1.삽입 2.삭제 3.모두보기 4.종료 >> 4
에디터를 종료합니다.
*/
abstract class ShapeAbst {
private ShapeAbst next;
public ShapeAbst() { next = null; }
public void setNext(ShapeAbst obj) { next = obj; } // 링크 연결
public ShapeAbst getNext() { return next; }
abstract public void draw(); // 추상메서드 : 도형이름 출력하는 기능을 갖고 있다.
}
public class ClassEx28 {
public static void main(String[] args) {
}
}
728x90
'P-Language > [Java]' 카테고리의 다른 글
[Java] 15 - 패키지와 API (0) | 2022.06.03 |
---|---|
[Java] 14 - 싱글턴 (0) | 2022.06.03 |
[Java] 12 - 상속과 생성자 오버라이딩, 추상클래스, 인터페이스 (0) | 2022.06.03 |
[Java] 11 - 상속과 접근자, final (0) | 2022.06.03 |
[Java] 10 - this와 객체배열, 캡슐화(getter setter) (0) | 2022.06.03 |