Java 编写一个表示风扇Fans的类,集合中成员数量表示变量有:

编程题 1.编写一个学生类Students,该类成员变量包括学号no、姓名 name、性别sex和_百度知道
编程题 1.编写一个学生类Students,该类成员变量包括学号no、姓名 name、性别sex和
1.编写一个学生类Students,该类成员变量包括学号no、姓名 name、性别sex和年龄age,该类的成员方法有getNo()、getName()、getSex()、getAge()、和setAge()。添加构造方法为所有成员变量赋初值,构造方法要有4种格式:(1)包括no、name、sex和a...
若以下回答无法解决问题,邀请你更新回答
我有更好的答案求助知道网友
1、先安装程序编译软件。2、打开程序编译器,编写代码。3、当程序编写、调试完成,就可以编译为可执行程序。怎么编写程序?(C++程序怎么编写?):也许你已经学了C++,也许你正在犹豫要不要学习编程语言,或者正在纠结到底该学哪门编程语言。在徘徊犹豫的此刻,你也许更想知道怎么编写一个简单的程序,好吧!就让我们一起来看看吧!1.在这里我们借助的软件是Visual C++6.0。在文章的后面有他的下载链接。打开软件,先新建一个工程,在新建一个C++源文件,这些你听起来可能有点陌生,不要担心,你只需要按照下面的图片进行操作就好。2.在建好文件之后,我们一起来动手编写属于自己的第一个C++程序吧!3.在源文件处,输入下列代码#include&iostream&void main(){cout&&&我的第一个程序!&&&}4.然后我们编译这个程序,点击图中右上角有红色边框的按钮。5.最后运行这个程序,点击图中右上角的红色框里的按钮,看看运行结果吧!
package com.public class Students {
public String getNo() {
public void setNo(String no) {
public String getName() {
public void setName(String name) {
this.name =
public char getSex() {
public void setSex(char sex) {
this.sex =
public int getAge() {
public void setAge(int age) {
this.age =
public Students(String no){}
public Students(String no,String name){}
public Students(String no,String name,char sex){}
public Students(String no,String name,char sex,int age){}}
本回答被提问者和网友采纳
怎么没反应
2条折叠回答
为您推荐:
其他类似问题
成员变量的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。您现在的位置: >> java实验报告格式 >> java实验报告
java实验报告
来源: 时间:
【java实验报告】实验报告实验一:安装与熟悉 J2SDK实验目的:1. 了解和使用 J2sdk(JDK)环境; 2. 熟悉 J2sdk 集成环境的基本命令、功能和基本用法,并学会配置 path 等环 境变量; 3. 学习并熟练掌握使用 J2SDK 环境进行 Java 程序开发的全过程(包括:源程 序的编辑、编译、调试、运行和结果查看) ;4. 掌握 Java 应用程序的构成特点,并熟悉基本的错误调试。实验内容1. J2SDK 开发包的安装(可选) 2. 根据要求写出统环境变量的配置 3. 编写一个 JAVA 程序进行调试,掌握在 JDK 开发包支持下编译和运行的过程实验数据记录及分析(或程序及运行结果)1. 调试下面程序,指出错误 public Class HelloWorldApp { public void static main(string[] args) { system.out.println(“Hello World!”); } } 错误 1:class 的首字母“c”应小写 错误 2:void static 应为 static void错误 3:string 的首字母“s”应大写 错误 4:system 的首字母“s”应大写2. 写出调试过程编译和执行的具体命令 1) 上述文件保存后的文件名是:HelloWorldApp.java2) 编译命令:javac HelloWorldApp.java河南财经政法大学计算机与信息工程学院第 1 页 实验报告3) 编译正确后,产生的另外一个文件全称是:HelloWorldApp.class4) 运行该程序的命令:java HelloWorldApp3. 如果上面的编译命令中的 HelloWorldApp.java 换成以下格式, 写出执行 编译结果javac helloworldapp.java 编译可以成功,若执行的是 java helloworldapp 则 could not found the main class helloworldapp; 若执行 java HelloWorldApp 则可以运行,运行结果为 Hello World! javac HelloWorldApp 编译错误:仅当显示请求注释处理时才接受类名称“HelloWorldApp”4. 如果上面的运行命令改成以下格式,写出执行结果,如有错误请改正。java helloWorldApp.java 找不到 helloWorldApp。Exit java helloworldapp 找不到 helloworldapp。Exit java HelloWorldApp.class 不能执行。class 不能写5. 输入下面的随机数生成程序,重复上述编译和执行过程,分步写出两个命令 执行过程。import java.util.R public class MathDemo { public static void main(String[] args) { Random rand=new Random(); int v=rand.nextInt(); System.out.println(&你得到了一个&+v); } }河南财经政法大学计算机与信息工程学院第 2 页 实验报告1) 保存文件名为:MathDemo.java 2) 编译命令为:javac MathDemo.java 3) 运行程序命令为:java MathDemo 4) 如果删除该程序的第一行 import 语,重新编译,有何问题? MathDemo.Java:4:找不到符号 符号类 R 位置类 MathDemo Random rand=new Random(); 2 错误 需要加载类在能运行函数评语:日期:年 月 日河南财经政法大学计算机与信息工程学院第 3 页 实验报告实验二:Eclipse 开发环境实验目的:了解最基本的环境配置过程。2. 掌握 Eclipse 环境下的 Java 程序开发过程。1.实验数据记录及分析(或程序及运行结果)1. 在 Eclispe 环境下创建一个项目 MyExexercise,添加一个包 demo,向包 内添加一个类 CommandParameter,配置运行参数,使得程序能够逐个输 出命令行参数 的要求。1) 自动生成的 CommandParameter 程序的第一行是什么?package demo; public class CommandParameter {}2) 在何处配置运行时的命令行参数? public static void main(strings[] args) 2. 在何处调整项目缺省的字符集。windows--preferences--general--workspace--text file encoding--other3. 在何处可以调整代码编辑框内的文字大小,写出操作指南。Window》preferences》Appearances》colors and fonts》java》改字体 4. 如果 console 窗口关闭了,如何操作才能重新显示出来? Window\show view\console 5. 写出设置断点的步骤。在 java 视图下,在要设置断点的那行的最左边双击6. 如何查看运行过程中各个变量的值。评语:日期:年 月日河南财经政法大学计算机与信息工程学院第 4 页 实验报告实验三:数值类型与变量声明实验目的:掌握标识符的定义 认识 Java 的数据类型以及掌握各种变量的声明方法。3. 掌握不同数值类型间的赋值问题 1. 2.实验内容 1. 编写一个 JAVA 程序,对不同类型的类型进行变量的定义并输出相应的值。实验数据记录及分析(或程序及运行结果)public class Variable { public static void main(String[] args) { int a=10; System.out.println(&a=&+a); } } 1. 写出程序运行的结果 a=10 2. 修改上述程序,定义一个字符型变量,并定义其初值为 100,然后输出该变 量的值,在下面写出修改后的代码片。public class Variable { public static void main(String[] args) { char a=100; System.out.println(&a=&+(int)a); } } a=1003. 修改上述程序,定义一个字符型变量,并定义其初值为 65536,然后输出该 变量的值,在下面写出修改后的代码片段,如果有错误,说明原因。public class Variable { public static void main(String[] args) { char a=65536; System.out.println(&a=&+(int)a); } }河南财经政法大学计算机与信息工程学院第 5 页 实验报告错误:Exception in thread &main& java.lang.ErrorUnresolved compilation problemType mismatchcannot convert from int to char at Variable.main(Variable.java:4)Int 型的 65536 不能自动转换成位数少的 char 型4. 修改上述程序,定义一个单精度类型的变量,并定义其初值为 10.0,然后输 出该变量的值,在下面写出修改后的代码片段。public class Variable { public static void main(String[] args) { float a=10.0f; System.out.println(&a=&+a); } } a=10.05. 修改上述程序,定义一个双精度类型的变量,并定义其初值为 10.0,然后输 出该变量的值,在下面写出修改后的代码片段。public class Variable { public static void main(String[] args) { double a=10.0; System.out.println(&a=&+a); } } a=10.06. 替换下面的代码段到上述程序,分析现象。float f=10.0f; double d=100.0d; f=d; System.out.println(&f=&+f);无法运行。Type mismatchcannot convert from double to float 7. 替换下面的代码段到上述程序,分析结果。int a=10; char c='a'; a=c+a; System.out.printf(&c=%c&,a); C=k 不同数据类型的数据做运算时可自动转换成位数少的数据河南财经政法大学计算机与信息工程学院第 6 页 实验报告8. 声明一个布尔类型的变量,其初值为真。boolean truth=true9. 替换下面的代码段到上述程序,分析现象。Variable v=new Variable(); System.out.println(&v=&+v);v=Variable@de6ced Variable是v的类型,@de6ced是v在内存中的代码5评语:日期:年 月日河南财经政法大学计算机与信息工程学院第 7 页 实验报告实验四:运算符和表达式实验目的:熟悉运算符的作用、优先级和结合方向 2. 认识 Java 的表达式,理解表达式的构成、类型 1.实验数据记录及分析(或程序及运行结果)1. 运行下面的程序,写出运行结果 public class ArithmaticOp{ public static void main(String args[ ]){ int a=5+4, b=a*2, c=b/4; int d=b-c; int e=-d; int f=e%4; double g=18.4, h=g%4; int i=3; int j=i++; int k=++i; System.out.println(&a=&+a); System.out.println(&b=&+b); System.out.println(&c=&+c); System.out.println(&d=&+d); System.out.println(&e=&+e); System.out.println(&f=&+f); System.out.println(&g=&+g); System.out.println(&h=&+h); System.out.println(&i=&+i); System.out.println(&j=&+j+&\nk=&+k); } }a=9 b=18 c=4 d=14 e=-14 f=-2 g=18.4 h=2.9986 i=5 j=3 k=5河南财经政法大学计算机与信息工程学院第 8 页 实验报告2. 写一个程序, 计算 32/5 的结果,如果需要得到浮点数的结果该如何重写 表达式。分别写出这两个赋值表达式。public class Acd { public static void main(String args[ ]){ int a=32, b=5; System.out.println(&c=&+a/b); System.out.println(&d=&+(float)a/b); } }3. 创建一个类名为 GravityCalculator 的类,在该类中写上以下代码, public static void main(String[] arguments){ double gravity =9.81; // 重力速度, m/s^2 double initialVelocity = 0.0; double fallingTime = 10.0; double initialPosition = 0.0; double finalPosition = 0.0; System.out.println(&The object's position after & + fallingTime +& seconds is & + finalPosition + & m.&); } 运行此程序,输出运行结果。The object's position after 10.0 seconds is 0.0 m.4. 修改上述例子程序,完成以下功能:计算 10 秒钟后物体的位置。输出位 置以米为单位,计算的数学公式为:x(t) = 0.5 × gt2 + vit + xi 变量 作用 值 g 加速度 (m/s2) 9.81 t 时间 (s) 10 vi 初速度 (m/s) 0 xi 初始位置 0public class GravityCalculator { public static void main(String[] arguments){ double double double double g=9.81; // 重力速度,单位 m/s^2 v= 0.0; t = 10.0; x= 0.0;第 9 页河南财经政法大学计算机与信息工程学院 实验报告System.out.println(&The object's position after & + t +& seconds is & + (0.5*g*t*t+v*t+x) + & m.&); } }评语:日期:年 月日河南财经政法大学计算机与信息工程学院第 10 页 实验报告实验 05:输入/输出处理 实验目的:掌握输入输出程序的处理。实验数据记录及分析(或程序及运行结果)1. 编写程序, 从键盘上输入一个整数作为圆的半径, 分别计算并输出圆周长、 圆面积。package Wo; import java.util.S public class Yuan { public Yuan(int r, double grith, double area) { super(); this.r = this.grith =area = } public static void Jisuan(int a){double b=2*Math.PI*a; double c=Math.PI*a*a; System.out.println(&周长=&+b); System.out.println(&面积=&+c); } public static void main(String[] args){ System.out.print(&r=&); Scanner sc=new Scanner(System.in); int r=sc.nextInt(); Jisuan(r); } }2. 编写程序,从键盘上输入三个实数,比较大小,分别输出最大值和最小值。package Wo; import java.util.S public class Compare { public static void Bijiao(double a,double b,double c){ if(a&b)Change(a,b); if(b&c)Change(b,c); System.out.print(&最大数=&+a);河南财经政法大学计算机与信息工程学院 第 11 页 实验报告System.out.print(&最小数=&+c); } public static void Change(double d,double e){ t=e;e=d;d=t; } public static void main(String args[]){ System.out.print(&x=&); Scanner sc=new Scanner(System.in); double x=sc.nextDouble(); System.out.print(&y=&); double y=sc.nextDouble(); System.out.print(&z=&); double z=sc.nextDouble(); Bijiao(x,y,z); } }不对 change只是值传递,a.b3.运行下面的程序,写出运行结果,解释每种格式的含义。import java.util.C public class CommandParameter { public static void main(String[] args) { long n = 461012; System.out.printf(&%d%n&, n); System.out.printf(&%08d%n&, n); System.out.printf(&%+8d%n&, n); System.out.printf(&%,8d%n&, n); System.out.printf(&%+,8d%n%n&, n); double pi = Math.PI; System.out.printf(&%f%n&, pi); System.out.printf(&%.3f%n&, pi); System.out.printf(&%10.3f%n&, pi); System.out.printf(&%-10.3f%n&, pi); Calendar c = Calendar.getInstance(); System.out.printf(&%tB %te, %tY%n&, c, c, c); System.out.printf(&%tl:%tM %tp%n&, c, c, c); System.out.printf(&%tD%n&, c); }} 6 461,012 +461,012河南财经政法大学计算机与信息工程学院第 12 页 实验报告3..142 3.142 3.142 七月 5,
上午 07/05/11评语:日期实验 06:流程控制(一)年月 日实验目的1. 2. 能够正确使用 if,switch 语句,并且能正确使用合法的参数类型。在不同情况下, 能够正确选择使用循环语句, 能正确使用 break,continue, 能计算在循环中或循环后循环计数器的值。实验数据记录及分析(或程序及运行结果) 编写程序,声明一个 double 型变量,并任意赋值,然后判断该书是否在 1 到 10000 之间,根据结果输出下面其中一行结果(x 需用实际的数值代替)The number x is between 1 and 10000. The number x is not between 1 and 10000.import java.util.R public class Panduan { public static void main (String[] args){ Random rand=new Random(); int x=rand.nextInt(); System.out.print(x); if(x&1&&x&10000) System.out.print(&The number x is between 1 and 10000.&); else System.out.print(&The number x is not between 1 and 10000.&); } }分析下面程序,写出运行结果 class TestApp{ public static void main(String[] args){ for(int i=0;i&10;i++){ if(i==3) System.out.print(i);河南财经政法大学计算机与信息工程学院 第 13 页 实验报告} } }012分析 switch 的作用,写出下面程序运行的结果class SwitchDemo{ public static void main(String args[]) { int c=38; switch (c&10?1:c&25?2:c&35?3:4) { case 1System.out.println(& &+c+&℃ 有点冷。要多穿衣服。&); case 2System.out.println(& &+c+&℃ 正合适。出去玩吧。&); case 3System.out.println(& &+c+&℃ 有点热。&); defaultSystem.out.println(& &+c+&℃ 太热了!开空调。&); } } 1) 当 c=38 时,运行结果是什么?38℃ 太热了!开空调。2) 当 c=8 时,运行结果是什么? 8℃ 有点冷。要多穿衣服。8℃ 正合适。出去玩吧。8℃ 有点热。8℃ 太热了!开空调。3) 当 c=48 时,运行结果是什么? 48℃ 太热了!开空调。4) 在每个 case 的输出语句后,添加 break 语句后,充分执行上述几种情况,分析 有何变化,分析写出每次运行结果。当 c=38 时,运行结果是 38℃ 太热了!开空调。当 c=8 时,运行结果是 8℃ 有点冷。要多穿衣服。当 c=48 时,运行结果是 48℃ 太热了!开空调。写出下面的代码段执行后,j 的值是多少? int i=1, j=0 switch(i) { case 2j+=6; case 4j+=1; defaultj +=2; case 0:河南财经政法大学计算机与信息工程学院 第 14 页 实验报告j +=4; }public class Test { public static void main(String[] args){ int i=1, j=0; switch(i) { case 2j+=6; case 4j+=1; defaultj +=2; case 0j +=4 System.out.print(i+&\n&); System.out.print(j); } } } i=1,j=6写出下列程序片段中 while 循环体重复执行的次数 int a = 1, b = 1; while ( a &= 1000 ){ a = 2 * b = b + 1; } System.out.println( b ); 循环了 10 次 编写一个程序,输出 1 到 200 之间所有能被 3 整除的整数,要求输出时每行 输出 5 个整数。public class Chuthree { public static void main(String[] args){ int count=0; for(int i=1;i&=200;i++) if(i%3==0) {System.out.print(i+& &);count++; if(count%5==0) System.out.println(&\n &); } } }编写一个程序,输出一个 99 乘法表,输出格式如下。河南财经政法大学计算机与信息工程学院 第 15 页 实验报告1× 1=1 1× 2=2 2× 2=4 1× 3=3 2× 3=6 3× 3=9 1× 4=4 2× 4=8 3× 4=12 4× 4=16 1× 5=5 2× 5=10 3× 5=15 4× 5=20 5× 5=25 1× 6=6 2× 6=12 3× 6=18 4× 6=24 5× 6=30 6× 6=36 1× 7=7 2× 7=14 3× 7=21 4× 7=28 5× 7=35 6× 7=42 7× 7=49 1× 8=8 2× 8=16 3× 8=24 4× 8=32 5× 8=40 6× 8=48 7× 8=56 8× 8=64 1× 9=9 2× 9=18 3× 9=27 4× 9=36 5× 9=45 6× 9=54 7× 9=63 8× 9=72 9× 9=8public class Chengfa { public static void main(String[] args){ for(int i=1;i&10;i++) for(int j=1;j&=i;j++) {System.out.print(j+&*&+i+&=&+i*j+& &); if(i==j) System.out.print(&\n&); } } }下面输出素数的程序原意输出为下面的结果, 但现在不是, 请调试下面程序, 使之能够按要求输出:2 13 31 53 73 3 5 7 23 43 67 89 11 29 47 71 97 17 19 37 41 59 61 79 83public class Prime { public static void main(String[] args) { boolean isPrime=//用来标识每次计算的数是否是素数,如果不是则为 for (int x = 2; x &= 100; x++) { int m = x / 2; for (int i = 2; i &= i++) { if (x % i == 0) isPrime=// } if(isPrime){ System.out.print(x+&\t&); } if(n==5){ System.out.println(); } }河南财经政法大学计算机与信息工程学院第 16 页 实验报告} } public class Prime { public static void main(String[] args) { boolean isPrime=//用来标识每次计算的数是否是素数,如果不是则为 int n=0; for (int x = 2; x &= 100; x++) { int m = x / 2; for (int i = i &1; i--) { if (x % i == 0) {isPrime=} else isPrime= } if(isPrime){ System.out.print(x+&\t&); n++; } if(n%5==0) System.out.println(); } } }评语:日期:年月日实验 07:流程控制(二)实验目的1. 进一步巩固对分支、循环结构的掌握,并简单了解有关方法的使用 实验数据记录及分析(或程序及运行结果) 分析程序 class TestApp{ public static void main(String[] args){ System.out.println(multiply(2,3,4,5)); } public int multiply(int nums){ int result = 1; for(int x :nums) result *=河南财经政法大学计算机与信息工程学院 第 17 页 实验报告} } 能否正常运行?原因是什么? 不能。Multiply 方法中只有一个参数,main 方法中的调用也只能有一个参数。Multiply 方法要有“static” 。For 语句不正确public class TestApp { public static void main(String[] args){ System.out.println(multiply(2)); System.out.println(multiply(3)); System.out.println(multiply(4)); System.out.println(multiply(5)); } public static int multiply(int int result = 1; for(int x=1 ;x&=x++) result *= } } nums){编写程序,接受命令行传入的的一个参数,能够统计其中大写字母、小写 字母、数字、空格及其他字符的个数,并且分别输出统计数据,如果碰到了”\n” 则终止统计。提示声明并出初始化一个字符串变量:如 String s=”hello\nMike”; s.charAt(0)表示取该字符串的第 1 个字符 s.charAt(1) 取第 2 个字符 e,其他依次类推; s.length()返回该字符串的字符个数,如”hello”的字符个数是 5。程序:public class CopyOfNum { public static void main(String[] args){ String s=&hello\nMike&; int xiaoxie=0,daxie=0,number=0,kongge=0,qita=0; for(int i=0;i&s.length();i++){ if(s.charAt(i)&='z'&& s.charAt(i)&='a') xiaoxie++; else if(s.charAt(i)&='Z'&& s.charAt(i)&='A') daxie++; else if(s.charAt(i)&='9'&& s.charAt(i)&='0') number++; else if(s.charAt(i)==' ') kongge++; else if(s.charAt(i)=='\\'&&s.charAt(i++)=='n') else qita++; } System.out.println(&小写字母个数为:&+xiaoxie); System.out.println(&大写字母个数为:&+daxie);河南财经政法大学计算机与信息工程学院第 18 页 实验报告System.out.println(&数字个数为:&+number); System.out.println(&空格个数为:&+kongge); System.out.println(&其他:&+qita); } }利用 java.util.Random 类,编写一个程序,每次执行时生成一个 1~6 之间(含 1 和 6)的整数。Random 类的用法如下面代码段所示。Ramdom rand = new Random( ); //第一步要创建一个随机数生成对象 int x = rand.nextInt(5)+1; //第二步 rand.nextInt(5)生成一个 0 到 5 之间的数字,然后简单加 1,调整到 1 到 6 之间,以满足需要:程序:import java.util.R public class Rand { public static void main(String[] args){ Random rand = new Random( ); int x = rand.nextInt(5)+1; System.out.println(x); } } //第一步要创建一个随机数生成对象利用 java.util.Random 类,编写一个程序,每次执行时生成一个 1~6 之间(含 1 和 6)的整数。Random 类的用法如下面代码段所示。Ramdom rand = new Random( ); //第一步要创建一个随机数生成对象 int x = rand.nextInt(5)+1; //第二步 rand.nextInt(5)生成一个 0 到 5 之间的数 字,然后简单加 1,调整到 1 到 6 之间,以满足需要。写一个程序, 使用上述随机数生成方法模拟掷筛子(Dice)。一每次掷 3 个筛子,根据下面的规则来显示获胜的奖金情况。? 如果 3 个筛子都是 6,赢 20 元; ? 如果 3 个筛子都相同,赢 10 元; ? 如果 3 个筛子中有任何两个都相同,赢 5 元。程序:public class Dice { public static void main(String[] args){ int jine=0; Random rand = new Random( ); int[] x=new int[3]; for(int i=0;i&3;i++) x[i] = rand.nextInt(5)+1;河南财经政法大学计算机与信息工程学院第 19 页 实验报告if(x[0]==6&&x[1]==6&&x[2]==6)jine=20; jine=10; jine=5;else if(x[0]==x[1]&&x[0]==x[2]&&x[1]==x[2]) else if(x[0]==x[1]||x[0]==x[2]||x[1]==x[2]) System.out.println(&钱数为:&+jine); } }分析程序 public static double power( double x, int n ){ if ( n == 0 ) return 1.0; else if ( n % 2 == 0 ) return power( x * x, n / 2 ); else return power( x * x, n / 2 ) * } 程序的功能是当n为0是返回值为1.0;当n为偶数时返回值为x的n次方 当n为奇数是返回值为x的n次方 当x=4,n=2时,调用power方法返回值是什么?写出每次的调用过程 第一次:执行power(4,2),因为2%2==0,所以执行power(16,1), 此时x=16,又因为1%2==0,所以返回power(196,0)*16=1.0*16=16.0分析下面的两段程序 public static int P( int n ){ return Q( n &= 0 ? n - n ); } private static int Q( int m ){ final int BASE = 10; return ( m & BASE ? 1 1 + Q( m / BASE ) ); } 例如:当 n=5 时,调用方法 p 返回值是多少?写出执行过程。Step1:n=5, p (5) = Q (5 ) ; Q (5 ) = 1 1) 当 n=9 时,调用方法 p 返回值是多少?写出执行过程。Step1:n=9,P(9)=Q(9);Q(9)=1 返回值为:1河南财经政法大学计算机与信息工程学院第 20 页 实验报告2) 当 n=10 时,调用方法 p 返回值是多少?写出执行过程。Step1:n=10,P(10)=Q(10);Q(10)=1+Q(1) Step2:Q(1)=1 返回值为:2 3) 当 n=123 时,调用方法 p 返回值是多少?写出执行过程。Step1:n=123,P(123)=Q(123);Q(123)=1+Q(12) Step2:Q(12)=1+Q(1) Step3:Q(1)=1 返回值为:3 4)
P 方法的功能是什么? 求 n 的位数 5) 修改方法 P, 完成同样功能, 达到不调用其它函数包括递归调用自己。程序:import java.util.S public class Weishu { public static void main(String[] args) { Scanner sc =new Scanner(System.in); System.out.println(&请输入a的值:&); int a=sc.nextInt(); int count=1; if(a&0) a=-a; while(a/10!=0){ count++; a=a/10; } System.out.println(+count); }评语:河南财经政法大学计算机与信息工程学院第 21 页 实验报告日期:年月日河南财经政法大学计算机与信息工程学院第 22 页 实验报告实验 08:数组 实验目的1. 知道怎么样声明、创建和初始化各种类型的数组 2. 理解二维数组的概念,能够声明、创建和初始化各种类型的二维数组 3. 掌握对一维或二维数组元素的访问方法 4. 掌握不规则二维数组的声明、创建和初始化以及访问的方法了解 Arrays 类及 System 类中关于数组操作的方法的使用 实验内容 编写程序,掌握数组的声明和访问数组元素,了解数组对象 length 的作用 编写程序,掌握二维数组的定义和应用 编写程序,掌握二维不规则数组的定义和使用 编写程序,熟悉命令行参数 实验数据记录及分析(或程序及运行结果) 1. 根据教科书程序 5-5,写出外循环每次循环后数组的元素变化。循环开始前的数组元素依次是:51,38,49,27,62,5,16 第一次循环后的数组元素依次是:38,49,27,51,5,16,62 第二次循环后的数组元素依次是:38,27,49,5,16,51,62 循环后的数组元素依次是:5,16,27,38,49,51,622. 分析下面程序,写出运行结果 public class ArrayTest { public static void main(String[] args) { int i, int a[] = { 2, 1, 4, 8, 9, 5, 3 }; for (i = 0; i & a.length - 1; i++) { int k = for (j = j & a. j++){ if (a[j] & a[k]){ k = } } int temp = a[i]; a[i] = a[k]; a[k] = } for (i = 0; i & a. i++) System.out.print(a[i] + & &);河南财经政法大学计算机与信息工程学院 第 23 页 实验报告System.out.println(); } } 循环开始前的数组元素依次是:2,1,4,8,9,5,3 第一次循环后:i = 0 第二次循环后:i = 1 第三次循环后:i = 2 第四次循环后:i = 3 第五次循环后:i = 4 第六次循环后:i = 5 j=6 j =6 j=6 j=6 j=6 j=6 k= 1 k= 1 k= 6 k= 3 k= 6 k= 5 数组元素依次是 1,2,4,8,9,5,3, 数组元素依次是 1,2,4,8,9,5,3, 数组元素依次是 1,2,3,4,8,9,5, 数组元素依次是 1,2,3,4,8,9,5, 数组元素依次是 1,2,3,4,5,8,9, 数组元素依次是 1,2,3,4,5,8,9,最后运行结果1,2,3,4,5,8,93. 按照要求运行下面的程序,回答问题。public class CommandParameter { public static void main(String[] args) { for(int i=0;i&args.i++){ System.out.println(&第&+i+&个参数是&+args[i]); } } } 请按照下面的运行命令运行,并写出运行结果。D:\&javac CommandParameter.java D:\&java CommandParameter D:\&Java CommandParameter Alice Mike Mary 第 0 个参数是 Alice 第 1 个参数是 Mike 第 2 个参数是 Mary你认为 args 数组的作用是什么?河南财经政法大学计算机与信息工程学院第 24 页 实验报告用名为 args 表示的字符串数组是有虚拟机在执行该程序时创建的,用来保存 用户提供的参数4. 有如下数组 int myArray[] = { 1, 2, 3, 4, 5, 6 }; int yourArray[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; 请利用 System.arraycopy 方法编写一个程序,复制 myArray 数组的所有内容到 yourArray 数组,放在 yourArray 数组下标 0 开始的位置,并输出 yourArray 数 组的每个元素。程序:public class ArrayCopy { public static void main(String[] args) { int myArray[] = { 1, 2, 3, 4, 5, 6 }; int yourArray[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; System.arraycopy(myArray,0,yourArray,0,myArray.length); for(int x:yourArray){ System.out.print(x+&,&); } System.out.print(&\n&); for(int x:yourArray){ System.out.print(yourArray[x]+&,&); } System.out.print(&\n&); for(int x:yourArray){ System.out.print(yourArray[x-1]+&,&); } System.out.print(&\n&); for(int x=0;x&yourArray.x++){ System.out.print(yourArray[x]+&,&); } } }结果:1,2,3,4,5,6,4,3,2,1, 2,3,4,5,6,4,5,4,3,2, 1,2,3,4,5,6,4,3,2,1, 1,2,3,4,5,6,4,3,2,1, for(int x:yourArray){ System.out.print(x+&,&); }河南财经政法大学计算机与信息工程学院 第 25 页 实验报告中 x 是从 1 开始的,数组从第 1 个开始输出,也即从 0 下标开始输出, 所以使用System.out.print(yourArray[x-1]+&,&);有 整 型 数 组 , 按 顺 序 包 含 元 素 10,7,9,2,4,5,1,3,6,8 , 请 编 写 程 序 利 用 Arrays.sort 方法对数组进行排序,并输出该数组的每个元素。程序:public class PaiXu { public static void main(String[] args) { int myarray[]={10,7,9,2,4,5,1,3,6,8}; Arrays.sort(myarray); for(int x:myarray){ System.out.print(x+&,&); } } }结果:1,2,3,4,5,6,7,8,9,10,河南财经政法大学计算机与信息工程学院第 26 页 实验报告补充完整下面的程序,使得程序能够计算出指定字符数组中连续字符块的 个数A countRuns( A ) &empty & 0 A 1 A A A a a a a a b a b b a b a c b c b c c 1 3 4A b c d e f g 7 public class CountRuns { public static void main(String[] args) { char[] ch={};//重复用上面的字符数组来初始化,验证你的算法是否正 确 int count=0; //请在此处补充程序河南财经政法大学计算机与信息工程学院第 27 页 实验报告System.out.println(&字符数组中连续字符块个数是&+count); } }程序:public class CountRuns { public static void main(String[] args) { char[] ch={'b'}; int count=0; if(ch.length==0) System.out.println(&字符数组中连续字符块个数是&+count); else{ count++; for(int i=ch.length-1;i&0;i--){ if(ch[i].compareTo(ch[i-1])==0) count= else count++; } System.out.println(&字符数组中连续字符块个数是&+count); count=0; for(int i=0;i&ch.length-1;i++){ result=ch[i].equals(ch[i+1]); if(result=true) count= else count++; } System.out.println(&字符数组中连续字符块个数是&+count); for(int i=0;i&ch.length-1;i++){ if(ch[i]==ch[i+1]) count= else count++; } System.out.println(&字符数组中连续字符块个数是&+count); } } }评语:日期:年月日实验 09:算法设计 实验目的掌握常用的查找算法 掌握常用的排序算法 掌握递归的应用河南财经政法大学计算机与信息工程学院第 28 页 实验报告实验数据记录及分析(或程序及运行结果) 1. 编写程序,从键盘上输入一个整数,判断该程序是否是素数,如果是在 打印该数是素数,如果不是打印该数不是素数。程序:public class Sushu{public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println(&请输入一个整数!&); int a=sc.nextInt(); for(int i=2;i&=a/2;i++){ if(a%i==0){ System.out.println(a+&该数不是素数!&);} else if(i==a/2) System.out.println(a+&该数是素数!&); } } }2.已有一个升序排列的整数数组(含 20 个整数) ,要求从键盘输入一个整 数后,将该数插入数组,并保持升序,输出该数组的数值验证程序的正 确性。程序:import java.util.A import java.util.S public class Paixu { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] a = new int[20]; for (int i = 0; i & 20; i++) a[i] = sc.nextInt(); System.out.println(&输入一个整数!&); int b = sc.nextInt(); sc.close(); Arrays.sort(a); for (int i = 0; i & 20; i++) System.out.print(a[i]+&\t&); int[] c = new int[21]; for(i=0;i&20;i++)河南财经政法大学计算机与信息工程学院 第 29 页 实验报告if(a[i]&=b&&a[i+1]&=b) {a[i+1]=b;} for(int j=i;j&=0;j--) c[j]=a[i]; for(int j=i;j&=20;j++) c[j+1]=a[i]; for (i = 0; i & 21; i++) System.out.println(&输出:& + c[i]); } }3.输出所有的水仙花数,所谓的水仙花数是指一个三位数,其各位数字的 立方和等于该数本身,例如,153=13+53+33。4.分析程序,回答题后问题。public class BinarySearch { public static void main(String[] args) { int[] r={5,13,19,21,37,56,64,75,80,88,92}; int key=56; int low=0,high=r.length-1,mid=-1; while(low&=high){ mid=(low+high)/2; if(r[mid]==key){ } if(r[mid]&key) high=mid-1; else low=mid+1; } if(low&high){ System.out.println(&not found!&); }else{ System.out.println(&found at &+mid);河南财经政法大学计算机与信息工程学院第 30 页 实验报告} } } 1) 当 key=21 时,写出每次循环 low、high 和 mid 以及运行输出结果的值 Step 1:low=0,high=10,mid=-1 Step 2:low=0,high=4,mid=5 Step 3:low=3,high=4,mid=2 Step 4:low=3,high=4,mid=3 输出结果为:found at 3 2) 当 key=22 时,写出每次循环 low、high 和 mid 以及运行输出结果的值 Step 1:low=0,high=10,mid=-1 Step 2:low=0,high=4,mid=5 Step 3:low=3,high=4,mid=2 Step 4:low=4,high=4,mid=3 Step 5:low=4,high=4,mid=4 输出结果为:not found! 3) 当 key=56 时,写出每次循环 low、high 和 mid 以及运行输出结果的值 Step 1:low=0,high=10,mid=-1 Step 2:low=0,high=10,mid=5 输出结果为:found at 5 4) 当 key=88 时,写出每次循环 low、high 和 mid 以及运行输出结果的值 Step 1:low=0,high=10,mid=-1 Step 2:low=6,high=10,mid=5 Step 3:low=7,high=10,mid=8 Step 4:low=8,high=10,mid=8 Step 5:low=8,high=10,mid=9 输出结果为:found at 95.用递归的方法求 法? n!120(即求 1!+2!+3!+….+20!) ,了解递归的用程序:public class Digui { static int fac(int n){ if(n==1) return 1; else return n*fac(n-1);河南财经政法大学计算机与信息工程学院 第 31 页 实验报告} public static void main(String[] args) { int i=1,sum=0; for(;i&=20;i++) sum+=fac(i); System.out.println(sum); } }6.MIT 的几个朋友决定要参加马拉松比赛。他们的姓名和比赛用时如下:Name Time (minutes) Elena 341 Thomas 273 Hamilton 278 Suzie 329 Phil 445 Matt 402 Alex 388 Emma 275 John 243 James 334 Jane 412 Emily 393 Daniel 299 Neda 343 Aaron 317 Kate 265 问题找出最快的运动员。打印出他们的姓名和他的时间(分钟为单位) 。选项:找出 次快的元动员。打印出他的姓名和时间 参考写一个方法以一个整数数组为参数,返回该数组中的最小值对应的下标索引值。调用此方法以下面的时间数组为参数, 根据返回的索引找出姓名数组和时间数组 中对应得元素,即跑的最快的人的姓名和对应时间。写第二个方法去查找第二快的队员。第二个方法使用第一个方法找出最快者,然 后在剩下的元素中找出最快者即可。河南财经政法大学计算机与信息工程学院第 32 页 实验报告评语日期年 月 日实验 10:面向对象编程基础(一)简单的类和对象 实验目的掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明 变量和方法返回值; 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方 法和属性; 实验数据记录及分析(或程序及运行结果) 写一个名为 Rectangle 的类表示矩形。其属性包括宽 width、高 height 和颜色 color,width 和 height 都是 double 型的,而 color 则是 String 类型的。要求该类 具有使用构造函数完成各属性的初始赋值 使用 getter 和 setter 的形式完成属性的访问及修改 提供计算面积的 getArea()方法public class Rectangle {
public S public double getWidth() { } public void setWidth(double width) { this.width = } public double getHeight() { } public void setHeight(double height) { this.height = } public String getColor() { } public void setColor(String color) { this.color =河南财经政法大学计算机与信息工程学院第 33 页 实验报告} public Rectangle(double width, double height, String color) { super(); this.width = this.height = this.color = } public static void getArea(double chang,double kuan){ area=chang* System.out.println(&面积=&+area); } public static void main(String[] args){ Rectangle a=new Rectangle(1.2,3.6,&红&); Rectangle.getArea(a.width, a.height); } }一副牌 Deck 有 52 张扑克 Card 组成(不含王牌), 每张牌有自己的牌型 suit (用 char 类型)和点数 rank(用 String 类型) ,补充完整下面的类的定义。public class Card {public Card( this.suit = this.rank = } public String toString() { return } suit+) {public static void main(String[] args){ Card c=new Card('红',&10&); System.out.println(c); } }public class Card { S public Card( char suit,String rank ) { this.suit = this.rank = }河南财经政法大学计算机与信息工程学院 第 34 页 实验报告public String toString() { return suit+ } public static void main(String[] args){ Card c=new Card('红',&10&); System.out.println(c); } }的账户记录 Account 有账户的唯一性标识 (11 个长度的字符和数字的组 合) ,用户的姓名,开户日期(Date),账户密码(六位的数字,可以用 0 开头) , 当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密 码 123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提 供时初始余额为 0。定义该类,并要求该类提供如下方法:存款、取款、变更密 码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。import java.sql.Dimport java.util.S public class Account { private S private S private Date D private S public Account() { super(); this.id = this.name = this.Date = D this.code = &123456&; this.balance = } public void openAccount(){ Scanner sc=new Scanner(System.in); System.out.println(&请给一个账号&); String id=sc.next(); System.out.println(&请输入客户姓名&); String name=sc.next(); System.out.println(&请输入开户日期&); String Date=sc.next(); int balance=0;河南财经政法大学计算机与信息工程学院 第 35 页 实验报告System.out.println(&请输入初始存款额&); int amount=sc.nextInt(); this.balance+= } public void deposit(){ System.out.println(&请输入存款额&); Scanner sc=new Scanner(System.in); int amount=sc.nextInt(); this.balance+= } public void withdraw(){ System.out.println(&请输入取款额&); Scanner sc=new Scanner(System.in); int amount=sc.nextInt(); this.balance-= } public void changecode(){ System.out.println(&请输入6位新密码&); Scanner sc=new Scanner(System.in); String newcode=sc.next(); this.code= } public String getId() { } public String getName() { } public Date getDate() { return D } public String getCode() { } public int getBalance() { } @Override public String toString() { return &Account [Date=& + getDate()+ &, balance=& + getBalance() + &, code=& + getCode()+ &, id=& + getId() + &, name=& + getName()+ &]&;河南财经政法大学计算机与信息工程学院 第 36 页 实验报告} public static void main(String[]args){ Account a=new Account(); a.openAccount(); a.deposit(); a.withdraw(); a.changecode(); System.out.println(a); System.out.println(&账户姓名:&+a.getName()); System.out.println(&账户账号:&+a.getId()); System.out.println(&账户日期:&+a.getDate()); System.out.println(&账户余额:&+a.getBalance()); } } 结果请给一个账号 123 请输入客户姓名 h 请输入开户日期 3234455 请输入初始存款额 1000 请输入存款额 200 请输入取款额 100 请输入6位新密码 123466 Account [Date=null, balance=1100, code=123466, id=null, name=null] 账户姓名:null 账户账号:null 账户日期:null 账户余额:1100 为什么date,id,name结果为空???一个 Fan 有自己的品牌和型号,其调速旋钮 speed 可以调整 Fan 的速度(档 级在 0、1、2、3 范围内) ,通电开关可以改变电扇状态(通电表示工作就绪或者 工作,断电表示停止工作)请仔细分析并提供 Fan 类的定义。并编写程序对其进 行测试。具体要求如下表示该类对象的状态可以用品牌(brand,可以用字符串型表示,因为品牌通 常都是多个字符组成的)、工作状态(status,用来反映一个电风扇是否处于通电就 绪 ready、正在转动 working 或者静止 stop)、当前的速度(speed,可以用数值型 表示);河南财经政法大学计算机与信息工程学院 第 37 页 实验报告用构造函数可以创建一个电风扇对象,指定其品牌就可以了,一个电风扇的 初始状态可以是静止,而且初始速度为0,分析为什么不适宜直接通过构造函数 的形式参数来指定初始状态和速度; 给该类提供如下的方法, 重点考虑每个动作导致的状态变化, 形成代码逻辑; ? 提供一个方法来表示电风扇的开与关,模拟一个开关动作 ? 通过加速和减速两个方法来调整速度, 当速度超出极限(0 或3), 向控制台输出一条告警信息作为提示,并且不执行该调速动作。? 提供一个方法能够返回电风扇当前的状态和速度 添加一个 main 方法,按顺序完成以下工作,考虑状态与速度变化和开关动 作之间的关系? 创建一个电风扇对象,用f表示; ? 加速一次电风扇; ? 向控制台输出 f 的状态和速度; ? 启动电风扇(调用对象的开关方法); ? 向控制台输出 f 的状态和速度; ? 连续加速4次(考虑发生什么情况); ? 向控制台输出 f 的状态和速度; ? 连续减速4次(考虑发生什么情况); ? 向控制台输出 f 的状态和速度; ? 关掉电风扇(调用对象的开关方法); ? 连续加速 2 次 ? 向控制台输出 f 的状态和速度;public class Fan {private S private S public static final String READY=&ready&; public static final String STOP=&stop&; public static final String WORKING=&working&; public Fan(String brand, int speed, String status) { super(); this.brand = this.speed = 0; this.status =Fan.STOP; } public void kaiguan(){ if(this.status.compareTo(Fan.STOP)==0) this.status=Fan.READY; else{this.status=Fan.STOP;this.speed=0;} } public void speedup(){ if(this.speed&3) speed++; if(this.speed==3)System.out.println(&不可再加速!&); }河南财经政法大学计算机与信息工程学院 第 38 页 实验报告public void speeddown(){ if(this.speed&0) speed--; if(this.speed==0)System.out.println(&不可再减速!&); } @Override public String toString() { return &Fan [& + & speed=& + speed + &, status=& + status + &]&; } public static void main(String args[]){ Fan f=new Fan(&海尔&,0,&STOP&); f.speedup(); System.out.println(f); f.kaiguan(); System.out.println(f); f.speedup(); f.speedup(); f.speedup(); f.speedup(); System.out.println(f); f.kaiguan(); f.speedup(); f.speedup(); System.out.println(f); } } 结果:Fan [ speed=1, status=stop] Fan [ speed=1, status=ready] 不可再加速! 不可再加速! 不可再加速! Fan [ speed=3, status=ready] Fan [ speed=2, status=stop]解释下面的程序运行结果输出为什么是 null public class My { S public void My(){ s = &Constructor&;河南财经政法大学计算机与信息工程学院 第 39 页 实验报告} public void go() { System.out.println(s); } public static void main(String args[]) { My m = new My(); m.go(); } } 没有执行 My 方法。S 的初始值为空 可以将 void 去掉,使 My()成为一个构造方法给出下面的类,找出后面的 5 个声明中,哪些是重载后的构造函数 public class ConstOver { public ConstOver (int x, int y, int z) { } } A. ConstOver ( ) { } B. Protected int ConstOver ( ) { } C. Private ConstOver (int z, int y, byte x) { } D. Public Object ConstOver (int x, int y, int z) { } E. Public void ConstOver (byte x, byte y, byte z) { } 方法的名称,形参的类型和次序和个数 AB C E给出下面的类,找出后面的 5 个声明中,哪些是重载后的 setVar 函数 public class MethodOver { public void setVar (int a, int b, float c) { } } A. Private void setVar (int a, float c, int b) { } B. Protected void setVar (int a, int b, float c) { } C. Public int setVar (int a, float c, int b) {} D. Public int setVar (int a, int b, float c) {} E. Protected float setVar (int a, int b, float c) {} AC评语日期年 月 日河南财经政法大学计算机与信息工程学院第 40 页 实验报告实验 11:面向对象编程基础(二)方法中的参数传递 实验目的1. 理解方法中的形参和实参传递在传递基本类型和引用类型时的差异 实验要求和过程 程序运行后的输出是什么? class TestReference{ public static void main(String[] args){ int x=2; TestReference tr = new TestReference(); System.out.print(x); tr.change(x); System.out.print(x); } public void change(int num){ num = num + 1; } } 22 写出程序运行结果public class Foo { public static void main (String [] args) { StringBuffer a = new StringBuffer (“A”); StringBuffer b = new StringBuffer (“B”); operate(a,b);//调用了Foo类的一个类方法 System.out.printIn(a + “,” +b); } static void operate (StringBuffer x, StringBuffer y) { x.append(y); y = } }AB,B输入下面的类,结合实验十(2)中的 Card 类,完成下面的要求public class JLab1201 { static final char suits[] ={'H','S','D','C'}; static final String ranks[] ={&A&,&2&,&3&,&4&,&5&,&6&,&7&,&8&,&9&,&10&,&J&,&Q&,&K&}; static Card[] cards=new Card[52];河南财经政法大学计算机与信息工程学院第 41 页 实验报告/** * 初始化扑克牌数组 */ public static void init(){ for(int i=0;i&suits.i++){ for(int j=0;j&ranks.j++){ cards[i*ranks.length+j]=new Card(suits[i],ranks[j]); } } } public static void swap1(Card c1,Card c2){ Card c=c1; c1=c2; c2=c; } public static void swap1(int i,int j){ Card c=cards[i]; cards[i]=cards[j]; cards[j]=c; } public static void swap2(Card c1,Card c2){ char s=c1. c1.suit=c2. c2.suit=s; String r=c1. c1.rank=c2. c2.rank=r; } /** * @param args */ public static void main(final String[] args) { init(); //任取两张牌 Card c1=cards[10]; Card c2=cards[12]; System.out.println(&第 11 张牌是:&+c1+&\t 第 13 张牌是:&+c2); } }1) 在 main 方法中,添加下面几行语句,写出并分析结果swap1(c1,c2); System.out.println(&执行 swap1(c1,c2)后&); System.out.println(&c1 引用的牌是:&+c1+&\tc2 引用的牌是:&+c2); System.out.println(&第 11 张牌是:&+cards[10]+&\t 第 13 张牌是:&+cards[12]);河南财经政法大学计算机与信息工程学院 第 42 页 实验报告第11张牌是:HJ 第13张牌是:HK 执行swap1(c1,c2)后 c1引用的牌是:HJ c2引用的牌是:HK 第11张牌是:HJ 第13张牌是:HK // c1,c2分别指向cards[10]和 cards[12]指向的对象 //经过Swap1(c1,c2)Swap1中c1、c2的值交换了,但main函数中的c1,c2指向不变2) 在 main 方法中,去掉刚才添加的语句,添加下面几行语句,写出并分析 结果swap1(10,12); System.out.println(&执行 swap1(10,12)后&); System.out.println(&c1 引用的牌是:&+c1+&\tc2 引用的牌是:&+c2); System.out.println(&第 11 张牌是:&+cards[10]+&\t 第 13 张牌是:&+cards[12]);第11张牌是:HJ 第13张牌是:HK 执行swap1(10,12)后 c1引用的牌是:HJ c2引用的牌是:HK 第11张牌是:HK 第13张牌是:HJ // c1,c2分别指向cards[10]和 cards[12]指向的对象,swap1(10,12)是将10传给 i,12传给j,但函数将cards[10]和 cards[12]指向的对象交换了,c1,c2指向不变3) 在 main 方法中,去掉刚才添加的语句,添加下面几行语句,写出并分析 结果swap2(c1,c2); System.out.println(&执行 swap2(c1,c2)后&); System.out.println(&c1 引用的牌是:&+c1+&\tc2 引用的牌是:&+c2); System.out.println(&第 11 张牌是:&+cards[10]+&\t 第 13 张牌是:&+cards[12]);第11张牌是:HJ 第13张牌是:HK 执行swap2(c1,c2)后 c1引用的牌是:HK c2引用的牌是:HJ 第11张牌是:HK 第13张牌是:HJ Swap2(c1,c2)中cards[10]和 cards[12]分别传给c1、c2后,指向对象的内容分发 生了变化评语签字:河南财经政法大学计算机与信息工程学院日期:年月日第 43 页 实验报告实验 12:面向对象编程基础(三)访问范围和作用域 实验目的1. 理解访问控制修饰符的作用; 2. 掌握 package 的作用 实验要求和过程 1. 下列程序有错误,调试、写出错误原因并改正。public class VariableScope { public static void main(String args[]) { int i=10; { int k=10; System.out.println(&i=&+i); System.out.println(&k=&+k); } System.out.println(&i=&+i); System.out.println(&k=&+k); } } K是局部变量,只能在{ int k=10; System.out.println(&i=&+i); System.out.println(&k=&+k); }里使用 i是全局变量,可以在整个类中使用2.在项目中创建两个包 p1,将实验十(3)中的 Account 类移至 p1 下,保 证 Account 类的账户余额属性 balance 的访问范围修饰为 public, 并在 p1 下创建一个测试类 Test,如下:public class Test { public static void main(String[] args){ Account c=new Account(&&,&丁怡&); System.out.println(&当前账户余额是:&+c.balance); } }执行下面的实验步骤,观察并分析程序中出现的现象。依次将 balance 前的访问范围修饰符变为 protected、private 和缺省,观察上 面的 Test 程序会出现何种现象,如有错误,解释原因。public,protected和缺省可以得出河南财经政法大学计算机与信息工程学院当前账户余额是:0第 44 页 实验报告private会显示c.balance错误 public修饰的属性所有类都可见 protected修饰的属性在同一包下,本类和子类中可见 缺省修饰的属性在同一包下和本类中可见 private修饰的属性仅在本类中类可见创建和 p1 并列的包 p2,并将 Account 类从 p1 移至 p2 下,观察上面的 Test 程序会出现何种现象,如有错误,如何解决。public可以得出 当前账户余额是:0 private,protected和缺省会显示c.balance错误 解决balance的修饰改为public或者将Account和Test类放在一个包下将 p2 下的 Account 类 class 前的 public 去掉,观察上面的 Test 类会出现何种 现象,如有错误,可否解决。显示 Account c=new Account(&&,&丁怡&);错误 解决Account 的修饰改为 public 或者将 Account 和 Test 类放在一个包下恢复 Account 类为 public 后,依次将 balance 前的访问范围修饰符变为 protected、private 和缺省,观察上面的 Test 程序会出现何种现象,如有错误,解 释原因。private,protected和缺省会显示c.balance错误 Account和Test类不在一个包下,只有balance的修饰为public时才能在Test类中 使用Account类的属性balance5)比较并写出上述 1 和 4 中的异同。类的修饰和类中属性的修饰 出错的地方不同,但原因都是相同的评语教师签字日期年 月 日河南财经政法大学计算机与信息工程学院第 45 页 实验报告实验 13:面向对象编程基础(四) 类的关系 实验目的1. 理解类之间的关联关系 2. 理解类之间的整体与部分关系 3. 理解类之间的组合/聚合关系 实验要求和过程1. 定义一个类 Deck,表示一副牌,每副牌包含 52 张牌(不含王牌,用数组 实现) 1) Deck 和 Card 之间属于什么关系。2) 定义一个构造方法,实现一副牌的初始化,即按照“黑、红、梅、 方”的花色顺序,点数从 A-K,生成 52 张扑克牌。static final String suits[] ={&黑&,&红&,&梅&,&方&}; static final String suits[] ={&A&,&2&,&3&,&4&,&5&,&6&,&7&,&8&,&9&,&10&,&J&,&Q&,&K&}; static Card[] cards=new Card[52]; public static void init(){ for(int i=0;i&suits.i++){ for(int j=0;j&ranks.j++){ cards[i*ranks.length+j]=new Card(suits[i],ranks[j]); } } }3) 提供一个方法 show,按照每行 13 张,逐一显示全部的扑克牌。如 黑 A 黑 2……黑 K 红 A 红 2……红 K 梅 A 梅 2……梅 K 方 A 方 2……方 Kpublic static void show(){ int count=0; for(int i=0;i&suits.i++){ for(int j=0;j&ranks.j++){ System.out.print(&河南财经政法大学计算机与信息工程学院第&+(i*ranks.length+j+1)+&张牌第 46 页 实验报告是:&+cards[i*ranks.length+j]+&\t&); count++;} if(count%13==0) System.out.println(&\n&); } }4) 提供一个 show(int idx)方法,返回 idx-1 位置处的扑克牌对象。2. 定义一个 Teacher 类,该类提供教师的工号、姓名、职称信息,再定义一 个课程类,描述了课程的编号、课程名称、理论课时和实验课时、学分 以及每门课程都要指定一个教师,通过课程能够获得任课教师对象。public class Teacher { private S private S private S } public class Class { public S public S
public T }3. 小镇的图书馆需要一个新的电子租借系统。该小镇有两个图书馆,每个 图书馆提供很多书让客户去租赁。客户可以打印出所有的图书清单,包 括现存的,已经借出的和归还的。现在我们需要提供两个类,Book 类和 Library 类,有这俩个类提供图书数据库的功能。请补充缺少的方法去完成 类的功能。步骤1) 补充完成 Book 类的定义。这个类需要定义获取图书标题的方法,查看图 书的作者,查看是否可用的方法,是否借出方法。图书的状态用“available” 和“borrowed”表示。现在的结构框架内缺少这些方法的执行内容,请用合 适的功能代码补充方法体的内容。用 main 方法测试方法,当你运行程序时 应该输出的内容图书名称:Java 程序设计教程 图书作者:丁一 出租状态:出租中 可用状态:不可用 public class Book {河南财经政法大学计算机与信息工程学院 第 47 页 实验报告//图书名称 //作者 //图书状态,初始状态为可用 //使用给定的图书名称,创建一个新的 Book 对象 ,并置该对象的初始状态为可用SSpublic Book(String bookTitle,String author){ // Implement this method}// 借书,标记该书为借出状态public void borrowed() { // Implement this method} //还书,标记该书为可用状态 public void returned() { // Implement this method} // Returns true if the book is rented, false otherwise public boolean isBorrowed() { // Implement this method } // Returns true if the book is available, false otherwise public Boolean isAvailable(){ // Implement this method}//返回图书的名称public String getTitle() { // Implement this method } public static void main(String[] arguments){ Book example = new Book(&Java 程序设计教程&); System.out.println(&图书名称:& + example.getTitle()); System.out.println(&图书作者:& + example.getAuthor()); System.out.println(&出租状态:& + example.isBorrowed()); System.out.println(&可用状态:& + example. isAvailable()); }河南财经政法大学计算机与信息工程学院 第 48 页 实验报告}package L public class Book { S S //图书名称 //作者 //图书状态,初始状态为可用//使用给定的图书名称,创建一个新的Book对象 ,并置该对象的初始状态为可用 public Book(String title, String author) { super(); this.title = this.author = this.borrowed = } // 借书,标记该书为借出状态 public String borrowed() { this.borrowed= String a=&variable&; return(a); } //还书,标记该书为可用状态 public String returned() { this.borrowed= String a=&borrowed&; return(a); } // Returns true if the book is rented, false otherwise public boolean isBorrowed() { if(this.borrowed==false) else } // Returns true if the book is available, false otherwise public Boolean isAvailable(){ if(this.borrowed==true) else } //返回图书的名称 public String getTitle() { return(this.title); } public String getAuthor() { return(this.author); } return(false); return(true); return(false); return(true);河南财经政法大学计算机与信息工程学院第 49 页 实验报告public static void main(String[] arguments){ Book example = new Book(&Java程序设计教程&,&丁一&); example.borrowed(); System.out.println(&图书名称:& + example.getTitle()); System.out.println(&图书作者:& + example.getAuthor()); System.out.println(&出租状态:& + example.isBorrowed()); System.out.println(&可用状态:& + example. isAvailable()); } }2) 完成 library 类:我们应该建立一个类用于表示每个图书馆,并且管理每 个图书馆的图书,他们有不同的地址和图书数目。建立一个 Library 类。复 制下面的结构代码。我们提供一个 main 方法用于建立两个图书馆,然后执 行一些对书目的操作,图书入库、出借图书和图书归还,当一本处于出租中 的图书被借阅时,输出“图书已被出租” ,如果没有要借的图书,输出“没 有发现要借的图书” 。当然,这些所有的方法成员和变量成员都是缺少的, 你需要填充这些缺省的部分。解读下面的 main 方法检查编译错误,填充缺 少的方法和成员内容。public class Libaray { // Add the missing implementation to this classpublic static void main(String[] args){ // Create two libraries Library firstLibrary = new Library(&文化路80号&); Library secondLibrary = new Library(&建设路22号&); // Add four books to the first library firstLibrary.addBook(new Book(&飞鸟集&,&泰戈尔&)); firstLibrary.addBook(new Book(&安妮日记&,&弗兰克&)); firstLibrary.addBook(new Book(&红楼梦&,&曹雪芹&)); firstLibrary.addBook(new Book(&希腊的神话和传说&,&斯威布&)); secondLibrary.addBook(new Book(&安妮日记&,&弗兰克&)); secondLibrary.addBook(new Book(&红楼梦&,&曹雪芹&)); secondLibrary.addBook(new Book(&希腊的神话和传说&,&斯威布&)); System.out.println(&图书馆地址:&); firstLibrary.printAddress(); secondLibrary.printAddress(); System.out.println(); System.out.println(&从两个图书馆借【飞鸟集】&); firstLibrary.borrowBook(&飞鸟集&); firstLibrary.borrowBook(&飞鸟集&); secondLibrary.borrowBook(&飞鸟集&); System.out.println();河南财经政法大学计算机与信息工程学院 第 50 页 实验报告/ /输出两个图书馆各自可外借的图书 System.out.println(&first library可外借的图书:&); firstLibrary.printAvailableBooks(); System.out.println(); System.out.println(&second library可外借的图书:&); secondLibrary.printAvailableBooks(); System.out.println(); // 向first library归还【飞鸟集】 System.out.println(&向first library归还【飞鸟集】:&); firstLibrary.returnBook(&飞鸟集&); System.out.println(); //输出first library图书馆可外借的图书 System.out.println(&first library可外借的图书:&); firstLibrary.printAvailableBooks(); } }public class Library { public S public Library(String address) { super(); this.address = } private void addBook(Book book) { this.number++; } private String printAddress() { Scanner sc=new Scanner(System.in); String c=sc.next(toString()); return(c); } private void printAvailableBooks() { } private void returnBook(String string) { this.number++; } private void borrowBook(String string) { this.number--; } public static void main(String[] args){ // Create two libraries河南财经政法大学计算机与信息工程学院第 51 页 实验报告Library firstLibrary = new Library(&文化路80号&); Library secondLibrary = new Library(&建设路22号&); // Add four books to the first library firstLibrary.addBook(new Book(&飞鸟集&,&泰戈尔&)); firstLibrary.addBook(new Book(&安妮日记&,&弗兰克&)); firstLibrary.addBook(new Book(&红楼梦&,&曹雪芹&)); firstLibrary.addBook(new Book(&希腊的神话和传说&,&斯威布&)); secondLibrary.addBook(new Book(&安妮日记&,&弗兰克&)); secondLibrary.addBook(new Book(&红楼梦&,&曹雪芹&)); secondLibrary.addBook(new Book(&希腊的神话和传说&,&斯威布&)); System.out.println(&图书馆地址:&); firstLibrary.printAddress(); secondLibrary.printAddress(); System.out.println(); System.out.println(&从两个图书馆借【飞鸟集】&); firstLibrary.borrowBook(&飞鸟集&); firstLibrary.borrowBook(&飞鸟集&); secondLibrary.borrowBook(&飞鸟集&); System.out.println(); //输出两个图书馆各自可外借的图书 System.out.println(&first library可外借的图书:&); firstLibrary.printAvailableBooks(); System.out.println(); System.out.println(&second library可外借的图书:&); secondLibrary.printAvailableBooks(); System.out.println(); // 向first library归还【飞鸟集】 System.out.println(&向first library归还【飞鸟集】:&); firstLibrary.returnBook(&飞鸟集&); System.out.println(); //输出first library图书馆可外借的图书 System.out.println(&first library可外借的图书:&); firstLibrary.printAvailableBooks(); } }评语教师签字日期年 月 日河南财经政法大学计算机与信息工程学院第 52 页 实验报告实验 14:面向对象编程基础(五)static 实验目的理解 static 的作用和使用方法 实验要求和过程 1. 写出程序运行结果,如有错误,指出原因并改正 class StaticDemo { static{ x=10; } public static int getX() { } public static void setX(int newX) { x = newX; } public int getY() { } public void setY(int newY) { y = newY; } public static void main(String[] args) { System.out.println(&静态变量x=&+StaticDemo.getX()); System.out.println(&实例变量y=&+StaticDemo.getY()); StaticDemo a= new StaticDemo(); StaticDemo b= new StaticDemo(); a.setX(1); a.setY(2); b.setX(3); b.setY(4); System.out.println(&静态变量a.x=&+a.getX()); System.out.println(&实例变量a.y=&+a.getY()); System.out.println(&静态变量b.x=&+b.getX()); System.out.println(&实例变量b.y=&+b.getY());河南财经政法大学计算机与信息工程学院 第 53 页 实验报告} } x 是静态变量,要用类名引用; y 是实例变量,要用变量名引用评语教师签字日期年 月 日实验 15:面向对象基础编程(六)继承 实验目的1. 理解继承的概念 2. 掌握继承的实现 3. 理解继承中的覆盖现象 4. 理解抽象类的概念,掌握定义抽象类的方法和应用 实验数据记录及分析(或程序及运行结果) 1. 首先预测下列程序的运行结果,然后编译、执行以验证你的预测class Pet{ protected S public Pet(String n){ name = } public String getName(){ } public String move(){ return &run&; } public String speak(){ return &&; } public String toString(){ return &My pet & + }河南财经政法大学计算机与信息工程学院 第 54 页 实验报告} class Dog extends Pet{
public Dog(String s){ super(s); } } class PetTest{ public static void main(String[] args){ Pet myPet = new Pet(&George&); Dog myDog = new Dog(&Spot&); System.out.println(myPet.toString() + &\n& + &Speak& + myPet.speak() +&\n& + myPet.move() + & & + myPet.getName() + &\n&); System.out.println(myDog.toString() + &\n& + &Speak& + myDog.speak() +&\n& + myDog.move() + & & +myDog.getName() + &\n&); } } 1) 修改 Dog 类的构造函数,使之可以初始化 Dog 类中的 weight 值。class Dog extends Pet{
public Dog(String s,int weight){ super(s); this.weight= } }2) 在 Dog 类中添加方法 getWeight 以获得其 weight 值。public int getWeight() { }3) 修改类 Test,使用新的构造函数创建一个名为“snoopy”,重为 10 的 Dog 类的对象,并利用其方法输出包括重量在内的各项值 class PetTest{ public static void main(String[] args){ Pet myPet = new Pet(&snoopy&); Dog myDog = new Dog(&Spot&); myDog.weight=10; System.out.println(myPet.toString() + &\n& + &Speak& + myPet.speak() +&\n& + myPet.move() + & & + myPet.getName() + &\n&); System.out.println(myDog.toString() + &\n& + &Speak& + myDog.speak() +&\n& + myDog.move() + & & +myDog.getName() + &\n&);河南财经政法大学计算机与信息工程学院 第 55 页 实验报告}class PetTest{ public static void main(String[] args){ Dog myDog = new Dog(&George&,10); System.out.println(myDog.toString() + &\n& + &Speak& + myDog.speak() +&\n& + myDog.move() + & & +myDog.getName() + &\n&+myDog.weight); }}4) 要使 Dog 类对象的 speak()方法返回值为“Woof Woof”,应如何做?修 改(1)中程序,并验证你的做法。在 Dog 类中加入函数public String speak(){ return &Woof Woof&;2.试改写实验十五(1)中的 Pet 类, 其中 move 与 speak 方法定义为抽象方法。定义一个 Pet 类的子类 Cat 类。在 Cat 类中实现 move 和 Speak 方法。其 中 move 方法返回值为“run” ,speak 方法返回值为“meow meow”。编 写含有 main 方法的公共类 PetTest,在其方法体中创建一个名为 myCat 的 Cat 的实例。并依照十五(1)PetTest 类 main 方法中的相应格式输出结 果。class PetTest{ public static void main(String[] args){ Cat myCat = new Cat(&George&); System.out.println(myCat.toString() + &\n& + &Speak& + myCat.speak() +&\n& + myCat.move() + & & +myCat.getName() + &\n&); } }3. 按照要求完成程序 public class Test{ public static void main(String args[]){ System.out.println(&创建父类对象:&); SuperClass sc0 = new SuperClass(); System.out.println(&\n 创建第一个子类对象:&); SubClass sc1 = new SubClass(); System.out.println(&\n 创建第二个子类对象:&); SubClass sc2 = new SubClass(1); } } class SuperClass{ SuperClass( ){ System.out.println(&父类的构造函数&); } }河南财经政法大学计算机与信息工程学院 第 56 页 实验报告class SubClass extends SuperClass{ SubClass( ){ System.out.println(&子类的第一个构造函数&); } SubClass(int i){ System.out.println(&子类的第二个构造函数&); } } 要求:去掉 SubClass 的所有构造函数,然后将 Test 类中 main 方法的最后两 行语句删除。预测修改后程序的运行结果,并进行验证。运行结果:创建父类对象父类的构造函数 创建第一个子类对象父类的构造函数 子类的第一个构造函数 创建第二个子类对象父类的构造函数 子类的第二个构造函数 去掉后public class Test { public static void main(String args[]){ System.out.println(&创建父类对象:&); SuperClass sc0 = new SuperClass(); System.out.println(&\n创建第一个子类对象:&); SubClass sc1 = new SubClass(); } } class SuperClass{ SuperClass( ){ System.out.println(&父类的构造函数&); } } class SubClass extends SuperClass{ } 结果创建父类对象父类的构造函数 创建第一个子类对象:河南财经政法大学计算机与信息工程学院 第 57 页 实验报告父类的构造函数4. 按照要求完成程序1) 第一个类是图形类(Shape) ,含有一个成员变量color(字符串类型) ,一 个没有参数的构造方法,以及一个有一个字符串类型参数的构造方法来 初始化颜色变量,还有一个返回颜色变量值的成员方法show,以及一个 抽象方法getArea获取面积,返回值为double类型;public abstract class Shape { public S public Shape() { super(); } public Shape(String color) { super(); this.color = } public String show(){ return this. } public abstract double area(); }2) 第二个类是圆形类(Circle)继承自图形类,含有一个成员变量半径r,有 一个有两个参数的构造方法,来初始化颜色和半径,一个方法getArea, 返回值为double,获取圆的面积值。public class Cricle extends Shape{ public Cricle(String s,double r) { super(s); this.r =} public double getarea(){ double area=Math.PI*r*r; } }3) 第三个类是矩形类(Rectangle)继承自图形,含有两个double类型的成员 变量长a和宽b,有一个有三个参数的构造方法,来初始化颜色、长和宽, 一个方法getArea,返回值为double,获取矩形的面积值。public class Rectangle extends Shape{河南财经政法大学计算机与信息工程学院 第 58 页 实验报告public Rectangle(String s,double a, double b) { super(s); this.a = this.b =} public double getarea(){ double area=this.a*this.b; } }4) 第四个类是测试类(TestShape) ,分别定义圆形类和矩形类的实例对象, 并用 show 方法,getArea 方法来测试自己的定义public class Test { public static void main(String args[]){ Cricle a=new Cricle(&红&,2); Rectangle b=new Rectangle(&红&,2,4); System.out.println(a.show()+&\n&+b.show()+&\n&+a.area()+&\n&+b.ar ea()); } }假定银行的一个存取款系统有两类客户,一类是现金用户,一类是信用 卡用户。银行对每个客户都要登记其姓名 name,并为之分配一个唯一的 账户号码 aid,现金用户还要记录其卡的类型(工资卡、借记卡、理财卡), 而信用卡用户则根据其信用级别有一定的透支限额 lineOfCredit(A 级 10000 元、B 级 5000 元、C 级 2000 元、D 级 1000 元) 。每种客户都可以 实现存 deposit、取 withdraw、和查询余额 getBalance,信用卡用户还可 以查询透支情况 findOverdraw。对于现金用户,每次取款操作只能在账 户实际额度 balance 内操作,允许现金用户改变自己的帐户类型。分析有哪些属性和方法可以作为两个子类的共同属性和方法,然后写出抽象 类 Account 的定义。5.public class Account { protected S protected S p public Account(String name, String aid, int balance) { super(); this.name = this.aid = this.balance = } public void deposit(int amount){ this.balance+= } public void withdraw(int amount){河南财经政法大学计算机与信息工程学院 第 59 页 实验报告this.balance-= } public int getBalance(){ return this. } }分析 CashAccount 有那些新增的属性和方法,定义一个继承于 Account 的子类 CashAccount。public class CashAccount extends Account{ public S public CashAccount(String name, String aid, int balance,String kind) { super(name, aid, balance); this.kind=} public void withdraw(int amount){ if(amount&=this.balance) this.balance-= else System.out.println(&Wrong!&); } }分析 CreditAccount 有那些新增的属性和方法,然后定义一个继承于 Account 的子类 CreditAccount,添加增加的属性和方法。public class CredictAccount extends Account{ public int lineOfC public CredictAccount(String name, String aid, int balance, int lineOfCredit) { super(name, aid, balance); this.lineOfCredit = lineOfC } public int findOverdraw(){ return this. } }请按照要求编写一个程序 Test,用你所定义的类完成下列业务操作。用 Account 作为类型定义两个变量 credit 和 debit,分别引用 CreditAccount 和 CashAccount 的对象, 并完成存款 500 元的操作。每个对象完成取款 200 元的操作后再次取款 400 元,请输出 各自的余额。可以通过 credit 查看引用对象的透支额吗,如果不能,怎样 修改可以查看?public class Test { public static void main(String args[]){ Account credit=new CreditAccount(&小王&,&123&,0,1000);河南财经政法大学计算机与信息工程学院第 60 页 实验报告Account debit=new CashAccount(&小王&,&123&,0,&理财卡&); credit.deposit(500); System.out.println(credit.getBalance()); debit.deposit(500); System.out.println(debit.getBalance()); credit.withdraw(200); System.out.println(credit.getBalance()); debit.withdraw(200); System.out.println(debit.getBalance()); credit.withdraw(400); System.out.println(credit.getBalance()); debit.withdraw(400); System.out.println(debit.getBalance()); } } 结果500 500 300 300 -100 Wrong! 300评语日期年 月 日实验 16:面向对象基础编程(七)接口 实验目的1. 掌握接口的定义和实现接口的方法 实验要求和过程 如果将实验十五(1)的 Pet 类定义为接口,即所有相应方法全部为抽象方法, 则 Cat 类该如何改动?试改写相关代码,并验证你的结果。public interface Pet { public String move(); public String speak(); public String toString(); }河南财经政法大学计算机与信息工程学院第 61 页 实验报告public class Cat implements Pet{ protected S
public Cat(String name, int weight) { super(); this.name = this.weight = } public String getName() { } public String move(){ return &run&; } public String speak(){ return &Miao&; } public String toString(){ return &My pet & + } } public class Test { public static void main(String[] args){ Cat myCat = new Cat(&Spot&,10); System.out.println(myCat.toString() + &\n& + &Speak& + myCat.speak() +&\n& + myCat.move() + & & +myCat.getName() + &\n&); } } 实验结果My pet Spot SpeakMiao run Spot根据要求完成程序定义名称为 PCI()的接口,包括启动的方法 start 和关闭的方法 stop; 定义名称为 NetworkCard 的类表示网卡,实现 PCI 接口,并且其在实现 start 方法时输出“sending data……”,在实现 stop 方法时输出“network stop.” 定义名称为 SoundCard 的类表示声卡,实现 PCI 接口;并且其在实现 start 方法时输出“dudu……”,在实现 stop 方法时输出“sound stop.” 定义名称为 MainBoard 的类表示主板,包含方法 public void usePCICard(PCI p),在方法体内通过 p 来启动和关闭组件; 定义一个包含 main 方法的 Test 类,在 main 方法内创建一个 MainBoard 对河南财经政法大学计算机与信息工程学院 第 62 页 实验报告象用 mb 来引用,创建一个 NetworkCard 对象用 nc 来引用,创建一个 SoundCard 对象用 sc 来引用,分别通过 mb 来使用 usePCICard(PCI p)启动声卡和网卡。public interface PCI { public String start(); public String stop(); }public class SoundCard implements PCI{ public String start(){ return &dudu……”,&; } public String stop(){ return &sound stop&; } } public class NetworkCard implements PCI{ public String start(){ return &sending data……&; } public String stop(){ return &network stop.&; } } public class MainBoard { public void usePCICard(PCI p){ p.start(); p.stop(); } } public class Test { public static void main(String[] args){ SoundCard sc=new SoundCard(); NetworkCard nc=new NetworkCard(); MainBoard mb=new MainBoard(); if(mb.usePCICard()) {sc.stop();nc.stop();} }评语教师签字实验 17:泛型、枚举和内部类 实验目的1. 理解泛型的作用,掌握泛型类的声明及实例化的方法 2. 掌握枚举类的定义和使用 3. 掌握内部类的定义和使用方法河南财经政法大学计算机与信息工程学院 第 63 页日期:年月日 实验报告4. 掌握匿名类的使用方法 实验要求和过程 在 java.util 包中有一接口 Comparator&T&,提供了比较器的功能。请根据此 接口定义的比较方法,针对 10(3)的类 Account 中的余额字段,完成比较器类 的定义,并改写实验 10(3) ,验证你的设计是否正确,要求利用泛型机制。将实验 15(5)中的信用等级作为一个枚举类定义,并改写实验 15(5)的相关程 序程序:public public public public abstract class Account { S Spublic Account(String name, String aid) { super(); this.name = this.aid = } public void deposit(double balance){ this.balance+= } public void withdraw(double balance){ this.balance-= } public double getBalance(){ return this. } } public enum Dengji { A(0,10000), B(0,5000), C(0,2000), D(0,1000); private Grade(int c,int d){ this.a=c; this.b=d; } public int getGrade(){ return this.b-this.a;河南财经政法大学计算机与信息工程学院 第 64 页 实验报告} }模仿程序 8-5,利用匿名内部类实现对 Account 数组进行按照属性 id 的大小 进行排序。将实验 15(5)中的信用等级作为一个内部类定义, Account 类中提供查询信 在 用等级的方法,并改写实验 15(5)的相关程序评语教师签字实验 18:异常 实验目的理解异常现象及异常的抛出机制 掌握异常抛出时的处理机制 掌握自定义异常的方法 能够掌握在方法代码中添加抛出异常的处理 实验要求和过程 public class TestApp{ public static void main(String[] args){ try{ int i = 0; int j = 1 / String myname= if(myname.length()&2) System.out.print(“1”); }catch(NullPointerException e){ System.out.print(“2”); }catch(Exception e){ System.out.print(“3”); } } } 分析上述程序运行后的输出的结果和原因。结果:3 在运行 try 语句块过程中先出现了 ArithmeticException 异常,河南财经政法大学计算机与信息工程学院 第 65 页日期:年月日 实验报告是 Exception 的子类,所以运行 catch(Exception e){ System.out.print(“3”); } 结果为 3 下面是一个名称为 NegativeAmountException 的自定义异常类, 表示一个不正常 的银行账目事件类。填充下面的语句,完成该类的编写。class NegativeAmountException _extends____ Exception____{ //NegativeAmountException异常:用消息s创建异常 NegativeAmountException(String s){ super(s); } } class Account{ //构造函数,余额为0; public Account(){ balance = 0; } //构造函数,余额为n,如果初始余额小于0抛出异常 public Account( double n) throws NegativeAmountException{ if(n&0){ this.balance = }else { _ throw new NegativeAmountException (“余额小于 0”);______________________________________________; } } //查询余额方法,返回当前余额 public double getBalance(){ return this. } //存款方法,存款数额 如果存款数目小于0抛出异常 public void deposit(double amount)_throws__ NegativeAmountException { if(amount&=0){ balance+= }else { throw new NegativeAmountException(&存款出错&); } } //取款方法,取款数额 如果取款数目小于0抛出异常 public void withdraw(double amount)_ throws__ NegativeAmountException { if(amount&0){河南财经政法大学计算机与信息工程学院 第 66 页 实验报告throw new NegativeAmountException(&操作错误&); }else if(balance&amount){ throw new NegativeAmountException(&取款出错&); }else{ balance-= } } }程序:public class Account { //构造函数,余额为0; public Account(){ balance = 0; } //构造函数,余额为n,如果初始余额小于0抛出异常 public Account( double n) throws NegativeAmountException{ if(n&0){ this.balance = }else { throw new NegativeAmountExce}

我要回帖

更多关于 cpu风扇 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信