1、写出程序运行结果
public class Test{
static boolean foo(char c){
System.out.println(c);
return true;
}
public static void main(String[] args){
int i=0;
for(foo('A');foo('B')&&(i<2);foo('C')){
i++;
foo('D');
}
}
}
-----------------
结果:
A
B
D
C
B
D
C
B
-----------------
2.写出程序运行结果
import java.lang.StringBuffer;
public class Test2{
public static void stringReplace(String str){
str = str.replace('j','i');
}
public static void bufferReplace(StringBuffer sbf){
sbf = sbf.append("C");
}
public static void main(String args[]){
String strText=new String("java");
StringBuffer sbfText = new StringBuffer("java");
stringReplace(strText);
bufferReplace(sbfText);
System.out.println(strText+sbfText);
}
}
----------------------
运行结果:javajavaC
----------------------
3、现有程序如下,写出程序运行结果:
class A
{
int a =1;
double d=2.0;
void show(){
System.out.println("Class A:a="+a+"\td="+d);
}
}
class B extends A
{
float a=3.0f;
String d="Java program.";
void show(){
super.show();
System.out.println("Class B:a="+a+"\td="+d);
}
public static void main(String[] args){
//A a = new A();
//a.show();
A b = new B();
b.show();
System.out.println(b.a);
}
}
--------------------------------------------
①若在应用程序的main方法中有以下语句:
A a = new A();
a.show();
结果是:
②若在应用程序的main方法中定义类B的对象b;
A b = new B();
b.show();
System.out.println(b.a);
结果是:
--------------------------------------------
4、若应用程序的main方法中,定义字符串数组f和s:分别表示扑克牌的牌面值和花
色;定义52个元素的Card类型数组deck,用来存放4个花色的52张牌。如下所示:
String number[] =
{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String color[] = {"黑桃","红桃","梅花","方块"};
Card deck = new Card[52];
Card类的源码如下:
class Card{
private String face;
private String suit;
public Card(String f,String s){
face = f;
suit = s;
}
protected String getSuit(){
return suit;
}
protected String getFace(){
return face;
}
public String toString(){
return face + "of" +suit;
}
}
①用Card类的构造方法给deck数组的52张牌赋值,要求黑红梅方,从大到小的顺
序存放
②编写模拟洗牌的程序,把数组deck中的扑克牌随机打乱存放顺序
public class Test{
static boolean foo(char c){
System.out.println(c);
return true;
}
public static void main(String[] args){
int i=0;
for(foo('A');foo('B')&&(i<2);foo('C')){
i++;
foo('D');
}
}
}
-----------------
结果:
A
B
D
C
B
D
C
B
-----------------
2.写出程序运行结果
import java.lang.StringBuffer;
public class Test2{
public static void stringReplace(String str){
str = str.replace('j','i');
}
public static void bufferReplace(StringBuffer sbf){
sbf = sbf.append("C");
}
public static void main(String args[]){
String strText=new String("java");
StringBuffer sbfText = new StringBuffer("java");
stringReplace(strText);
bufferReplace(sbfText);
System.out.println(strText+sbfText);
}
}
----------------------
运行结果:javajavaC
----------------------
3、现有程序如下,写出程序运行结果:
class A
{
int a =1;
double d=2.0;
void show(){
System.out.println("Class A:a="+a+"\td="+d);
}
}
class B extends A
{
float a=3.0f;
String d="Java program.";
void show(){
super.show();
System.out.println("Class B:a="+a+"\td="+d);
}
public static void main(String[] args){
//A a = new A();
//a.show();
A b = new B();
b.show();
System.out.println(b.a);
}
}
--------------------------------------------
①若在应用程序的main方法中有以下语句:
A a = new A();
a.show();
结果是:
②若在应用程序的main方法中定义类B的对象b;
A b = new B();
b.show();
System.out.println(b.a);
结果是:
--------------------------------------------
4、若应用程序的main方法中,定义字符串数组f和s:分别表示扑克牌的牌面值和花
色;定义52个元素的Card类型数组deck,用来存放4个花色的52张牌。如下所示:
String number[] =
{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String color[] = {"黑桃","红桃","梅花","方块"};
Card deck = new Card[52];
Card类的源码如下:
class Card{
private String face;
private String suit;
public Card(String f,String s){
face = f;
suit = s;
}
protected String getSuit(){
return suit;
}
protected String getFace(){
return face;
}
public String toString(){
return face + "of" +suit;
}
}
①用Card类的构造方法给deck数组的52张牌赋值,要求黑红梅方,从大到小的顺
序存放
②编写模拟洗牌的程序,把数组deck中的扑克牌随机打乱存放顺序