Java面向对象编程上机-练习题汇总.pdfVIP

  • 0
  • 0
  • 约9.95千字
  • 约 10页
  • 2026-02-08 发布于山东
  • 举报

1.【练习题】类的成员变量:

猜数字游戏:一个类A有一个成员变量v,有一个初值100。定义一个类,对A

类的成员变量v进行猜。如果大了则提示大了,小了则提示小了。等于则提示猜

测成功。

代码://1.【练习题】类的成员变量:

//猜数字游戏:一个类A有一个成员变量v,有一个初值100。定义一个类,对A

类的成员变量v进行猜。如果大了则提示大了,小了则提示小了。等于则提示猜

测成功。

importjava.util.*;

classA{

intv;

A(){

this.v=100;

}

}

publicclassTest{

publicstaticvoidmain(String[]args){

Aa=newA();

in

Scannerinput=newScanner(System.);

intinput_value=input.nextInt();

if(input_valuea.v){

outout

System.你的输入大于这个值你的输入大于这个值

}

elseif(input_valuea.v){

outout

System.你的输入小于这个值你的输入小于这个值

}

else{

outout

System.你的输入等于这个值你的输入等于这个值

}

}

}

2.【练习题】类的成员变量:

请定义一个交通工具(Vehicle)的类,其中有:

属性:速度(speed),体积(size)等等

方法:移动(move()),设置速度(setSpeed(intspeed)),加速speedUp(),减速

speedDown()等等.

最后在测试类Vehicle中的main()中实例化一个交通工具对象,并通过方法给它

初始化speed,size的值,并且通过打印出来。另外,调用加速,减速的方法对速

度进行改变。

//练习题】类的成员变量:

//请定义一个交通工具(Vehicle)的类,其中有:

//属性:速度(speed),体积(size)等等

//方法:移动(move()),设置速度(setSpeed(intspeed)),加速speedUp(),减速

speedDown()等等.

//最后在测试类Vehicle中的main()中实例化一个交通工具对象,并通过方法给它

初始化speed,size的值,并且通过打印出来。另外,调用加速,减速的方法对速

度进行改变。

publicclassVehicle{

privateintspeed;

privateintsize;

Vehicle(intsize,intspeed){

this.size=size;

this.speed=speed;

}

publicvoidmove(){

}

publicvoidsetSpeed(intspeed){

this.speed=speed;

}

publicvoidspeedUp(intspeed){

this.speed+=speed;

}

publicvoidspeedDown(intspeed){

this.speed-=speed;

}

publicstaticvoidmain(String[]args){

Vehiclecar=newVehicle(100,250);

car.setSpeed(10000);

car.speedUp(999);

car.speedDown

文档评论(0)

1亿VIP精品文档

相关文档