Java语言程序设计模拟题8.docx
- 文档编号:8341496
- 上传时间:2023-01-30
- 格式:DOCX
- 页数:10
- 大小:36.09KB
Java语言程序设计模拟题8.docx
《Java语言程序设计模拟题8.docx》由会员分享,可在线阅读,更多相关《Java语言程序设计模拟题8.docx(10页珍藏版)》请在冰豆网上搜索。
Java语言程序设计模拟题8
Java语言程序设计模拟题(8)
一、编程题。
本题共七题,第1题5分,2、3、4题每题10分,第5、6、7题每题15分,共80分。
1、编程实现,当在命令行中输入“好”时,输出为“good”,当在命令行中输入“坏”时,输出为“bad”。
2.编写程序计算12+22+32+42+……+972+982+992+1002的值,输出。
3.编写类C1,C1中只有一个方法pa(),显示“济南职业学院”,编写类C2,调用pa()方法,在屏幕上显示10行“济南职业学院”。
4、自定义类Days及其方法dayInmonth()。
该方法的功能是返回用户输入月份的天数(二月份就按28天计算)。
例如用户输入3,则该方法将返回值31;用户输入4,则该方法将返回值30。
通过main()方法将返回值显示出来。
(本题10分)
5、自定义类Cla,该类中有方法convert(),其功能是将传入的数字字符串转换成数字,并显示出来。
但若传入的字符串中含有字母,则该方法能抛出NumberFormatException异常,并输出“所输入的字符串中含有非数字的文字,无法转换”。
(本题10分)
6、编写程序,实现如图所示功能:
在宽和高文本框中输入数值,当单击“更改”按钮时,将窗体大小改为文本框中输入的数值。
单击“退出”时,退出应用程序。
(本题10分)
7、编写一个Applet程序Myapplet,实现如图所示的界面,并将怎样把该程序插入网页中的代码段写出来。
(本题15分)
8、编写程序,实现如图所示功能:
在x和y两个文本框中输入数值,按下右面的任意按钮能进行相应的计算,并将结果显示在中间的文本区中,该区域可以用清除按钮清除。
(本题15分)
二、程序解释题(共1题,共20分)
importjava.awt.*;
importjava.awt.event.*;
publicclassMouseeventextendsWindowAdapterimplementsMouseMotionListener{
Framef=newFrame();
Panelp1=newPanel();
Panelp2=newPanel();
Labell1=newLabel("X轴");
Labell2=newLabel("Y轴");
TextFieldtf1=newTextField(4);
TextFieldtf2=newTextField(4);
intx;
inty;
publicstaticvoidmain(String[]asgs){
Mouseeventhua=newMouseevent();
hua.go();
}
privatevoidgo(){
f.setSize(300,200);
f.add(p2,"South");
p2.add(l1);
p2.add(tf1);
p2.add(l2);
p2.add(tf2);
f.add(p1);
p1.addMouseMotionListener(this);
f.addWindowListener(this);
f.setVisible(true);
}
publicvoidmouseDragged(MouseEventevent){
}
publicvoidmouseMoved(MouseEventevent){
tf1.setText(Integer.toString(event.getX()));
tf2.setText(Integer.toString(event.getY()));
}
publicvoidwindowClosing(WindowEventwe){
System.exit(0);
}
}
}
参考答案:
1、classSa{
publicstaticvoidmain(String[]args){
if(args[0].equals(“Good”)){
System.out.println(“好”);}
if(args[0].equals(“Bad”)){
System.out.println(“坏”);}
}
}
2、classSa{
publicSa(Stringx){
if(x.equals(“Good”)){
System.out.println(“好”);}
if(x.equals(“Bad”)){
System.out.println(“坏”);}
}
}
publicclassSb{
publicstaticvoidmain(String[]args){
Sasa=newSa(args[0]);
}
}
3、在Sb类的定义前加上语句importmypackage.jiafa;即可。
4、classDays{
staticintdayInmonth(inta){
ints=31;
switch(a)
{case2:
s=28;break;
case4:
case6:
case9:
case11:
s=30;
}
returns;
}
publicstaticvoidmain(String[]args){
inta=Integer.parseInt(args[0]);
intb=dayInmonth(a);
System.out.println(a+"月有"+b+"天");
}
}
5、classCla{
staticvoidconvert(Strings){
try{
doublea=Double.parseDouble(s);
System.out.println(a);
}catch(NumberFormatExceptione){
System.out.println("所输入的字符串中含有非数字的文字,无法转换");
}
}
publicstaticvoidmain(String[]args){
convert(args[0]);
}
}
6、importjava.awt.*;
importjava.awt.event.*;
classMyframeimplementsActionListener{
Framef=newFrame("窗口更改大小");
Panelp=newPanel();
Panelp1=newPanel();
Labell=newLabel("宽:
");
Labell1=newLabel("高:
");
TextFieldtf=newTextField(4);
TextFieldtf1=newTextField(4);
Buttonb=newButton("更改");
Buttonb1=newButton("退出");
publicstaticvoidmain(String[]args){
Myframemy=newMyframe();
my.go();
}
voidgo(){
p.add(l);
p.add(tf);
p.add(l1);
p.add(tf1);
p1.add(b);
p1.add(b1);
b.addActionListener(this);
b1.addActionListener(this);
f.add(p,"North");
f.add(p1);
f.setSize(200,100);
f.setVisible(true);
}
publicvoidactionPerformed(ActionEvente){
Strings=e.getActionCommand();
if(s.equals("更改")){
inta=Integer.parseInt(tf.getText());
intb=Integer.parseInt(tf1.getText());
//f.setSize(a,b);
f.resize(a,b);
}
elseSystem.exit(0);
}
}
7、importjava.awt.*;
importjavax.swing.*;
publicclassMyappletextendsJApplet{
JRadioButtonjRadioButton1=newJRadioButton("RadioButton1");
JRadioButtonjRadioButton2=newJRadioButton("RadioButton2");
ButtonGroupbtg=newButtonGroup();
JCheckBoxjCheckBox1=newJCheckBox("CheckBox1");
JCheckBoxjCheckBox2=newJCheckBox("CheckBox2");
JCheckBoxjCheckBox3=newJCheckBox("CheckBox3");
JTextFieldjTextField1=newJTextField("TextField1");
JLabeljLabel1=newJLabel("Label1");
FlowLayoutflowLayout1=newFlowLayout();
publicvoidinit(){
myInit();
}
voidmyInit(){
this.getContentPane().setLayout(flowLayout1);
btg.add(jRadioButton1);
btg.add(jRadioButton2);
this.getContentPane().add(jRadioButton2,null);
this.getContentPane().add(jRadioButton1,null);
this.getContentPane().add(jCheckBox1,null);
this.getContentPane().add(jCheckBox2,null);
this.getContentPane().add(jCheckBox3,null);
this.getContentPane().add(jLabel1,null);
this.getContentPane().add(jTextField1,null);
}
}
8、importjava.awt.*;
importjava.awt.event.*;
classjisuanqiimplementsActionListener{
Framef=newFrame("计算器");
Panelp=newPanel();
Panelp1=newPanel();
Panelp2=newPanel();
Buttonb=newButton("x+y");
Buttonb1=newButton("x-y");
Buttonb2=newButton("清除");
Labellbx=newLabel("x");
Labellby=newLabel("y");
TextFieldtfx=newTextField(4);
TextFieldtfy=newTextField(4);
TextAreata=newTextArea();
Fontft=newFont("宋体",Font.BOLD,18);
GridLayoutg1=newGridLayout(4,1);
GridLayoutg2=newGridLayout(2,1);
doublex,y,sum;
publicstaticvoidmain(String[]args){
jisuanqiji=newjisuanqi();
ji.go();
}
publicvoidgo(){
b.setFont(ft);
b1.setFont(ft);
b2.setFont(ft);
lbx.setAlignment(Label.CENTER);
lby.setAlignment(Label.CENTER);
lbx.setFont(ft);
lby.setFont(ft);
tfx.setFont(ft);
tfy.setFont(ft);
ta.setFont(ft);
p.setLayout(g1);
p.add(lbx);
p.add(tfx);
p.add(lby);
p.add(tfy);
f.add(p,"West");
p1.setLayout(g2);
p1.add(b);
p1.add(b1);
b.addActionListener(this);
b1.addActionListener(this);
f.add(p1,"East");
p2.add(b2);
b2.addActionListener(this);
f.add(p2,"South");
f.add(ta,"Center");
f.setSize(250,150);
f.setVisible(true);
}
publicvoidactionPerformed(ActionEventae){
Strings=ae.getActionCommand();
x=Double.parseDouble(tfx.getText());
y=Double.parseDouble(tfy.getText());
if(s.equals("x+y")==true){
ta.append("x+y="+(x+y)+"\n");
}
if(s.equals("x-y")==true){
ta.append("x-y="+(x-y)+"\n");}
if(s.equals("清除")==true){
ta.setText("");
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 语言程序设计 模拟