⑴ java練習題
publicclassVehicle{
privateStringname;
privateStringgender;
privateintage;
privateStringdriverLicense;
privateStringplateNumber;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetGender(){
returngender;
}
publicvoidsetGender(Stringgender){
this.gender=gender;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetDriverLicense(){
returndriverLicense;
}
publicvoidsetDriverLicense(StringdriverLicense){
this.driverLicense=driverLicense;
}
publicStringgetPlateNumber(){
returnplateNumber;
}
publicvoidsetPlateNumber(StringplateNumber){
this.plateNumber=plateNumber;
}
@Override
publicStringtoString(){
return"Vehicle[name="+name+",gender="+gender+",age="+age+",driverLicense="+driverLicense
+",plateNumber="+plateNumber+"]";
}
publicvoiddrive(){
System.out.println(name+"司機正在開"+plateNumber+"牌號的車。");
}
publicvoidstop(){
System.out.println(name+"司機把"+plateNumber+"牌號的車停了下來。");
}
publicbooleancheckName(Stringcheck){
if(this.name!=null&&this.name!=""){
returnthis.name.equals(check);
}
returnfalse;
}
publicbooleancheckPlateNumber(Stringcheck){
if(this.plateNumber!=null&&this.plateNumber!=""){
returnthis.plateNumber.equals(check);
}
returnfalse;
}
}
懶得寫注釋了
⑵ java基礎練習題
intoldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
//建立一個list集合
ArrayList<Integer>list=newArrayList<Integer>();
for(inti=0;i<oldArr.length;i++){
if(oldArr[i]>0){
list.add(oldArr[i]);
}
}
intnewArr[]=newint[list.size()];
for(inti=0;i<list.size();i++){
newArr[i]=list.get(i);
}
//另一種寫法,不過返回的應用型數據類型但是跟int也差不多
IntegerlistArr[]=list.toArray(newInteger[list.size()]);
⑶ java編程基礎練習題
答案是抄C
A: num1=new double[num2+1];等價為:num1=new double[6];所以num1指向一個有襲6個元素的double型數組
B num2是一個整數
C:num1[num2]=num3;等價為:num1[5]=2.0;因為數組標識從0開始,所以num1[5]是最後一個元素
D: num1數組的第三個元素沒有賦值,默認應該是0.0;