编程题
按指定的要求编写程序段,
1.编写一个程序,把六个按钮分别标识为‘A'至‘F’,并排列成一行。
用java编写一个简单的计算器参考程序:
import java.awt。*;
public class MyClass {
  public static void main(String args[]) {
      String[] labels = {”A”,"B","C","D”,"E",”F”};
      Window win =  new Frame();
      win。setLayout(new GridLayout(1,6));
      for(int i=0;i 〈 labels.length;i++)
        win.add(new Button(labels[i]));
      win。pack();   
      win。setVisible(true);
  }
解析】该题目的知识点在于图形用户界面设计中的布局设计,只能采用GridLayout的方式,不能采用默认的FlowLayout布局方式
2.设计一个applet,加载和显示图象“student。gif”.图像文件与包含applet的HTML文件在同一个目录下。
参考程序:
import java。awt。*;
import java.applet。*;
public class Mypicture extends java。applet。Applet{
    Image image;
    public void init (){
      image=getImage(getDocumentBase(),”Student.gif”);
    }
    public void paint(Graphics g){
      g。drawImage(image,0,0, this );
    }
   
}
解析】该题要求掌握如何在applet中显示图像文件的编程能力,具体可参照参考教材第十章。
3。 编写removeRandChar()方法,该方法有两个参数,类型分别是String和int,第二个参数表示从第一个参数指定的字符串中删除字符的个数.删除哪个字符由随机数决定。如果第二个参数比第一个参数指定的字符串长度大,则该方法返回空字符串。
removeRandChar()方法必须包括调用removeSingChar()方法,该方法定义在test4_1应用程序中。
例如执行完整的test4_1应用程序两次后,产生如下的输出:
C:\〉 java test4_1
Remove 3 random characters from INTERESTING: INEETING
Remove 6 random characters from INTERESTING: EESTN
C:\> java test4_1
Remove 3 random characters from INTERESTING: ITERETIN
Remove 6 random characters from INTERESTING: IEESG
下面是不包括removeRandChar()方法的test4_1应用程序:
  public class test4_1{
public static void main(String[] args) {
String word1 = removeRandChar("INTERESTING", 3);
System。out.println(”Remove 3 random characters from INTERESTING: " + word1);
word1 = removeRandChar(”INTERESTING”, 6);
System。out.println("Remove 6 random characters from INTERESTING: " + word1);
}
/**
This method removes the character at position: indexNum, from the String: str,
and returns the resulting String.
*/
private static String removeSingChar(String str, int indexNum) {
return str.substring(0,indexNum) + str.substring(indexNum+1);
……
}
参考程序:
private static String removeRandChar(String str,  int howMany){
  if (str。length() 〈= howMany)
    return ”";
  int randNo;
  for(int j=0; j<howMany; j++){
    randNo = (int) (Math。random() * str.length());
    str = removeSingChar(str, randNo);
  }
  return str;
解析】该题要求掌握编写方法和调用方法的能力,要注意形参的使用,以及有返回值和没有返回值的不同编写形式。

4。仔细阅读下面student类的定义,该类的定义可以存放学生的姓名(name)、测验分数(testMark)和考试分数(examMark)。
public class Student {
private String name;
private int testMark;
private int examMark;
public Student(String theName) {
name = theName;
}
public Student(String theName, int test, int exam) {
name = theName;
testMark = test;
examMark = exam;
}
public void setExamMark(int exam) {
examMark = exam;
System.out.println(name + ”\’s exam mark changed to " + examMark);
}
public int getTestMark() {
return testMark;
}
public void displayInfo() {
System.out.println(name + ” got ” + testMark + " in
the test and " + examMark + " in the exam");
public void compareTo(Student other) {
if (examMark 〉 other。examMark)
System.out.println(name + " did better than ” + other。name);
else
System。out。println(name + " did worse than ” + other.name);
}
}
要求完成test4_3的编程,它将创建两个Student对象并调用相应的方法,产生的输出如下所示:
C:\> java test4_3
张楠 got 70 in the test and 85 in the exam
李浩 got 80 in the test and 90 in the exam
李浩's exam mark changed to 40
李浩 did worse than 张楠
注意不能使用任何System。out.print() 或 System.out.println()语句,产生的输出只需简单地调用所创建的Student对象的对应方法。
public class test4_3 {
public static void main(String[] args) {
Student student1;
Student student2;
……
参考程序:
student1 = new Student(”张楠”, 70, 85);
  student2 = new Student(”李浩”, 80, 90);
  student1.displayInfo();
  student2。displayInfo();
  student2.setExamMark(40);
  student2。compareTo(student1);
解析】该题考核的重点是面向对象程序设计的基本应用:对象的声明与创建,以及根据需求调用实例方法。
5.编写change( ) 方法,该方法有一个参数,类型为int,通过方法,计算并输出由给定参数(元)的人民币兑换成一元、两元、五元的所有方案。
例如当用户输入10,执行Test4_1应用程序后,产生如下的输出:
import java.io.*;
public class Test4_1{
  public static void main(String args[]) throws IOException {
      int money;
      String str;
      BufferedReader buf;
     
      buf=new BufferedReader(new InputStreamReader(System.in));
     
      System。out。print("Input an integer:");
      str=buf。readLine();                             
      money=Integer。parseInt(str);