资源描述:
《程序设计综合训练》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1、题目:编写程序,定义类Vehicles,其中:a)域:name(品牌)、color(颜色)、weight(重量)、plate(车牌号码);b)一个构造方法,对四个域进行初始化;c)一个返回所有属性信息的方法getDetails();d)对name属性的get和set方法;e)该类在包vehicle中。1)源程序packagevehicle;publicclassVehicles{Stringname;Stringcolor;doubleweight;Stringplate;publicVehicles(Stringname,Stringcolor,doubleweig
2、ht,Stringplate){this.name=name;this.color=color;this.weight=weight;this.plate=plate;}publicStringgetDetails(){returnname+","+color+","+weight+","+plate;}publicStringgetName(){returnname;}publicStringsetName(){returnname;}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubVeh
3、iclesv=newVehicles("玛莎拉蒂","黄色",1000,"BJ9708");System.out.println("汽车的细节为:"+v.getDetails());System.out.println("汽车的名字为:"+v.getName());}}2)运行结果截图汽车的细节为:玛莎拉蒂,黄色,1000.0,BJ9708汽车的名字为:玛莎拉蒂1.题目:编写程序,定义类Car,它是类Vehicles的子类。其中:a)该类在包vehicle中;b)域:seats(座位数),speed(速度);c)对seats的get和set方法;d)构造方法,对6个域进行
4、初始化,对继承自父类的域,调用父类的构造方法进行初始化;e)覆盖父类中的getDetails()方法,返回所有域的信息;f)对speed的speedUp()和slowDown()方法,通过方法的参数修改speed的值。其中,最高时速不超过200,最低时速不低于0,在方法中实现对速度的合法性检查;g)main方法:定义两个Car类型的对象,分别调用getDetails()方法返回这两个对象的信息,调用speedUp()和slowDown()方法改变Car对象的速度。1)源程序packagevehicle;publicclassCarextendsVehicles{intse
5、ats;doublespeed;publicintget_seats(){returnseats;}publicintset_seats(){returnseats;}publicCar(Stringname,Stringcolor,doubleweight,Stringplate,intseats,doublespeed){super(name,color,weight,plate);this.seats=seats;this.speed=speed;}publicStringgetDetails(){returnname+","+color+","+weight+",
6、"+plate+","+seats+","+speed;}publicStringspeedUp(){if(speed>200)return"您超速了!";elsereturn"您的速度合法,小于最高速度";}publicStringslowDown(){if(speed<0)return"您应该提速!";elsereturn"您的速度合法,大于最低速度";}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubCarca=newCar("宾利","黑色",1500,"BJ9999",4,236)
7、;Carc=newCar("福特","白色",1000,"BJ7766",1,185);System.out.println("汽车的细节为:"+ca.getDetails());System.out.println("该汽车的速度为:"+ca.speedUp());System.out.println("该汽车的速度为:"+ca.slowDown());System.out.println("汽车的细节为:"+c.getDetails());System.out.println("该汽车的速度为:"+c.speedUp())