A. 一道java編程題 通過繼承java.Util.Random類設計一個類RandomEX,並實現
Java程序:
importjava.util.Random;
publicclassHardWork{
publicstaticvoidmain(String[]args){
RandomEXrand=newRandomEX();
intn=4;
intgroup=10;
for(inti=0;i<group;i++){
.out.printf("第%2d組: ",i+1);
System.out.printf("1~%d長度二進制字元串: %s ",n,rand.nextBinaryString1(n));
System.out.printf("固定%d長度二進制字元串: %s ",n,rand.nextBinaryString2(n));
System.out.printf("1~%d長度十六進制字元串: %s ",n,rand.nextHexString1(n));
System.out.printf("固定%d長度十六進制字元串: %s ",n,rand.nextHexString2(n));
System.out.println();
}
}
}
/**
*隨機數類
*@author馮向科
*@version2016.05.07
*/
classRandomEXextendsRandom{
=1L;
/**
*產生隨機長度為1~n的二進字元串
*@paramn字元串最大長度
*@return長度為1~n的二進制字元串
*/
publicStringnextBinaryString1(intn){
longnum;
StringBuilderstr=newStringBuilder();
intlen;
do{
num=this.nextLong();
str.append(Long.toBinaryString(num));
len=str.length();
}while(len<n);
intsize=this.nextInt(n)+1;
returnstr.substring(0,size);
}
/**
*產生固定長度為n的二進字元串
*@paramn字元串固定長度
*@return固定長度為n的二進字元串
*/
publicStringnextBinaryString2(intn){
longnum;
StringBuilderstr=newStringBuilder();
intlen;
do{
num=this.nextLong();
str.append(Long.toBinaryString(num));
len=str.length();
}while(len<n);
returnstr.substring(0,n).toUpperCase();
}
/**
*產生隨機長度為1~n的十六進字元串
*@paramn字元串最大長度
*@return長度為1~n的十六進制字元串
*/
publicStringnextHexString1(intn){
longnum;
StringBuilderstr=newStringBuilder();
intlen;
do{
num=this.nextLong();
str.append(Long.toHexString(num));
len=str.length();
}while(len<n);
intsize=this.nextInt(n)+1;
returnstr.substring(0,size).toUpperCase();
}
/**
*產生固定長度為n的十六進字元串
*@paramn字元串固定長度
*@return固定長度為n的十六進字元串
*/
publicStringnextHexString2(intn){
longnum;
StringBuilderstr=newStringBuilder();
intlen;
do{
num=this.nextLong();
str.append(Long.toHexString(num));
len=str.length();
}while(len<n);
returnstr.substring(0,n).toUpperCase();
}
}
運行測試:
第 1 組:
1~4長度二進制字元串: 11
固定4長度二進制字元串: 1010
1~4長度十六進制字元串: 44B
固定4長度十六進制字元串: A7C3
第 2 組:
1~4長度二進制字元串: 110
固定4長度二進制字元串: 1110
1~4長度十六進制字元串: E96D
固定4長度十六進制字元串: 61F1
第 3 組:
1~4長度二進制字元串: 101
固定4長度二進制字元串: 1111
1~4長度十六進制字元串: B1C
固定4長度十六進制字元串: A0F1
第 4 組:
1~4長度二進制字元串: 1101
固定4長度二進制字元串: 1010
1~4長度十六進制字元串: 1DC
固定4長度十六進制字元串: D38E
第 5 組:
1~4長度二進制字元串: 1
固定4長度二進制字元串: 1110
1~4長度十六進制字元串: 70D8
固定4長度十六進制字元串: 495B
第 6 組:
1~4長度二進制字元串: 1100
固定4長度二進制字元串: 1011
1~4長度十六進制字元串: F6
固定4長度十六進制字元串: 5086
第 7 組:
1~4長度二進制字元串: 100
固定4長度二進制字元串: 1100
1~4長度十六進制字元串: 86A
固定4長度十六進制字元串: 23A0
第 8 組:
1~4長度二進制字元串: 1
固定4長度二進制字元串: 1101
1~4長度十六進制字元串: 8
固定4長度十六進制字元串: F967
第 9 組:
1~4長度二進制字元串: 110
固定4長度二進制字元串: 1000
1~4長度十六進制字元串: F194
固定4長度十六進制字元串: 7C3D
第 10 組:
1~4長度二進制字元串: 1101
固定4長度二進制字元串: 1100
1~4長度十六進制字元串: 4
固定4長度十六進制字元串: FB2A
B. java繼承多態的練習題
Java繼承是使用已存在的類的定義作為基礎建立新類的技術,新類的定義可以增加新的數據或新的功能,也可以用父類的功能,但不能選擇性地繼承父類。
java多態存在的三個必要條件:
1.需要有繼承關系的存在
2.需要有方法的重寫
3.需要有父類的引用指向子類對象
希望對你有幫助。
第一題應該選D,第二題選C,D。
第一題屬於多態,methodB()方法屬於子類,父類沒有重寫子類的方法
第二題屬於繼承,子類可以繼承父類的方法
C. Java關於繼承、多態(介面和包)的編程題
package animal.mammal;
// 狗類
public class Dog extends Mammal{
void speak(){
System.out.println("Woof!");
}
void yaoweiba(){
System.out.println("Tail wagging...");
}
void qitaoshiwu(){
System.out.println("begging for food...");
}
}
// 同理寫貓,馬,豬,其中都必須有方法 void speak() 輸出各不相同,其他方法自定義
public class Cat extends Mammal{
}
public class Horse extends Mammal{
}
public class Pig extends Mammal{
}
// 另外是寵物貓,寵物狗的
package animal.mammal.pet;
public class PetCat extends Cat{
doucle price = 40.00;
String ownername = peter;
void Price(){
System.out.println("PetCat price:" + this.price);
}
void Owner(){
System.out.println("this's " + this.ownername +" petCat");
}
}
// 同理寫寵物狗的,如果需要 get/set 則為 price,ownername 加上該方法
public class PetDog extends Dog{
}