java by example_lab3

Upload: hoang-tuan

Post on 14-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Java By Example_lab3

    1/5

    Java by Example lab 3

    2009 FPT-Aptech Trang 1 / 5

    I - Chun b: Kin thc cn c: Khi nim Class, Object, Field, Property, Method, Constructor. Knng cn rn luyn:

    Tp vit mt chng trnh bng Java:o t tn v khai bo Class.o Bit cch xy dng Class, khi to v s dng object trong Java.o Cch s dng cc t khapublic, private, static.o Trnh by code.o Cch chy v gli chng trnh.

    Tp gii quyt bi ton di gc lp trnh hng i tng.

    II - Ni dung:

    Phn 1: Workshop- Lm li phn workshop ca module 4 trn a.

    PDF created with FinePrint pdfFactory Pro trial version http://www.fineprint.com

    http://www.fineprint.com/http://www.fineprint.com/
  • 7/27/2019 Java By Example_lab3

    2/5

    Java by Example lab 3

    2009 FPT-Aptech Trang 2 / 5

    Phn 2: Vit li cc class sau v sa ht li cho chng trnh:/**

    * @(#)Student.java

    *

    *

    * @author

    * @version 1.00 2009/1/19

    */

    import java.util.Scanner;

    public class Student {

    //khai bao cac truong(field)

    private int id;

    private String name;

    private boolean gender;

    //constructor khong co doi so

    public Student() {

    this.id=-1;this.name="noname";

    this.gender=true;

    }

    //constructor co doi so

    public Student(int id,String name,boolean gender) {

    this.id=id;

    this.name=name;

    this.gender=gender;

    }

    //get accessor

    public int getId() {

    return this.id;

    }

    public void setId(int value) {

    this.id=value;

    }

    public String getName() {

    return this.name;

    }

    public void setName(String value) {

    this.name=value;

    }

    public boolean isMale() {

    return this.gender;

    }public void setMale(boolean value) {

    this.gender=value;

    }//To be continued...

    PDF created with FinePrint pdfFactory Pro trial version http://www.fineprint.com

    http://www.fineprint.com/http://www.fineprint.com/
  • 7/27/2019 Java By Example_lab3

    3/5

    Java by Example lab 3

    2009 FPT-Aptech Trang 3 / 5

    /**

    * @(#)Main.java

    *

    *

    * @author

    * @version 1.00 2009/1/3

    */

    public class Main {

    public static void main(String[] args) {

    //tao doi tuong cua lop student

    Student std=new Student();

    //goi phuong thuc cua doi tuong student

    std.scanInfo();

    std.printInfo();

    }

    }

    //phuong thuc nhap

    public void scanInfo() {

    Scanner input=new Scanner(System.in);System.out.print("Enter ID:");

    this.id=input.nextInt();

    System.out.print("Enter Name:");

    this.name=input.nextLine();

    System.out.print("Enter Gender:");

    this.gender=input.nextBoolean();

    }

    public void printInfo() {

    System.out.println("-----------------------------------");

    System.out.println("| ID | Name | Male |");

    System.out.printf("| %d | %s | %b |\n",this.id,this.name,this.gender);

    }

    }

    PDF created with FinePrint pdfFactory Pro trial version http://www.fineprint.com

    http://www.fineprint.com/http://www.fineprint.com/
  • 7/27/2019 Java By Example_lab3

    4/5

    Java by Example lab 3

    2009 FPT-Aptech Trang 4 / 5

    Phn 3: Bi tp tlm3.1. Vit cc Class da theo s sau:

    - Lu : vi mi thuc tnh ca class Person, cn khai bo 1 field, v 1 cp phng thc get, set tngng. V d: vi thuc tnh ID, ta cn khai bo trong class Person nh sau:

    //Cch ci t thuc tnh ID:privateint id; //field ny dnglu thng tin vID ca PersonpublicintgetID() {

    return this.id;

    }

    publicvoidsetID(int value) {

    this.id=value;}

    a. Sau khi xy dng xong cc thuc tnh ca class Person, hy vit: 1 Constructor khi to cc gi tr ban u cho cc thuc tnh ID, FirstName, LastName 1 Constructor khc khi to gi tr cho cc thuc tnh ID, FirstName, LastName, Email,

    Phone.

    1 Constructor khng cha tham s.

    b. Vit 2 phng thc printInfo() v scanInfo(), trong : printInfo() dng in thng tin ca Person theo nh dng sau:

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

    | ID | First Name | Last Name | Email | Phone |---------------------------------------------------------------------------

    scanInfo() dng nhp thng tin Person t console theo th t nh sau:Nhp ID:...Nhp FirstName:...Nhp LastName:...Nhp Email:...Nhp Phone:...

    3.2. Vit mt Main Class (class cha hm main )vi tn l Main nh trn s.Trong hm main, vit chng trnh th hin menu sau:===Menu===

    1. To mi mt object Person dng constructor.

    PDF created with FinePrint pdfFactory Pro trial version http://www.fineprint.com

    http://www.fineprint.com/http://www.fineprint.com/
  • 7/27/2019 Java By Example_lab3

    5/5

    Java by Example lab 3

    2009 FPT-Aptech Trang 5 / 5

    2. Cp nht thuc tnh ca object va to bng phng thc scanInfo().3. Hin th thng tin object va to bng phng thc printInfo().4. Thay i thuc tnh Email ca object va to.

    5. Thot.#Chn:

    3.3. Vit cc class theo s sau:

    a. Sau khi xy dng cc thuc tnh ca class Rectangle, hy vit: 1 Constructor khi to cc gi tr ban u cho cc thuc tnh x, y, width, height. 1 Constructor khng cha tham s, v gn gi tr mc nh cho x=0, y=0, witdh=10,

    height=10.

    b. Vit 2 phng thc printInfo() v scanInfo(), trong : printInfo() dng in thng tin ca mt Rectangle theo nh dng sau:

    // v d hnh chnht c cnh width=12, height=3 th in nhsau:************

    ************************

    scanInfo() dng nhp thng tin ca mt Rectangle t console theo th t nh sau:Nhp x:Nhp y:Nhp width:Nhp height:

    c. Vit mt Main Class (class cha hm main )vi tn l Main nh trn s.Trong hm main, vit chng trnh th hin menu sau:===Menu===

    6. To mi mt hnh ch nht dng constructor.

    7. Cp nht thuc tnh ca hnh va to bng phng thc scanInfo()8. V hnh ch nht va to bng phng thc printInfo()9. Thot.

    #Chn:

    3.4. Lm bi assignment 4 trong a CD.

    PDF created with FinePrint pdfFactory Pro trial version http://www.fineprint.com

    http://www.fineprint.com/http://www.fineprint.com/