Java程序设计教程答案.doc_第1页
Java程序设计教程答案.doc_第2页
Java程序设计教程答案.doc_第3页
Java程序设计教程答案.doc_第4页
Java程序设计教程答案.doc_第5页
免费预览已结束,剩余25页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

项目一 认识Java一选择题1 D2 B3 D4 D二简答题1Java语言有三个技术分支,并且产生了三个版本的Java运行平台,分别是J2SE 标准版,主要用于开发桌面应用程序、低端服务器应用程序和Java Applet程序;J2EE 企业版,主要用于开发分布式网络程序,如电子商务网站和ERP系统等;J2ME 精简版,主要用于嵌入式系统开发,如移动电话、掌上电脑(PDA)以及其他无线设备。2Java应用程序和Java Applet程序。Java应用程序要依赖JRE或JDK中的解释器来运行,而Java Applet程序的调用命令嵌入在网页的HTML代码文件中,显示网页时由Web浏览器内置的Java解释器解释执行,并将其内容显示网页中。3Java虚拟机JVM是Java Virtual Machine的缩写。JVM是一个虚构出来的计算机,它有自己完善的硬件架构,如处理器、堆栈、寄存器等,还具有相应的指令系统。Java字节码文件包括的代码就是基于JVM的。4Java语言主要有简单、面向对象、分布式、健壮、安全、跨平台、解释执行、多线程和动态执行的特点。5开发Java应用程序时,首先使用任何文本编辑器创建和编辑Java源程序,保存源程序文件时,文件的扩展名为“*.java”。然后使用“javac 程序名.java”命令把源程序编译为字节码文件,最后使用“java 程序名”将字节码文件翻译为计算机可以理解的指令并运行。 开发Java小程序时,首先使用任意纯文本编辑器编写Java源程序和HTML文件,并分别以扩展名“.java”和“.html”保存。其中,HTML文件中应包含调用Java字节码文件的指令。HTML文件可以使用IE浏览器浏览,也可以使用JDK提供的Appletviewer工具浏览。三编程题1解答/Test.javaclass Person String name;String address;String telephone;Person(String name1, String address1, String telephone1) name = name1;address = address1;telephone = telephone1;String getName() return name;String getAddress() return address;String getTelephone() return telephone;public class Test public static void main(String args) Person lily = new Person(lily, 金企鹅18号,;System.out.println(姓名: + lily.getName();System.out.println(地址: + lily.getAddress();System.out.println(电话号码: + lily.getTelephone();2解答文件TestApplet.java与TestApplet.html都放在d盘根目录下。/TestApplet.javaimport java.applet.Applet;import java.awt.Graphics;public class TestApplet extends Applet public void paint(Graphics g) g.drawString(姓名:lily, 20, 20);g.drawString(地址:金企鹅18号, 20, 40);g.drawString(电话号码 20, 60);/TestApplet.htmlTestApplet项目二 Java编程基础一选择题1. C2. D3. C4. C5. B6. C7. B二简答题1、Java语言有byte(字节型)、short(短整型)、int(整型)、long(长整型)、float(单精度)、double(双精度)、char(字符型)、boolean(布尔型)8中基本类型。2、变量是指在程序运行过程中可以发生变化的量,它用于在程序运行时临时存放数据。常量是指在程序运行过程中不能改变的量,它用来记忆一个固定的数值。3、在参与运算的数据包含多种数据类型,或者赋值语句中左侧赋值变量和右侧表达式结果类型不同时,如果赋值变量的数据类型优先级低于表达式结果数据类型的优先级,或者两者同级,则表达式结果的数据类型必须强制转换为赋值变量的数据类型。4、break语句可以用在循环语句的内部,用来结束循环。continue语句只能用在循环语句内部,用来跳过本次循环,继续执行下一次循环。三编程题1、解答/ IsTriangle.javapublic class IsTriangle public static void main(String args) int a = 0, b = 0, c = 0;/定义三边长System.out.println(请输入三角形的三条边长:(注意:边长为整数));Scanner in = new Scanner(System.in);a = in.nextInt();b = in.nextInt();c = in.nextInt();if (a + b c & a + c b & b + c a) /判断是否两边之和大于第三边System.out.println(a + , + b + , + c + 三条边能组成三角形!); else System.out.println(a + , + b + , + c + 三条边不能组成三角形!);2、解答/ ComputeDay.javapublic class ComputeDay static final int SUN_SELL = 20;/定义晴天卖花的数量static final int RAIN_SELL = 12;/定义雨天卖花的数量public static void main(String args) int Flowers, sunDays = 0, rainDays = 0;System.out.println(请输入总共卖出的鲜花数:);Scanner in = new Scanner(System.in);Flowers = in.nextInt();/得到卖花的总数int days = Flowers / 14;/计算总共卖花的天数for (rainDays = 1; rainDays = days; rainDays+) sunDays = days - rainDays;if (SUN_SELL * sunDays + RAIN_SELL * rainDays = Flowers)break;System.out.println(一共有 + sunDays + 个晴天!);System.out.println(一共有 + rainDays + 个阴天!);项目三 Java面向对象程序设计一 选择题1 D2 C3 A4 B5 B6 D二 简答题1、如果用static关键字修饰成员变量,则该变量是一个类变量(又称静态变量)。不加static修饰的成员变量称为实例变量。类变量跟实例变量的区别是,第一次调用类的时候,系统为类变量分配一次内存。不管以后类创建多少个对象,所有对象都共享该类的类变量。声明实例变量之后,每次创建类的新对象的时候,系统就会为该对象创建实例变量的副本,即该对象每个实例变量都有自己的内存空间。2.、在方法体中声明的变量称为局部变量,它只能在方法体内使用。局部变量的作用域开始于它的声明处,结束于当前代码块结束处。如果没有声明代码块,则其开始于声明处,结束于方法体结束处。3、访问控制修饰符有4个等级:private、protected、public以及默认(即不指定修饰符)。用private修饰的变量只能被所属类访问;用protected修饰的变量能被同一包中的类以及不同包中的子类访问;用public修饰的变量能被所有类访问;默认修饰的变量只能被同一包中的类访问。4、多个方法共享一个名字,但方法参数的个数、类型或顺序会有所区别。5、this用来表示当前类。当成员变量的名字和局部变量的名字相同时,如果要在方法中访问成员变量,可以使用this关键字。在方法体中,通过this关键字可访问当前类的成员变量和方法。在类的构造方法中可以调用其他形式的构造方法。6、包是Java语言中有效管理类的一个机制。在程序中可以使用“import 包名.类名”的形式引用其他包中的类。三 编程题1、解答public class Box private int length;/定义长度private int width;/定义宽度private int height;/定义高度Box(int length, int width, int height) /构造方法,初始化成员变量this.length = length;this.width = width;this.height = height;public void showMessage() System.out.println(盒子的长是: + length);System.out.println(盒子的宽是: + width);System.out.println(盒子的高是: + height);public static void main(String args) Box box = new Box(1, 5, 7);/创建对象box.showMessage();/调用显示属性的方法2、解答import java.util.Scanner;public class Compute static int x, y;/定义两个数Compute(int x, int y) /构造方法,初始化成员变量Compute.x = x;Compute.y = y;void add() /定义加法方法System.out.println(两数的和为: + (x + y);void sub() /定义减法方法System.out.println(两数的差为: + (x - y);void Multiplication() /定义乘法方法System.out.println(两数的积为: + (x * y);void division() /定义除法方法System.out.println(两数的商为: + (x / y);public static void main(String args) System.out.println(请输入两个数:(注意:中间以空格分隔));Scanner in = new Scanner(System.in);Compute data = new Compute(in.nextInt(), in.nextInt();/创建对象data.add();data.sub();data.Multiplication();data.division();项目四 类的深入解析一 选择题1、 D2、 D3、 D4、 C二 简答题1、方法的覆盖是指子类定义与父类名字相同、参数相同、返回类型相同的方法。重载方法是指在类中定义同名的几个方法,但方法的参数、类型和个数要有所区别。2、多态性可以用“一个对外接口,多个内在实现方法”来表示。我们可以在一个类中定义多个同名方法,程序在调用某个方法时,系统会自动根据参数类型和个数的不同调用不同的方法。方法的覆盖和方法的重载是实现多态的两种形式。3、定义类和方法时在声明时用abstract关键字修饰则就称为了抽象类和抽象方法。抽象方法只能在抽象类中定义,而抽象类中除了可以定义抽象方法,还可以定义一般的变量和方法。4、接口的定义包括接口声明和接口体。定义接口的形式如下:interface 接口名 extends 父接口名列表/常量声明/抽象方法声明为了声明一个类来实现一个接口,在类的声明中要包括一条implements语句,可以在implements后面列出要实现的多个接口,这些接口名称之间应以逗号分隔。5、接口中只能定义抽象方法,而抽象类中可以定义非抽象方法。接口中只能定义public static final类型常量,而不能包含成员变量。抽象类则不然,它可以包含各种形式的成员变量和常量。类可以继承(实现)多个接口,但只能继承一个抽象父类。三编程题1、解答public class Employee extends Person /定义子类private String office;/定义办公室private double salary;/定义薪水private int date;/定义入职日期Employee(String office, double salary, int date) /构造方法super();this.office = office;this.salary = salary;this.date = date;void showMessage() System.out.println(办公室: + office);System.out.println(入职日期: + date);System.out.println(薪水: + salary);public static void main(String args) Person xiaoli = new Person(xiaoli, 北京市海淀区大钟寺,xiaoli_111163.com);/创建父类对象xiaoli.showMessage();/调用父类方法Employee xiaowang = new Employee(事业部, 2500, 20100611);/定义子类对象xiaowang.showMessage();/调用子类方法class Person /定义父类private String name;/定义姓名private String address;/定义地址private String telephone;/定义电话号码private String email;/定义邮箱Person() Person(String name, String address, String telephone, String email) = name;this.address = address;this.telephone = telephone;this.email = email;void showMessage() System.out.println(姓名: + name);System.out.println(地址: + address);System.out.println(电话号码: + telephone);System.out.println(邮箱: + email);2、解答interface Graphices / 计算面积的抽象方法,分号;必不可少public abstract double area();abstract class PlaneGraphics protected String shape; / 形状/ 构造方法,将形状类型字符串赋予变量shapepublic PlaneGraphics(String shape) this.shape = shape;/ 无参构造方法,图形类型为未知图形public PlaneGraphics() this(未知图形);public void print() / 设计长方形类Rectangle,继承平面图形类并实现图形接口class Rectangle extends PlaneGraphics implements Graphices protected double length; / 长度protected double width; / 宽度/ 长方形构造方法public Rectangle(double length, double width) super(长方形);this.length = length;this.width = width;/ 正方形构造方法,正方形是长方形的特例public Rectangle(double width) super(正方形);this.length = width;this.width = width;/ 无参构造方法,将length和width均赋0,此时图形形状为未知图形public Rectangle() / 计算长方形面积,实现父类的抽象方法public double area() return width * length;public void print() System.out.println(shape + 面积为 + this.area();/ 设计椭圆类Eclipse,继承平面图形类并实现图形接口class Eclipse extends PlaneGraphics implements Graphices protected double radius_a; / a轴半径protected double radius_b; / b轴半径/ 椭圆构造方法public Eclipse(double radius_a, double radius_b) super(椭圆);this.radius_a = radius_a;this.radius_b = radius_b;/ 圆构造方法,圆是椭圆的特例public Eclipse(double radius_a) super(圆);this.radius_a = radius_a;this.radius_b = radius_a;/ 无参构造方法,将radius_a和radius_b均赋0,此时图形形状为未知图形public Eclipse() / 计算椭圆的面积,实现父类的抽象方法public double area() return Math.PI * radius_a * radius_b;public void print() System.out.println(shape + 面积为 + this.area();/ 设计三角形类Triangle,继承平面图形类并实现图形接口class Triangle extends PlaneGraphics implements Graphices protected double bottom;protected double height;Triangle(double bottom, double height) super(三角形);this.bottom = bottom;this.height = height;public double area() return 0.5 * bottom * height;public void print() System.out.println(shape + 面积为 + this.area();public class PlaneGraphics_ex public static void main(String args) / 获得长方形子类实例PlaneGraphics g = new Rectangle(10, 20);/ 调用抽象类中的print()方法g.print();g = new Eclipse(10, 20); / 椭圆g.print();g = new Triangle(10, 20);/三角形g.print();项目五 数组和字符串一 选择题1、B2、B3、B4、D5、B6、D二 简答题1、创建数组分为数组声明和数组初始化,声明一个数组指定了数组名和元素的数据类型,并未指定数组元素的个数和初始值。数组的初始化分为静态初始化和动态初始化,静态初始化是指声明的同时为数组元素赋值;动态初始化是指利用new运算符为数组分配内存空间。2、使用“数组名下标值”的形式访问数组元素,但要注意的是下标值是从0开始的。3、使用String类的valueOf方法可将byte、int、long、float、double等类型的数值转换为字符串。4、,String类的对象一旦被初始化,它的值和所分配的内存就不能被改变了。如果想要改变它的值,则必须创建一个新的String对象。StringBuffer类用于创建和操作动态字符串,为该类对象分配的内存会自动扩展以容纳新增的文本,StringBuffer类适合于处理可变字符串。三 编程题1、解答public class NumberSort public static void main(String args) int array = 3, 1, 6, 2, 9, 0, 7, 4, 5, 8 ;int temp; / 存储交换的变量值for (int i = 0; i array.length - 1; i+) / 比较n-1轮for (int j = 0; j arrayj + 1) temp = arrayj;arrayj = arrayj + 1;arrayj + 1 = temp;System.out.println(数组中最大值为: + arrayarray.length - 1);System.out.println(数组中最小值为: + array0);System.out.println(最大值与最小值的差为: + (arrayarray.length - 1 - array0);2、解答public class Number public static void main(String args) int array = 3, 1, 6, 2, 9, 0, 7, 4, 5, 8 ;for (int i = array.length - 1; i = 0; i-) / 比较n-1轮System.out.print(arrayi+ );3、解答public class NumberSort public static void main(String args) int array = new int34;System.out.println(array.length);for (int i = 0; i array.length; i+) System.out.print(arrayi.length + );4、解答public class StringChange public static void main(String args) StringBuffer ss = new StringBuffer(abetsea);ss.replace(0, 3, adc);ss.insert(1, hello);System.out.println(ss);项目六 异常处理一 选择题1、 D2、 B3、 B4、 D二 简答题1、在Java程序运行期间,如果出现了异常事件,系统会自动生成一个异常对象。当出现这样的异常时,总是由虚拟机接管。出现运行时异常后,系统会把异常一直往上层抛,一直遇到处理代码。如果没有处理代码,到最上层后,如果程序是多线程就由Thread.run() 抛出 ,如果程序是单线程就被main() 抛出。抛出之后,如果是线程,这个线程也就退出了;如果是主程序,那么整个程序也就退出了。2、对于异常的处理,我们可以在方法体中增加try-catch语句,从而告知系统,当try代码块出现异常时,利用catch语句捕捉异常,并利用catch代码块对异常加以处理,从而让程序能继续运行。为方法名增加throws声明,抛出一些异常。如果用户能够明确预测到异常,则可通过在程序中增加throw语句,让程序自身而不是系统来抛出异常。3、如果希望在程序退出异常处理代码时对程序进行一些统一处理,可以将必须执行的代码放在finally代码块中。不一定需要finally语句。4利用throw语句可以明确地抛出一个异常。而对于方法中可能出现的异常,如果不想在方法中进行捕获,可以在方法声明时利用throws声明进行抛出。三 编程题1、解答public class Loan private double total;/定义总额private double rate;/定义利率private int year;/定义年数Loan(double total, double rate, int year) this.total = total;this.rate = rate;this.year = year;public static void main(String args) Loan la = new Loan(2025.6, 0, 10);if (la.rate = 0 | la.total = 0 | la.year = 0) throw new IllegalArgumentException();/抛出IllegalArgumentException异常2、解答import java.util.InputMismatchException;import java.util.Scanner;public class ElectricityFree double free;/定义电费double computeFree(double count) if (count 240 & count 540) free = count * 0.45;return free;public static void main(String args) System.out.println(请输入用电度数:);Scanner in = new Scanner(System.in);boolean dataright = false;double count = 0.0;do try count = in.nextDouble();if (count = 0) /抛出自定义异常throw new ElectNotNegativeException();dataright = false;ElectricityFree ef = new ElectricityFree();puteFree(count);System.out.println(本月电费和用电量的比值为: + (ef.free / count); catch (InputMismatchException e) System.out.println(你输入的用电数不合法,请重新输入:);dataright = true;String x = in.nextLine(); catch (ElectNotNegativeException ee) System.out.println(你输入的用电数不能小于等于0,请重新输入:);dataright = true;String x = in.nextLine(); while (dataright);class ElectNotNegativeException extends Exception /定义用电度数小于等于零时异常ElectNotNegativeException() 项目七 数据输入输出一 选择题1、 C2、 A3、 AB4、 AD5、 A6、 A7、 B二 简答题1、 字节流以字节的形式读取数据,字符流以字符的形式读取数据。2、 创建字节或字符输入/输出流对象,若创建的是文件字节/字符输入/输出流对象,此时文件自动打开或创建;读写数据; 关闭数据流,同时关闭了文件。3、 File类的构造方法File(String pathname) File(String parent, String child) File(File parent, String child)。查询文件名和路径名的方法:String getName(), String getPath()。抽象路径查询的方法:boolean exists(),boolean isDirectory(),boolean isFile()文件与目录操作的方法:boolean cteateNewFile(),boolean mkdir(),boolean delete(),String list()。三编程题1、 解答import java.io.*import java.util.Scanner;public class FileInputTestpublic static void main(String args) throws IOException System.out.println(请输入字符串:);Scanner in = new Scanner(System.in);String line = in.nextLine();line = line.toUpperCase();/将输入的字符串转换为大写字母System.out.println(line);FileWriter out = new FileWriter(d:test.txt);out.write(line);out.close();2、 解答import java.io.*;public class FileWrite public static void main(String args) throws IOException File file = new File(d:test.txt);BufferedOutputStream bufout = null;try if (!file.exists() file.createNewFile(); else bufout = new BufferedOutputStream(new FileOutputStream(file);DataOutputStream da_out = new DataOutputStream(bufout);for (int i = 0; i 100; i+) int ram = (int) (Math.random() * 10);da_out.writeInt(ram);bufout.close(); catch (IOException e) System.out.println(e);项目八 Java的多线程机制一、选择题1、 D2、 D3、 D4、 D5、 D6、 B二、简答题1、进程是程序的一次动态执行的过程。一个进程既包括程序的代码,同时也包括了系统的资源,如CPU、内存空间等,但不同的进程所占用的系统资源都是独立的。线程是比进程更小的执行单位。一个进程中可以运行多个线程,线程可以共享进程的系统资源,进程不能脱离线程独立运行。2、线程的生命周期分为创建、就绪、运行、阻塞、死亡五种状态,新建的线程调用start()方法便处于就绪状态;就绪状态的线程获得CPU资源后即可处于运行状态,系统将自动调用run()方法。线程调用sleep()方法或缺少某些资源会使线程处于阻塞状态。当线程运行完run()方法或被强制终止时,线程处于死亡状态。3、创建线程一种方式是继承线程类Thread,另一种方法是实现Runnable接口。4、线程调度的sleep()方法使线程处于休眠,使线程状态转向阻塞状态。yield()方法使线程让步,但只让给高优先级或同等优先级的线程,线程转向就绪状态。join()方法是当前线程运行结束才执行其他线程。5、为了解决资源共享的问题,线程中引入了同步机制。只要在方法的声明中使用synchronized关键字修饰,该方法就称为同步方法,可以实现对共享资源进行加锁,避免了多线程之间共享资源的竞争问题。三编程题1、 解答/ TicketThreadMain.javapublic class TicketThreadMain implements Runnable static Thread MrZhang, MrLi, MrWang;static TicketThread MissWang;public void run() if (Thread.currentThread() = MrZhang) / 判断当前的线程MissWang.sellRegulate(20); / 调用买票的方法 else if (Thread.currentThread() = MrLi) MissWang.sellRegulate(5); else if (Thread.currentThread() = MrWang) MissWang.sellRegulate(10);public static void main(String args) TicketThreadMain t = new TicketThreadMain();MissWang = new TicketThread();MrZhang = new Thread(t);MrWang = new Thread(t);MrLi = new Thread(t);MrZhang.start(); / 启动张先生的线程MrWang.start();/启动王先生的线程MrLi.start(); / 启动李先生的线程class TicketThread int sumFive = 3, sumTwenty = 0, sumTen = 0; / 定义5元钱、10元钱与20元钱的个数public synchronized void sellRegulate(int money) if (money = 5) sumFive = sumFive + 1;System.out.println(先生,您给的钱数正好。); else if (money = 10) while (sumFive 1) try wait(); / 如果5元的个数少于2张,则线程等待 catch (InterruptedException e) sumFive = sumFive - 1;sumTen = sumTen + 1;System.out.println(先生,您给我10元,找您5元。); else if (money = 20) while (sumFive 3) try wait(); / 如果5元的个数少于3张,则线程等待 catch (InterruptedException e) sumFive = sumFive - 3;sumTwenty = sumTwenty + 1;System.out.println(先生,您给我20元,找您15元。);notifyAll(); / 通知等待的线程2、 解答/ PrimeNumberMain.javaclass PrimeNumberOne extends Thread public void run() int n, i;for (n = 2; n = 1000; n+) for (i = 2; i = n; i+) if (n % i = 0) break;if (i = n) System.out.print(n + );System.out.println();class PrimeNumberTwo extends Threadpublic void run() int n, i;for (n = 1000; n = 2000; n+) for (i = 2; i = n; i+) if (n % i = 0) break;if (i = n) System.out.print(n + );System.out.println();public class PrimeN

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论