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

package com.homework.zw;

public class Jidongche
{
    public String chepaihao;
    public int speed;
    public double zaizhong;
    
    Jidongche()
    {
        chepaihao="XX1234";
        speed=100;
        zaizhong=100;
    }
    Jidongche(String cph, int sp, double zz)
    {
        chepaihao=cph;
        speed=sp;
        zaizhong=zz;
    }
    
    public int addSpeed(int sd)
    {
        speed+=sd;
        return speed;
    }
    public int downSpeed(int sd)
    {
           speed-=sd;
           return speed;   
    }
    public String xiugai(String chepaihao)
    {
        this.chepaihao=chepaihao;
        return chepaihao;
    }
    public double zaizh(double zaizhong)
    {
        this.zaizhong=zaizhong;
        return zaizhong;
    }
}
package com.homework.zw;

public class Text_jidongche 
{

    public static void main(String[] args)
    {
        Jidongche jidongche1=new Jidongche();
        System.out.println("机动车1,车牌号:"+jidongche1.chepaihao+"   车速:"+jidongche1.speed+"km/h   载重:"+jidongche1.zaizhong+"吨");
        System.out.println("修改车牌号为:"+jidongche1.xiugai("辽A9752"));
        System.out.println("加速20,此时速度为:"+jidongche1.addSpeed(20)+"km/h");
        
        
        Jidongche jidongche2=new Jidongche("辽B5086",150,200);
        System.out.println("机动车2,车牌号:"+jidongche2.chepaihao+"   车速:"+jidongche2.speed+"km/h   载重:"+jidongche2.zaizhong+"吨");
        System.out.println("减速30,此时速度为:"+jidongche2.downSpeed(30)+"km/h");
        
    }

}

 

posted @ 2016-09-19 20:00  HRZJ  阅读(2090)  评论(0编辑  收藏  举报