《JAVA程序设计2》
- 格式:doc
- 大小:81.00 KB
- 文档页数:9
JAVA2程序设计实用教程教学设计1. 前言JAVA2是一门面向对象的编程语言,广泛应用于软件开发领域。
在今天这个大数据时代,JAVA2的重要性愈加凸显。
JAVA2程序设计也成为了大量人才需求的热门领域。
本文的目的是为广大程序设计学习者提供一份JAVA2程序设计实用教程教学设计,帮助大家掌握JAVA2程序设计的核心知识和技能。
2. 教学目标本教学设计旨在使学生掌握以下知识和技能:•掌握JAVA2编程语言的基础知识;•熟练掌握JAVA2程序设计的常用技能;•能够在开发过程中运用JAVA2编程语言进行问题解决;•培养学生团队协作、信息查找和学习能力。
3. 教学内容本教学设计的核心内容如下:3.1 JAVA2编程基础•程序语言基础:变量定义、数据类型、运算符、流程控制语句;•面向对象编程:类和对象、继承与多态、接口;•异常处理:异常的概念、异常处理方式。
3.2 JAVA2程序设计进阶•IO流:字节流、字符流、文件读写;•JDBC连接:连接MySQL数据库、对数据库进行增删改查操作;•网络编程:Socket编程、客户端和服务端交互。
3.3 JAVA2程序开发实践•实现一个JAVA2程序,包含数据库连接和网络编程的内容;•使用Eclipse软件进行开发,运用Git进行版本控制;•采用团队合作方式进行开发。
4. 教学方法本教学设计采用以下教学方法:4.1 授课法授课法是本课程的主要教学方法,通过教师讲解、示范和演示,使学生掌握JAVA2编程知识和技能。
4.2 实践性教学实践性教学是本课程的重要组成部分,通过课堂实验、作业等方式,使学生进一步掌握JAVA2编程的实用技能。
4.3 课程设计课程设计是本课程的最终目标,让学生在团队中完成一个JAVA2程序,从而深入实践,加深对JAVA2编程的理解与掌握。
5. 考核方法本课程采用以下考核方法:5.1 课堂表现考核学生在教学过程中的听讲、提问、参与活动等表现进行综合考核。
Javaweb程序设计任务教程第二版介绍本教程是针对初学者而设计的一本Javaweb程序设计任务教程第二版。
通过本教程,读者将学习如何使用Java编程语言和相关的技术开发一个完整的Javaweb应用程序。
本教程包含了从基本概念到实际项目开发的全面内容,方便读者逐步学习和掌握Javaweb开发的过程。
目录1.第一章:Javaweb基础 .1 HTTP协议基础 .2 Servlet入门 .3 JSP入门 .4 MVC模式介绍2.第二章:数据库操作 .1 MySQL数据库入门 .2 JDBC基础 .3 ORM框架介绍 .4 数据库连接池配置3.第三章:Javaweb开发进阶 .1 Javaweb框架介绍 .2Maven项目管理工具 .3 Spring框架入门 .4 SpringMVC框架入门 .5 搭建Javaweb开发环境4.第四章:项目实战 .1 项目需求分析 .2 项目架构设计 .3 前端页面开发 .4 后端功能开发 .5 项目测试与部署5.第五章:Javaweb安全与优化 .1 Javaweb安全防护 .2 性能优化技巧 .3 Javaweb项目部署与维护第一章:Javaweb基础1.1 HTTP协议基础HTTP协议是Javaweb开发的基础,本节将介绍HTTP协议的基本概念和运作原理。
1.1.1 HTTP协议概述HTTP(Hypertext Transfer Protocol)是一种用于传输超文本的应用层协议,采用TCP作为传输协议。
它是Web的基础,通过HTTP协议,Web浏览器与Web服务器之间可以进行通信和数据传输。
1.1.2 HTTP请求与响应HTTP协议通过客户端发送请求到服务器,并获得服务器的响应来实现通信。
本节将介绍HTTP请求和响应的结构和内容。
1.1.2.1 HTTP请求一个HTTP请求包含了请求行、请求头和请求体三个部分。
请求行指定了请求的方法(GET、POST等)、请求的URL和HTTP协议的版本。
第一章习题答案一、简答题1.Java语言有哪些特点?主要用于能够哪些方面的软件开发?特点:(1)面向对象,(2)平台无关性,(3)分布式,(4)可靠性和安全性,(5)多线程,(6)简单性,(7)健壮性,(8)高性能,(9)灵活性。
适用范围:(1)所有面向对象的应用开发,包括面向对象的事件描述、处理、综合等。
(2)计算过程的可视化、可操作化的软件的开发。
(3)动态画面的设计,包括图形图象的调用。
(4)交互操作的设计。
(5)Internet 的系统管理功能模块的设计,包括 Web 页面的动态设计、管理和交互操作设计等。
(6)Intranet上的软件开发(直接面向企业内部用户的软件)。
(7)与各类数据库连接查询的 SQL 语句实现。
(8)网络通信与移动通信,网络集成方面。
2.Java程序分为哪两类?各有什么特点?如何区分它们?Java语言程序分为两类:应用程序Application和小程序Applet,它们是利用Java语言生成的可运行代码中最常用的两种,两者都必须在Java虚拟机上运行。
Application更象我们平常用VB,VC等编写的桌面程序,多以控制台(CONSOLE)方式运行,当然也很容易实现窗口应用。
Applet则需要嵌入Web页面中,在支持Java虚拟机的浏览器上运行。
区分办法:应用程序Application中一定包括一个主方法main,而小程序Applet没有这个方法。
3.简述Java程序的运行过程。
一个Java源的运行需要经过三个步骤:(1)编辑源程序,产生.Java的源程序文件(2)运用Java虚拟机JVM将源文件编译为.class的字节码文件(3)根据程序的不同类型运行程序。
对于应用程序直接调用运行命令执行字节码文件,而对于小程序Applet,需要先将字节码文件嵌入HTML文件,然后用浏览器软件打开HTML文件。
4.什么是Java虚拟机?简述其工作机制。
Java虚拟机(Java Virtual Machine—JVM)是一种利用软件方法实现硬件功能的抽象化的计算机,它基于下层的操作系统和硬件平台,可以在上面执行Java的字节码程序。
java语言程序设计第二版习题答案Java语言程序设计第二版习题答案Java语言程序设计是一门广泛应用于软件开发领域的编程语言。
无论是初学者还是有经验的开发人员,都可以通过学习Java语言来提升自己的编程能力。
为了帮助读者更好地掌握Java语言的知识,本文将提供《Java语言程序设计第二版》中一些习题的答案,并对其中一些重要的概念进行解释和讨论。
第一章:计算机、程序和Java1.1 问题:编写一个Java程序,输出“Hello, World!”。
答案:```javapublic class HelloWorld {public static void main(String[] args) {System.out.println("Hello, World!");}}```1.2 问题:Java应用程序的执行过程是怎样的?答案:Java应用程序的执行过程可以分为三个阶段:编辑、编译和运行。
首先,我们使用文本编辑器编写Java源代码文件,文件的扩展名为.java。
然后,使用Java编译器将源代码文件编译成字节码文件,文件的扩展名为.class。
最后,使用Java虚拟机(JVM)加载字节码文件并执行程序。
第二章:基本程序设计2.1 问题:编写一个Java程序,计算两个整数的和。
答案:```javaimport java.util.Scanner;public class Sum {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the first number: ");int num1 = input.nextInt();System.out.print("Enter the second number: ");int num2 = input.nextInt();int sum = num1 + num2;System.out.println("The sum is " + sum);}}```2.2 问题:什么是变量?如何在Java中声明和使用变量?答案:变量是用于存储数据的内存位置。
java程序设计教程 pdf 第二版引言概述:《Java程序设计教程 PDF 第二版》是一本针对Java程序设计的教程书籍,它提供了全面而系统的学习资源,帮助读者掌握Java编程的基本知识和技能。
本文将从五个大点来阐述这本教程书籍的内容,包括基础语法、面向对象编程、异常处理、多线程编程和图形用户界面编程。
正文内容:1. 基础语法1.1 变量和数据类型:教程详细介绍了Java中的基本数据类型和变量的声明和使用方法,包括整型、浮点型、字符型等。
1.2 运算符和表达式:教程详细讲解了Java中的各种运算符和表达式的使用方法,包括算术运算符、关系运算符、逻辑运算符等。
1.3 控制流程:教程详细介绍了Java中的条件语句和循环语句的使用方法,包括if语句、switch语句、for循环、while循环等。
2. 面向对象编程2.1 类和对象:教程详细介绍了Java中类和对象的概念和使用方法,包括类的定义、对象的创建和使用等。
2.2 继承和多态:教程详细讲解了Java中的继承和多态的概念和使用方法,包括继承的实现、方法重写、多态的实现等。
2.3 封装和抽象:教程详细介绍了Java中的封装和抽象的概念和使用方法,包括访问修饰符的使用、接口和抽象类的定义等。
3. 异常处理3.1 异常的概念:教程详细介绍了Java中异常的概念和分类,包括受检异常和非受检异常。
3.2 异常处理机制:教程详细讲解了Java中的异常处理机制,包括try-catch语句、finally语句、throw语句等。
3.3 自定义异常:教程详细介绍了如何在Java中自定义异常类,以及如何使用自定义异常类来处理特定的异常情况。
4. 多线程编程4.1 线程的概念:教程详细介绍了Java中线程的概念和创建线程的方法,包括继承Thread类和实现Runnable接口。
4.2 线程同步:教程详细讲解了Java中线程同步的概念和方法,包括synchronized关键字、锁机制等。
java程序设计教程 pdf 第二版标题:Java程序设计教程PDF第二版引言概述:Java程序设计是计算机科学中的一门重要课程,它涵盖了从基础知识到高级概念的全面内容。
本文将介绍Java程序设计教程PDF第二版的内容,包括其结构、特点以及适用对象等方面。
正文内容:1. Java程序设计教程PDF第二版的结构1.1 第一章:Java概述- 介绍Java的发展历程和特点- 解释Java的跨平台性和面向对象的特性- 简要介绍Java的基本语法和编程环境1.2 第二章:Java基础知识- 详细介绍Java的数据类型、运算符和控制结构- 解释Java中的数组和字符串的使用方法- 介绍面向对象编程的基本概念和Java中的类与对象1.3 第三章:Java高级概念- 详细介绍Java中的继承、多态和接口等高级特性- 解释Java中的异常处理机制和文件操作- 介绍Java中的集合框架和泛型的使用方法2. Java程序设计教程PDF第二版的特点2.1 全面性- 本教程涵盖了Java程序设计的各个方面,从基础知识到高级概念,适合初学者和有一定经验的开发者阅读。
- 通过实例和案例的方式,帮助读者理解和掌握Java的各种编程技巧和最佳实践。
2.2 实用性- 本教程提供了大量的实践项目和练习,读者可以通过实际操作来巩固所学知识。
- 教程中的示例代码和实例项目可以帮助读者快速上手,并且可以根据自己的需求进行修改和扩展。
2.3 更新性- 第二版教程对Java的最新版本进行了更新,包括Java 8和Java 11的新特性和改进。
- 本教程还介绍了一些流行的开发工具和框架,如Eclipse和Spring,帮助读者更好地进行Java开发。
3. Java程序设计教程PDF第二版的适用对象3.1 初学者- 本教程以简洁清晰的语言和逻辑结构编写,适合初学者快速入门Java编程。
- 通过本教程的学习,初学者可以掌握Java的基本语法和编程思想,为进一步学习和开发打下坚实基础。
习题解答习题一(第1章)1.James Gosling2.需3个步骤:1) 用文本编辑器编写源文件.2) 使用javac 编译源文件,得到字节码文件。
3) 使用解释器运行程序.3.set classpath=D :\jdk\jre\lib\rt 。
jar ;.;4. B5。
Java 源文件的扩展名是。
java ,Java 字节码的扩展名是.class 。
6.D 。
习题二(第2章)1.2. Teac her.javapublic class Teacher {double add (double a,double b) {return a+b;}double sub (double a,double b) {return a-b;}}Student 。
javapublic class Student {public void speak () {System 。
out 。
println ("老师好");}}MainClass 。
javapublic class MainClass {public static void main(String args[]) {height bottomTeacher zhang=new Teacher();System.out.println(zhang。
add(12,236));System。
out.println(zhang.add(234,120));Student jiang=new Student();jiang。
speak();}}3.如果源文件中有多个类,但没有public类,那么源文件的名字只要和某个类的名字相同,并且扩展名是.java就可以了,如果有一个类是public类,那么源文件的名字必须与这个类的名字完全相同,扩展名是.java。
4.行尾风格。
习题三(第3章)1.用来标识类名、变量名、方法名、类型名、数组名、文件名的有效字符序列称为标识符。
java程序设计教程第二版课后答案【篇一:《java程序设计》课后习题参考答案】参考答案――武汉大学出版社习题1参考答案1.java语言的特点有哪些??答:参考1.1.2防止直接访问数据变量看起来有些奇怪,但它实际上却对使用类的程序质量有极大的好处。
既然数据的单个项是不可访问的,那么惟一的办法就是通过方法来读或写。
因此,如果要求类成员内部的一致性,就应该通过类本身的方法来处理。
这种数据隐藏技术就是面向对象的重要特性——封装。
它将类的外部界面与类功能的实现区分开来,隐藏实现细节,(通过公共方法)保留有限的对外接口,迫使用户使用外部界面,通过访问接口实现对数据的操作。
即使实现细节发生了改变,还可通过界面承担其功能而保留原样,确保调用它的代码还继续工作,这使代码维护更简单。
2.简述封装的优点。
?答:封装是一个简单而有效的思想,优点有:(1)模块化,对内成为一个结构完整、可进行自我管理、自我平衡、高度集中的整体。
(2)信息隐蔽,对外则是一个功能明确、接口单一、可在各种适合的环境下都能独立工作的有机单元。
面向对象的程序设计实现了对象的封装,使得用户不必关心诸如对象的行为是如何实现的这样一些细节。
通过对对象的封装,实现了模块化和信息隐藏,有利于程序的可移植性和安全性,同时也有利于对复杂对象的管理。
类的封装性使得代码的可重用性大为提高,这样的有机单元特别适合构建大型标准化的软件系统,具有很高的开发效率。
3.java的基本工具有哪些??(1) javac 编译器(2) java 解释器(3) jdb java 语言调试器(4) javadoc api文档管理器(5) javah 头文件生成器(6) appletviewer 小应用程序浏览器(7) javap 类文件反汇编器4.java开发环境是如何配置的?答:对于windows 2000以上版本的操作系统,可以打开[控制面板]窗口,双击其中的[系统]图标,在[系统特性]窗口中单击[高级]选项卡,进而单击[环境变量]按钮。
//习题2.2import java.util.*;class MyDate{private int year;private int month;private int day;public MyDate(int y,int m,int d){//构造函数,构造方法year=y;month=m;day=d;}//end public MyDate(int y,int m,int d)public int getYear(){//返回年return year;}//end getYear()public int getMonth(){//返回月return month;}//end getMonth()public int getDay(){//返回日return day;}//end getDay()}//end class MyDateclass Employee{private String name;private double salary;private MyDate hireDay;public Employee(String n,double s,MyDate d){name=n;salary=s;hireDay=d;}//end public Employee(String n,double s,MyDate d)public void print(){System.out.println("名字:"+name+"\n工资:"+salary+"\n雇佣年份:"+hireYear()+"\n");}//end print()public void raiseSalary(double byPercent){salary*=1+byPercent/100;}//endpublic int hireYear(){return hireDay.getYear();}}//end class Employeepublic class MyTestClass {public static void main(String[] args) {Employee[]staff=new Employee[3];staff[0]=new Employee("Harry Hacker",35000,new MyDate(1989,10,1));- 2 - staff[1]=new Employee("Carl Carcker",75000,new MyDate(1987,12,15)); staff[2]=new Employee("Tony T ester",38000,new MyDate(1990,3,12)); int integerValue;System.out.println("The information of employee are:");for(integerValue=0;integerValue<=2;integerValue++){staff[integerValue].raiseSalary(5);}//end for()for(integerValue=0;integerValue<=2;integerValue++){staff[integerValue].print();}//end for()}//end main() }//end class MyTestClass//习题2.4import java.util.*;public class DataType {public static void main(String[] args) {boolean flag;char yesChar;byte finByte;int intValue;long longValue;short shortValue;float floatValue;double doubleValue;flag=true;yesChar='y';finByte=30;intValue=-7000;longValue=200l;shortValue=20000;floatValue=9.997E-5f;doubleValue=floatValue*floatValue;System.out.println("the values are:");System.out.println("布尔类型变量flag="+flag);System.out.println("字符型变量yesChar="+yesChar);System.out.println("字节型变量finByte="+finByte);System.out.println("整型变量intValue="+intValue);System.out.println("长整型变量longValue="+longValue);System.out.println("短整型变量shortValue="+shortValue);System.out.println("浮点型变量floatValue="+floatValue);System.out.println("双精度浮点型变量doubleValue="+doubleValue);}//end main()}//习题2.9import java.util.*;class PubTest1{private int ivar1;private float fvar1,fvar2;public PubTest1(){fvar2=0.0f;}public float sum_f_I(){fvar2=fvar1+ivar1;return fvar2;}public void print(){System.out.println("fvar2="+fvar2);}public void setIvar1(int ivalue){ivar1=ivalue;}public void setFvar1(float ivalue){fvar1=ivalue;}}public class PubMainTest {public static void main(String[] args) {PubTest1 pubt1=new PubTest1();pubt1.setIvar1(10);pubt1.setFvar1(100.02f);pubt1.sum_f_I();pubt1.print();}}//习题2.10import java.util.*;class Date {private int year;private int month;private int day;public Date(int day, int month, int year) { //构造函数,构造方法this.year = year;this.month = month;this.day = day;} //end public MyDate(int y,int m,int d)- 4 -public int getYear() { //返回年return year;} //end getYear()public int getMonth() { //返回月return month;} //end getMonth()public int getDay() { //返回日return day;} //end getDay()} //end class Datepublic class Teacher {String name;//教师名字boolean sex;//性别,true 表示男性Date birth;//出生日期String salaryID;//工资号String depart;//教师所在系所String posit;//教师职称String getName() {return name;}void setName(String name) { = name;}boolean getSex() {return sex;}void setSex(boolean sex) {this.sex = sex;}Date getBirth() {return birth;}void setBirth(Date birth) {this.birth = birth;}String getSalaryID() {return salaryID;}void setSalaryID(String salaryID) {this.salaryID = salaryID;}String getDepart() {return depart;}void setDepart(String depart) {this.depart = depart;}String getPosit() {return posit;}void setPosit(String posit) {this.posit = posit;}public Teacher(){System.out.println("父类无参数的构造方法!!!!!!!");}//如果这里不加上这个无参数的构造方法将会出错!!!!public Teacher(String name,boolean sex,Date birth,String salaryid,String depart,String posit){ =name;this.sex=sex;this.birth=birth;this.salaryID=salaryid;this.depart=depart;this.posit=posit;}//end Teacher()public void print(){System.out.print("the teacher'name:");System.out.println(this.getName());System.out.print("the teacher'sex:");if(this.getSex()==false){System.out.println("女");}else{System.out.println("男");}System.out.print("the teacher'birth:");System.out.println(this.getBirth().getYear()+"-"+this.getBirth().getMonth()+"-"+this.getBirth().getDay()); System.out.print("the teacher'salaryid:");System.out.println(this.getSalaryID());System.out.print("the teacher'posit:");System.out.println(this.getPosit());System.out.print("the teacher'depart:");System.out.println(this.getDepart());}//end print()public static void main(String[] args) {Date dt1=new Date(11,23,1989);Date dt2=new Date(2,6,1975);- 6 -Date dt3=new Date(11,8,1964);Date dt4=new Date(10,4,1975);Date dt5=new Date(8,9,1969);//创建各系教师实例,用来测试Teacher t1=new Teacher("王莹",false,dt1,"123","经济学","prefessor");ResearchTeacher rt=new ResearchTeacher("杨zi 青",true,dt2,"421","软件工程", "associate prefessor","software"); LabTeacher lat=new LabTeacher("王夏瑾",false,dt3,"163","外语","pinstrucor","speech lab");LibTeacher lit=new LibTeacher("马二孩",true,dt4,"521","大学物理","prefessor","physicalLib");AdminTeacher at=new AdminTeacher("王xi",false,dt5,"663","环境","prefessor","dean");/////////分别调用各自的输出方法,输出相应信息//////////////////////////// System.out.println("-------------------------------");t1.print();//普通教师信息System.out.println("-------------------------------");rt.print();//研究系列教师信息System.out.println("-------------------------------");lat.print();//普通教师信息System.out.println("-------------------------------");lit.print();//实验系列教师信息System.out.println("-------------------------------");at.print();//行政系列教师信息System.out.println("-------------------------------");}//end main()}//end public class Teacherclass ResearchT eacher extends Teacher{private String resField;public ResearchT eacher(String name, boolean sex, Date birth, String salaryid, String depart, String posit, String resField) { = name;this.sex = sex;this.birth = birth;this.salaryID = salaryid;this.depart = depart;this.posit = posit;this.resField = resField;} //end public ResearchTeacher(){}String getResField(){return resField;}void setResField(String resField){this.resField=resField;}public void print() {System.out.print("research teacher info is:");System.out.print("the teacher'name:");System.out.println(this.getName());System.out.print("the teacher'sex:");if (this.getSex() == false) {System.out.println("女");}else {System.out.println("男");}System.out.print("the teacher'birth:");System.out.println(this.getBirth().getYear() + "-" +this.getBirth().getMonth() + "-" +this.getBirth().getDay());System.out.print("the teacher'salaryid:");System.out.println(this.getSalaryID());System.out.print("the teacher'posit:");System.out.println(this.getPosit());System.out.print("the teacher'depart:");System.out.println(this.getDepart());System.out.print("the teacher'resField:");System.out.println(this.getResField());} //end print()}//end class ResearchTeacherclass LabTeacher extends T eacher{private String labName;public LabTeacher(String name, boolean sex, Date birth,String salaryid, String depart,String posit, String labName) { = name;this.sex = sex;this.birth = birth;this.salaryID = salaryid;this.depart = depart;this.posit = posit;bName = labName;} //end public ResearchTeacher(){}String getLabName(){return labName;}void setLabName(String labName){- 8 -bName=labName;}public void print() {System.out.print("lab teacher info is:");System.out.print("the teacher'name:");System.out.println(this.getName());System.out.print("the teacher'sex:");if (this.getSex() == false) {System.out.println("女");}else {System.out.println("男");}System.out.print("the teacher'birth:");System.out.println(this.getBirth().getYear() + "-" +this.getBirth().getMonth() + "-" +this.getBirth().getDay());System.out.print("the teacher'salaryid:");System.out.println(this.getSalaryID());System.out.print("the teacher'posit:");System.out.println(this.getPosit());System.out.print("the teacher'depart:");System.out.println(this.getDepart());System.out.print("the teacher'labName:");System.out.println(bName);} //end print()}//end class LabTeacherclass LibTeacher extends Teacher{private String libName;public LibTeacher(String name,boolean sex,Date birth,String salaryid,String depart,String posit,String libName){ = name;this.sex = sex;this.birth = birth;this.salaryID = salaryid;this.depart = depart;this.posit = posit;this.libName=libName;}//end public ResearchT eacher(){}String getLibName(){return libName;}void setLibName(String libName){this.libName=libName;}public void print() {System.out.print("lib teacher info is:");System.out.print("the teacher'name:");System.out.println(this.getName());System.out.print("the teacher'sex:");if (this.getSex() == false) {System.out.println("女");}else {System.out.println("男");}System.out.print("the teacher'birth:");System.out.println(this.getBirth().getYear() + "-" +this.getBirth().getMonth() + "-" +this.getBirth().getDay());System.out.print("the teacher'salaryid:");System.out.println(this.getSalaryID());System.out.print("the teacher'posit:");System.out.println(this.getPosit());System.out.print("the teacher'depart:");System.out.println(this.getDepart());System.out.print("the teacher'libName:");System.out.println(this.libName);} //end print()}//end class LibTeacherclass AdminTeacher extends Teacher{private String managePos;public AdminTeacher(String name,boolean sex,Date birth,String salaryid,String depart,String posit,String managePos){ = name;this.sex = sex;this.birth = birth;this.salaryID = salaryid;this.depart = depart;this.posit = posit;this.managePos=managePos;- 10 -}//end public ResearchT eacher(){}String getManagePos(){return managePos;}void setManagePos(String managePos){this.managePos=managePos;}public void print() {System.out.print("adminteacher info is:");System.out.print("the teacher'name:");System.out.println(this.getName());System.out.print("the teacher'sex:");if (this.getSex() == false) {System.out.println("女");}else {System.out.println("男");}System.out.print("the teacher'birth:");System.out.println(this.getBirth().getYear() + "-" +this.getBirth().getMonth() + "-" +this.getBirth().getDay());System.out.print("the teacher'salaryid:");System.out.println(this.getSalaryID());System.out.print("the teacher'posit:");System.out.println(this.getPosit());System.out.print("the teacher'depart:");System.out.println(this.getDepart());System.out.print("the teacher'managePos:");System.out.println(this.managePos);} //end print() }//end class AdminTeacher习题2.11public class Course {private String courseID;private String courseName;private String courseType;private int classHour;private float credit;public Course(String courseID, String courseName, String courseType, int classHour, float credit) {this.courseID=courseID;this.courseName=courseName; this.courseType=courseType; this.classHour=classHour; this.credit=credit;}//end public Course(){}String getID() {return courseID;}void setID(String id) {this.courseID = id;}String getName() {return courseName;}void setName(String name) { this.courseName = name;}String getType() {return courseType;}void setType(String type) { this.courseType = type;}int getClassHour() {return classHour;}void setClassHour(int hour) { this.classHour = hour;}float getCredit() {return classHour;}void setCredit(float credit) { this.credit= credit;}- 12 -public void print(){System.out.println("the basic info of this course as followed:"); System.out.println("courseID="+this.getID());System.out.println("courseName="+this.getName());System.out.println("courseType="+this.getType());System.out.println("classHour="+this.getClassHour());System.out.println("credit="+this.getCredit());}public static void main(String[] args) {Course cs=new Course("d12","java 程序设计(第二版)","cs",64,3.0f); System.out.println("----------------------------------");cs.print();System.out.println("修改课程学分为4.0f");cs.setCredit(4);cs.print();} }//习题2.12public class MyGraphic {String lineColor;String fillColor;MyGraphic(String lc,String fc){this.lineColor=lc;this.fillColor=fc;}void print(){System.out.println("line color is "+this.lineColor+"\t fill color is "+this.fillColor);}public static void main(String[] args) {float rd=(float)4.5;MyCircle mc=new MyCircle(rd,"black","white");MyRectangle mr=new MyRectangle(4,6,"red","blue");System.out.println("Circle info ");mc.print();System.out.println("circumference is " + mc.calCircum());System.out.println("square is " + mc.calSquare());System.out.println("rectangle info: ");mr.print();System.out.println("circumference is " + mr.calCircum());System.out.println("square is " + mr.calSquare()); }//end main(){}}//end public class MyGraphicclass MyRectangle extends MyGraphic{float rLong;float rWidth;MyRectangle (float rl,float rw,String lc,String fc){super(lc,fc);this.rLong=rl;this.rWidth=rw;}//end MyRectangle (){}float calCircum(){return ((float)((this.rLong+this.rWidth)*2));}float calSquare(){return ((float)(this.rLong*this.rWidth));}}//end class MyRectangleclass MyCircle extends MyGraphic{float radius;MyCircle (float rd,String lc,String fc){super(lc,fc);this.radius=rd;}//end MyRectangle (){}float calCircum(){return (float)((this.radius*3.12*2));}float calSquare(){return ((float)(this.radius*this.radius*3.14));}}//end class MyCircle//习题2.13public class Vehicle {String brand;String color;int price;int number;- 14 -public Vehicle(String b, String c) {this.brand = b;this.color = c;}public Vehicle(String b, String c, int p, int n) {this(b, c);this.price = p;this.number = n;}void print() {System.out.println("\n-------------------------");System.out.println("the vehicle info as followed :");System.out.println("brand=" + this.brand + "\t");System.out.println("color=" + this.color + "\t");System.out.println("price=" + this.price + "\t");System.out.println("number=" + this.number + "\t");} //end void print()public static void main(String[] args) {Vehicle c1=new Vehicle("vehicle1","white");Vehicle c2=new Vehicle("vehicle2","white",300,1);Car cr=new Car("car1","red",300,4,400);Truck tk2=new Truck("truck1","black",300,400);c1.print();c2.print();cr.print();tk2.print();} //end main()} //end public class Vehicleclass Car extends Vehicle{int speed;Car(String b, String c, int p, int n,int s){super(b,c,p,n);this.speed=s;}void print(){super.print();System.out.print("speed="+this.speed);}}//end class Carclass Truck extends Vehicle{int weight;Truck(String b, String c, int s,int w){super(b,c);this.speed=s;this.weight=w;}void print(){super.print();System.out.print("speed="+this.speed);System.out.print("weight="+this.weight); }}//end class Truck//习题3.3public class Test {public static void main(String[] args) {int b1=1;int b2=1;System.out.println("b1=" + b1);System.out.println("b2=" + b2);b1<<=31;b2<<=31;System.out.println("b1=" + b1);System.out.println("b2=" + b2);b1 >>= 31;System.out.println("b1=" + b1);b1 >>= 1;System.out.println("b1=" + b1);b2 >>>= 31;System.out.println("b2=" + b2);b2 >>>= 1;System.out.println("b2=" + b2);}}//习题3.4public class Factorial {private int result,initVal;public static int Factorial(int n){if(n==0){- 16 -}return n*Factorial(n-1);}public void print(){System.out.println(initVal+"!="+result);}public void setInitVal(int n){initVal=n;}public static void main(String[] args) {Factorial ff=new Factorial();for(int i=0;i<=4;i++){ff.setInitVal(2*(i+1));ff.result=Factorial(ff.initVal);ff.print();}//end for()}//end main()}//end public class Factorialpublic class Factorial2 {private int result,initVal;public void print(){System.out.println(initVal+"!="+result);}public void setInitVal(int n){initVal=n;}public static void main(String[] args) {Factorial2 ff=new Factorial2();for(int i=0;i<=4;i++){ff.setInitVal(2*(i+1));ff.result=1;for(int j=2;j<=ff.initVal;j++){ff.result*=j;}ff.print();}//end for()}//end main() }//习题3.5public class MathRandomTest {public static void main(String[] args) {int count=0,MAXof100,MINof100;int num,i;MAXof100=(int)(100*Math.random());MINof100=(int)(100*Math.random());System.out.print(MAXof100+" ");System.out.print(MINof100+" ");if(MAXof100>50)count++;if(MINof100>50)count++;if( MAXof100<MINof100){num=MINof100;MINof100=MAXof100;MAXof100=num;}//end if()for(i=0;i<98;i++){num=(int)(100*Math.random());System.out.print(num+((i+2)%10==9?"\n":" "));if(num>MAXof100){MAXof100=num;}else if(num<MINof100){MINof100=num;}if(num>50){count++;}}//end for()System.out.println("the max of 100 random integers is "+MAXof100);System.out.println("the min of 100 random integers is "+MINof100);System.out.println("the number of random more than50 is "+count); }//end main()}//end public class MathRandomTest//习题3.7public class PrintAst {public void printAstar() {System.out.print("*");}public void printSpace() {System.out.print(" ");}- 18 -public static void main(String[] args) {PrintAst pa = new PrintAst();int initNum = 13;for (int i = 1; i <= initNum / 2 + 1; i++) {for (int n = 1; n <= i; n++) {pa.printSpace();pa.printSpace();}for (int m = 1; m <= initNum - 2 * i + 2; m++) {pa.printSpace();pa.printAstar();}System.out.println();} //end forif (initNum % 2 == 0) {for (int i = 1; i <= initNum / 2; i++) {pa.printSpace();pa.printSpace();}pa.printSpace();pa.printAstar();pa.printSpace();pa.printAstar();System.out.println();}for (int i = initNum / 2 + 2; i <= initNum; i++) {for (int n = 1; n <= initNum - i + 1; n++) {pa.printSpace();pa.printSpace();}for (int m = 1; m <= 2 * i - initNum; m++) {pa.printSpace();pa.printAstar();}System.out.println();} //end forSystem.out.println();} //end main()} //end public class PrintAst//习题3.8public class PrintTriag {public void printAstar() {System.out.print("*");}public static void main(String[] args) {int initLine = 10;int initNum = 10;PrintTriag pt = new PrintTriag();for (int i = 0; i < initLine; i++) {for (int j = 0; j < initNum - i; j++) {pt.printAstar();}System.out.println();}}//end main()}//end public class PrintTriag习题3.9import java.util.*;public class MultipleT able {public void printFormula(int i,int j,int res){ System.out.print(i+"*"+j+"="+res+" "); }public static void main(String[] args) {MultipleT able mt=new MultipleT able();int initNum=9;int res=0;for(int i=1;i<=initNum;i++){for(int j=1;j<=i;j++){res=i*j;mt.printFormula(i,j,res);}System.out.println();}//end for}//end main()- 20 - }//end public class MultipleT able习题3,10import java.io.*;public class HuiWen {boolean isHuiWen(char str[], int n) {int net = 0;int i, j;for (i = 0, j = n - 1; i < n / 2; i++, j--) {if (str[i] == str[j]) {net++;} //end if} //end forif (net == (int) (n / 2)) {return true;} //end ifelse {return false;}} //end boolean isHuiWen(char str[], int n)public static void main(String[] args) {HuiWen hw1 = new HuiWen();String pm = "";try {InputStreamReader reader = new InputStreamReader(System.in); BufferedReader input = new BufferedReader(reader);System.out.print("give your test string:\n");pm = input.readLine();System.out.println(pm);} //end trycatch (IOException e) {System.out.print(e);} //end catchboolean bw = hw1.isHuiWen(pm.toCharArray(), pm.length()); if (bw == true) {System.out.println("是回文");}else {System.out.println("不是回文");}} //end main()} //end public class HuiWenimport java.io.*;public class HuiWen2 {String reverse(String w1){String w2;char[]str1=w1.toCharArray();int len=w1.length();char[]str2=new char[len];for(int i=0;i<len;i++){str2[i]=str1[len-1-i];}w2=new String(str2);return w2;}public static void main(String[] args) {HuiWen2 hw1 = new HuiWen2();String pm = "";try {InputStreamReader reader = new InputStreamReader(System.in);BufferedReader input = new BufferedReader(reader);System.out.print("give your test string:\n");pm = input.readLine();} //end trycatch (IOException e) {System.out.print(e);} //end catchString w2=hw1.reverse(pm);if(pareTo(pm)==0){System.out.println("是回文");}else {System.out.println("不是回文");}}}//习题3.11import java.io.*;public class PrimeNumber {- 22 -private int pm;public void setPm(int pm){this.pm=pm;}public boolean isPrime(){boolean bl=true;int i=2;for(i=2;i<=Math.sqrt(pm);){if(pm%i==0){bl=false;break;}else{i++;}}//end forreturn bl;}//end public boolean isPrime()public static void main(String[] args) {PrimeNumber prim=new PrimeNumber();int testNum=0;try{InputStreamReader reader = new InputStreamReader(System.in);BufferedReader input = new BufferedReader(reader);System.out.print("give your test number:\n");testNum=Integer.parseInt(input.readLine());}//end trycatch(IOException e){System.out.println(e);}//end catchprim.setPm(testNum);boolean bl=prim.isPrime();if(bl==true){System.out.println(testNum+"是质数");}else {System.out.println(testNum+"不是质数");}}//end main }//end public class PrimeNumber习题3.12import java.io.*;public class Tempconverter {double celsius(double y){return ((y-32)/9*5);}public static void main(String[] args) {Tempconverter tc=new Tempconverter ();double tmp=0;try{InputStreamReader reader = new InputStreamReader(System.in);BufferedReader input = new BufferedReader(reader);System.out.print("give your fahrenheit number:\n");tmp=Double.parseDouble(input.readLine());}//end trycatch(NumberFormatException e){System.out.println(e);}//end catchcatch(IOException e){System.out.println(e);}//end catchSystem.out.println("the celsius of temperature is "+tc.celsius(tmp));}//end main()}//end public class Tempconverter习题3.13import java.io.*;public class Trigsquare {double x, y, z;Trigsquare(double x, double y, double z) {this.x = x;this.y = y;this.z = z;}boolean isTriangle() {boolean bl = false;if (this.x > 0 && this.y > 0 && this.z > 0) {if ( (this.x + this.y) > this.z && (this.x + this.z) > this.y && (this.z + this.y) > this.x) {bl = true;} //ebd ifelse {bl = false;} //end else} //end if(this.x>0&&this.y>0&&this.z>0)return bl;} //end boolean isTriangle()double getArea() {double s = (this.x + this.y + this.z) / 2.0;return (Math.sqrt(s * (s - this.x) * (s - this.y) * (s - this.z)));} //end double getArea()public static void main(String[] args) {double s[] = new double[3];try {InputStreamReader reader = new InputStreamReader(System.in);BufferedReader input = new BufferedReader(reader);System.out.print("输入三角形的三边的长度:\n");for (int i = 0; i <= 2; i++) { //输入三个数s[i] = Double.parseDouble(input.readLine());} //end for} //end trycatch (NumberFormatException e) {System.out.println("format error!!!");} //end catch- 24 - catch (IOException e) {System.out.print(e);} //end catchTrigsquare ts = new Trigsquare(s[0], s[1], s[2]);if (ts.isTriangle() == true) {System.out.println("三角形的面积是" + ts.getArea());} //end ifelse {System.out.println("输入的三边不能组成三角形!!");} //end else} //end main()} //end public class Trigsquare//习题3.14import java.util.*;import java.io.*;import java.util.Date;import java.text.*;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.GregorianCalendar.*;。
第1章Java语言概述选择题1-1 在下列概念中,Java语言只保留了(B)A. 运算符重载B. 方法重载C. 指针D. 结构和联合1-2 下列关于Java语言特性的描述中,错误的是(D)A. 支持多线程操作B. Java程序与平台无关C. Java和程序可以直接访问Internet上的对象D. 支持单继承和多继承1-3 下列关于Java Application程序在结构上的特点的中,错误的是(C)A. Java程序是由一个或多个类组成的B. 组成Java程序的若干个类可以放在一个文件中,也可以放在多个文件中C. Java程序的文件名要与某个类名相同D. 组成Java程序的多个类中,有且仅有一个主类1-4 Java程序经过编译后生成的文件的后缀是(C)A. .objB. .exeC. .classD. .java1-5 下列关于运行字节码文件的命令行参数的描述中,正确的是(A)A. 第一个命令行参数(紧跟命令字的参数)被存放在args[0]中B. 第一个命令行参数被存放在args[1]中C. 命令行的命令字被存放在args[0]中D.数组args[]的大小与命令行参数的个数无关判断题1-1JavaC++的语言之前问世的。
(错)1-2Java语言具有较好的安全性和可移植性及与平台无关等特性。
(对)1-3Java语言中取消了联合的概念,保留了结构概念。
(错)1-4Java语言中数据类型占内在字节数与平台无关。
(对)1-5Java语言中可用下标和指针两种方式表示数组元素。
(错)1-6Java语言的源程序不是编译型的,而是编译解释型的。
(对)1-7操作系统中进程和线程两个概念是没有区别的。
(错)1-8Java语言既是面向对象的又是面向网络的高级语言。
(对)1-9Java程序分为两大类:一类是Application程序,另一类是Applet程序。
前者又称Java应用程序,后者又称为Java小应用程序。
(对)1-10Java Application程序是由多个文件组成的,其中可以有也可以没有主文件。
jaav程序设计教程第2版贾珺答案1、下面这段代码的输出为( D )int year = 2046;if(year % 2 == 0){System、out、println("进入了if");}else{System、out、println("进入了else");}System、out、println("退出");A、进入了ifB、进入了elseC、进入了elseD、进入了if 退出退出2、关于Java程序的main()方法的说法中,错误的是( A )A、一个Java程序可以有多个main()方法B、 main()方法是Java程序的入口C、 main()方法使用public、static和void修饰,且顺序不能改变D、main()方法后面要有一对大括号,计算机需要执行的命令都写在大括号里3、分析如下Java代码,编译运行后的输出结果是( B)public static void main(String[] args){boolean b = true;System、out、print(b);if(b){System、out、print("真");}else{System、out、print("假");}}A、trueB、true 真C、true 假D、14、在Java程序的程序控制流语句中,可以省略且不影响编译通过的子句是( BC)A、caseB、breakC、defaultD、switch5、在Java语言中,下面( B)是逻辑运算符中优先级最高的A、&&B、!C、||D、|6、在Java中,以下变量赋值无法通过编译的是( A)A、char c1 = "男";B、char c2 = '女';C、int f1 = 128;D、double d1 =1、2;7、以下是文件HELLO、java中的源代码,请分析该代码的运行结果( B )public class Hello{public static void main(String[] args){String str = "Hello World";System、out、print(str);}}A、Hello WorldB、提示:公共类型Hello必须在它自己的文件中定义C、strD、提示:数据类型错误8、在Java中,如下代码输出结果是( A)public static void main(String[] args){int i=0;System、out、print(i+=2);System、out、print(i+=2);}A、2B、2C、0D、0。
《JA V A程序设计》期末考试试题(三)一、单项选择题1、如下哪个是Java中的标识符()A、publicB、superC、3numberD、width2、如下哪个是Java中的标识符( )A、fieldnameB、superC、3numberD、#number3、已知如下定义:String s = "story"; 下面哪个语句不是合法的( )A、s += "books";B、s = s + 100;C、int len = s.length;D、String t = s + “abc”;4、如下哪个是Java中有效的关键字()A、nameB、helloC、falseD、good5、下面的代码段执行之后count的值是什么( )int count = 1;for (int i = 1; i <= 5; i++) {count += i;}System.out.println(count);A、5B、1C、15D、166、定义一个类,必须使用的关键字是( )A、publicB、classC、interfaceD、static7、定义一个接口必须使用的关键字是()A、publicB、classC、interfaceD、static8、如果容器组件p的布局是BorderLayout,则在p的下边中添加一个按钮b,应该使用的语句是()A、p.add(b);B、p.add(b,"North");C、p.add(b,"South");D、b.add(p,"North");9、声明并创建一个按钮对象b,应该使用的语句是()A、Button b=new Button();B、button b=new button();C、Button b=new b();D、b.setLabel(“确定”);10、Frame对象默认的布局管理器是()A、FlowLayoutB、BorderLayoutC、CardLayoutD、null11、下列哪一个import命令可以使我们在程序中创建输入/输出流对象()A、import java.sql.*;B、import java.util.*;C、import java.io.*;D、import .*;12、下面哪一个import命令可以为我们提供编写网络应用程序的类()A、import java.sql.*;B、import java.util.*;C、import java.io.*;D、import .*;13、如果需要从文件中读取数据,则可以在程序中创建哪一个类的对象()A、FileInputStreamB、FileOutputStreamC、DataOutputStreamD、FileWriter二、填空题1、如果将类MyClass声明为public,它的文件名称必须是(MyClass.java)才能正常编译。
2、Java程序中的单行注释符是(//),多行注释符是(/* */ )。
3、Java中布尔类型的常量有两种,它们是(true)和(false)。
4、Java中用于定义小数的关键字有两个:(float)和(double),后者精度高于前者。
5、Java中用于两个数相等比较的运算符是:(==),用于不相等比较的运算符是(<>)。
6、在Java中定义一个字符串类型的变量str的语句是:(String str;),定义一个具有10个元素的整型数组a的语句是:(int [] arr = new int[10];)。
7、导入mypackage包中的所类的命令是(import mypackage.*;)。
8、当声明一个数组int arr[] = new int[5]; 时,这代表这个数组所保存的变量类型是(int),数组名是(arr),数组的大小为(5),数组元素下标的使用范围是(0到4)。
9、假设x=13,y=4,则表达式x%y != 0的值是(true),其数据类型是(boolean)。
10、异常处理是由(try)、(catch)和finally块三个关键所组成的程序块。
11、以下程序段的输出结果是(三角形)int x = 5, y = 6, z = 4;if (x + y > z && x + z > y && z + y > x)System.out.println("三角形");elseSystem.out.println("不是三角形");12、下面程序段的执行结果是(6 5 4 3 2)int a[] = { 2, 3, 4, 5, 6 };for (int i = a.length - 1; i >= 0; i--)System.out.print(a[i] + "");三、程序阅读题1、以下程序的输出结果为_Peter is 17 years old!___。
public class Person {String name;int age;public Person(String name, int age) { = name;this.age = age;}public static void main(String[] args) {Person c = new Person("Peter", 17);System.out.println( + " is " + c.age + " years old!");}}2、以下程序的输出结果为__课程号:101 课程名:ASP 学分:3___。
public class Course {private String cNumber;private String cName;private int cUnit;public Course(String number, String name, int unit) {cNumber = number;cName = name;cUnit = unit;}public void printCourseInfo() {System.out.println("课程号:" + cNumber + " 课程名:" + cName + " 学分:" + cUnit);}}class CourseTest {public static void main(String[] args) {Course c;c = new Course("101", "ASP", 3);c.printCourseInfo();}}3、以下程序的输出结果为__汤姆猫体重:20.0斤___。
public class Tom {private float weight;private static String name;public void setWeight(float weight) {this.weight = weight;}private void out() {System.out.println(name + "体重:" + weight + "斤");}public static void main(String[] args) { = "汤姆猫";Tom cat = new Tom();cat.setWeight(20);cat.out();}}4、以下程序的输出结果_姓名:Tom 年龄:15 家庭住址:金水区电话:66123456 学校:九中_。
public class Father {String name, address, tel;int age;public Father(String name, int age) { = name;this.age = age;}void out() {System.out.print("姓名:" + name);System.out.print(" 年龄:" + age);}void outOther() {System.out.print(" 家庭住址:" + address);System.out.print(" 电话:" + tel);}}class Son extends Father {String school;public Son(String name, int age) {super(name, age);}void out() {super.out();super.outOther();System.out.println(" 学校:" + school);}public static void main(String args[]) {Son son = new Son("Tom", 15);son.address = "金水区";son.school = "九中";son.tel = "66123456";son.out();}}5、下列程序的运行结果是__12345____。
public class MyClass {int a[] = { 1, 2, 3, 4, 5 };void out() {for (int j = 0; j < a.length; j++)System.out.print(a[j] + "");}public static void main(String[] args) {MyClass my = new MyClass();my.out();}}四、简答题1、简述Java中异常处理的机制?答:首先Java的异常是面向对象的。
一个Java的Exception是一个描述异常情况的对象.当出现异常情况时,一个Exception对象就产生了,并放到异常的成员函数里。
Java的异常处理是通过5个关键词来实现的:try,catch,throw,throws和finally。
1.异常处理:在Java语言的错误处理结构由try,catch,finally三个块组成。
其中try块存放将可能发生异常的Java语言,并管理相关的异常指针;catch块紧跟在try块后面,用来激发被捕获的异常;finally 块包含清除程序没有释放的资源,句柄等。