/**
* Write a description of class Student here.
*
* @author (CodeLean.vn)
* @version (a version number or a date)
*/
public class Student
{
/**
* Khai bao cac truong dung de luu gia tri cua cac thuoc tinh trong lop
*/
private int id;
private String name;
private boolean gender;
/**
* Constructor khong co doi so
*/
public Student()
{
// To do:
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;
}
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;
}
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);
}
}
|
/**
* Lớp Client sử dụng lớp Student để xây dựng chương trình java theo kịch bản.
*
* @author (CodeLean.vn)
* @version (a version number or a date)
*/
public class Client {
public static void main(String []agrs) {
Student studentA;
Student studentB;
studentA=new Student();
studentB=new Student(1,"Nguyen Van A",true);
studentB.printInfo();
studentB.setName("Nguyen Van B");
studentB.printInfo();
}
}
|
/**
* Write a description of class Battery here.
*
* @author (CodeLean.vn)
* @version (a version number or a date)
*/
public class Battery
{
/**
* Fields
*/
private int energy;
/**
* Constructor for objects of class Battery
*/
public Battery()
{
// To do:
energy=100;
}
/**
* Method
*/
public void setEnergy(int value) {
energy=value;
}
public int getEnergy() {
return energy;
}
public void decreaseEnergy() {
energy--;
}
}
|
/**
* Write a description of class FlashLamp here.
*
* @author (CodeLean.vn)
* @version (a version number or a date)
*/
public class FlashLamp
{
/**
* Fields
*/
private boolean status;
private Battery battery;
/**
* Constructor for objects of class FlashLamp
*/
public FlashLamp()
{
// To do:
status=false;
}
/**
* Methods
*/
public void setBattery(Battery battery) {
this.battery=battery;
}
public int getBatteryInfo() {
return battery.getEnergy();
}
public void light() {
if(status==true&&battery!=null&&battery.getEnergy()>0) {
battery.decreaseEnergy();
}
}
public void turnOn() {
if(battery!=null&&battery.getEnergy()>0) {
status=true;
}
light();
}
public void turnOff() {
status=false;
}
}
|
Đăng nhận xét