admin 管理员组文章数量: 1184232
2024年3月9日发(作者:strcmp的函数怎么写)
Java语言程序设计
第九章课后习题答案
1.编写一个程序,该程序绘制一个5×9的网络,使用drawLine方法。
//NetWork类
import cs;
import ;
public class NetWork extends JFrame{
}
public NetWork(){
}
//横纵格之间都间隔10像素,起点在(20,40)
public void paint(Graphics g){
}
//绘制横向线
for(int i=0;i<=5;i++){
}
//绘制纵向线
for(int i=0;i<=9;i++){
}
ne(20, 40+i*10, 110, 40+i*10);
// 设置窗体大小
e(130, 130);
//设置窗体大小不可改变
izable(false);
// 设置默认关闭方式,关闭窗体的同时结束程序
aultCloseOperation(_ON_CLOSE);
// 将窗体显示出来
ible(true);
ne(20+i*10, 40, 20+i*10, 90);
//test9_1类
public class test9_1 {
}
public static void main(String[] args){
}
new NetWork();
运行结果:
2.编写一个程序,该程序以不同的颜色随机产生三角形,每个三角形用不同的颜色进行填充。
//Triangle类
import ;
import cs;
import ;
import ;
public class Triangle extends JFrame{
Random rnd = new Random();
//这里定义4个三角形
int[][] x=new int[4][3];
int[][] y=new int[4][3];
int[][] color=new int[4][3];
public Triangle(){
for(int i=0;i<4;i++){
for(int j=0;j<3;j++){
color[i][j]=t(255);
x[i][j]=t(i*100+100);
y[i][j]=t(i*100+100)+50;//加50像素是为了避免顶到窗体上沿
}
}
//窗体标题
le("随机三角形");
//窗体大小
e(500,500);
//窗体大小不可变
izable(false);
//关闭窗体的同时结束程序
aultCloseOperation(_ON_CLOSE);
//显示窗体
}
}
ible(true);
public void paint(Graphics g){
}
for(int i=0;i<4;i++){
}
or(new Color(color[i][0],color[i][1],color[i][2]));
lygon(x[i], y[i], 3);
//test9_2
public class test9_2 {
}
public static void main(String[] args){
}
new Triangle();
运行结果:
3.编写一个Applet,该程序请求用户输入圆的半径,然后显示该圆的直径、周长和面积。
//test9_3
import .*;
import .*;
import Event;
import Listener;
public class test9_3 extends JApplet {
private void initComponents() {
//声明8个组件
jLabel1 = new JLabel("输入圆的半径:", );
jLabel2 = new JLabel("圆的周长:", );
jLabel3 = new JLabel("", );
//初始化
public void init() {
}
try {
}
AndWait(new Runnable() {
public void run() {
}
initComponents();
//声明5个标签
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
//1个单行文本
private JTextField textOfRadius;
//2个按钮
private JButton jButton1;
private JButton jButton2;
});
tackTrace();
} catch (Exception ex) {
}
jLabel4 = new JLabel("圆的面积:", );
jLabel5 = new JLabel("", );
textOfRadius = new JTextField("半径");
jButton1 = new JButton("计算");
jButton2 = new JButton("退出");
//按钮添加监听器
ionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
}
jButton1ActionPerformed(evt);
});
//按钮添加监听器
ionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
}
jButton2ActionPerformed(evt);
});
//声明定义内容面板,并且设置其布局格式为:4行2列格子
Container c = getContentPane();
out(new GridLayout(4, 2));
//将8个组件加入到内容面板
(jLabel1);
(textOfRadius);
(jLabel2);
(jLabel3);
(jLabel4);
(jLabel5);
(jButton1);
(jButton2);
// 求周长方法
private String Round(double a) {
}
// 求面积方法
private String Area(double a) {
double perimeter = a * 2 * 3.14;
String s = new String(f(perimeter));
return s;
}
double area = a * a * 3.14;
String s = new String(f(area));
return s;
//点击“计算”按钮jButton1触发的方法
private void jButton1ActionPerformed(ActionEvent evt) {
//捕获单文本输入非数字的异常
try {
String s = t();//获得单文本字符
double a = f(s).floatValue();//字符转化为双精度
t(Round(a));//标签内容为周长
t(Area(a));//标签内容为面积
} catch (NumberFormatException r) {
//单文本为非数字弹出提示“输入错误”框
ssageDialog(this, "请输入数字类型", "输入错误",G_MESSAGE);
}
//点击“退出”按钮jButton2触发的方法
public void jButton2ActionPerformed(ActionEvent evt) {
}
(0);
}
}
t("");
运行结果:
编译text9_产生字节码文件test9_,接下来需要编写一个
HTML文件text9_来嵌入text9_,代码如下:
将test9_文件和test9_文件放在同一个目录下,在浏览器中打开这个test9_文件,实现的效果如下:
4.编写一个Applet,向其输入五个数,然后以条形图(bar graph)的形式来表示这些数。
5.编写一个绘制圆形的程序,当鼠标在绘制区域中单击时,该正方形的左上角顶点应准确的跟随鼠标光标移动,重绘该圆形。
//MyJFrame类
import cs;
import vent;
import istener;
import ;
public class MyJFrame extends JFrame implements MouseListener{
public MyJFrame(){
}
public void paint(Graphics g){
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
// TODO Auto-generated method stub
// TODO Auto-generated method stub
this.x=();
this.y=();
t();
n("x: " + () + "ny: " + ());
al(x, y, radius, radius);
le("绘制圆形");
e(200,200);
izable(false);
aultCloseOperation(_ON_CLOSE);
seListener(this);
ible(true);
int x=50;
int y=50;
int radius=50;
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
// TODO Auto-generated method stub
// TODO Auto-generated method stub
// TODO Auto-generated method stub
//test9_5
public class test9_3 {
}
public static void main(String[] args){
}
new MyJFrame();
运行结果:
“猜数”程序:该程序随机在1到100的范围内选择一个供用户猜测的整数,然后改程序显示提示信息,要求用户输入一个1到100之间的整数,根据输入偏大、偏小、正确,程序将显示不同的图标。
//GuessNumber类
import yout;
import Event;
import Listener;
import ;
import n;
import ;
import ;
import nPane;
import ield;
import onstants;
public class GuessNumber extends JFrame implements ActionListener {
// 随机数
Random random = new Random();
numberOfRandom = t(100);
// 各组件的定义
label1 = new JLabel("请输入一个1到100之间的整数:");
label2 = new JLabel("提示:", );
label3 = new JLabel("猜猜看");
buttonOfSure = new JButton("确定");
tPane().setDefaultButton(buttonOfSure);// 默认用户按int numberOfRandom;// 游戏程序随机数
int numberOfUser;// 玩家输入整数
JLabel label1;// 标签1:“输入一个1到100之间的整数”
JLabel label2;// 标签2:“提示”
JLabel label3;// 标签3:提示的内容(偏大、偏小、正确)
JButton buttonOfSure;// 确定按钮
JButton buttonOfExit;// 退出按钮
JTextField jTextField;// 猜数输入单行文本框
public void init() {
// 窗体的定义
le("这是一个猜数游戏");
e(400, 120);
izable(false);
out(new GridLayout(3, 2));
aultCloseOperation(_ON_CLOSE);
下回车键触发“确定”按钮
ionListener(this);
buttonOfExit = new JButton("退出");
ionListener(this);
jTextField = new JTextField("输入");
// 窗体加入各组件
(label1);
(jTextField);
(label2);
(label3);
(buttonOfSure);
(buttonOfExit);
// 显示窗体
ible(true);
n(numberOfRandom);// 偷偷看看随机数,哈哈
}
// 事件处理
public void eventHandle() {
//捕获非整型输入异常
try {
numberOfUser = nt(t());//获得用户输入数
if (numberOfUser < 1 || numberOfUser > 100) {
ssageDialog(this, "请输入1到100之间的整数");
}
} else {
}
ssageDialog(this, "请输入整数", "输入错误",
G_MESSAGE);
tFocus();
if (numberOfUser > numberOfRandom) {
t("偏大");
tFocus();//清空文本框并使重新获得焦点
}
if (numberOfUser < numberOfRandom) {
}
if (numberOfUser == numberOfRandom) {
}
t("恭喜你,答对了。");
tFocus();
t("偏小");
tFocus();
} catch (NumberFormatException e) {
}
public static void main(String[] args) {
}
new GuessNumber().init();
}
//按钮触发选择
public void actionPerformed(ActionEvent e) {
if (ionCommand() == "确定") {
andle();
}
if (ionCommand() == "退出") {
(0);
}
}
运行结果:
7.练习使用JscrollPane。使用BorderLayout将JFrame布局分为左右两块;左边又使用GridLayout,包含三个按钮,右边在JLabel里显示一幅图画,按钮控制JLabel是否显示滚动条。
//test9_7
import Layout;
import ;
import ner;
import yout;
import Event;
import Listener;
import con;
import ;
import ;
import nPane;
import ;
import lPane;
import PaneConstants;
public class test9_7 extends JFrame implements ActionListener {
// 将左右两边的中间容器放弃JFrame中
// 读取图片作为图标
picture = new ImageIcon("D:/image//");
// 将图标赋给标签label
label = new JLabel(picture);
// 定义滚动框,总是显示滚动条
jscrollPane = new JScrollPane(label,
AL_SCROLLBAR_ALWAYS,
NTAL_SCROLLBAR_ALWAYS);
// 定义左边镶板JPanel框和三个按钮
jPanel = new JPanel(new GridLayout(3, 1, 0, 30));
button1 = new Button("滚动");
ionListener(this);
button2 = new Button("试试");
ionListener(this);
button3 = new Button("退出");
ionListener(this);
(button1);
(button2);
(button3);
private JPanel jPanel;//左边panel框
private Button button1;
private Button button2;
private Button button3;
private JScrollPane jscrollPane;//右边滚动框
private JLabel label;//滚动框中的标签
public ImageIcon picture = null;//标签中的图标
public boolean whetherCroll = true;//用于切换滚动条的显示
public void init() {
JFrame jFrame = new JFrame("练习使用JscrollPane");
Container pane = tentPane();
aultCloseOperation((_ON_CLOSE));
}
(jPanel, );
(jscrollPane, );
//排版显示
();
ible(true);
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (ionCommand() == "滚动") {
if (whetherCroll) {
ticalScrollBarPolicy(AL_SCROLLBAR_NEVER);//垂直不显示
izontalScrollBarPolicy(NTAL_SCROLLBAR_NEVER);//水平不显示
whetherCroll = false;
} else {
ticalScrollBarPolicy(AL_SCROLLBAR_ALWAYS);//垂直显示
izontalScrollBarPolicy(NTAL_SCROLLBAR_ALWAYS);//水平显示
whetherCroll = true;
public static void main(String[] args) {
}
new test9_7().init();
}
}
if (ionCommand() == "试试") {
ssageDialog(this,
}
if (ionCommand() == "退出") {
(0);
}
"oh,,,Fuck your teacher? yeal……good!");
}
}
运行结果:
显示滚动条
点击“滚动”按钮后,没显示滚动条
8.练习使用JList。建立两个JList,双击其中任何一个中的某一项,此项就会跑到另外一个JList
中。
//test9_8
import ner;
import yout;
import vent;
import istener;
import tListModel;
import ;
import ;
public class test9_8 extends JFrame implements MouseListener {
listModel1 = new DefaultListModel();
ment("First");
ment("Second");
ment("Third");
listModel2=new DefaultListModel();
ment("one");
ment("two");
ment("three");
jList1 = new JList(listModel1);
jList2=new JList(listModel2);
seListener(this);
seListener(this);
(jList1);
(jList2);
aultCloseOperation(_ON_CLOSE);
public void init() {
JFrame jFrame = new JFrame("练习使用List");
Container pane = tentPane();
out(new GridLayout(1,2));
public JFrame jFrame;
public JList jList1;
public JList jList2;
public DefaultListModel listModel1;
public DefaultListModel listModel2;
}
();
ible(true);
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
// TODO Auto-generated method stub
// TODO Auto-generated method stub
// TODO Auto-generated method stub
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if(rce() == jList1){
}
if(rce()==jList2){
int index = onToIndex(nt());
Object o = el().getElementAt(index);
ment(o);
int index = onToIndex(nt());
Object o = el().getElementAt(index);
ment(o);
}
}
public static void main(String[] args) {
}
new test9_8().init();
运行结果:
9.练习使用JComboBox。包括一个JLable、一个JComboBox,可以通过输入或者选择JComboBox中的某一项来控制JLable中文字的大小。
//test9_9
import ner;
import ;
import yout;
import ent;
import stener;
import Box;
import ;
import ;
import nPane;
public class test9_9 extends JFrame implements ItemListener {
public JFrame jFrame;
}
private JLabel jLabel;
private JComboBox jComboBox;
private String[] fontSize = { "8", "14", "20", "28", "32", "40" };
public void init() {
}
public void itemStateChanged(ItemEvent e) {
}
public static void main(String[] args) {
}
new test9_9().init();
//使用这个判断的目的的是为了避免触发事件总是执行两次的问题
if (teChange() == ED) {
}
try {
String s = ectedItem().toString();
int intFontSize = nt(s);
t(new Font("", 0, intFontSize));
n(intFontSize);
jFrame = new JFrame("练习ComboBox");
Container container = tentPane();
out(new GridLayout(2, 1));
aultCloseOperation(_ON_CLOSE);
jLabel = new JLabel("你好吗?");
t(new Font("", 0, 10));// 设置字体大小
jComboBox = new JComboBox(fontSize);
table(true);// 设置复选框可以输入
mListener(this);// 添加ItemListener监听
// 添加两个组件
(jLabel);
(jComboBox);
();// 排版
ible(true);// 显示
//
} catch (NumberFormatException r) {
ssageDialog(this, "请输入整数");
}
运行结果:
10.练习使用JTable。包括姓名、学号、语文成绩、数学成绩、总分五项,单击总分会自动将语文数学成绩相加。
import .*;
import .*;
import .*;
public class Test9_10 extends JFrame implements MouseListener {
private JTable jTable;
private Object[][] cellData = { { "姓名", new String("林楚金") },
{ "学号", new String("200905") }, { "语文成绩", new
String("86") },
{ "数学成绩", new String("96") }, { "总分", new String("点击出总分") } };
private String[] columnNames = { "col1", "col2" };
public void mouseClicked(MouseEvent e) {
}
le("表格演示");
e(300, 350);
ation(200, 200);
aultCloseOperation(_ON_CLOSE);
ible(true);
public Test9_10() {
DefaultTableModel model = new DefaultTableModel(cellData,
};
jTable = new JTable(model);
seListener(this);
(jTable);
public boolean isCellEditable(int row, int column) {
}
return false;
columnNames) {
DefaultTableModel tableModel = (DefaultTableModel)
int row = ectedRow();
int column = ectedColumn();
if(row==4 && column==1){
String yuwen = (String) ueAt(2, column);//String shuxue = (String) ueAt(3, column);//Integer count =
ueAt(ng(), row, column);
el();
语文成绩
数学成绩
}
}
}
}
nt(yuwen)+nt(shuxue);
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public static void main(String[] args) {
new Test9_10();
运行结果:
11.练习使用对话框。包括一个JLable和两个JButton,按任何一个JButton都会产生一个对话框,按确定后将输入内容在JLable中显示出来。
//test9_11
import ner;
import ;
import yout;
import Event;
import Listener;
import n;
import ;
import ;
import nPane;
public class test9_11 extends Frame implements ActionListener {
public void init() {
JFrame jFrame = new JFrame("练习使用对话框");
Container pane = tentPane();
out(new GridLayout(3, 1));
JFrame jFrame;
JButton jButton1;
JButton jButton2;
JLabel jLabel;
}
}
jButton1 = new JButton("按钮1");
ionListener(this);
jButton2 = new JButton("按钮2");
ionListener(this);
jLabel = new JLabel();
aultCloseOperation(_ON_CLOSE);
(jButton1);
(jButton2);
(jLabel);
();
ible(true);
@Override
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
}
new test9_11().init();
// TODO Auto-generated method stub
if (ionCommand() == "按钮1" || ionCommand() == "
}
String s = putDialog(this, "", "请输入");
t(s);
按钮2") {
运行结果:
(下面的题目有空再做)
12.练习使用JMenu、JFileChooser、JColorChooser。通过菜单可以打开文件选择对话框,来打开某一指定文本文件,通过菜单可打开颜色选择对话框控制显示文本的颜色。
import .*;
import .*;
import .*;
import edReader;
import ader;
import ption;
public class Test9_12 extends JFrame implements ActionListener{
//文本域
private JTextArea jta=null;
//菜单栏
private JMenuBar jmb = null;
//菜单栏中的一个菜单项
private JMenu jm1=null;
//菜单项中的下拉列表
private JMenuItem jmi1=null;
private JMenuItem jmi2=null;
//颜色对象选择
private JFileChooser jfc = null;
public Test9_12(){
jta = new JTextArea();
=new JMenuBar();
1 = new JMenu("选择");
//设置助记符
monic('F');
1 = new JMenuItem("打开文件");
2 = new JMenuItem("选择颜色");
//注册监听
ionListener(this);
ionCommand("openFile");
ionListener(this);
ionCommand("chooseColor");
}
//把JMenuBar加入JFrame
nuBar(jmb);
//把菜单放到
(1);
//把Item放到jm1
(1);
(2);
(jta);
le("记事本/颜色选择案例");
e(400, 300);
ation(200,200);
aultCloseOperation(_ON_CLOSE);
ible(true);
public void actionPerformed(ActionEvent e) {
if( ionCommand().equals("openFile")){
n("打开");
= new JFileChooser();
logTitle("请选择文件...");
//打开的时候默认的属性
enDialog(null);
ible(true);
//得到用户选择的文件路径,绝对路径
String filePath =
n(filePath);
FileReader fr =null;
BufferedReader br =null;
try {
fr = new FileReader(filePath);
br = new BufferedReader(fr);
String s="";
String sall="";
while((s=ne())!=null){
// bufferedReader 遇到换行 即一行。。
ectedFile().getAbsolutePath();
}
}
}
}
}
s+="rn";//所以后面补上换行号
sall+=s;
t(sall);
tackTrace();
try {
}
();
();
tackTrace();
} catch (Exception e1) {
}finally{
} catch (IOException e1) {
if(ionCommand().equals("chooseColor")){
}
Color color = new Color (0, 0, 0);
color = alog(this, "Choose Color",color);
//kground(color);
//设置字体颜色
eground(color);
public static void main(String[] args) {
}
new Test9_12();
运行结果:
13.编写一个图形用户界面,包括三个JSlider对象和三个JTexField对象。每个JSlider代表颜色中的红、绿、蓝三部分,它们的值从0到255,在相应的JTextField中显示各个JSlider的当前值。用这三个值作为Color类构造方法的参数创建一个新的Color对象,用来填充一个矩形。
import .*;
import .*;
import .*;
public class Test9_13 extends JFrame implements ChangeListener {
public Test9_13() {
le("JSliderDemo");
e(200, 200);
ible(true);
aultCloseOperation(_ON_CLOSE);
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JSlider slider1;
private JSlider slider2;
private JSlider slider3;
private MyPanel myPanel;
}
out(new GridLayout(4, 3));
1 = new JLabel("红");
2 = new JLabel("绿");
3 = new JLabel("蓝");
eld1 = new JTextField(5);
eld2 = new JTextField(5);
eld3 = new JTextField(5);
1 = new JSlider(NTAL, 0, 255, 125);
2 = new JSlider(NTAL, 0, 255, 125);
3 = new JSlider(NTAL, 0, 255, 125);
(label1);
(slider1);
(textField1);
(label2);
(slider2);
(textField2);
(label3);
(slider3);
(textField3);
l = new MyPanel();
(myPanel);
ngeListener(this);
ngeListener(this);
ngeListener(this);
public void stateChanged(ChangeEvent event) {
if ((JSlider) rce() == 1) {
}
if ((JSlider) rce() == 2) {
}
String str = "" + ue();
t(str);
String str = "" + ue();
t(str);
if ((JSlider) rce() == 3) {
}
or(new Color(ue(),
.getValue(), ue()));
String str = "" + ue();
t(str);
2
t();
}
public static void main(String[] args) {
new Test9_13();
}
}
class MyPanel extends JPanel {
private Color color = ;
public void paint(Graphics g) {
or();
ct(0, 0, 200, 200);
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
= color;
}
}
运行结果:
14.编写Application程序,构造一CUI,实现对两个数的相加、减、乘、除功能。用含有三个JTextField、一个JButton、三个JTextField分别用于输入两个数字和运算符号,结果用JLable显示出来。
import .*;
import .*;
import .*;
public class Test9_14 extends JFrame implements ActionListener {
(jl1);
(jl2);
out(new GridLayout(3, 3));
= new JButton("结果");
ionListener(this);
1 = new JTextField();
2 = new JTextField();
3 = new JTextField();
1 = new JLabel("数字1");
2 = new JLabel("符号");
3 = new JLabel("数字2");
4 = new JLabel();
public Test9_14() {
private JLabel jl1, jl2, jl3, jl4;
private JTextField jtf1, jtf2, jtf3;
private JButton jb;
}
(jl3);
(jtf1);
(jtf2);
(jtf3);
(jb);
(jl4);
le("加减乘除");
e(250, 100);
ation(200, 200);
aultCloseOperation(_ON_CLOSE);
ible(true);
public void actionPerformed(ActionEvent e) {
float n1 = 0;
float n2 = 0;
n("输入符号为:" + t());
n1 = loat(t());
n2 = loat(t());
if (t().equals("+")) {
t("结果为:" + (n1 + n2));
} else {
if (t().equals("-")) {
t("结果为:" + (n1 - n2));
} else {
}
if (t().equals("*")) {
t("结果为:" + (n1 * n2));
} else {
}
if (t().equals("/")) {
t("结果为:" + (n1 / n2));
} else {
t("输入有误!");
}
}
}
}
public static void main(String[] args) {
}
new Test9_14();
运行结果:
15.扩充上一个练习,先弹出一对话框,输入密码正确,方能进行计算界面。
import .*;
import .*;
import .*;
public class Test9_15 extends JFrame implements ActionListener {
private JLabel jl1, jl2, jl3, jl4;
private JTextField jtf1, jtf2, jtf3;
private JButton jb;
public Test9_15() {
1 = new JLabel("数字1");
2 = new JLabel("符号");
3 = new JLabel("数字2");
4 = new JLabel();
1 = new JTextField();
2 = new JTextField();
3 = new JTextField();
= new JButton("结果");
}
ionListener(this);
out(new GridLayout(3, 3));
(jl1);
(jl2);
(jl3);
(jtf1);
(jtf2);
(jtf3);
(jb);
(jl4);
le("加减乘除");
e(250, 100);
ation(450, 300);
aultCloseOperation(_ON_CLOSE);
ible(true);
public void actionPerformed(ActionEvent e) {
float n1 = 0;
float n2 = 0;
n("输入符号为:" + t());
n1 = loat(t());
n2 = loat(t());
if (t().equals("+")) {
t("结果为:" + (n1 + n2));
} else {
if (t().equals("-")) {
t("结果为:" + (n1 - n2));
} else {
if (t().equals("*")) {
t("结果为:" + (n1 * n2));
} else {
if (t().equals("/")) {
t("结果为:" + (n1 / n2));
}
}
}
}
} else {
}
t("输入有误!");
public static void main(String[] args) {
String inputValue = putDialog("请输入密码(我爱苍老师):");
while(!("我爱苍老师")){
ssageDialog(null, "密码错误", "提示",
_MESSAGE);
}
}
inputValue = putDialog("请输入密码(我爱苍老师):");
break;
}
new Test9_15();
运行结果:
16.改造上一个练习为Applet。
编译text9_产生字节码文件test9_,接下来需要编写一个
HTML文件text9_来嵌入text9_,代码如下:
将test9_文件和test9_文件放在同一个目录下,在浏览器中打开这个test9_文件,就有你想要的效果。
17.编写一个程序,以随机的颜色和宽度绘制10条长度随机的直线,使用对象和Fraphics2D类的方法来绘制直线。该绘制区域设置为300×400.
import .*;
import 2D;
import .*;
public class Test9_17 extends JFrame {
public static void main(String[] args) {
new Test9_17();
}
}
//随即长度
int x1 = (int) (() * 300);
int y1 = (int) (() * 400);
int x2 = (int) (() * 300);
int y2 = (int) (() * 400);
ne(x1, y1, x2, y2);
//10次
for (int i = 0; i < 10; i++) {
// 随即颜色
int r = (int) (() * 255);
int g = (int) (() * 255);
int b = (int) (() * 255);
or(new Color(r, g, b));
public void paint(Graphics e) {
}
public Test9_17() {
le("随机绘直线");
e(400, 400);
ation(500, 300);// 窗口显示的位置
aultCloseOperation(_ON_CLOSE);
ible(true);
}
}
运行结果:
Java语言程序设计 及 试题答案
一、单选题(共20分,每小题1分)
1、下面哪一个不是java语言的特点( d )。
(A)面向对象 (B)安全性高
(C)与平台无关 (D)复杂多变
2、下面的哪一个标识符是合法可用的( d )。
(A)2abcd (B)int (C)var# (D)abcd123
3、在基本数据类型中,整型(int)位长为( c )。
(A)8 (B)16 (C)32 (D)64
4、Java中的所有对象都是在运行时动态创建的,创建新对象可用的关键字为((A)new (B)string (C)byte (D)true
5、继承是指新的类获得已有类的属性和行为,称新类为已有类的( a )。(A)子类 (B)父类 (C)对象 (D)实体
6、读下面的程序:
public _______clock
{
a )。
int hour;
int minute;
int second;
public void settime (int newh, int newm,int news)
{hour=newh;
minute=newm;
second=news;
}
}
划线位置应选择的关键字为( b )。
(A)new (B)class (C)system (D)out
7、读下面的三个程序段:
public class Circle {
int radius;
}
public class Rectangle{
double width=10.128;
double height=5.1245;
}
public (7) ShaperTester {
public static void (8) (String args[]) {
Circle x;
Rectangle y;
x=(9) Circle( );
y=(9) Rectangle( );
n(x+” ”+y);
}
}
在程序中(7)处应填写的是( C )。
(A)main (B)new (C)class (D)int
8、读第7题程序,题中(8)处应填写的是( A )。
(A)main (B)new (C)class (D)int
9、读第7题程序,题中(9)处应填写的是( B )。
(A)main (B)new (C)class (D)int
10、类变量是针对在类中对所有属性都相同的对象,在类中的静态属性也是用来存储需要( B )的数据。
(A) 复制 (B)共享 (C)移动 (D)修改
11、一个类为终结类,或声明一个方法为终结方法时所用的关键字为( D )。
(A)public (B)abstract (C)class (D)final
12、 Java程序中为使用其它包中所提供的类,所用到的引入语句为( C )。
(A)in (B)input (C)import (D)include
13、在类成员的访问控制中,如果一个对象只可以被同一类及其子类的实例对象访问,那么该关键字为( A )。
(A)public (B)private (C)protected (D)friendly
14、下面哪一个语句可以被用来在流程控制语句中控制循环( D )。
(A)case (B)switch (C)if (D)for
15、Java要求声明的每个类都有父类,当没有显式指定父类时,父类隐含为包中的( B )类。
(A)Float (B)Object (C)Long (D)Short
16、( B )类是Java程序中所有类的直接父类,也是类库中所有类的父类,处在类层次的最高点。
(A)Float(B)Object(C)Long(D)Short
17、读下面的程序:
interface Shape2D{ //声明Shape2D接口
double pi=3.14;
double area( );
//************************
(17) Color{ //声明Color接口
void setColor(String str);
}
//***************************
class Circle (18) Shape2D,Color // 实现Circle类
{
double radius;
String color;
public Circle(double r)
{
radius=r;
}
public double area()
{
return (pi*radius*radius);
}
public void setColor(String str)
{
color=str;
n("color="+color);
}
}
//***********************************
public class MultiInterfaceTester{ //申明测试主方法
public static void (19) (String args[]) {
Circle cir;
cir=new Circle(2.0);
or("blue");
n("Area = " + ( ));
}
}
在程序中(17)处应填写的语句是( A )。
(A)interface (B)class
(C)main (D)implements
18、在17题程序中(18)处应填写的语句是( D )。
(A)interface (B)class (C)main (D)implements
19、在17题程序中(19)处应填写的语句是( C )。
(A)interface (B)class (C)main (D)implements
20、在17题程序输出的结果中变量“color”和“Area”的值分别是( A )。
(A)blue 12.56 (B)yellow 56.12
二、填空题(共20分,每小题2分)
1、____JAVA____语言是一个功能强大的跨平台设计语言,是目前应用最为广泛的计算机语言之一。
2、Java是一个面向____对象_____的程序设计语言。
3、程序设计语言从最开始的___机器______到汇编语言到各种结构化高级语言,到现在所使用的面对象的语言向。
4、布尔数据(Boolean)表示一个逻辑量,只有两个值分别是___true__和false,它们不对应于任何整数值。
5、结构化程序设计的基本思想是采用“_____函数____________”的控制结构。
6、面向对象程序的基本组成成分是类与___对象____。
7、___异常__又称为例外,是特殊的运行错误对象,对应着Java语言特定的运行错误处理机制。
8、______封装_____是面向对象的核心特征之一,它提供了一种信息隐藏技术。
9、类中有一种特殊的成员方法,其方法名与类名相同,称为____构造方法________。
10、类具有封装性、_继承_________和多态性。
三、判断题(共10分,每小题2分)
1、在Java中,一个子类可以有多个直接父类。( )
2、在java程序中用final指明的变量可以被多次赋值。( )
3、在java程序中可以在不同的类中申明相同的方法。( )
4、构造方法是类的一种特殊方法,它可以重载,也可以从父类那里继承。( )
5、使用new方法可以对一个抽象类进行实例化。( )
四、简答题(共30分,每小题6分)
1、为了解决类名冲突问题,Java提供了包机制来管理类名空间,请问包主要有哪些作用?
2、简述类的继承原则。
3、什么是接口?它的特点是什么?
4、何为异常,为什么要进行异常处理?
5、什么是构造方法?构造方法有哪些特点?
五、程序分析(本题20分,每小题10分)
1、分析下面的程序,指出其中的错误并改正。
(1) public interface Example1{
(2) private double privateOne=0.0;
(3) protected int protectTwo=0;
(4) public int publicThree=10;
(5) public int publicFour;
(6) public void methodOne( ) {
(7) n(“aaa’);
(8) }
(9) public static void methodTwo( );
(10) public static final int methodThree( );
(11) public abstract void methodFour( );
(12) }
2、计算1!+2!+3!+…+10!,其中阶乘的计算用方法实现。
Java语言程序设计试题答案
一、选择题:
1、D 2、D 3、C 4、A 5、A 6、B 7、C 8、A 9、B 10、B
11、D 12、C 13、C 14、D 15、B 16、B 17、A 18、D 19、C 20、A
二、填空题
1、JAVA 2、对象 3、机器语言 4、true 5、单入口单出口
6、对象 7、异常 8、封装性 9、构造方法 10、继承性
三、判断题
1、× 2、× 3、√ 4、× 5、×
四、简答题
1、(1)
(2)
(3)
将相关的源代码文件组织在一起;
不同包中的类名可以相同,从而可以避免名字冲突;
提供包一级的封装及存取权限。
2、(1)子类继承父类的成员变量(2)子类继父类承除构造方法外成员方法(3)子类不能继承父类的构造方法(4)子类可以重新定义父类成员。
3、接口是一些抽象和常量所组成的集合。
特点:(1)接口可以实现多继承。(2)接口不存在最高层(3)接口中的方法都
是使用abstract修饰的方法(4)接口的成员变量为final定义的常量。
4、异常指程序运行过程中出现的非正常现象
由于异常情况总是难免的,良好的应用程序除了具备用户所要求的基本功能外,还应该具备预见并处理可能发生的名种异常的功能。
5、构造方法是类中方法名与类名相同的方法
(1)构造方法的名字与类名相同
(2)构造方法是类方法的一种特殊形式,它的主要作用是初始化对象中的成员变量。(3)构造方法没有返回值,方法名前面也没有void关键字(4)一个类中可定义多个拥有不同数量、不同类型参数的构造方法。
五、(2)
public class factorial
{
Public static int factor(int m)
{
If(m<=1)
Return 1;
Else
Return m*factor(m-1);
}
Public static void main(String[] args)
{
Long sum=0;
For (int i=1;i<=10;i++)
Sum=sum+factor(i);
n(“1!+2!+3!+…+10!=”+sum);
}
}
版权声明:本文标题:Java语言程序设计(郑莉)第九章课后习题答案 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1709927638a550019.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论