郑州大学现代远程教育Java程序设计Word下载.doc
《郑州大学现代远程教育Java程序设计Word下载.doc》由会员分享,可在线阅读,更多相关《郑州大学现代远程教育Java程序设计Word下载.doc(9页珍藏版)》请在冰点文库上搜索。
2)简述基于Swing技术的GUI应用程序基开发步骤。
答:
1) 引入Swing包:
2) importjavax.swing.*;
3) importjava.awt.*;
4) importjava.awt.event.*;
5) 选择GUI的外观风格Look&
Feel
6) 创建并设置顶级窗口容器
7) 创建与添加相关Swing组件
8) 实现相应的事件监听器类,将组件注册
9) 调整大小并显示顶层容器,将整个GUI界面显示出来。
3)常用的异常处理方法有哪些?
异常处理是指程序获得异常并处理,然后程序继续执行。
常用异常处理方法:
1)捕获并处理例外
2)将方法中产生的例外抛出:
声明异常和抛出异常
1.异常处理:
在Java语言的错误处理结构由try,catch,finally三个块组成。
其中try块存放将可能发生异常的Java语言,并管理相关的异常指针;
catch块紧跟在try块后面,用来激发被捕获的异常;
finally块包含清除程序没有释放的资源,句柄等。
不管try块中的代码如何退出,都将执行finally块。
2.抛出异常:
Java语言可以不在方法中直接捕获,而用throw语句将异常抛给上层的调用者。
Throw语句就是来明确地抛出一个异常;
首先你必需得到一个Throwable的实例句柄,通过参数传到catch中,或者采用new操作符来创建一个。
2.读程序题
1)程序Assign,要求注释带标号1,2,3,4等的句子,并给出程序的运行结果。
publicclassAssign{
publicstaticvoidmain(Stringargs[]){
intx,y;
floatz=3.414f;
doublew=3.1415;
//1.声明并赋值double型变量
booleantruth=true;
//2.声明并赋值boolean型变量
charc;
Stringstr;
Stringstr1="
bye"
;
//3.声明并赋值String类变量
c='
A'
//4.给字符变量赋值
str="
Hioutthere"
x=6;
y=1000;
System.out.println("
x="
+x);
y="
+y);
z="
+z);
w="
+w);
truth="
+truth);
c="
+c);
str="
+str);
str1="
+str1);
}
}
程序的运行结果为:
x=6
y=1000
z=3.414
w=3.1415
truth=true
c=A
str=Hioutthere
str1=bye
2)读下面程序,从键盘输入5,回车后输出的结果如何?
从键盘输入quit,回车后程序执行情况如何?
importjava.io.*;
publicclassTest{
publicstaticvoidmain(Stringargs[])throwsIOException{
BufferedReaderbuf=newBufferedReader(
newInputStreamReader(System.in));
while(true){
Stringstr=buf.readLine();
if(str.equals("
quit"
))
break;
intx=Integer.parseInt(str);
System.out.println(x*x);
}
}
从键盘输入5,回车后输出:
25
从键盘输入quit,回车后:
终止应用程序的运行
3)读程序,画图表示程序运行后的图形界面
importjava.awt.*;
importjavax.swing.*;
publicclassTextendsJFrame{
publicT(){
super("
GridLayout"
);
Containercon=this.getContentPane();
con.setLayout(newGridLayout(2,3));
con.add(newJButton("
a"
));
b"
c"
d"
e"
f"
setSize(200,80);
setVisible(true);
publicstaticvoidmain(Stringargs[]){
newT();
}
程序运行后的图形界面为:
3.程序设计题目
1)编程序实现,用Switch语句输出2000年2月所包含的天数。
(要求适当加上程序注释。
)
程序如下:
publicclassCalculateDays{
publicstaticvoidmain(String[]args){
intyear=2000;
//年份
intmonth=2;
//月份
intdays=0;
//天数
switch(month){
case1:
case3:
case5:
case7:
case8:
case10:
case12:
days=31;
//1、3、5、7、8、10、12月份有31天
break;
case4:
case6:
case9:
case11:
days=30;
//4、6、9、11月份有30天
case2:
//根据年份计算2月份的天数
if(((year%4==0)&
&
!
(year%100==0))||(year%400==0)){
//如果年份能被4整除但不能被100整除,或者年份能被400整除
days=29;
//则这个年份为闰年,2月份有29天
}else{
days=28;
//其它的年份为平年,2月份有28天
}
System.out.println(year+"
年"
+month+"
月所包含的天数:
"
+days);
//输出结果
}
2)编写一个Rectangle类,含有一个点(point对象,创建方法p=newPoint(0,0);
),宽(width)和高(high),在其中完成下面功能:
l试着写至少一个构造方法;
l写出求矩形类的长、高、面积方法;
Rectangle类为:
publicclassRectangle{
privatePointp;
privateintwidth,high;
publicRectangle(){
p=newPoint(0,0);
width=0;
high=0;
publicRectangle(Pointp,intwidth,inthigh){
this.p=p;
this.width=width;
this.high=high;
publicintgetWidth(){
returnwidth;
publicintgetHigh(){
returnhigh;
publicintcalculateArea(){
returnwidth*high;
3)写一个Applet,其完成功能就是在坐标(20,20)处打印出字符串:
”HelloWorld!
”;
同时其可以作为一个Application运行,在控制台处打印出字符串:
”。
Applet为:
importjavax.swing.JApplet;
importjava.awt.Graphics;
publicclassHelloWorldAppletextendsJApplet{
publicvoidpaint(Graphicsg){
g.drawString("
HelloWorld!
20,20);
publicstaticvoidmain(Stringargs[]){
System.out.println("