1、二级 JAVA 机试-208 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是随机产生一个 110 的随机数,根据提示,用户可以猜这个随机数。例如程序随机产生的随机数是 7,如果输入 8,则提示“Try smaller”并继续提示输入;如果输出 6,则提示“Try bigger”并继续提示输入;如果输入 7,则会输出“Great!”并退出程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.io.*;public class basicpublic static void mai
2、n(String args)InputStreamReader it;Bur feredReader in;Ir = new InputStreamReader(System.in)In = new BufferedReader(ir);int ranNum = _;int guessNum = -1;String str;while (_)System.out.print (“Please Input:“);try_;guessNum = Integer.parseInt(str);catch (Exception e)if(guessNum ranNum)System.out.printl
3、n(“Try smaller.“);elseSystem.out.println(“Try bigger.“);System.out.println (“Great ! “);(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是程序的主窗口有一个“登录”按钮和两个文本框“请输入密码:”和“密码验证”,用户在密码框中输入密码后单击“登录”按钮,然后与预先定义的密码比较(密码预设定为password),如果相同,则在“密码验证,文本框中提示“通过!”否则提示“失败!”。“密码验证”文本框是只读的。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)
4、。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class simplepublic static void main(String args)LoginFrame frame = new LoginFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame,show ();class LoginFrame extends JFramepublic LoginFrame()s
5、etTitle(“simple“);setSize(WIDTH,HEIGHT);Container contentPane = getContentPane();JPanel textPanel = new JPanel();JLabel inputPassword = new JLabel(“请输入密码:“,JLabel.CENTER);MyPassword = new JPasswordField(20);textPanel.add(inputPassword);textPanel.add(myPassword);JLabel checkedPassword = new JLabel(“密
6、码验证.“,JLabel.LEFT);CheckPassword = new JTextField(20);_;textPanel.add(checkedPassword);textPanel.add(checkPassword);loginButton = new JButton(“登录“);loginButton.addActionListener(new LoginAction();textPane1.add(ioginButton);contentPane.add(textPanel);private class LoginAction implements ActionListene
7、rpublic void actionPerformed(ActionEvent event)char s = myPassword.getPassword();String gets = new String(s);if(gets.equals(CHECKPASSWORD)checkPassword,setText (“通过!“);elsecheckPassword.setText (“失败“);public static final int WIDTH = 280;public static final int HEIGHT = 180;public static final String
8、 CHECKPASSWORD = “password“;_;private JTextField checkPassword;private JButton loginButton;(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是求两个交叉图形的并、减、交及异或。窗口中有 4 个单选按钮和一个图形面板,面板中有两个交叉的图形,选中其中一个单选按钮,图形面板中会以黑色填充的方式显示运算的结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;import
9、java.awt.geom.*;import java.util.*;import javax.swing.*;public class advancepublic static void main(String args)JFrame frame = new AreaTestFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();class AreaTestFrame extends JFramepublic AreaTestFrame()setTitle(“advance“);setSize(WID
10、TH,HEIGHT);area1 = new Area(new Ellipse2D.Double(100,100,150,100);area2 = new Area(new Rectangle2D.Double(150,150,150,100);_;panel = new JPanel()public void paintComponent(Graphics g)super.paintComponent(g);Graphics2D g2 = (Graphics2D)g;g2.draw(area1);g2.draw(area2);if (area ! = null)g2.fill(area);c
11、ontentPane.add(panel,BorderLayout.CENTER);JPanel buttonPanel = new JPanel();ButtonGroup group = new ButtonGroup();JRadioButton addButtoh = new JRadioButton(“并“,false);buttonPanel.add(addButton);group.add(addButton);addButton.addActionListener(new ActionListener()public void actionPerformed(ActionEve
12、nt event)area = new Area();area.add(area1);area.add(area2);panel.repaint(););JRadioButton subtractButton = new JRadioButton(“减“,false);buttonPanel.add(subtractButton);group.add(subtractButton);subtractButton.addActionListener(new ActienListener()public void actionPerformed(ActionEvent event)area = n
13、ew Area();area.add(areal);_;panel.repaint(););JRadioButton intersectButton = new JRadioButton(“交“,false);buttonPanel.add(intersectButton);group.add(intersectButton);intersectButton.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event)area = new Area();area.add(area1);a
14、rea.intersect(area2);panel.repaint(););JRadioButten exclusiveOrButton = new JRadioButton(“异或“,false);buttonPanel.add(exclusiveOrButton);group.add(exclusiveOrButton);exclusiveOrButton.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event)area = new Area();area.add(area1)
15、;_;panel,repaint (););contentPane.add(buttonPanel,BorderLayout.NORTH);private JPanel pane1;private Area area;private Area area1;private Area area2;private static final int WIDTH = 400;private static final int HEIGHT = 400;(分数:30.00)_二级 JAVA 机试-208 答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.
16、本题程序的功能是随机产生一个 110 的随机数,根据提示,用户可以猜这个随机数。例如程序随机产生的随机数是 7,如果输入 8,则提示“Try smaller”并继续提示输入;如果输出 6,则提示“Try bigger”并继续提示输入;如果输入 7,则会输出“Great!”并退出程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.io.*;public class basicpublic static void main(String args)InputStreamReader it;Bur feredReader in;Ir = new Input
17、StreamReader(System.in)In = new BufferedReader(ir);int ranNum = _;int guessNum = -1;String str;while (_)System.out.print (“Please Input:“);try_;guessNum = Integer.parseInt(str);catch (Exception e)if(guessNum ranNum)System.out.println(“Try smaller.“);elseSystem.out.println(“Try bigger.“);System.out.p
18、rintln (“Great ! “);(分数:30.00)_正确答案:(int)(Math.random()*10)。guessNum!=ranNum。str=in.readLine()。)解析:二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是程序的主窗口有一个“登录”按钮和两个文本框“请输入密码:”和“密码验证”,用户在密码框中输入密码后单击“登录”按钮,然后与预先定义的密码比较(密码预设定为password),如果相同,则在“密码验证,文本框中提示“通过!”否则提示“失败!”。“密码验证”文本框是只读的。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)
19、。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class simplepublic static void main(String args)LoginFrame frame = new LoginFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame,show ();class LoginFrame extends JFramepublic LoginFrame()s
20、etTitle(“simple“);setSize(WIDTH,HEIGHT);Container contentPane = getContentPane();JPanel textPanel = new JPanel();JLabel inputPassword = new JLabel(“请输入密码:“,JLabel.CENTER);MyPassword = new JPasswordField(20);textPanel.add(inputPassword);textPanel.add(myPassword);JLabel checkedPassword = new JLabel(“密
21、码验证.“,JLabel.LEFT);CheckPassword = new JTextField(20);_;textPanel.add(checkedPassword);textPanel.add(checkPassword);loginButton = new JButton(“登录“);loginButton.addActionListener(new LoginAction();textPane1.add(ioginButton);contentPane.add(textPanel);private class LoginAction implements ActionListene
22、rpublic void actionPerformed(ActionEvent event)char s = myPassword.getPassword();String gets = new String(s);if(gets.equals(CHECKPASSWORD)checkPassword,setText (“通过!“);elsecheckPassword.setText (“失败“);public static final int WIDTH = 280;public static final int HEIGHT = 180;public static final String
23、 CHECKPASSWORD = “password“;_;private JTextField checkPassword;private JButton loginButton;(分数:40.00)_正确答案:(checkPassword.setEditable(false)。private JPasswordField myPassword。)解析:三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是求两个交叉图形的并、减、交及异或。窗口中有 4 个单选按钮和一个图形面板,面板中有两个交叉的图形,选中其中一个单选按钮,图形面板中会以黑色填充的方式显示运算的结果。请将下述程
24、序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import java.util.*;import javax.swing.*;public class advancepublic static void main(String args)JFrame frame = new AreaTestFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();class Area
25、TestFrame extends JFramepublic AreaTestFrame()setTitle(“advance“);setSize(WIDTH,HEIGHT);area1 = new Area(new Ellipse2D.Double(100,100,150,100);area2 = new Area(new Rectangle2D.Double(150,150,150,100);_;panel = new JPanel()public void paintComponent(Graphics g)super.paintComponent(g);Graphics2D g2 =
26、(Graphics2D)g;g2.draw(area1);g2.draw(area2);if (area ! = null)g2.fill(area);contentPane.add(panel,BorderLayout.CENTER);JPanel buttonPanel = new JPanel();ButtonGroup group = new ButtonGroup();JRadioButton addButtoh = new JRadioButton(“并“,false);buttonPanel.add(addButton);group.add(addButton);addButto
27、n.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event)area = new Area();area.add(area1);area.add(area2);panel.repaint(););JRadioButton subtractButton = new JRadioButton(“减“,false);buttonPanel.add(subtractButton);group.add(subtractButton);subtractButton.addActionListen
28、er(new ActienListener()public void actionPerformed(ActionEvent event)area = new Area();area.add(areal);_;panel.repaint(););JRadioButton intersectButton = new JRadioButton(“交“,false);buttonPanel.add(intersectButton);group.add(intersectButton);intersectButton.addActionListener(new ActionListener()publ
29、ic void actionPerformed(ActionEvent event)area = new Area();area.add(area1);area.intersect(area2);panel.repaint(););JRadioButten exclusiveOrButton = new JRadioButton(“异或“,false);buttonPanel.add(exclusiveOrButton);group.add(exclusiveOrButton);exclusiveOrButton.addActionListener(new ActionListener()pu
30、blic void actionPerformed(ActionEvent event)area = new Area();area.add(area1);_;panel,repaint (););contentPane.add(buttonPanel,BorderLayout.NORTH);private JPanel pane1;private Area area;private Area area1;private Area area2;private static final int WIDTH = 400;private static final int HEIGHT = 400;(分数:30.00)_正确答案:(Container contentPane = getContentPane()。area.subtract(area2)。area.exclusiveOr(a rea2)。)解析:
copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1