} }
3、编写程序实现窗口,包含一个标签、一个文本框和一个按钮,当用户单击按钮时,程序把文本框中的内容提制到标签中。 //引入相关包
public MyFrame{
public MyFrame(){
JFrame fr = new JFrame(); //设置窗体的布局为FlowLayout
JLabel lbl = new JLabel(“Init info”); JTextField txt = new JTextField(30); JButton btn = new JButton(“Sure”); fr.add(lbl); fr.add(txt); fr.add(btn);
//给按钮注册监听器
//使用窗体可见,并设置大小
}
//定义内部类监听ActionEvent事件 {
public void actionPerformed(ActionEvent e){ lbl.setText(txt.getText()); } }
public static void main(String[] args){ new MyFrame(); } }
4、仔细阅读下面的程序代码,请将划线上①~⑤的语句补充完整。 abstract class Person{ private String name;
public Person(String n){ name = n; }
public ____________ String getDescription(); public _____________ (){ return name; } }
class Student ____ _________ Person{ private String major;
public Student(String n, String m){ super(n); major = m; }
public String _____________(){ return \学生专业是:\ } }
public class TestPerson{
public static void main(____________){
Person p = new Student(\王甜甜\计算机科学\
System.out.println(p.getName() + \ } }
5、阅读下面的程序代码,根据注释要求补充、完成代码(划线是需要补充的地方),最后简要说明该程序的功能。
___________________{ //声明一个接口,接口名为Area public double CalsulateArea(); }
{//定义圆类Mycircle实现接口Area double r;
public MyCircle(double r){ this.r=r; }
{//实现接口中的方法 return Math.PI*r*r; } }
class MyRectangle implements Area{ double width,height;
public MyRectangle(double w,double h){ width=w; height=h; }
public double CalsulateArea(){ return width*height; } }
class TestArea{
public static void main(String []args){
//创建MyCircle的对象,对象名为c ,
半径为5
System.out.println(\ } }
6、定义一个抽象类AbstractTest,其中有一个公共的抽象方法printMsg()。然后定义此抽象类的一个子类DefaultTest,包括的成员变量有姓名,学号,分数,且此类中包括二个构造方法。
abstract class AbstractTest{
}
{ String name; String id; int score;
//接收三个参数的构造方法
//接收姓名和学号二个参数的构造方法
//实现抽象方法,方法体为打印出学生的姓名与成绩 }
7、定义接口Student,该接口中有一个无参、无返回值的方法prtMsg;定义类College,包括的私有属性有id和name,包括一个接收学生学号和姓名的构造方法,并且实现Student接口。
interface Student{
}
//College类的声明,实现Student接口 {
//定义私有成员变量id和name,类型自定
//构造方法声明和定义,接收二个参数 //实现prtMsg方法的声明 {
//prtMsg的方法体
System.out.println(id + name); } }
8、 定义类Stu,并把该类放在当前目录下的包com中:
包括的成员变量有:常量classid,类变量school,公共成员变量id和name,私有成员变量age。
包括的方法有:一个具有二个参数的构造方法;公共静态方法getSchool();主方法。 //把该类放在当前目录下的包com中
public class Stu{
//字符串常量classid //字符串类变量school,并把其值初始化为 “YEU”
private int age; public String id; public String name;
//构造方法声明,接收二个参数ids和names
{
id = ids; name = names; }
//公共静态方法getSchool()的声明 {
return school; }
//主方法
public static void main(String[] args){ System.out.println(Stu.getSchool()); System.out.println(Stu.school); } }
五、程序改错题
1、阅读下面的程序,修改程序中编译错误的地方(提示:共五处错误) interface Shape{ double PI;
public double area( );
public double perimeter( ); }
class Cycle extends Shape{ private double r;
public Cycle(double r){ this.r=r; }
double area( ){
System.out.println(PI*r*r); } }
public class Test{
public static void main(String args[]){ Cycle c=new Cycle(1.5);
System.out.println(\面积为:\
} }
2、阅读下面的程序,修改程序中错误的地方(提示:共四处错误) 1. class Person{
2. private String name; 3. private int age; 4. private String sex; 5. static int count=0;
6. public Person(String name,int age,String sex){ 7. this.name=name; 8. this.age=age; 9. this.sex=sex; 10. }
11. public void setAge(int age){ 12. this.age=age; 13. }
14. public void setCount(){ 15. count++; 16. }
17. public void toString(){
18. return \ 19. } 20. }
21. public class TestPerson{
22. public static void main(String args[]){ 23. Person p=new Person(\张三\男\24. p.age=21;//修改年龄为21 25. p.setCount();
26. System.out.println(Person.tostring()); 27. } 28. }
相关推荐: