자기개발/Java

Chapter 7 객체지향 프로그래밍 2

실버블렛 2012. 1. 21. 15:42
반응형


----------------------------------------------------------------------------------------
class Tv {
 boolean power;
 int channel;

 void power(){ power = !power;}
 void channelUp(){ ++channel; }
 void channelDown(){ --channel; }

}

class CaptionTv extends Tv {
 boolean caption;
 void displayCaption(String text){
  if (caption) {
   System.out.println(text);
  }
 }

}

class CaptionTvTest
{
 public static void main(String[] args)
 {
  CaptionTv ctv = new CaptionTv();
  ctv.channel = 10;
  ctv.channelUp();
  System.out.println(ctv.channel);
  ctv.displayCaption("Hello, World");
  ctv.caption = true;
  ctv.displayCaption("Hello, World");  
 }
}

----------------------------------------------------------------------------------------
package com.javachobo.book;

class PackageTest {
 public static void main(String[] args)
 {
  System.out.println("Hello World!");
 }
}

----------------------------------------------------------------------------------------
class DeckTest 
{
 public static void main(String[] args)
 {
  Deck d = new Deck();  // 카드 한벌(Deck)을 만든다.
  Card c = d.pick(0);   // 섞기 전에 제일 위의 카드를 뽑는다.
  System.out.println(c);// System.out.println(c.toString());과 같다.
  d.shuffle(); // 카드를 섞는다.
  c = d.pick(0);
  System.out.println(c);
 }
}

//Deck 클래스
class Deck {
 final int CARD_NUM = 52;
 Card c[] = new Card[CARD_NUM];

 Deck () {
  int i =0;

  for (int k = Card.KIND_MAX; k > 0; k--) {
   for (int n=1; n < Card.NUM_MAX+1; n++) {
    c[i++] = new Card(k, n);
   }
  }
 }

 Card pick(int index) {
  if (0<=index && index<= CARD_NUM) {
   return c[index];
  } else { return pick(); }
 }

 Card pick(){
  int index = (int)(Math.random()*CARD_NUM);
  return pick(index);
 }

 void shuffle(){
  for (int n = 0; n<1000; n++) {
   int i = (int)(Math.random()*CARD_NUM);
   Card temp = c[0];
   c[0] = c[i];
   c[i] = temp;
  }
 }
}// end of class Deck

//Card 클래스
class Card {
 static final int KIND_MAX =4;
 static final int NUM_MAX = 13;

 static final int SPADE = 4;
 static final int DIAMOND = 3;
 static final int HEART = 2;
 static final int CLOVER = 1;

 int kind;
 int number;

 Card() {
  this(SPADE, 1);
 }

 Card(int kind, int number){
  this.kind = kind;
  this.number = number;
 }

 public String toString() {
 
 String kind = "";
 String number = "";

 switch (this.kind) {
  case 4:  
   kind = "SPADE";
   break;
     case 3:
   kind = "DIAMOND";
   break;
     case 2:
   kind = "HEART";
   break;
     case 1:
   kind = "CLOVER";
   break;
  default:
 }  //end of switch

 switch (this.number) {
  case 13:
   number = "k";
   break;
  case 12:
   number = "Q";
   break;
  case 11:
   number = "J";
   break;
  default :
   number = this.number + ""; 
 } //end of switch
 return "kind : " + kind + ", number : " + number;
 } // end of toString
} // end of Card class
import java.awt.Frame;
import java.awt.Graphics;

class DrawShape extends Frame {
 public static void main(String[] args)
 {
  DrawShape win = new DrawShape("도형그리기");
 }

 public void paint(Graphics g){
  Point[] p = { new Point(100, 100)
           , new Point(140, 50)
           , new Point(200, 100) };
  Triangle t = new Triangle(p);
  Circle c = new Circle(new Point(150, 150), 50);

        //원을 그린다.
  g.drawOval(c.center.x, c.center.y, c.r, c.r);

  //직선을 그린다.
  g.drawLine(t.p[0].x, t.p[0].y, t.p[1].x, t.p[1].y);
  g.drawLine(t.p[1].x, t.p[1].y, t.p[2].x, t.p[2].y);
  g.drawLine(t.p[2].x, t.p[2].y, t.p[0].x, t.p[0].y);
 }
 DrawShape(String title){
  super(title);
  setSize(300, 300);
  setVisible(true);
 }
}

class Point {
 int x;
 int y;

 Point(){
  this(0,0);
 }

 Point(int x, int y){
  this.x = x;
  this.y = y;
 }
}

class Circle {
 Point center;
 int r;

 Circle(){
  this(new Point(0,0), 100);
 }

 Circle(Point center, int r){
  this.center = center;
  this.r = r;
 }
}

class Triangle {
 Point[] p = new Point[3];

 Triangle(Point[] p){
  this.p = p;
 }

 Triangle(Point p1, Point p2, Point p3){
  p[0] = p1;
  p[1] = p2;
  p[2] = p3;  
 }
}

----------------------------------------------------------------------------------------
class FighterTest
{
 public static void main(String[] args)
 {
  Fighter f = new Fighter();

  if (f instanceof Unit) {
   System.out.println("f는 Unit클래스의 자손클래스입니다.");
  }

  if (f instanceof Fightable) {
   System.out.println("f는 Fightable을 구현했습니다.");
  }

  if (f instanceof Movable) {
   System.out.println("f는 Movable을 구현했습니다.");
  }

  if (f instanceof Attackable) {
   System.out.println("f는 Attackable을 구현했습니다.");
  }

  if (f instanceof Object) {
   System.out.println("f는 Object클래스의 자손클래스입니다.");
  }  
 }
}

class Fighter extends Unit implements Fightable {
 public void move(int x, int y){/*내용생략*/}
 public void attack(Unit u){/*내용생략*/}
}

class Unit {
 int currentHP;
 int x;
 int y;
}

interface Fightable extends Attackable, Movable { }
interface Attackable { public abstract void attack(Unit u); }
interface Movable { public abstract void move(int x, int y); }
 
----------------------------------------------------------------------------------------
import java.text.SimpleDateFormat;
import java.util.Date;

class  ImportTest
{
 public static void main(String[] args)
 {
  Date today = new Date();

  SimpleDateFormat date = new SimpleDateFormat("yyyy/MM/dd");
  SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss a");

  System.out.println("오늘 날짜는 " + date.format(today));
  System.out.println("현재 시간은 " + time.format(today));
 }
}

----------------------------------------------------------------------------------------
class InterfaceTest
{
 public static void main(String[] args)
 {
  A a = new A();
  a.methodA(new B());  
 }
}

class A {
 public void methodA(B b){
  b.methodB();
 }
}

class B {
 public void methodB(){
  System.out.println("methodB()");
 }

}

----------------------------------------------------------------------------------------
class InterfaceTest2
{
 public static void main(String[] args)
 {
  A a = new A();
  a.autoPlay(new B());
  a.autoPlay(new C());  
 }
}

class A {
 public void autoPlay(I i){
  i.play();
 }
}

class B implements I{
 public void play(){
  System.out.println("play in B class");
 }
}

class C implements I {
 public void play(){
  System.out.println("Play in C class");
 }
}

interface I {
 public abstract void play();
}

----------------------------------------------------------------------------------------
class InterfaceTest3
{
 public static void main(String[] args)
 {
  A a = new A();
  a.methodA();
 }
}

class A {
 public void methodA(){
  I i = InstanceManager.getInstance();
  i.methodB();
 }
}

class B implements I {
 public void methodB(){
  System.out.println("method in B class");
 }

}

interface I{
 public abstract void methodB();
}

class InstanceManager {
 public static I getInstance(){
  return new B();
 }
}
----------------------------------------------------------------------------------------
package com.javachobo.book;

class PackageTest {
 public static void main(String[] args)
 {
  System.out.println("Hello World!");
 }
}

----------------------------------------------------------------------------------------
interface Parseable {

// 구문분석 작업을 수행한다.
    public abstract void parse(String fileName);
}

class ParserManager {
 public static Parseable getParser(String type){
  if (type.equals("XML")) {
   return new XMLParser();
   
  } else {
    Parseable p = new HTMLParser();
    return p;
    //return new HTMLParser();
  }
 }
}

class XMLParser implements Parseable {
 public void parse (String fileName){
  // 구문분석작업을 수행하는 코드를 적는다.
 System.out.println(fileName + " - XML parsing completed.");
 }
}

class HTMLParser implements Parseable {
 public void parse (String fileName){
  // 구문분석작업을 수행하는 코드르 적는다.
 System.out.println(fileName + "- HTML parsing completed.");
 }
}

class ParserTest
{
 public static void main(String[] args)
 {
  Parseable parser = ParserManager.getParser("XML");
  parser.parse("document.xml");
  parser = ParserManager.getParser("HTML");
  parser.parse("document2.html");
 }
}


----------------------------------------------------------------------------------------
interface Parseable {

// 구문분석 작업을 수행한다.
    public abstract void parse(String fileName);
}

class ParserManager {
 public static Parseable getParser(String type){
  if (type.equals("XML")) {
   return new XMLParser();
   
  } else {
    Parseable p = new HTMLParser();
    return p;
    //return new HTMLParser();
  }
 }
}

class XMLParser implements Parseable {
 public void parse (String fileName){
  // 구문분석작업을 수행하는 코드를 적는다.
 System.out.println(fileName + " - XML parsing completed.");
 }
}

class HTMLParser implements Parseable {
 public void parse (String fileName){
  // 구문분석작업을 수행하는 코드르 적는다.
 System.out.println(fileName + "- HTML parsing completed.");
 }
}

class ParserTest2
{
 public static void main(String[] args)
 {
  Parseable parser = ParserManager.getParser("XML");
  parser.parse("document.xml");
  parser = ParserManager.getParser("HTML");
  parser.parse("document2.html");
 }
}


----------------------------------------------------------------------------------------
class ParserTest
{
 public static void main(String[] args)
 {
  Parseable parser = PerserManager.getParser("XML");
  Parserable.parse("document.xml");
  parse = PerserManager.getParer("HTML");
  parserable.parse("document.html");
 }
}

interface Perseable {

// 구문분석 작업을 수행한다.
    public abstract void perse(String fileName);
}

class PerserManager {
 public static Perseable getParser(String fileName){
  if (fileName.equals("XML")) {
   return new XMLParer();
   
  } else {
    Perseable p = new HTMLParser();
    return p;
    //return new HTMLParser();
  }
 }
}

class XMLParser implements Perseable {
 public void getParser (String fileName){
  // 구문분석작업을 수행하는 코드를 적는다.
 System.out.println("- XML parsing completed.");
 }
}

class HTMLParer implements Perseable {
 public void getParser (String fileName){
  // 구문분석작업을 수행하는 코드르 적는다.
 System.out.println("- HTML parsing completed.");
 }
}
----------------------------------------------------------------------------------------
class PointTest
{
 public static void main(String[] args)
 {
  Point3D p3 = new Point3D(1,2,3);
  System.out.println(p3.getLocation());
 }
}

class Point {
 int x;
 int y;

 /*
 Point(){
  this(x, y);
 }
 */
 Point(int x, int y){
  this.x = x;
  this.y = y;
 }

 String getLocation(){
  return "x :" + x + ", y :" + y;
 }
}

class Point3D extends Point {
 int z;
 
 /*
 Point3D(){
  this(x, y, z);
 }
 */

 Point3D(int x, int y, int z){
  super(x,y);
  this.x = x;
  this.y = y;
  this.z = z;
 }

 String getLocation(){
  //return "x :" + x + ", y :" + y + ", z :" + z;
  return super.getLocation() + ", z :" + z;
 }
}
----------------------------------------------------------------------------------------
class PointTest2
{
 public static void main(String[] args)
 {
  Point3D p3 = new Point3D();
  System.out.println("p3.x=" + p3.x);
  System.out.println("p3.y=" + p3.y);
  System.out.println("p3.z=" + p3.z);
 }
}

