Java实验课经典题型加代码答案.doc_第1页
Java实验课经典题型加代码答案.doc_第2页
Java实验课经典题型加代码答案.doc_第3页
Java实验课经典题型加代码答案.doc_第4页
Java实验课经典题型加代码答案.doc_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

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

文档简介

Java实验课经典题型加程序代码1. 求出2-1000内的所有个位为3或7的素数,并按每行5列的格式输出。package first_work;public class First_work public static void main(String args) int i,m;int j=0;for(i=2;i=1000;i+)for(m=2;mMath.sqrt(i)&(i%10=3|i%10=7) System.out.print (i+ );j+;if(j!=0&j%5=0)System.out.println(); ;2. 教材71页15题,增加以下内容:同时要求用选择排序、冒泡排序、插入排序实现,分别用不同的函数实现。package first_work;public class Sort public static void choose(int x) for (int i=0; ix.length;i+) int lowerIndex=i;for (int j=i+1;jx.length;j+) if (xjxlowerIndex) lowerIndex=j; int temp=xi; xi=xlowerIndex; xlowerIndex=temp; public static void insert(int x) for (int i=1;i0;j-) if (xjxj-1) int temp=xj; xj=xj-1; xj-1=temp; public static void main(String args) int a=20,10,55,40,30,70,60,80,90,100;int i,j,x,T=0;for( i=1;i10;i+) for(j=0;jaj+1) T=aj;aj=aj+1;aj+1=T;for(x=0;x10;x+)System.out.print( );System.out.print(ax);System.out.print(n);choose(a);for(x=0;x10;x+)System.out.print( );System.out.print(ax);System.out.print(n);insert(a);for(x=0;x10;x+)System.out.print( );System.out.print(ax);3. 实现一个三行三列的矩阵和它的转置相加。package first_work;public class Matrix public static void main(String args) int array= 1, 2, 3, 4, 5, 6, 7, 8, 9; int i, j, t; int count=0; for (i = 0; i 2; i+) for (j = i + 1; j 3; j+) t = arrayij; arrayij = arrayji; arrayji = t; for (i = 0; i 3; i+) for (j = 0; j 3; j+) System.out.print(arrayij + ); count+; if(count!=0&count%3=0) System.out.println(); for (i = 0; i 3; i+) for (j = 0; j 3; j+) System.out.print(arrayij + arrayji+ ); count+; if(count!=0&count%3=0)System.out.println(); 4. 建立交通工具类TransTool,里面包含两个方法void run()和void brake(),然后建立它的Bike、Car以及Bus子类, 在各个子类中重写void run()和void brake()方法,在实现各个方法时只要输出一个自己定义的对应的提示信息即可。最后建立一个测试类MyTest, 该类中包含主函数,测试运行时的多态性。package yao2;public class MyTest public static void main(String args) Transtools s = new Transtools();s.run();s.brake();Car t = new Car();t.run();t.brake();Bus w = new Bus();w.brake();w.run(); class Transtools public void run() System.out.println(aa);public void brake() System.out.println(b);class Car extends Transtools public void run() System.out.println(aa);public void brake() System.out.println(b);class Bus extends Transtools public void run() System.out.println(aa);public void brake() System.out.println(b);5. 在工作目录中建立AAA.BBB.CCC包,在包CCC中建立一个名为Stack的public类,有两个私有成员变量:int stk和 int pos,分别表示创建的任意大小的堆栈和栈顶的索引,写出该类的构造函数、入栈函数void push(int x)、出栈函数int pop(), 另外,建立AAA.BBB.DDD包,在DDD包中定义一个测试类MyTest,其中包含主函数,测试Stack类的各个方法。package AAA.BBB.CCC;public class Stack int stk;int pos;public Stack(int stk, int pos) super();this.stk = stk;this.pos = pos;public void push(int x)if(pos0) System.pause();pos+;stkpos=x;for(int i=0;ipos;i+)System.out.println(stki);public void pop()pos-;package AAA.BBB.CCC.DDD;import AAA.BBB.CCC.*;public class MyTest extends Stackpublic MyTest(int stk, int pos) super(stk, pos);public static void main(String args)inta=1,2,3,4,5,6,7,8,9;int s=a.length;Stack m = new Stack(a, s);m.push(10);m.pop();System.out.println(s);6.查阅JAVA API文档,用StringBuffer类的相关方法实现如下字符串的操作:用字符串“Im a student.”构造一个StringBuffer类型的对象;用该对象的相关方法显示这个字符串的长度;用该对象的相关方法,在上述字符串的单词student前面插入一个单词good,形成一个新的字符串“Im a good student.”,显示新串。用该对象的相关方法,删除刚才插入的good,将字符串还原为原来的字符串“Im a student.”用该对象的相关方法,提取字符串“Im a student.”中索引下标为奇数同时它的ASCII码也为奇数的字符,形成一个新的字符串;用该对象的相关方法,在字符串“Im a student.”的末尾追加一个新的字符串:“I study at Nantong University.”,显示新串.package yao3;public class Str StringBufferpublic static void main(String args) StringBuffer s=new StringBuffer(I am a student);int k=s.length();System.out.println(k);s.insert(7, good );System.out.println(s);s.insert(1, 2, 3, 4);s.delete(7, 12);System.out.println(s);int h=s.lastIndexOf(I);System.out.println(h);int x=s.offsetByCodePoints(1, 3);System.out.println(x);String y=s.substring(2);System.out.println(y);String z = s.substring(3, 7);System.out.println(z);String w = s.toString();System.out.println(w);CharSequence a =s.subSequence(3, 7);System.out.println(a);System.out.println(b);for(int i=1;ik;i=i+2)char b = s.charAt(i);int e = b;System.out.println(e);if(e%2!=0) System.out.print(b);System.out.println();s.insert(14, , I study at Nantong University);System.out.println(s);7. 编写一个函数实现两个三行三列的矩阵相乘,函数的形参是两个二维矩阵,函数的返回值是成绩的二维矩阵,在主函数中进行测试。package third_4_22;public class Matrix public int Mul(int a,int b)intc=new int33;for(int i=0;i3;i+)for(int j=0;j3;j+)cij=aij*bij;System.out.println(cij);return c;public static void main(String args) int x=new int1,2,3,4,5,6,7,8,9;int y=new int1,1,1,1,1,1,1,1,1;Matrix w =new Matrix();int c=w.Mul(x, y);for(int i=0;i3;i+)for(int j=0;j3;j+)System.out.print(cij);int =w.Mul(x, y);w.Mul(x, y);8. 用二维for-each语句输出二维数组中每个元素的值。package third_4_22;public class Ergodic public static void main(String args) int array=12,23,34,45,56,67,78;for(int a:array)System.out.print(a+ );9. 在工作目录中建立AAA.BBB.CCC包,在包CCC中建立一个名为Queue队列类,有三个私有成员变量:int que, int front,int rear,分别表示创建的任意大小的队列、队列的头部和队列尾部的索引,写出该类的构造函数、入队函数void insert(int x)、出对函数int remove(), 另外,建立AAA.BBB.DDD包,在DDD包中定义一个测试类MyTest,其中包含主函数,测试Queue类的各个方法。package AAA.BBB.CCC;public class Queue int Queue;int front;int rear;public Queue(int queue, int front, int rear) super();Queue = queue;this.front = front;this.rear = rear;public void insert(int x)rear+;Queuerear=x;public int remove()int n=Queuefront;front+;return n;package AAA.BBB.DDD;import AAA.BBB.CCC.Queue;public class MyTest extends Queuepublic MyTest(int queue, int front, int rear) super(queue, front, rear);public static void main(String args) int a=1,2,3,4,5,6;Queue q = new Queue(a, 0, 5);q.insert(19);int m = q.remove();System.out.println(m);10. 建立交通工具抽象类TransTool,里面包含两个抽象方法void run()和void brake(),然后建立它的Bike、Car以及Bus子类, 在各个子类中重写void run()和void brake()方法,在实现各个方法时只要输出一个自己定义的对应的提示信息即可。最后建立一个测试类MyTest, 该类中包含主函数,测试运行时的多态性。package one;abstract public class TransTool public static void main(String args) Transtools s = new Car();s.run();s.brake();Car t = new Car();t.run();t.brake();s = new Bus();s.brake();s.run(); class Transtools public void run() System.out.println(TransTool);public void brake() System.out.println(TTT);class Car extends Transtools public void run() System.out.println(Car);public void brake() System.out.println(CCC);class Bus extends Transtools public void run() System.out.println(Bus);public void brake() System.out.println(BBB);11. 建立交通工具接口TransTool,里面包含两个方法void run()和void brake(),然后分别建立Bike、Car以及Bus类实现该接口,在实现各个方法时只要输出一个自己定义的对应的提示信息即可。最后建立一个测试类MyTest, 该类中包含主函数,测试接口回调。package two;public interface TransTool public void run();public void brake();package two;public class MyTest implements TransTool public void run() System.out.println(TransTool);public void brake() System.out.println(TTT);public static void main(String args) MyTest s = new MyTest();s.run();s.brake();12. 建立学生Student接口,包含方法查询学号方法void get_Snumber()和查询班级方法void get_Class(),建立Teacher接口,包含方法查询工号方法void get_Wnumber()和查询工资方法void get_Salary(), 建立一个在职研究生类Poststudent,实现这两个接口。最后建立一个测试类MyTest, 该类中包含主函数,测试接口回调。package three;public class Poststudent implements Student, Teacher public void get_Wnumber() System.out.println(工号:0001);public void get_Salary() System.out.println(工资:8000);public void get_Snumber() System.out.println(学号:1313012002);public void get_Class() System.out.println(班级:计师131);package three;public interface Student void get_Snumber();void get_Class();package three;public interface Teacher void get_Wnumber();void get_Salary();package three;public class MyTest public static void main(String args) Teacher t=new Poststudent();Student s =new Poststudent();s.get_Class();t.get_Salary();s.get_Snumber();t.get_Wnumber();13. 在工作目录中建立AAA.BBB.CCC包,在包CCC中建立一个名为Stack的类,有三个私有成员变量:char stk、int pos以及int size,分别表示创建的堆栈、栈顶的索引以及栈的容量大小,写出该类的构造函数、入栈函数void push(int x)、出栈函数int pop(), 取栈顶元素(元素不出栈)函数int peek()以及判断栈是否为空栈的函数void isEmpty();另外,建立AAA.BBB.RRR包,在包中建立一个字符串逆序类Myreverser,该类包括两个私有数据成员String input和String output,分别表示逆序前和逆序后的字符串,请写出Myreverser类的构造函数public Myreverser(String myinput)以及将字符串逆序函数public String reverse(),实现该逆序函数时要求使用AAA.BBB.CCC包中的Stack的类来做,算法思想是按照堆栈操作的先进后出的原则,将一个字符串的每个字符取出后依次入栈,所有字符都入栈完毕后再将栈中字符依次出栈后连接,即为逆序字符。注意,取字符串中索引为index的字符可以使用系统类库中String类的方法:public char charAt(int index),具体可以查阅API文档。最后,建立AAA.BBB.DDD包,在DDD包中定义一个测试类MyTest,其中包含主函数,使用Stack类和Myreverser类测试字符串的逆序功能。源代码:package AAA.BBB.DDD;import AAA.BBB.CCC.*;import AAA.BBB.RRR.Myreverser;public class MyTest private static final String input = null;private static final String output = null;public static void main(String args) TODO Auto-generated method stubchara=a;for(int i=-1;i4;i+)Stack m=new Stack(a,i,6);m.push(o);m.push(w);String s1=new String(a);System.out.println(s1);Myreverser k=new Myreverser(abc, cba);k.reverse();package AAA.BBB.CCC;public class Stack char stk;int pos;int size;public Stack(char stk, int pos, int size) super();this.stk = stk;this.pos = pos;this.size = size;public void push(char x)if(pos=size-1)System.out.println(栈满,无法入栈!);pos+;stkpos=x;for(int i=0;ipos;i+)System.out.println(stki);public void pop()if(pos=-1) System.out.println(栈空!不能出栈!);pos-;public int peek()return stkpos;public void isEmpty()if(pos=-1)System.out.println(栈为空!);package AAA.BBB.RRR;import java.lang.String;import AAA.BBB.CCC.Stack;public class Myreverser String input;String output;public Myreverser(String input, String output) super();this.input = input;this.output = output;public String reverse()char stk=a;Stack s=new Stack(stk, 0, 6);String str1=new String();String str2=new String(stk);int k=str2.length();System.out.println(k);Myreverser k=new Myreverser(abcdef,fedcba);for(int i=0;i=0;i-)char x=s2.charAt(i);System.out.print(x);return s2;14. 在工作目录中建立AAA.BBB.CCC包,在包CCC中建立一个名为MyStack的范型堆栈类,有两个私有成员变量:E stk和 int pos,分别表示创建的任意大小的堆栈和栈顶的索引,写出该类的构造函数、入栈函数void push(E x)、出栈函数E pop(), 另外,建立AAA.BBB.DDD包,在DDD包中定义一个测试类MyTest,其中包含主函数,测试MyStack类的各个方法,测试时要求分别E的取值包括Integer, String以及自己建立的一个类Student(自己定义) 。package AAA.BBB.CCC;public class MyStack E stk;int pos;int size;public MyStack(E stk,int size) super();this.stk = stk;this.pos = pos;this.size = size;public E push(E x)if(pos=size-1)System.out.println(栈满,无法入栈);pos+;return stkpos;public E pop()if(pos=-1)System.out.println(栈空,无法出栈);pos-;return stkpos;public void Show() if(pos=0;i-) System.out.print(stki+, ); package AAA.BBB.DDD;import AAA.BBB.CCC.MyStack;class Studentpublic int b;System.out.println()public class MyTest public static void main(String args) Integer a=new Integer3;MyStack b=new MyStack(a,3);b.push(8);b.pop();b.Show();String c=new String3;MyStack d=new MyStack(c,3);d.push(13);d.push(2);d.Show();d.pop();d.Show();Student stu=new Student3;MyStack f=new MyStack(stu,3);stu0=new Student();f.push(stu0);f.Show();System.out.println(successful);15. 利用jdk提供的类库中的Stack完成上述测试类MyTest的测试要求。package AAA.BBB.DDD;import java.util.*;public class MyTest_2 public static void main(String args) Stack a=new Stack();a.push(1);a.push(2);a.push(3);System.out.println(a.pop();Stack b=new Stack();b.push(45);b.push(88);System.out.print(b.pop();Stack c=new Stack();Student s=new Student3;s0=new Student();c.push(s0);System.out.print(c.pop();16. 利用jdk提供的类库中的Scanner类从键盘输入任意一组字符串,要求在字符串的中间位置插入一个字串:hello,使用jdk提供的类库中的StringBuffer类。package three;import java.util.Scanner;public class StrFanxing public static void main(String args) char ss; Scanner cin=new Scanner(System.in); String str,b=; str=cin.nextLine(); char s; s=new charstr.length(); for(int i=0;is.length;i+) si=str.charAt(i); for(int j=0;jstr.length();j+) for(int k=j+1;ksk) ss=sj; sj=sk; sk=ss; for(int q=0;qstr.length();q+) b=b+sq; StringBuffer s1=new StringBuffer(str);String ar=hello;System.out.println(s1.insert(str.length()/2, ar);17. 利用jdk提供的类库中的ArrayList类建立一个字符串的动态数组,数组中的所有元素要求从键盘输入,数组建立好了以后,要求当输入一个指定的字符串时,在刚才的数组中查找该字符串,如果找到,将该字符串元素删除,如果找不到,则给出提示信息。package four;import java.util.*;public class Array public static void main(String args) ArrayList ar=new ArrayList();Scanner cin=new Scanner(System.in);String str; str=cin.nextLine(); ar.add(str); Scanner cin1=new Scanner(System.in); String str1; str1=cin1.nextLine(); int local1=str.indexOf(str1); if(local1str.length() str=str.replace(str1, ); System.out.println(local1); System.out.println(str); else System.out.println(-1);18. 设计一个程序,不考虑程序代码的算法意义,只要求程序代码可能产生NullPointerException异常、ArithmetricException异常、ArrayIndexOutofBoundException异常以及NubmerFormatException异常,并通过try-catch语句捕获以上各种异常,做不同的处理。package one;public class wrong public static void main(String args) tryString s=null;int l=s.length();int a=new int 6;a8=89;int k=8/0;String str =230U;double d= Double.parseDouble(str);catch(NullPointerException e)System.out.println(空指针!);catch(ArrayIndexOutOfBoundsException e)System.out.println(越界!);catch(ArithmeticException e)System.out.println(除数为0!);catch(NumberFormatException e)System.out.println(转换异常!);19. 自定义一个异常类MyException, 重写他ToString() 方法,给出个性化的异常显示信息。当从键盘输入一个字符串,当该字符串中包含数字字符时,抛出一个MyException异常,否则不抛出异常。用两种方法完成该题,第一种方法要求异常的抛出和处理在main()函数中实现;第二种方法要求抛出异常和处理异常在不同的函数中处理,也就是在一个函数中抛出异常,但是不处理,在调用该函数的上层函数体中捕获并处理该异常。package two;import java.util.Scanner;class MyException extends Exception private String myString;public MyException(String myString)this.myString = myString; public String getMyString()return myString;public String toString()return myString; public class Two1 public static void main(String args) String str;Scanner sc=new Scanner(System.in);System.out.println(请输入字符串:);str=sc.next(); try for(int i=0;i=48&str.charAt(i)=58) throw new MyException(异常,包含数字!); System.out.println(不抛出异常);catch (MyException e) System.out.println(e);package Two_2;import java.util.Scanner;class MyException extends Exception private String myString;public MyException(String myString)this.myString = myString; public String getMyString()return myString;public String to

温馨提示

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

评论

0/150

提交评论