版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 指出JAVA语言的主要特点和JAVA程序的执行过程。答:(1)强类型;(2)编译和解释;(3)自动无用内存回收功能;(4)面向对象;(5)与平台无关;(6)安全性;(7)分布式计算;(8)多线程;程序执行过程如图所示:编写源文件,编译器编译源文件转换成字节码,解释器执行字节码。 说出开发与运行JAVA程序的重要步骤。答:(1)编写源文件:使用一个文本编译器,如Edit或记事本,不可以使用Word.将编好的源文件保存起来,源文件的扩展名必须是.java; (2)编译Java源文件:使用Java编译器编译源文件得到字节码文件; (3)运行Java程序:Java程序分为两类Java应用程序必须通过
2、Java解释器来解释执行其字节码文件;Java小应用程序必须通过支持Java标准的浏览器来解释执行。 如何区分应用程序和小应用程序 答:应用程序在与源文件名字相同的类中,有main()方法,该方法代表应用程序的入口; 小应用程序必须有一个Applet类的子类,该类称作主类,必须用public修饰。 说出JAVA源文件的命名规则。答:源文件命名规则和类命名规则一样,所有的单词首字母都用大写字母,且必须和源文件的public类同名。 JAVA语言使用什么字符集共有多少个不同的字符 答:Java语言使用Unicode字符集,共有65535个字符。 JAVA语言标识符的命名规则是什么(1)由字母(包括
3、英文字母、下划线字符、美元字符、文字字符)和数字字符组成(2)限定标识符的第一个字符不能是数字字符(3)不能和关键字重名(4)长度不能超过255个字符 JAVA有那些基本数据类型,它们的常量又是如何书写的数据类型关键字字节数默认值常量举例范围逻辑类型布尔型boolean1falsefalse,truefalse,true 字符型char20a,我,0 65535 整数类型字节性byte1012,13,45-128 127 短整型short2012,13,45-32768 32767整型int8012,13,45-2E31 2E31-1长整型long80L12L,13L,45L-2E6
4、3 2E63-1浮点类型单精度浮点float40F,-10E38 -10E-3810E-38 10E38双精度浮点double80D,-10E308 -10E-30810E-308 10E308 指出下列内容哪些是JAVA语言的整型常量,哪些是浮点数类型常量,哪些两者都不是。整型常量: 4)0xABCL,8)003,10)077,12)056L浮点数类型常量:3)-1E-31,5).32E31 13)0.,14).0两者都不是: 1),2),6),7),9),11)第二章 运算和语句 Java的字符能参加算术运算吗可以。(1)字符与整型数据运算,结果为整型数据,只能赋给整型变量,如果附给字符型
5、变量会损失精度,需要强制类型装换; (2)字符与字符运算,结果为字符型数据,可以直接赋给字符型或整型变量。 占字节多的变量能直接赋值给占字节少的变量么 答:不能直接赋值,需要强制类型转换。 试用Java语言表达式描述以下数学计算式或逻辑条件:1)解:V = 3)*(r,3);2)解:R = / R1+R2);3)解:y = (x,5) + (x,3) + 6;4)解:F = * M1 * M2 / (R * R);5)解:(x) / a * x + ( ( * x / 2) );6)0 < a <10解:a > 0 && a < 10 7) 条件 x=1
6、 与 y=2 有且只有一个成立. 解:( x = 1 && y != 2) | (x != 1 && y = 2) 设在求以下表达式之前,整型变量a的值是4,试指出在求了以下表达式之后,变量a、b和c的值。1) 解:a=5 ; b=16;2);解:a=5 ; c=10 若一个数恰好等于它的因子之和,则这个数称为“完全数”。编写程序求1000之内的所有完全数。分析:若一个自然数,它所有的真因子(即除了自身以外的约数)的和恰好等于它本身,这种数叫做完全数。例如6=1+23;28=124714程序运行结果:/* author 段智敏 */public class Wo
7、rk2_6 public static void main(String args) String resultString = "1000以内,完全数有:" ;/* * 输入正整数n,输出由n行n列星号字符组成的三角形图案。 * author 段智敏 */ public class Work2_8public static void main(String args)Scanner consoleScanner = new Scanner;"请输入正整数n:");String str = ();int n = -1;tryn = (str);catch
8、( NumberFormatException e )"输入非法字符,程序结束!");return;if (n < 1)"输入的数不是正实数,程序结束!");elsefor(int i=0;i<n;i+)for(int j=0;j<n-i-1;j+)" ");for(int k=0;k<i+1;k+)"*"); 设有整型变量x和y的值分别为5和110。试指出执行了以下循环语句后,变量x和y的值分别多少1)while(x<=y) x*=2;解:x=160 ;y=110;2)dox=y/x
9、; y=y-x;while(y>=1);解:x=18; y=0; 求小于999的所有水仙花数。程序运行结果:源代码文件:/* * 求小于999的所有水仙花数 * author 段智敏 */ public class Work2_10public static void main(String args) String resultString = new String("1000之前的所有水仙花数有:");./* * 按公式计算:e=1+1/1!+1/2!+1/3!+1/4!+. * author 段智敏 */public class Work2_12public s
10、tatic void main(String args)int n = 1000; .程序运行结果:源代码文件:/* * 圆周率计算 * author 段智敏 */public class Work2_13public static void main(String args)int num = 1000000; 例变量名被实例方法,构造方法访问类变量直接分配内存各个对象之间共享这段已经分配完的内存对象名.类变量名;类名.类变量名被实例方法,类方法,构造方法访问成员方法实例方法不分配入口地址共享一个入口地址对象名.实例方法名实例变量、类变量,实例方法、类方法类方法直接分配入口地址共享这个入口地
11、址对象名.类方法名;类名.类方法名类变量、类方法 子类能继承超类的哪些成员变量和方法 同包继承不同包继承(import进来的)私有(private)不继承不继承友好(缺省默认)继承不继承受保护(protected)继承继承共有(public)继承继承 子类在什么情况下能隐藏超类的成员变量和方法解:子类重载父类的成员变量、方法时候。 在子类中是否允许有一个方法和超类的方法名字相同,而类型不同解:不允许。方法重写是指:子类中定义一个方法,并且这个方法的名字、返回类型、参数个数、和类型与从父类继承的方法完全相同。 以下程序有什么错误解:类方法main()调用了类的实例变量,main方法是类
12、方法,不能调用类的实例变量a,导致错误! 声明一个复数类Complex。实现其基本功能。复数类:/* * 复数类 * author 段智敏 */public class Complexprivate double real;ength,而确定一个String对象的长度用对象名.length()用toUpperCase()和toLowerCase()方法实现大小写转换答:public class UpperAndLowerTest public static void main(String args) String s1=("ABCdefgHIJkhl123"),s2,s3
13、; "未转换时的字符串s1="+s1); byte t1=();arrayIntindex = i;index+;/* * 打印数组元素 */public String printArray()String result = ""for (int i = 0; i < ; i+)if (arrayInti != 0)result += arrayInti + " "return result;public static void main(String args)Work4_10 w = new Work4_10(100); 编
14、写实现从两个字符串中找出最长的相同字符列的代码。程序运行结果:/* * 找出两个字符串中,最长的相同字符列 * author 段智敏 */public class Work4_11public static void main(String args)String str1 = "0000abcdef0000"String str2 = "123ab4567cdef789abcdef"String str3 = getLongSameString(str1,str2);"str1 = " + str1);"str2 = &q
15、uot; + str2);"最长的相同字符串为:" + str3);public static String getLongSameString(String str1, String str2)String temp = ""String same = ""String longSame = ""for(int i=0; i<();i+)for(int j=i+1; j<=();j+)temp = (i, j);if(temp)>0)same = temp;if() < ()longSam
16、e = temp;return longSame; 整理字符串,将字符串的前导空白符和后随空白符删去,并将字符串中非空白字符之间的连接的多个空白符只保留一个,而去掉多余的空白符。程序运行结果:public class Work4_12public static void main(String args) String str = " I Love You "str=();StringTokenizer s=new StringTokenizer(str);String newStr=""while()newStr=newStr+" "
17、;+();"旧字符串:"+str);"处理后新字符串:"+newStr); 编写用数组实现大整数的类,提供大整数的加、减、乘等运算。程序运行结果:大整数类源文件:/* * 大整数类 * author 段智敏 */public class MyBigInteger/* 能存储的最大位数 */public static final int MAXLENGTH = 100;/* 正负号标识 */private int signum = 1;/* 实际存储用的数组 */private int dataArray;/* * 将参数的十进制字符串表示形式转换为 Bi
18、gInteger。 * param val - 要转换的十进制字符串表示形式。 */public MyBigInteger(String val)(val);/* * 声明一个MAXLENGTH位的,空的大整数类 */public MyBigInteger()dataArray = new int;/* * 根据十进制字符串表示形式转换为MyBigInteger的数据 * param str - 十进制字符串表示形式。 */public void setData(String str)dataArray = new int;tryint end = 0;signum = 1;if (0) =
19、'-')signum = -1;end = 1;int index = 0;for (int i = () - 1; i >= end; i-)dataArrayindex+ = (i);catch( NumberFormatException e )();/* * 加法,返回其值为(this + val)的BigInteger。 * param val- 将添加到此BigInteger中的值。 * return - this + val */public MyBigInteger add(MyBigInteger val)MyBigInteger result = ne
20、w MyBigInteger();if > 0 && < 0)ubtract();ength() < ().length()ength() = ().length()oString().length();/* * 检查进位,做加法时检测 * param i */private void carryBit(int i)if (i < - 1)i + 1 += i / 10;i = i % 10;/* * 检查借位,做减法时检测 */private void borrowBit(int i)if (i < - 1)if i < 0)i = i +
21、 10;i + 1-;大整数类测试类源文件:import * 大整数测试类 * author 段智敏 */public class TestBigIntegerpublic static void main(String args)String a = ""String b = "57"MyBigInteger m1 = new MyBigInteger(a);MyBigInteger m2 = new MyBigInteger(b);MyBigInteger m3;BigInteger b1 = new BigInteger(a);import .*;
22、/* * 创建一个有文本框和三个按钮的程序。当按下某个按钮时,使不同的文字显示在文本框中。 * author 段智敏 */public class Work5_7 extends JFrame implements ActionListenerprivate static final long serialVersionUID = 1L;private JTextField text;private JButton button1, button2, button3, exit_button;private JPanel panel;public Work5_7()text = new JTe
23、xtField(10);button1 = new JButton("刘德华");button2 = new JButton("张学友");button3 = new JButton("蔡依林");exit_button = new JButton("退 出");(this);(this);(this);(this);panel = new JPanel();(button1);(button2);(button3);(new FlowLayout();(text);(panel);(exit_button);(3
24、00, 200);(true);public void actionPerformed(ActionEvent e)if () = exit_button)(0);else();public static void main(String args)new Work5_7(); 编写一个有两个文本框的小应用程序,在第一个文本框输入英语单词,在第二个文本框会自动显示汉语解释;在第一个文本框输入汉语单词,在第二个文本框中显示英语解释。设英语单词表只有少许几个。程序运行结果:源文件:import .*;import .*;/* * author 段智敏 */public class Work5_8
25、extends JFrame implements ActionListener ,KeyListenerprivate static final long serialVersionUID = 1L;private JTextField input_English, input_Chinese;private JLabel label1, label2;private JPanel panel1, panel2;private String word_English = "moon", "star", "sun" ;import .
26、*;import .*;/* * * author 段智敏 */public class Work5_9 extends JFrame implements ActionListenerprivate static final long serialVersionUID = 1L;private JLabel result_label;private JTextArea input_textArea;private JTextField showResult_textField;private JButton sum_button, average_button, clear_button;p
27、rivate JPanel panelNorth, panelSouth;public Work5_9()result_label = new JLabel("请选择你的操作");showResult_textField = new JTextField(12);input_textArea = new JTextArea();sum_button = new JButton("求 和");average_button = new JButton("平均值");clear_button = new JButton("清空&q
28、uot;);panelNorth = new JPanel();panelSouth = new JPanel();(this);(this);(this);(sum_button);(average_button);(result_label);(showResult_textField);(clear_button);(panelNorth, ;(new JScrollPane(input_textArea), ;(panelSouth, ;(new WindowAdapter()public void windowClosing(WindowEvent e)(0););(100, 100
29、, 350, 200);(true);();public void actionPerformed(ActionEvent e)if () = clear_button)("");("");("请选择你的操作");int number_array = getNumber_array();if () = sum_button)("输入数的和");int sum = 0;for (int i = 0; i < ; i+)sum = sum + number_arrayi;("" + sum);
30、if () = average_button)("输入数的平均值");int sum = 0;for (int i = 0; i < ; i+)sum = sum + number_arrayi;double average = * sum / ;("" + average);public int getNumber_array()int numArray = null;tryString s = ();String temp = new String();StringTokenizer t = new StringTokenizer(s, &qu
31、ot;'n' ,. ");int n = ();numArray = new intn;for (int i = 0; i < n; i+)temp = ();numArrayi = (temp);catch( NumberFormatException e )(null, "请确认输入的是数字字符n", "错误警告", ;return numArray;public static void main(String args)new Work5_9(); 布局设计程序运行结果:源文件:import .*;import .*
32、;/* * author 段智敏 */class Work5_10 extends JFrameprivate static final long serialVersionUID = 1L;private JPanel panel2, panel3, panel4, panel6;import .*;import * author 段智敏 */public class Work6_1 extends JFrameprivate static final long serialVersionUID = 1L;private MyPanel6_1 panel;import * * author
33、段智敏 */public class Work6_2 extends JFrameprivate static final long serialVersionUID = 1L;private MyPanel6_2 panel;public Work6_2()super("第六章,第二题");panel = new MyPanel6_2();(panel);(100, 100, 400, 150);(true);();(new WindowAdapter()public void windowClosing(WindowEvent e)(0););public static
34、 void main(String args)new Work6_2();面板类源文件:/* * 需要设计的面板类 */class MyPanel6_2 extends JPanel implements ItemListenerprivate static final long serialVersionUID = 1L;private JCheckBox box1, box2, box3, box4;private ButtonGroup group;private JTextField textField;public MyPanel6_2()textField = new JTextF
35、ield(5);group = new ButtonGroup();box1 = new JCheckBox("足球");box2 = new JCheckBox("排球");box3 = new JCheckBox("篮球");box4 = new JCheckBox("台球");(this);(this);(this);(this);(box1);(box2);(box3);(box4);(box1);(box2);(box3);(box4);(textField);public void itemStateC
36、hanged(ItemEvent e)JCheckBox box = (JCheckBox) ();if (box = box1)() + "n");else if (box = box2)() + "n");else if (box = box3)() + "n");else if (box = box4)() + "n"); 设计一个面板,该面板中有四个运动项目单选按钮和一个文本框。当某个选择项目被选中时,在文本框中显示该选择项目。程序运行结果:源文件:import .*;import .*;import *
37、author 段智敏 */public class Work6_3 extends JFrameprivate static final long serialVersionUID = 1L;private MyPanel6_3 panel;public Work6_3()super("第六章,第三题");panel = new MyPanel6_3();(panel);(100, 100, 400, 150);(true);();(new WindowAdapter()public void windowClosing(WindowEvent e)(0););public
38、 static void main(String args)new Work6_3();面板类源文件:/* * 需要设计的面板类 */class MyPanel6_3 extends JPanel implements ItemListenerprivate static final long serialVersionUID = 1L;private JRadioButton box1, box2, box3, box4;private ButtonGroup group;private JTextField textField;public MyPanel6_3()textField =
39、new JTextField(5);group = new ButtonGroup();box1 = new JRadioButton("足球");box2 = new JRadioButton("排球");box3 = new JRadioButton("篮球");box4 = new JRadioButton("台球");(this);(this);(this);(this);(box1);(box2);(box3);(box4);(box1);(box2);(box3);(box4);(textField);
40、public void itemStateChanged(ItemEvent e)JRadioButton box = (JRadioButton) ();if (box = box1)() + "n");else if (box = box2)() + "n");else if (box = box3)() + "n");else if (box = box4)() + "n"); 设计一个窗口,取默认布局BorderLayout布局。北面添加一个列表,有4门课程选项。中心添加一个文本区,当选择列表中的某门课程后
41、,文本区显示相应课程的介绍;(JList没有addActionListener方法)程序运行结果:源文件:import .*;import .*;/* * author 段智敏 */public class Work6_4 extends JFrameprivate static final long serialVersionUID = 1L;private JList list;private JTextArea textArea;private String names = "大学英语", "Java语言程序设计", "操作系统"
42、;, "数据结构" ;private String introduce = "英语为本科一、二年级的最重要的基础课之一,为期一年,教学时间15周,总共14学分。","Java先修课程为程序设计基础、网络基础、数据库基础、操作系统;为期办年。教学时间15周,总共4学分(上机1学分)。","操作系统讲述如何管理计算机系统资源和控制程序执行。为期办年,教学时间15周,总共5学分(上机1学分)。","数据结构是计算机学科的核心课程之一,是一门专业基础课。对于训练学生程序设计能力和编程水平有重要作用。为期办年,教学
43、时间15周,总共5学分(上机学分)" ;private String times = "9月1日", "9月2日", "9月30日", "9月12日" ;public Work6_4()super("第六章,第四题");textArea = new JTextArea(5, 10);list = new JList(names);import .*;/* * author 段智敏 */public class Work6_5 extends JFrame implements Item
44、Listenerprivate static final long serialVersionUID = 1L;private String names = "数学", "物理", "语文", "化学" ;private JComboBox comboBox;private JTextField textField;public Work6_5()super("第六章,第五题");comboBox = new JComboBox(names);(this);textField = new JTe
45、xtField(10);add(comboBox, ;(textField, ;(100, 100, 300, 200);(true);();(new WindowAdapter()public void windowClosing(WindowEvent e)(0););public void itemStateChanged(ItemEvent e)().toString();public static void main(String args)new Work6_5(); 设计一个JFrame窗口,窗口中心添加一个文本区。另添加4个菜单,每个菜单都有菜单项,每个菜单项都有对应快捷键,选
46、择某个菜单项时,窗口中心的文本区显示相应信息。程序运行结果:源文件:import .*;import .*;/* * author 段智敏 */public class Work6_6 extends JFrame implements ActionListenerprivate static final long serialVersionUID = 1L;/* 文本区 */private JTextArea textArea;/* 菜单条 */private JMenuBar menubar;/* 菜单:文件、编辑、格式、帮助 */private JMenu file_menu, edit
47、_menu, format_menu, help_menu, style_menu;/* 添加的文件菜单上的菜单项:新建、打开、保存、另存为、退出 */private JMenuItem item_newFile, item_openFile, item_saveFile, item_saveAsFile, item_Exit;/* 添加到编辑菜单的菜单项:剪切、复制、粘贴、删除;查找、查找下一个、替换;全选、时间/日期 */private JMenuItem item_cut, item_copy, item_paste, item_delete, item_find, item_findN
48、ext,item_replace, item_selectAll, item_insertNowTime;/* 添加到格式菜单的菜单项:自动换行,换行时候,是否让整个单词在一行,设置字体 */private JMenuItem item_setLineWrap, item_setWrapStyleWord, item_setFont;/* 帮助菜单项:帮助主题,关于记事本 */private JMenuItem item_help, item_about;public Work6_6()super("第六章,第六题");textArea = new JTextArea();
49、menubar = new JMenuBar();();(menubar);(textArea, ;(new WindowAdapter()public void windowClosing(WindowEvent e)(0););(100, 100, 500, 400);(true);();/* * 初始化菜单项菜单条 并添加快捷键,然后都添加到主菜单条上 */public void init_menu()menubar = new JMenuBar();file_menu = new JMenu("文件(F)");edit_menu = new JMenu("
50、编辑(E)");format_menu = new JMenu("格式(O)");help_menu = new JMenu("帮助(H)");style_menu = new JMenu("风格(G)");item_newFile = new JMenuItem("新建(N)");item_openFile = new JMenuItem("打开(O).");item_saveFile = new JMenuItem("保存(S)");item_saveAsFil
51、e = new JMenuItem("另存为(A).");item_Exit = new JMenuItem("退出(X)");item_cut = new JMenuItem("剪切(T)");item_copy = new JMenuItem("复制(C)");item_paste = new JMenuItem("粘贴(P)");item_delete = new JMenuItem("删除(L)");item_find = new JMenuItem("查找(F)");item_findNext = new JMenuItem("查找下一个(N)");item_replace = new JMenuItem("替换(R)");item_selectAll = new JMenuItem("全选(A)");item_insertNowTime = new JMenuItem("时间/日期(D)"
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年上半年江苏南京市六合区人民医院招聘编外卫技人员24人备考题库及答案详解1套
- 2026辽宁省市场监管事务服务中心赴高校招聘4人备考题库及一套参考答案详解
- 2026四川数据集团有限公司第三批第一次员工招聘9人备考题库及答案详解一套
- 2026陕西西安铁路职业技术学院招聘20人备考题库及答案详解(全优)
- 2026宁波市海曙区面向2026年普通高校应届毕业生选聘紧缺人才20人备考题库附答案详解(模拟题)
- 2026北京怀柔区卫健委招聘医务人员17人备考题库及一套完整答案详解
- 2026陕西西安碑林区柏树林社区卫生服务中心招聘备考题库附答案详解(能力提升)
- 2026山东菏泽海吉亚医院招聘备考题库附答案详解(模拟题)
- 2026广西百色市平果市马头镇卫生院招聘编外卫生专业技术人员7人备考题库含答案详解(突破训练)
- 2026年抚州市曾巩学校公开选调教师备考题库【33人】及答案详解(典优)
- 设备搬迁及安装方案
- 西门子MAXUM II在线色谱简易操作技术手册
- 2025年贵州省委党校在职研究生招生考试(政治经济学原理)历年参考题库含答案详解(5卷)
- 多发性脑梗死课件
- 国企房屋租赁管理办法
- 储备土地巡查管理办法
- 上海市黄浦区2025年物理高二下期末统考试题含解析
- 茶文化课件图片
- 培训生态环境培训课件
- 统编版语文三年级下册第七单元 习作《国宝大熊猫》 课件
- TCTBA 001-2019 非招标方式采购代理服务规范
评论
0/150
提交评论