JAVA实验课内容.doc_第1页
JAVA实验课内容.doc_第2页
JAVA实验课内容.doc_第3页
JAVA实验课内容.doc_第4页
JAVA实验课内容.doc_第5页
已阅读5页,还剩66页未读 继续免费阅读

下载本文档

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

文档简介

实验1 JAVA基础知识1、 实验目的:(1) 掌握环境变量path、classpath的设置(2) 判断语句、分支语句、循环语句2、 实验内容:21编写“Hello world”显示程序。(1) 利用ECLIPSE开发环境开发并执行。(2) 若(1)已完成,此时关闭ECLIPSE, 如何使字节码执行?应做哪些工作?22编写一个字符界面的JAVA Application程序,接受用户输入的10个整数,比较并输出其中的最大值和最小值。23编写一个字符界面的JAVA Application程序,接受用户输入的字符,以“#”标志输入的结束;比较并输出按字典排序最小的字符。24输入两个整数,求它们的最大公约数。25输入两个整数,求它们的最小公倍数。26求sum=1 + 1/2 + 1/3 +1/n, 当n为多少时,sum5.027编写一个java应用程序 ,使用for循环计算8+88+888+。的前10项之和。28编写一个java应用程序,计算1+2!+3!+4!+10! 之和.29已知某整形数组,从小到大排列,并输出。210求解约瑟夫问题:12个人排成一圈,从1号报数,凡是数到5的人就走出队列(出局),然后继续报数。试问最后出局的一人是谁。211用“埃氏筛法”求2100以内的素数。2100以内的数,先去掉2的倍数,再去掉3的倍数,再去掉4的倍数,最后剩下的就是素数。3学习二维数组31规则二维数组初始化(1) int a = null;a = new int23;(2) int a=new int23(3) int a=1,2,3, 4,5,6 表明是2行3列的数组,分别是 a00,a01,a02a10,a11,a12等号左侧的内不能有数字。与c,c+几乎一致。32不规则二维数组初始化。JAVA有此特点,C没有(1) int a = 1,2,3,4,5;表明是2行3列数,第1维2个数:a00=1,a01=2; 第2维3个数: a10=3,a11=4,a12=5;多维数组如是初始化必须大括号套大括号:1,2,3,4,5,可清晰看出每维有多少元素。(2) int a = new int2; /给第1维分配后有a0和a1a0 = new int3; /再分别给a0,a1第2维分配空间a0有3个元素a1 = new int4 a1有4个元素33实例(1) 调通下面程序,分析结果,总结多维数组中length属性规律。public class Test public static void main (String args ) int a = new int2; a0 = new int3; a1 = new int4; System.out.println(a.length=+ a.length); System.out.println(a0.length=+ a0.length); System.out.println(a1.length=+ a1.length); (2) 调通下面程序,分析结果,总结什么时候是null, 什么时候不是null, 什么时候是数组元素的具体值,什么时候有是地址值。public class Test public static void main (String args ) int a = new int2; System.out.println(a=+ a); System.out.println(a0=+ a0); System.out.println(); a0 = new int3; System.out.println(a=+ a); System.out.println(a0=+ a0); System.out.println(a00=+ a00); 思考题:1、 简述path、classpath环境变量的作用。2、 写出3.3中两个问题的总结。3、 写出2.2,2.3中程序源码。实验2 类与对象应用1、 实验目的(1) 增强对构造函数含义的理解。(2) 增强对“对象引用”含义的理解。(3) 静态变量(类)变量static的运用。2、 实验内容2.1编写一个基本账户类。成员变量包含帐号、储户姓名和存款余额等。方法有存款和取款等。并编写一个测试程序加以测试。2.2先学习以下复数加法类的不同编制方法。然后编制实现分数加法的类。方法1:class Complexfloat real;/结果实部float virt;/结果虚部void Add(float r1, float v1, float r2, float v2)real = r1+r2;virt = v1+v2; void display()System.out.println(+ real + i + virt);方法2:class Complexfloat real;/复数实部float virt;/复数虚部Complex()Complex(float r, float v)real = r;virt = v;Complex Add(float r2, float v2)Complex result = new Complex();result.real = real + r2;result.virt = virt + v2;return result;void display()System.out.println(+ real + i + virt);方法3class Complexfloat real;/复数实部float virt;/复数虚部Complex()Complex(float r, float v)real = r;virt = v;Complex Add(Complex c2)/注意与法2的不同Complex result = new Complex();result.real = real + c2.real;result.virt = virt + c2.v2;return result;void display()System.out.println(+ real + i + virt);(1) 对方法1来说:定义的成员变量是结果复数的实部与虚部,而函数Add接收的是以4个实数表示的2个复数。复数的表示方法不一致。因此不是最优的。(2)方法2中Complex Add(float r2, float v2)表示含义是: 当前对象是Complex对象 结果返回是Complex对象 传入的复数是以两个实数r2,v2体现的,不是Complex对象。(3)方法3中Complex Add(Complex c2)表示含义是:当前复数、传入复数、结果复数都是Complex对象,因此方法3是最优的。通过对比,可以得出:(1) 设计类最困难的应该是成员变量的定义。(2) 类对象的描述是唯一的。2.3静态变量的用法与c+是一样的,成员静态变量可以作为类的全局变量,均只初始化一次。(1)调通下述程序,分析结果原因class Astatic int m;A()m +;public class Test1 /* * param args */public static void main (String args )System.out.println(A.m);A Obj1 = new A();System.out.println(Obj1.m);System.out.println(A.m);(2) 定义学生基本信息类:NO(学号),name(姓名), 要求学号动态生成,且唯一。并编测试类验证。4、 深入理解引用的含义:在传参时,基本数据类型是按拷贝传递的;数组及对象是作为引用传递的 (1)调通下述程序,为什么a0由1变为10 ?class Avoid f(int b)b0 = 10;public class Test1 public static void main (String args )int a = 1,2,3,4,5;A obj = new A();obj.f(a);System.out.println(a0);(2) 调通下述程序,与(1)比较,为何a0没有变化?class Avoid f(int b)b = new int10;b0 = 10;public class Test1 public static void main (String args )int a = 1,2,3,4,5;A obj = new A();obj.f(a);System.out.println(a0);4. 思考题(1) 写出2.2程序代码。(2) 写出2.3程序代码。(3) 分析4中的结果实验3 类与继承1、实验目的(1)基本数据类型封装类的应用。(2)类的继承。(3)this, super应用。2、实验内容21已知字符串s = “123”, 把它转换成整形数t = 123, 直接在main函数中完成即可。22 设计一个点类,它仅包含两个属性:横坐标和纵坐标。通过继承点类再设计一个圆类,它除了有一个圆心,还有半径,还应该能计算圆的周长和面积等。编写测试类加以测试。23先设计一个基本帐户类,再通过继承基本帐户类设计一个储蓄帐户类,储蓄帐户类中增加一个静态成员变量(年利率)。并增加如下方法:(1)计算月利息-存款金额*年利率/12(2)更改利率(静态方法)-重新设置年利率。最后,编写一个测试类。1、 学习知识31深入理解父子类构造函数已知A类如下:class Aint x;int y;A(int x, int y)this.x = x;this.y = y;问题1:若B类如下,为什么编译不过去?如何修改?class B extends Aint z;B(int x, int y, int z)this.x = x;this.y = y;this.z = z;问题2:假设问题1已经解决,你认为上述B类代码与下述代码哪个更好?class B extends Aint z;B(int x, int y, int z)super(x, y);this.z = z;问题3:假设测试类如下:public class Test1 public static void main (String args ) A obj = new A2; System.out.println(obj0+ +obj1); obj0 = new A(1,2); obj1 = new A(3,4); System.out.println(obj0+ +obj1); 请回答A obj = new A2调用构造函数了吗? 输出结果为地址null nullAa90653 Ade6ced32学习包package就目前所学知识而言,如果所有字节码文件都在d:jstudy下,那么环境变量设为classpath=d:jstudy即可,但很明显这是一种理想情况。通常字节码可以在任一目录下,那么若想让这些字节码运行,就必须设置任意多的环境变量(classpath)的值,而且还不一定设置完全。package技术较好地解决了这一问题:既尽可能少的设置环境变量classpath的值,确能使更多目录下的字节码都能运行。详细内容见课件实验4 多态1、实验目的:(1) 普通类、抽象类、接口的多态实现(2) 对“动态绑定”的深刻理解。2、实验内容:21利用普通类、抽象类、接口分别实现求圆和长方形的面积。并编制测试类。22 已知代码如下:abstract class Apublic int m = 10;abstract public void f1();public void f2()System.out.println(“这是抽象类A中的f2 函数”);class B extends Apublic int m=20;public void f1()System.out.println(“这是B类中的f1函数”);public void f3()System.out.println(“这是B类中f3函数”); public class Testpublic static void main(String args)A objA = new B();objA.f1();objA.f2();System.out.println(objA.m);B objB = (B)objA;objB.f3();(1) 使该程序执行,分析斜体语句输出结果原因.(2) 把抽象类A中的f2函数移到子类B中,A中则不包含f2函数,其它不变,为何编译通不过。23已知代码(非多态)如下:两个单独类,圆类及矩形类,都包括求面积及周长函数。测试类描述如下:shape = 1表示圆 = 2 表示矩形select = 1表示求周长 =2 表示求面积事例中shape=1,select=2 表示求圆的面积。class Circlefloat r;public Circle(float r)this.r = r;/求圆面积public float getArea()return (float)Math.PI*r*r;/求圆周长public float getTotalLen()return 2.0f*(float)Math.PI*r;class Rectanglefloat width; /宽度float height; /高度public Rectangle(float width, float height)this.width = width;this.height= height;/求矩形面积public float getArea()return width*height;/求矩形周长public float getTotalLen()return (width+height)*2.0f; public class Testpublic static void main(String args)int shape = 1;int select =2;Circle cObj = null;Rectangle rObj=null;/根据标志初始化对象switch(shape)case 1: /是圆cObj = new Circle(10.0f);break;case 2: /是矩形rObj = new Rectangle(8.0f, 9.0f);break;/执行功能switch(select)case 1: /求周长if(shape=1) /求圆的周长 System.out.println(cObj.getTotalLen();if(shape=2) /求矩形的周长 System.out.println(rObj.getTotalLen();break;case 2: /求面积if(shape=1) /求圆的面积 System.out.println(cObj.getArea();if(shape=2) /求矩形的面积 System.out.println(rObj.getArea();break;(1) 先调通该程序,并主要理解主程序的层次结构.(2) 在该例基础上,把之改为多态程序(用接口实现),尤其比较主程序与非多态主程序的区别。思考题:(1) 写出2.1的功能类代码,不包含测试类(2)写出2.3的多态功能类及测试类代码。(3)多态设计思想的基本模式是什么?补充材料1、 编制实现两正整数最大公约数、最小公倍数的类。public class MyInt int m;int n;public MyInt(int m, int n)this.m = m;this.n = n;int getGongYue()int value = mn?n:m;if(m%value!=0 | n%value!=0)for(value=value/2; value=1; value-)if(m%value=0 & n%value=0)break;return value;int getGongBei()int value = mn?m:n;int step = value;while(value%m!=0 | value%n!=0)value += step;return value;2、 编制分数相加功能类public class MyFenShu int fenzi;int fenmu;public MyFenShu()fenzi = 0;fenmu = 1;public MyFenShu(int fenzi, int fenmu)this.fenzi = fenzi;this.fenmu = fenmu;MyFenShu Add(MyFenShu second)MyFenShu result = new MyFenShu();MyInt obj = new MyInt(fenmu, second.fenmu);int gongbei = obj.getGongBei();int ratio1 = gongbei/fenmu;int ratio2 = gongbei/second.fenmu;result.fenzi = fenzi *ratio1 + second.fenzi*ratio2;result.fenmu = gongbei;MyInt obj2 = new MyInt(result.fenzi, result.fenmu);int gongyue = obj2.getGongYue();result.fenzi /= gongyue;result.fenmu /= gongyue;return result;void display()System.out.println(the fenshu is:t + fenzi + / + fenmu);(1) 体会在该类中是如何应用MyInt类的,进而理解编制类的好处所在。3、 关于Class类(本示例是解析类有哪些公有变量和公有方法)import java.lang.reflect.*;class Apublic int m;protected int n;int x;void func3()public void func2()public void func1()class RunTimeClassInfo public static void main(String args) Object obj = new A();Class cls = obj.getClass();System.out.println( 类名: + cls.getName() );Field fields = cls.getFields();for( int i=0; ifields.length; i+ )Field f = fieldsi;System.out.println( 域: + f.getName() + : + f );Method methods = cls.getMethods();for( int i=0; imethods.length; i+ )Method m = methodsi;System.out.println( 方法: + m.getName() + : + m );(1) 可以看出通过Class类可解析出public公有信息(成员或变量)。4、 关于instanceof关键字主要用于父子类中,如下图所示:Parentchild1child2childnParent obj = new 某个子类有可能在程序后续某位置处要判定父类声明obj到底是子类中哪一个child ,这时要用到instanceof。例如:有下述程序。interface MyWritepublic boolean write(String s);class ScreenWrite implements MyWritepublic boolean write(String s)return true;class FileWrite implements MyWritepublic boolean write(String s)return true;class ManageWriteMyWrite obj;public ManageWrite(MyWrite obj)this.obj = obj;if(obj instanceof ScreenWrite)System.out.println(Screen write begin!);if(obj instanceof FileWrite)System.out.println(File write begin!);public boolean write(String s)return obj.write(s);public class Test /* * param args */public static void main(String args) / TODO Auto-generated method stubScreenWrite obj = new ScreenWrite();ManageWrite mobj = new ManageWrite(obj);mobj.write(hello);父类是接口MyWrite, 一个接口函数write(String s); 向屏幕输出子类ScreenWrite及向文件输出子类FileWrite。着重注意ManageWrite类,封装了一个成员变量MyWrite Obj,它是FileWrite及ScreenWrite的父类,很明显是多态行为,在该类构造函数中利用instanceof判别了MyWrite到底是FileWrite还是ScreenWrite.也就是说当成员变量有一个多态声明或函数参数是多态的话,这时有可能在程序某位置处根据需要用到instanceof。实验5 异常处理1、实验目的(1)掌握try-catch-finally用法(2)throw, throws用法(3)理解JAVA异常处理框架思想2、实验内容2.1分析下列程序结果,体会其中的原因。import java.io.*;public class MyApp public static void main(String args) / TODO Auto-generated method stubint n = 1;while(true)tryif(n = 1)throw new IndexOutOfBoundsException(Index out);if(n = 2)throw new ArithmeticException(Arithmetic);catch(IndexOutOfBoundsException e)e.printStackTrace();n + ;if(n = 3)break;catch(ArithmeticException e)e.printStackTrace();n + ;if(n = 3)break;/while(true)System.out.println(END);任何异常都有产生(throw),捕获(catch)过程,上述示例中是通过强制throw产生异常的。那么下面示例中没有显示看出哪里throw生成异常,为什么却有异常显示结果(进入catch)呢?import java.io.*;public class MyApp public static void main(String args) / TODO Auto-generated method stubint a = new int3;int sum = 0;int b = 0;tryfor(int i=0; i10; i+)sum += ai;sum = sum /b;System.out.println(sum);catch(IndexOutOfBoundsException e)System.out.println(This is index out exception);catch(ArithmeticException e)System.out.println(this is math exception);System.out.println(END);很明显throw是系统隐式抛出的,也就是说我们看见的一行执行代码在系统中其实是执行了多个判断,如下所示。程序中一行代码在系统执行时Sum = sum+ai;Sum=sum+ai;If 越界异常 throw 越界异常If 数学异常 throw 数学异常也就是说,若有异常产生一定是显示throw, 或者系统隐式抛出的。2.2程序功能:从键盘输入一个整数,求100除以它的商,并显示。要求:必须保证键盘输入的数非0。请以下述程序段为基础加以修改。public class Test1 /* * param args */public static void main(String args) / TODO Auto-generated method stubbyte buf = new byte50;int count = System.in.read(buf);String strBuf = new String(buf, 0, count);int n = Integer.parseInt(strBuf);int m = 100/n;System.out.println(m);2.2已知学生基本类Student及年龄异常类AgeException,测试类如下:import java.io.*;class AgeException extends Exceptionpublic AgeException(String s)super(s);class StudentString strName;int age;public Student(String strName, int age) throws AgeExceptionthis.strName = strName;this.age = age;if(age200)throw new AgeException(年龄错误);public class Test public static void main(String args) try Student s = new Student(zhangsan, -10); catch(AgeException e) e.printStackTrace(); (1) 调通此程序,理解throw(术语:抛出异常),throws(术语:抛弃异常),try-catch之间的逻辑关系。(2) 仿上述,编如下功能:已知三角型类Triangle, 成员变量即三边长度,判断给定的三边长能否构成一个三角形,要有异常处理类TriangleException类。实验5 字符串操作1.实验目的:(1) 熟练掌握String, StringBuffer, StringTokenizer类的用法。2.学习StringJava使用java.lang包中的String类来创建一个字符串变量,因此字符串变量是对象。 (1)字符串常量 如:“你好”,“1234.987”,“weqweo”。 (2)创建字符串 使用String类的构造方法,例如:String s=new String(“we are students”);String类的常见构造方法:String(String original)创建一个String对象为original的拷贝。String(char value)用一个字符数组创建一个String对象String(char value,int offset,int count)用一个字符数组从offset项开始的count个字符序列创建一个String对象当然也可用“=”创建String s = “Hello”; (3)字符串常用方法public char charAt(int index)返回字符串中第index个字符。public int length()返回字符串的长度。public int indexOf(String str)返回字符串中出现str的第一个位置public int indexOf(String str,int fromIndex)返回字符串中从fromIndex开始出现str的第一个位置public boolean equalsIgnoreCase(String another)比较字符串与another是否一样(忽略大小写)public String replace(char oldChar,char newChar)在字符串中用newChar字符替换oldChar字符public boolean startsWith(String prefix)判断字符串是否以prefix字符串开头public boolean endsWith(String suffix)判断字符串是否以prefix字符串结尾public String toUpperCase()返回一个字符串为该字符串的大写形式public String toLowerCase()返回一个字符串为该字符串的小写形式public String substring(int beginIndex)返回该字符串从beginIndex开始到结尾的子字符串public String substring(int beginIndex,int endIndex)返回该字符串从beginIndex开始到endIndex结尾的子字符串public String trim()返回将该字符串去掉开头和结尾空格后的字符串例1:public class Test public static void main(String args) String s1 = sun java,s2 = Sun Java; System.out.println(s1.charAt(1); /u System.out.println(s2.length(); /8 System.out.println(s1.indexOf(java); /4 System.out.println(s1.indexOf(Java); /-1 System.out.println(s1.equals(s2); /false System.out.println(s1.equalsIgnoreCase(s2); /true String s = 我是程序员,我在学java; String sr = s.replace(我,你); System.out.println(sr); /你是程序员,你在学java 例2:public class Test public static void main(String args) String s = Welcome to Java World!; String s1 = sun java ; System.out.println(s.startsWith(Welcome); /true System.out.println(s.endsWith(World); /false String sL = s.toLowerCase(); String sU = s.toUpperCase(); System.out.println(sL); /welcome to java world! System.out.println(sU); /WELCOME TO JAVA WORLD! String subS = s.substring(11); System.out.println(subS); /Java World! String sp = s1.trim(); System.out.println(sp); /sun java (4)字符串与数字的转化数字转化为字符串:利用String中的静态方法可以将基本数据类型转化为字符串public static String valueOf(double d)public static String valueOf(int i) 字符串转化为数字:使用java.lang包中的Byte、Short、Integer 、 Long、Float、Double类调相应的类方法:public static byte parseByte(String s) throws NumberFormatExceptionpublic static short parseShort(String s) throws NumberFormatExceptionpublic static short parseInt(String s) throws NumberFormatExceptionpublic static long parseLong(String s) throws NumberFormatExceptionpublic static float parseFloat(String s) throws NumberFormatExceptionpublic static double parseDouble(String s) throws NumberFormatException 例1:class Testpublic static void main(String args)String s = 123; /将字符串“123”转化为123int a = Integer.parseInt(s);System.out.println(a+1); /124int b = 200;/将整形数200转化为“200”String s2=String.valueOf(b);System.out.println(s2);3、StringTokenizer类用法主要用来拆分字符串。(1)构造函数:StringTokenizer(String str) :构造

温馨提示

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

评论

0/150

提交评论