(1)创建一个叫做机动车的类: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)、减速(车速自减)、修改车牌号,查询车的载重量。 编写两个构造方法:一个没有形参,在方法中将车牌号设置“XX1234”,速 度设置为100,载重量设置为100;另一个能为对象的所有属性赋值; (2)创建主类: 在主类中创建两个机动车对象。

package a;

public class Jidongche {
    private String chepaihao;
    private int chesu;
    private double zaizhong;
    public String getChepaihao() {
        return chepaihao;
    }
    public void setChepaihao(String chepaihao) {
        this.chepaihao = chepaihao;
    }
    public int getChesu() {
        return chesu;
    }
    public void setChesu(int chesu) {
        this.chesu = chesu;
    }
    public double getZaizhong() {
        return zaizhong;
    }
    public void setZaizhong(double zaizhong) {
        this.zaizhong = zaizhong;
    }
    public Jidongche()
    {
        chepaihao="XX231";
        chesu=100;
        zaizhong=100;
    }
    public Jidongche(String chepaihao, int chesu, double zaizhong) {
        super();
        this.chepaihao = chepaihao;
        this.chesu = chesu;
        this.zaizhong = zaizhong;
    }
    public void jiasu(int sudu)
    {
        if(sudu<0)
        {
            System.out.println("无法加速");
        }
        else
        {
           chesu+=sudu;
        }
    }
    public void jiansu(int sudu)
    {
        if(sudu<0)
        {
            System.out.println("无法减速");
        }
        
        else
        {
           
           if((chesu-sudu)<0)
           {
               System.out.println("减速失败");
           }
           else
           {
               chesu-=sudu;
           }
               
        }
    }

}
package a;

public class Che {

    public static void main(String[] args) {
    Jidongche che1=new Jidongche();
    che1.setChepaihao("辽A9752");
    che1.jiasu(30);
    System.out.println("车牌号="+che1.getChepaihao());
    System.out.println("当前车速="+che1.getChesu());
    System.out.println("载重="+che1.getZaizhong());
    
    Jidongche che2=new Jidongche("辽B5086",150,200);
    che2.jiansu(20);    
    System.out.println("车牌号="+che2.getChepaihao());
    System.out.println("当前车速="+che2.getChesu());
    System.out.println("载重="+che2.getZaizhong());
    
    }

}

posted @ 2016-05-20 21:00  凌零聆  阅读(4085)  评论(0编辑  收藏  举报