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

package java1;

public class Che {

        //属性

        public String nub;
        public int speed;
        public double weight ;
        
        
        Che()
        {
            nub="XX1234";
            speed=100;
            weight=100;
        }

        Che(String nub, int speed,double weight) 
        {
            this.nub = nub;
            this.speed = speed;
            this.weight = weight;
        }
        public int add(int add)
        {
            System.out.println("车速增加"+add+"当前车速为:"+(speed+add));
            return speed;
        }
        public int jian(int jian)
        {
            System.out.println("车速减"+jian+"当前车速为:"+(speed-jian));
            return speed;
        }
        public String setNub(String nub)
        {
            this.nub=nub;
            System.out.println("当前车牌为"+nub);
            return nub;
        }
        public double setWeight(double weight)
        {
            this.weight=weight;
            System.out.println("当前载重"+weight);
            return weight;
        }
        
}


        Che che1=new Che();
        che1.setNub("辽B5086");
        che1.add(20);
        System.out.println("车牌为:"+che1.nub+",车速为:"+che1.speed+",载重为"+che1.weight);
        
        Che che2=new Che("辽B5086",150,200);
        che2.jian(20);
        System.out.println("车牌为:"+che2.nub+",车速为:"+che2.speed+",载重为"+che2.weight);

当前车牌为辽B5086
车速增加20当前车速为:120
车牌为:辽B5086,车速为:100,载重为100.0
车速减20当前车速为:130
车牌为:辽B5086,车速为:150,载重为200.0

posted @ 2016-09-19 23:39  张好好  阅读(1018)  评论(0编辑  收藏  举报