问答题本程序分别输出字符串:a/b、"c/d"和'e/f'。
public class exam_59{
public static void main(String[] args) {
String str=new String();
______;
System.out.println(str);
______;
System.out.println(str);
______;
System.out.println(str);
}
}
问答题请完成下列Java程序:用一个边框布局来安排一个工具栏和一个卡片布局。工具栏可浮动,包括2个按钮,一个向前一个向后控制卡片的显示;卡片布局包括3张卡片,分别标识为card1,card2,card3。注意;请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.border.LineBorder;publicclassex24_2{privatestaticCardLayoutcl=newCardLayout();privatestaticJPanelcp=newJPanel();publicstaticvoidmain(String[]args){JFrameif=newJFrame();jf.setTitle("ex24_2");Containerc=jf.getContentPane();JToolBarjtb=newJToolBar();JButtonbtnNext=newJButton(newImageIcon("next.gif"));JButtonbtnPre=newJButton(newImageIcon("back.gif"));jtb.add(btnPre);jtb.add(btnNext);cp.setLayout(cl);Dimensiondim=newDimension(150,80);cp.setPreferredSize(dim);cp.setBorder(newLineBorder(Color.BLACK));cp.add(newJLabel("cardl",JLabel.CENTER),"card1");cp.add(newJLabel("card2",JLabel.CENTER),"card2");cp.add(newJLabel("card3",JLabel.CENTER),"card3");c.add(jtb,BorderLayout.NORTH);c.add(cp,BorderLayout.CENTER);ActionListeneral=newActionResponse();____________________________________;____________________________________;jf.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventwe){System.exit(0);}});jf.pack();jf.setVisible(true);}staticclassActionResponseimplementsActionListener{publicvoidactionPerformed(ActionEventae){if(ae.getActionCommand().equals("next"))cl.next(cp);elsecl.previous(cp);}}}
问答题请完成下列Java程序:计算下列级数之和,直到该项之绝对值小于0.001为止。 S=1/1!-1/2!+1/3!-1/4!+··· 注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。 程序运行结果如下: i=1 s=1.0 i=2 s=0.5 i=3 s=0.6666666666666666 1=4 s=0.625 i=5 s=0.6333333333333333 1=6 s=0.6319444444444444 public class ex28_2 public static void main(String[] args) double s=0.0,sign=-1.0,t,f=1.0; for(int i=1;;i++) f=f*i; ____________; t=sign/f; if(__________) break; s=s+t; System.out.println("i="+i+" s="+s);
问答题本题分别比较两个字符串"A"和"a"是否相等,并比较两个字符"A"和"a"是否相等,并输出比较结果。
public class java1{
public static void main(String[]args){
______;
c1='A';c2='a';
String str1=new String("A"),str2=new String("a");
if(______)
System.out.println("char"+c1+"equals"+"char"+c2);
else
System.out.println("char"+c1+"doesn't equal"+"char"+c2);
if(______)
System.out.println("string"+str1+"equals"+"string"+str2);
else
System.out.println("string"+str1+"doesn't equal"+"string"+str2);
}
}
问答题本题的功能是用复选按钮来控制鼠标右键的弹出菜单是否弹出。窗口中有一个复选按钮“弹出菜单”,如果选中该复选按钮后,鼠标置于窗口上,单击鼠标右键会弹出一个菜单,单击菜单项中的选项后,后台会输出单击的菜单项,如果取消该复选按钮的选择,单击鼠标右键则不能弹出菜单。 import java.awt.*; import java.awt.event.*; class CanvasWithPopup extends Canvas Popupmenu popup; CanvasWithPopup(PopupMenu popup) enableEvents(AWTEvent.MOUSE_EVENT_MASK); this.popup=popup; void addPopup() add(popup); void removePopup() remove(popup); protected void processMouseEvent(MouseEvent evt) if(popup.getParent()!=null super.processMouseEvent(evt); public class java3 extends Frame implements ItemListener,ActionListener Checkbox cb=new Checkbox("弹出菜单",false); CanvasWithPopup canvas; java3() super("java3"); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); add(cb,BorderLayout.NORTH); cb.addItemListener(this); PopupMenu popup=new PopupMenu("Button Control"); popup.add("item1"); popup.add("item2"); popup.addActionListener(this); canvas=new CanvasWithPopup(popup); add(canvas,BorderLayout.CENTER); setSize(100,200); show(); public void itemStateChanged(ItemEVent evt) switch(evt.getState())( case ItemEvent.SELECTED: canvas.addPopup(); break; case ItemEvent.DESELECTED: canvas.removePopup(); break; public void actionPerformed(ActionEvent evt) System.out.printlnCgetActionCommand()+"is seIected"); static public void main(String[]args) new java3();
问答题System.out.println(vName.get(j));
问答题本题中定义了-个简单的计算器,可以进行基本的四则运算。程序中包含16个按钮用来表示0~9、+、-、
问答题请完成下列Java程序:输出某年的某月有多少天数,要求生成2个输入对话框,一个输入年份,一个输入月份,并且能检查输入的数字是否符合要求,如果符合要求,则根据输入的年份和月份计算出这一年的这一月有多少天。提示:闰年为年数能被4整除但是不能被100整除。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:importjavax.swing.JOptionPane;publicclassex10_2{publicstaticvoidmain(String[]args)(StringstrIn;intnYear,nMonth,nNumOfDays=0;strIn=JOptionPane.showInputDialog(,,year.);nYear=Integer.parseInt(strIn);if(nYear<0){System.out.println("Invalidatenumberofyear!")return;}strIn=JOptionPane.showInputDialog("month.);nMonth=Integer.parseInt(strIn);if(_____________){System.out.println("Invalidatenumberofmonth!"return;}switch(nMonth){case1:case3:case5:case7:case8:case10:case12:nNumOfDays=31;break;case4:case6:case9:case11:nNumOfDays=30;break;case2:if(________________)nNumOfDays=29;elsenNumOfDays=28;break;}System.out.println("Numberofdays:"+nNumOfDays);}}
问答题注意:下面出现的“考生文件夹”均为%USER%。 在考生文件夹中存有文件名为Java_2.java的文件,该程序是不完整的,请在注释行“//**********Found**********”下一行语句的下画线地方填入正确内容,然后删除下画线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。 本题的要求是: 创建了一个线程,连续输出6行字符串“Hello”。 运行结果为 Hello! Hello! Hello! Hello! Hello! Hello! 给定源程序: //Java_2.java public class Java_2 public static void main(String args[]) Hello h=new Hello(); //*********Found******** Thread t=______; t.start(); //*********Found******** class Hello implements______ int i=0; public void run() while(true) System.out.println("Hello!"); if(i++==5)break;
问答题本程序的功能是输出字符串:"Welcome!"和表达式:a/b=c,以及输出://do something。
public class exam_7{
public static void main(String[] args){
System.out.println(______);
System.out.println(______);
System.out.println(______);
}
}
问答题下面是一个Applet程序,其功能是构造n阶魔方阵,魔方阵是这样一个方阵,它的每一行,每一列和对角线之和均相等,例如3阶魔方阵为 8 1 6 3 5 7 4 9 2 要求用Applet实现上述魔方阵。窗口中包括提示栏,提示输入0~15之间的奇数;输入框,输入方阵的阶数;按钮;点击则输出魔方阵;画布用于输出结果。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 源程序文件代码清单如下: import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.lang.Math.*; /* <applet code="ex10_3.class" width=800 height=400> </applet> */ public class ex10_3 extends Applet implements ActionListener Panel pane=new Panel(); drawWnd dw; Label 11 = new Label("输入(0~15的奇数): "); TextField tf1 = new TextField(5); Button btn = new Button("OK"); int[] [] a=new int[16] [16]; int n=15; public void init() pane.setLayout(new FlowLayout(FlowLayout.CENTER, 25,5)); pane.add(11); pane.add(tf1); pane.add(btn); pane.addActionListener(this); add("North",pane); dw=new drawWnd(); add("South",dw); class drawWnd extends Canvas drawWnd ( ) setSize(300,300); setBackground(Color.GRAY); public void paint(Graphics g) g.setColor(Color. GREEN); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) g.drawString(Integer.toString(a[i] [j]),i*20,j*20); public void actionPerformed(ActionEvent ae) try n=Integer.parseInt (tf1.getText ()); int i=1, j=1; for (i=1;i<=n;i++) for (j=1; j<=n; j++) a[i] [j]=0; j=n/2+1; a[i] [j]=1; for (int k=1; k<=n*n; k++) i--; j++; if((i<1) j--; else if (i<1) i=n; if (j>n) j=1; if (a[i] [j]==0) a[i] [j]=k; else i=i+2; j--; a[i] [j]=k; dw. repaint ( ); catch (NumberFormatException nfe) tf1.setText ("error!"); ex10_3.html <HTML> <HEAD> <TITLE>ex10_3</TITLE> </HEAD> <BODY> <applet code="ex10_3.class" width=800 height=400> </applet> </BODY> </HTML>
问答题下面的程序的功能是求1~100的奇数的和及该和的平均值。请在程序的每条横线处填写一个语句,程序的功能完整。 注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 public class SumAndAve public static void main(String args[ ]) int count=0,sum=0,ave=0; for(int i=1;i<=100;____________________) if(_____________________) continue; else ___________________ sum=sum+i; ave=sum/count; System.out.println("sum="+sum); System.out.println("ave="+ave);
问答题本题使用下拉菜单来控制字体,窗口中有一个标签和一个下拉菜单,当选中下拉菜单中的任一项字体时,标签上字符串的字体就随之改变。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ComboBoxFrame extends JFrame {
public ComboBoxFrame(){
setTitle("java2");
setSize(300,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
style=new JComboBox():
style.setEditable(true);
style.addhem("Serif");
style.addItem("SansSerif");
style.addhem("Monospaced");
style.addhem("Dialog");
style.addhem("Dialoglnput");
style.addActionListener(this);
JPanel p=new JPanel();
P.add(style);
getContentPane().add(p,"South");
panel=new ComboBoxTestPanel();
getContentPane().add(panel,"Center");
}
public void actionPerformed(ActionEvent evt){
JComboBox source=(JComboBox) ;
String item=(String)source.getSelectedhem():
panel.setStyle(item);
}
private ComboBoxTestPanel panel;
private JComboBox style;
}
class ComboBoxTestPanel extends JPanel{
public ComboBoxTestPanel(){
setStyle("Serif");
}
public void setStyle(String s){
setFont(new Font(S,Font.PLAIN,12));
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
9.drawString("Welcome to China!",0,50);
}
}
public class java2{
public static void main(String[]args){
JFrame frame=new ComboBoxFrame();
frame.show();
}
}
问答题本程序中,将字符串"Tadf$asdf
int i;
______(i=0; ______){
char c=str.charAt(i);
if(______)
System.out.print(c);
}
}
}
问答题本程序提示输入月份,然后判断该月份的天数,如果输入的是2月份,则天数按28天计。
import java.io.*;
public class exam_14{
public static void main(String[] args){
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
int month=1;
int day=0;
System.out.print("请输入月份:");
try{
______;
month=Integer.parseInt(s);
}catch(Exception e){
}
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: ______;
case 4:
case 6:
case 9:
case 11: ______;
case 2: day=28;
}
System.out.println(month+"月有"+day+"天");
}
}
问答题综合应用题下面是一个Applet程序,其功能是将完整的图像显示于Applet的区块中,然后可以通过拖动鼠标让图像随着鼠标拖动的轨迹而移动。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:importjava.applet.*;importjava.awt.*;/**/publicclassex8_3extendsApplet{privateImageiImg;privateintxPos,yPos;publicvoidinit(){xPos=yPos=0;iImg=getImage("ex8_3.jpg");}publicvoidpaint(Graphicsg){g.drawImage(iImg,xPos,yPos);}publicbooleanmouseDrag(Evente,intx,inty){xPos=x;yPos=y;paint();returntrue;}}ex8_3.htmlex8_3
问答题本题的功能是监听鼠标的操作。鼠标置于窗口中单击时(左键或右键),在单击的地方会画一个小矩形,如果将鼠标置于小矩形上,则鼠标光标状态改为小十字,按下鼠标左键可拖曳,双击鼠标左键(或右键)时,小矩形消失。 import java.awt.*; import java.awt.event.*; import java.util.*; import java.awt.geom.*; import javax.swing.*; public class java3 public static void main(String[]args) MouseFrame frame=new MouseFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); class MouseFrame extends JFrame public MouseFrame() setTitle("java3"); setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); MousePanel panel=new MousePanel(); Container contentPane=geeContentPane(); contentPane.add(panel); public static final int DEFAULT_WIDTH=300; public static final int DEFAULT_HEIGHT=200; class MousePanel extends JPanel public MousePanel() squares=new ArrayList(); current=null; addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMorionHandler()); public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2=(Graphics2D)g; for(int i=0;i<squares.size();i++) g2.draw((Rectangle2D)squares.get(i)); public void find(Point2D p) for(int i=0;i<squares.size();i++) Rectangle2D r=(Rectangle2D)squares.get(i); if(r.contains(p))return r; return null; public void add(Point2D p) double x=p.getX(); double y=p.getY(); current=new Rectangle2D.Double( x-SIDELENGTH/2, y-SIDELENGTH/2, SIDELENGTH, SIDELENGTH), squares.add(current); repaint(); public void remove(Rectangle2D s) if(s==null)return; if(s==current)current=null; squares.remove(s); repaint(); private static final int SIDELENGTH=10; private ArrayList squares; private Rectangle2D current; private class MouseHandler extends MouseActionListener public void mousePressed(MouseEvent event) current=find(event.getPoint()); if(current==null) add(event.getPoint()); public void mouseClicked(MouseEvent event) current=find(event.getPoint()); if(current!=null private class MouseMotionHandler implements MouseMotionListener public void mouseMoved(MouseEvent event) if(find(event.getPoint)==null) setCursor(Cursor.getDefaultCursor()); else setCursor(Cursor.getPredefinedCursor (Cursor.CROSSHAIR_CURSOR)); public void mouseDragged(MouseEvent event) if(current!=null) int x=event.getX(); int y=event.getY(); current.setFrame( x-SIDELENGTH/2, y-SIDELENGTH/2, SIDELENGTH, SIDELENGTH); repaint();
问答题请完成下列Java程序:制作一个图形用户界面,上方包含一个TextField和Button构件,实现输入字符串,点击Button获取文本区的字符:中间显示Label的内容:下方是4个按钮,分别实现控制Label在最左边,在中间,在右边和退出程序的功能。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:importjava.awt.*;importjava.awt.event.*;publicclassex15_2extendsFrameimplementsActionListener{privateLabel1;privateTextFieldtf;publicstaticvoidmain(String[]args){ex15_2obj15_2=newex15_2();}publicex15_2(){setBackground(Color.gray);l=newLabel("WelcomtotheNCRExamination!");Fontfont=newFont("TimesRoman",Font.BOLD,20);l.setFont(font);add("Center",l);Panelp=newPanel();Buttonb=newButton("Left");b.addActionListener(this);p.add(b);b=newButton("Center");b.addActionListener(this);p.add(b);b=newButton("Right");b.addActionListener(this);p.add(b);________________;b=newButton("Exit");b.addActionListener(this);p.add(b);p=newPanel();tf=newTextField(40);p.add(tf);b=newButton("Set");b.addActionListener(this);p.add(b);add("North",p);setSize(500,300);show();}publicvoidactionPerformed(ActionEventae){if(ae.getActionCommand().equals("Exit"))System.exit(0);elseif(ae.getActionCommand().equals("Left"))____________________;elseif(ae.getActionCommand().equals("Center")l.setAlignment(Label.CENTER);elseif(ae.getActionCommand().equals("Right"))l.setAlignment(Label.RIGHT);elseif(ae.getActionCommand().equals("Set"))l.setText(tf.getText());}}
问答题本程序求三个整数a、b、c中的最大数,并打印输出计算结果,其中,a为69,b为54,c为87。请将程序中空缺的部分填写适当内容,使程序能正确运行。
public class exam_84{
public static void main(String[] args) {
int a=69,b=54,c=87;
______;
if(a>b){
if(a>c)
max=a;
else
______;
}
else{
if(b>c)
______;
else
max=c;
}
System.out.println("Max num:"+max);
}
}
问答题请在每条横线处填写一个语句,使程序的功能完整,且输出结果为911。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
public class Outer
{
public static void main(String args[])
{
Outer i=new Outer();
i.taskInner();
}
public class lnner
{
private int size;
public void doSomething(int size)
{
__________//访问局部变量
this.size++;//访问内部类的成员变量
__________//访问外部类的成员变量
System.out.println(size+" "+this.size+" "+Outer.this.size);
}
}
public void tasklnner()
{
__________
k.doSomething(8);
}
private static int size;
}