Java实验课经典题型加代码答案

2018-10-19 20:40

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;m<=Math.sqrt(i);m++) {if(i%m==0)break;} if(m>Math.sqrt(i)&&(i==3||i==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; i0;j--) { if (x[j]

int temp=x[j]; x[j]=x[j-1]; x[j-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;i<10;i++) for(j=0;j<9;j++) if(a[j]>a[j+1]) { T=a[j]; a[j]=a[j+1]; a[j+1]=T; } for(x=0;x<10;x++) { System.out.print(' '); System.out.print(a[x]); } System.out.print('\\n'); choose(a); for(x=0;x<10;x++) { System.out.print(' '); System.out.print(a[x]); } System.out.print('\\n'); insert(a); for(x=0;x<10;x++) { System.out.print(' '); System.out.print(a[x]); } } }

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 = array[i][j]; array[i][j] = array[j][i]; array[j][i] = t; } } for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { System.out.print(array[i][j] + \ 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(array[i][j] + array[j][i]+\ 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(\ } public void brake() { System.out.println(\ } }

class Car extends Transtools { public void run() { System.out.println(\ } public void brake() { System.out.println(\ } }

class Bus extends Transtools { public void run() { System.out.println(\ } public void brake() { System.out.println(\ } }

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(pos<0) System.pause(); pos++; stk[pos]=x; for(int i=0;i

package AAA.BBB.CCC.DDD; import AAA.BBB.CCC.*; public class MyTest extends Stack{ public MyTest(int[] stk, int pos) { super(stk, pos); } public static void main(String[] args) { int[]a={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类的相关方法实现如下字符串的操作: 用字符串“I'm a student.”构造一个StringBuffer类型的对象; 用该对象的相关方法显示这个字符串的长度; 用该对象的相关方法,在上述字符串的单词student前面插入一个单词good,形成一个新的字符串“I'm a good student.”,显示新串。

用该对象的相关方法,删除刚才插入的good,将字符串还原为原来的字符串“I'm a student.” 用该对象的相关方法,提取字符串“I'm a student.”中索引下标为奇数同时它的ASCII码也为奇数的字符,形成一个新的字符串;

用该对象的相关方法,在字符串“I'm a student.”的末尾追加一个新的字符串:“I study at Nantong University.”,显示新串. package yao3; public class Str { StringBuffer


Java实验课经典题型加代码答案.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:人教版五年级上册第一单元知识总结与练习

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: