Wednesday, February 4, 2015

Day 2: Create Class


Sun: 

public class Sun
{
    private Circle mySun;
 
    // Constructor
    public Sun(){
        mySun = new Circle();
        mySun.makeVisible();
        mySun.changeColor("yellow");
        mySun.moveUp();
        mySun.moveHorizontal(190);
    }
}



House:

public class House
{
    private Square wall;
    private Square window;
    private Triangle roof;

    // Constructor
    public House()
    {
       wall = new Square();
       wall.makeVisible();
       wall.changeSize(100);
       wall.moveVertical(100);
   
       window = new Square();
       window.makeVisible();
       window.changeColor("black");
       window.moveRight();
       window.moveDown();
       window.moveVertical(100);
   
       roof = new Triangle();
       roof.makeVisible();
       roof.changeColor("green");
       roof.changeSize(45, 140);
       roof.moveHorizontal(60);
       roof.moveVertical(90);
     
    }
}

Ground:

public class Ground
{
    private Circle myGround;
 
    //Constructor
    public Ground()
    {
      myGround = new Circle();
      myGround.makeVisible();
      myGround.changeSize(400);
      myGround.changeColor("Brown");
      myGround.moveHorizontal(-60);
      myGround.moveVertical(160);
    }
}

CodePad: 

Sun mySun = new Sun();
House myHouse = new House();
Ground myGround = new Ground();



  • What is structure of a class and name each components of application.
1 Class gồm có Field ( thuộc tính ), Constructor ( Hàm khởi dựng), và Method ( Phương thức)


  • What does key word this mean?(mark of 4).
Chữ " This" để không bị gọi trùng tên 1 thuộc tính nào đó 


  • Develop your version of class Person?
Code:
public class Person
{
    private int Head;
    private int Hand;
    private int Body;
    private int Leg;

    public Person(int pHead, int pHand, int pBody, int pLeg)
    {
        Head = pHead;
        Hand = pHand;
        Body = pBody;
        Leg = pLeg;
    }
 
    public String getInfo() {
        return "This person has" + " " + Head + " " + "Head" + " " + Hand + " "
        + "Hands" + " " + Body + " " + "Body" + " " + Leg + " " + "Legs";
    }
}

No comments:

Post a Comment