JAVA期末考试题库 下载本文

float x=(float )(Math.random())*100;float

y=(float )(Math.random())*100; MyExam6

MyExam6(2,3);System.out.println(\

System.out.println(\obj.min());}}

6. 对于一个二维整型数组,计算各行元素之和并查找其值最大的那个行。

public class MyExam7 {public static void main(String[] args) {int

s

[][]={{21,24,25,26,38,},{25,28,24,69,66}

};int

obj=new

sum[]=new int[2];int so[]=new int[5]; int i;int j;int min=0;for(j=0;j<2;j++){for(i=0;i<5;i++){so[i]=so[i]+s[j][i];sum[j]=sum[j]+so[i];min=sum[0];if(sum[j]

7. 现有接口定义如下interface shape2D {double pi=3.14;double area( ); }

请编写类Circle,实现上述接口,用于计算圆与矩形的面积。

29

public interface shape2D { double pi=3.14; double area();}public class Circle implements shape2D{public double area() { double r = 5;double area = pi*r*r;return area; }}public class Rectangle implements shape2D{public double area() { double w = 4; double h = 6; double area = w*h; return area; }}public class Test { public static void main(String[] args) { Circle circle = new Circle(); Rectangle rectangle = new Rectangle(); System.out.println(circle.area()); System.out.println(rectangle.area()); }}

8. 创建一个黄色(Color.yellow)面板,通过add()方法在该面板中添加一个按钮,然后将该面板添加到一个JFrame实例的靠南位置,JFrame实例的背景为蓝绿色(Color.CYAN)。

import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import javax.swing.JFrame;import

java.awt.EventQueue;import javax.swing.JPanel;public

class

Main{public static void main(String[] args) { final JFrame frame

=

new

JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setLayout(new

BorderLayout());frame.getContentPane().setBackground(Color.

30

CYAN);JPanel panel = new

JPanel();panel.setBackground(Color.YELLOW); panel.setPreferredSize(new Dimension(20,20)); frame.getContentPane().add(panel,

BorderLayout.SOUTH);frame.setSize(200, 200);

EventQueue.invokeLater(new Runnable() { public void run() {frame.setVisible(true); } }); }} import

java.awt.*;import

javax.swing.*;public

class

FrameWithPanel {public static void main(String s[]) {JFrame frame=new

JFrame(\

contentPane=frame.getContentPane();contentPane.setBackground(Color.CYAN);

JPanel contentPane=new JPanel();

panel.setBackground(Color.yellow);JButton

button=new

JButton(\orderLayout.SOUTH);frame.setSize(300,200);frame.setVisible(true); }}

*9. 创建一个包含一个按钮的窗体,当用户按下该按钮时,显示一个对话框。

import java.awt.BorderLayout;import java.awt.Button;import

31

java.awt.Container;import java.awt.Dialog;import

java.awt.event.ActionEvent;import java.awt.event.ActionListener;import

javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import

javax.swing.JLabel;public

class

java.awt.Frame;import

MyExam14 implements ActionListener {

JFrame frame; JDialog dialog; JButton button; public

static

void main(String arg[]) {MyExam14 jd = new MyExam14();

jd.go(); } public void go() {

dialog

=

new

frame = new JFrame(\ JDialog(frame, \

dialog.getContentPane().add(new JLabel(\

dialog.setSize

(60, 40); button = new JButton(\

button.addActionListener(this);Container

cp

=

frame.getContentPane();cp.add(button, BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(200, 150);

void

dialog.setVisible(true);

32

frame.setVisible(true); } public actionPerformed(ActionEvent e) {