Shape s2=new Circle(3.0);
System.out.println(s2.area());
s2.print();
}
}
8.public class ObjectCreation {
TestClass testClass = new TestClass(“对象变量fieldV alue1”);
static TestClass testClass2 = new TestClass(“静态变量staticV alue1”);
static {
testClass2.test();
}
public ObjectCreation(String name) {
System.out.println(“ObjectCreation有参数构造方法“+ name + “init”);
}
public static void main(String[] args) {
ObjectCreation objectCreation = new ObjectCreation(“object1”);
ObjectCreation objectCreation2 = new ObjectCreation(“object2”);
}
}
class TestClass {
public TestClass(String name) {
System.out.println(“TestClass有参数构造方法“+ name + “ init”);
}
void test() {
System.out.println(“普通test方法被调用”);
}
}
9.public class Stat{
public static void main(String args[ ]){
int n=0;
String s=”The 30th Olympic games is held in London last summer.”;
for (int i=0;iif (s.charAt(i)==’m’) n++;
System.out.println(n);
}
}
四、程序设计题
1.(本小题7分)编写一个程序接受用户从键盘输入的10个整数,并输出这10个整数中的最大值和最小值。
2.(本小题7分)编程定义一个描述矩形的类,类名为Rectangle ,并符合以下要求:
1)矩形类包括2个double类型的数据域height和width,分别代表长和宽。
2)无参构造方法创建矩形对象时height和width的值为1.0;有参构造方法的形式参数为height和width提供值。
3)对数据域height和width进行合理的封装。