1、南昌大学Java实验报告2南昌大学实验报告学生姓名:学号:专业班级:实训类型: 验证 综合 设计 创新 实验日期:2017.11.8 实验成绩:一、 实验项目名称字符串和数组二、 实验的评分标准实验分为AF,A为最高,F最低。F:在规定时间内没有完成所有的实验,而且没有及时提交实验报告,或者实验过程中出现了抄袭复制他人实验代码。D:能完成实验,但是实验结果出现严重错误,不能体现对教学内容的理解。C:能基本完成实验,实验结果基本正确。但是实验内容有较少的错误,提交的实验代码质量一般。B:能较好的完成实验,实验报告条理清楚,实验代码结构清晰,代码质量较高,及时更正试验中出现的错误,并对运行中一些异
2、常错误进行分析,解释错误产生的原因。A:能较好的完成实验,实验代码质量高,实验报告完成度高,能在实验完成的基础上,根据个人的理解增加实验的新功能,具有一定的创新能力。三、 实验目的和要求1.掌握java的基础知识。2.掌握和运用java的控制语句和数组。3.掌握和运用java的字符串。四、 实验内容1.使用BigInteger计算超大整数的问题。2.结合字符串和数组计算超大整数的问题这里,我模仿Math写了专门计算数组形式存放整数的一个工具类(见后面源代码),只有静态函数,想来挑战Biginteger类。在循环体计算前后,分别用时间函数获取系统当前时间,作差来近似等价于核心运算时间。Bigin
3、teger类耗时1ms,自己写的方法耗时13ms,相差13倍,惨败。3.字符串的“=”和equals()方法。详细结果解释在源代码注释中给出。4.统计英文单词5.用StringBuffer模拟扑克牌洗牌6.数独游戏Clear:清除用户输入的所有数据Restart:开始新的数独游戏Check:检查用户输入数据的正确性提示框用户输入的数字显示蓝色,已知的数字显示黑色为增加可判断性,做出了灰色阴影效果7.财务应用程序,比较不同利率下的贷款。8.财务应用程序,信用卡号的合法性实验源代码如下:1.使用Biginteger计算大数package one;importjava.math.BigInteger
4、;public class BigintegerCal public static void main(String args)BigInteger res = new BigInteger(1);BigInteger b = new BigInteger(1);long start = System.currentTimeMillis();for(inti = 1;i=100;i+) b = b.multiply(new BigInteger(2);res = res.multiply(b.add(BigInteger.ONE); long end = System.currentTimeM
5、illis();System.out.println(res.toString();System.out.println(After + (end-start) + ms); 2.字符串或数组计算大数=part1=package two;/* this class is designed to offer tools to calculate* huge integer numbers via plain integer array*/public class ArrayCal privateArrayCal(); /*array a multiply array b*/public stat
6、ic int mul(int a,int b)int res = new inta.length + b.length;for(inti = 0;i=0;-i)for(int j = a.length-1;j=0;-j)tmp += (resj+i+1 + aj*bi);resj+i+1 = tmp %10;tmp /= 10; resi = tmp;tmp = 0; returnArrayCal.clean(res); /*array a add a single integer*/public static int add(int a,intnum)int res = new inta.l
7、ength+1;for(inti = a.length-1;i=0;i-)num = ai +num;resi+1 = num % 10;num /= 10; res0 = num;returnArrayCal.clean(res); /* array a multiply a single integer*/public static int mul(int a,intnum)int res = new inta.length+1;int c = 0;/prefor(inti = a.length-1;i=0;i-) c = ai * num + c;resi+1 = c %10;c /=
8、10; res0 = c;returnArrayCal.clean(res); /*clean the redundant 0 in the front of the array*/public static int clean(int a)if(a0!=0)return a;elseint count = 0;for(inti = 0;ia.length& ai=0;+i)count+;int res = new inta.length - count;System.arraycopy(a,count,res,0,a.length-count);return res; =part2=pack
9、age two;public class Calculate public static void main(String args)int part2 = 1;int part1;int res = 1; long start = System.currentTimeMillis(); for(inti = 1;i0 for more ,0 for lessSystem.out.println(4.+ pareTo(three); /same as 2System.out.println(5.+one.intern() = three.intern(); 4.统计英文单词package fo
10、ur;importjava.util.ArrayList;importjava.util.Scanner;public class CountWord public static void main(String args) Scanner input = new Scanner(System.in);ArrayList list = new ArrayList();System.out.println(Please input an English sentence,seperated by blank:); String data = input.nextLine();int count
11、= new intdata.length(); /split the string inputted by the user with / making it StringString dataNew = data.split( );for(String e:dataNew)if(!list.contains(e)list.add(e);countlist.size()-1+; elsecountlist.indexOf(e)+; int max = count0;int index = 0;for(inti = 1;imax)max = counti;index = i; System.ou
12、t.println(The word with the most appearance time: + dataNewindex);System.out.println(The times of its appearance: + max); 5.StringBuffer模拟扑克牌洗牌=part1=package five;/* the class is designed to create the basic Poke model* in which user can shuffle it ,display the cards*/class Poke private char type =
13、,; private String value = 3 ,4 ,5 ,6 ,7 ,8 ,9 ,10,J ,Q ,K ,A ,2 ; privateStringBuffer cards = new StringBuffer(); public Poke() for(char x : type) for(String y : value) this.cards.append(x+y); void shuffle(int times) for(inti=0;i= 3 & (len-1 -right)/3 = 3) /the amount of cards to be shuffled at rand
14、om on each side(13) numOfL = (int)(Math.random()*3+1); numOfR = (int)(Math.random()*3+1); temp.append(cards.substring(left-numOfL*3, left); temp.append(cards.substring(right, right+numOfR*3); left -= numOfL*3; right += numOfR*3; /less than 3 cards in each side,put them all in the new string if(left!
15、=0) temp.append(cards.substring(0, left); if(right!=len-1) temp.append(cards.substring(right, len); cards = temp;/renew the original cards /shuffle finished voidshowCards() for(inti = 0;i52;i+) if(i+13)%13=0) System.out.println(); System.out.print(cards.substring(3*i, 3*i+3)+ ); System.out.println()
16、; =part2=package five;public class Shuffle public static void main(String args) Poke poke = new Poke();poke.shuffle(4); /shuffle for 4 timespoke.showCards(); 6.数独游戏=part1=package six;import java.util.Arrays;/*this class is designed to generate one solution of Sudo,to mask several numbers at random i
17、n all 81 numbers to raise a Sudo problem,to define the rules to check whether the numbers inputted by user are correct */public class Sudo private int solution; /initialize the sudoku according to /-the regularity of the sum of i and j public Sudo() solution = new int9; for(inti = 0;i9;i+) solutioni
18、 = new int9; for(int j = 1;j9)?(i+j-9):i+j; martrix();colSwap(); mask(); /return a concrete and random solution public intgetDigit(inti,int j) return solutionij; /construct a situation object to the sudoku rules /by extracting the line147,258,369 from the solution private void martrix()int temp = ne
19、w int99; for(inti = 0,j = 0;i3;i+) tempj+ = Arrays.copyOf(solutioni,solutioni.length); tempj+ = Arrays.copyOf(solutioni+3,solutioni+3.length); tempj+ = Arrays.copyOf(solutioni+6,solutioni+6.length); solution = temp; /based on the123,456,789 column group /swap each column value among each group priva
20、te void colSwap()intcolForSwap= 0;int temp = new int9; for(inti = 0;i9;i+) switch(i) /choose the column to swap case 0: case 1: case 2: colForSwap = (int)(Math.random()*3); break;/group1 case 3: case 4: case 5: colForSwap = (int)(Math.random()*3 + 3); break;/group2 case 6: case 7: case 8: colForSwap
21、 = (int)(Math.random()*3 + 6);/group3 if(colForSwap!=i) for(int j = 0;j9;j+) /j for line tempj = solutionjcolForSwap; /the same column,diffent line solutionjcolForSwap = solutionji; solutionji = tempj; /finish one-column swap /finish all-columns swap private void mask() /choose the value masked at r
22、andom for(inti = 0;i9;+i) for(int j = 0;j9;j+)int difficulty = (int)(Math.random()*2); /0 or 1 if(difficulty=0) solutionij = 0; private booleanlineVlid(inti,intj,int value) for(int m = 0;m9 & m != j;m+) if(solutionim = value) return false; return true; private booleancolValid(inti,intj,int value) fo
23、r(int n = 0;n9 & n!=i;n+) if(solutionnj = value) return false; return true; private booleanrecValid(inti,intj,int value) for(int m = 3*(i/3);m=3*(i/3)+2;+m) for(int n = 3*(j/3);n0 & value10) return (lineVlid(i,j,value)&colValid(i,j,value)&recValid(i,j,value); else return false; =part2=package six;im
24、port javafx.geometry.Insets;import javafx.geometry.Pos;import javafx.scene.control.Button;import javafx.scene.control.TextField;import javafx.scene.layout.BorderPane;import javafx.scene.layout.GridPane;import javafx.scene.text.Font;import javafx.scene.text.FontWeight;/*this class is designed to add UI elements of the plain Sudo gamedefine the simple rules of the users operation on uiand the layout of all these elements
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1