class Point {
 int x;
 int y;

 Point(int x, int y){
  this.x = x;
  this.y = y;
 }
}

class Point3D extends Point {
 int z;

 Point3D(){
  this(100, 200, 300);
 }

 Point3D(int x, int y, int z){
  super(x, y);
  this.z = z; 
 }
}
----------------------------------------------------------------------------------------
class RepairTest
{
 public static void main(String[] args)
 {
  Tank tank = new Tank();
  Dropship dropship = new Dropship();
  Marine marine = new Marine();
  Scv scv = new Scv();

  scv.repair(tank);
  scv.repair(dropship);
  // scv.repair(marine); 에러
 }
}

class Unit {
 int hitPoint;
 final int MAX_HP;

 Unit(int hp){
  MAX_HP = hp;
 }
}

class GroundUnit extends Unit{
 GroundUnit(int hp){
 super(hp);
 }
}

class AirUnit extends Unit{
 AirUnit(int hp){
  super(hp);
 }
}

interface Repairable{};

class Tank extends GroundUnit implements Repairable{
 Tank(){
 super(150);
 hitPoint = MAX_HP;
}

public String toString(){
 return "Tank";
}
}

class Dropship extends AirUnit implements Repairable{
 Dropship(){
 super(125);
 hitPoint = MAX_HP;
}

public String toString(){
 return "Dropship";
}
}

class Marine extends GroundUnit implements Repairable{
 Marine(){
 super(40);
 hitPoint = MAX_HP;
}

public String toString(){
 return "Marine";
}
}

class Scv extends GroundUnit implements Repairable{
 Scv(){
 super(60);
 hitPoint = MAX_HP;
}

public String toString(){
 return "Scv";
}

void repair(Repairable r){
 if (r instanceof Unit) {
  Unit u = (Unit)r;
 while (u.hitPoint != MAX_HP) {
  u.hitPoint++;
 }
 System.out.println(u.toString() + "의 수리가 끝났습니다.");
 } //end of if
}
}


----------------------------------------------------------------------------------------
class SuperTest
{
 public static void main(String[] args)
 {
  Child  c = new Child();
  c.method();
 }
}

class Parent {
 int x=10;
}

class Child extends Parent {
 void method(){
  System.out.println("x=" + x);
  System.out.println("this.x=" + this.x);
  System.out.println("super.x=" + super.x);
 }
}
----------------------------------------------------------------------------------------
class SuperTest2
{
 public static void main(String[] args)
 {
  Child  c = new Child();
  c.method();
 }
}

class Parent {
 int x=10;
}

class Child extends Parent {
 int x=20;

 void method(){
  System.out.println("x=" + x);
  System.out.println("this.x=" + this.x);
  System.out.println("super.x=" + super.x);
 }
}
----------------------------------------------------------------------------------------
class Tv {
 boolean power;
 int channel;

 void power(){power = !power;}
 void channelUp(){++channel;}
 void channelDown(){--channel;} 
}

class VCR {
 boolean power;
 int counter = 0;
 void power(){power = !power;}
 void play(){/*내용생략*/}
 void stop(){/*내용생략*/}
 void rew(){/*내용생략*/}
 void ff(){/*내용생략*/}
}

class TVCR extends Tv
{
  VCR vcr = new VCR();
  int counter = vcr.counter;

  void play(){
   vcr.play();
  }
     void stop(){
   vcr.stop();
  }
     void rew(){
   vcr.rew();
  }
     void ff(){
   vcr.ff();
  }
 
}

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------


반응형

'자기개발 > Java' 카테고리의 다른 글

Chapter 9 java.lang 패키지  (0) 2012.01.21
Chapter 8 예외처리(Exception Handling)  (0) 2012.01.21
Chapter 6 객체지향 프로그래밍 1  (0) 2012.01.21
Chapter 5 배열(Array)  (0) 2012.01.21
Chapter 4 조건문과 반복문  (0) 2012.01.12