南昌大学Java实验报告2.docx

上传人:b****7 文档编号:10322818 上传时间:2023-02-10 格式:DOCX 页数:27 大小:1.35MB
下载 相关 举报
南昌大学Java实验报告2.docx_第1页
第1页 / 共27页
南昌大学Java实验报告2.docx_第2页
第2页 / 共27页
南昌大学Java实验报告2.docx_第3页
第3页 / 共27页
南昌大学Java实验报告2.docx_第4页
第4页 / 共27页
南昌大学Java实验报告2.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

南昌大学Java实验报告2.docx

《南昌大学Java实验报告2.docx》由会员分享,可在线阅读,更多相关《南昌大学Java实验报告2.docx(27页珍藏版)》请在冰豆网上搜索。

南昌大学Java实验报告2.docx

南昌大学Java实验报告2

南昌大学实验报告

学生姓名:

学号:

专业班级:

实训类型:

□验证□综合□设计□创新实验日期:

2017.11.8实验成绩:

一、实验项目名称

字符串和数组

二、实验的评分标准

实验分为A~F,A为最高,F最低。

F:

在规定时间内没有完成所有的实验,而且没有及时提交实验报告,或者实验过程中出现了抄袭复制他人实验代码。

D:

能完成实验,但是实验结果出现严重错误,不能体现对教学内容的理解。

C:

能基本完成实验,实验结果基本正确。

但是实验内容有较少的错误,提交的实验代码质量一般。

B:

能较好的完成实验,实验报告条理清楚,实验代码结构清晰,代码质量较高,及时更正试验中出现的错误,并对运行中一些异常错误进行分析,解释错误产生的原因。

A:

能较好的完成实验,实验代码质量高,实验报告完成度高,能在实验完成的基础上,根据个人的理解增加实验的新功能,具有一定的创新能力。

三、实验目的和要求

1.掌握java的基础知识。

2.掌握和运用java的控制语句和数组。

3.掌握和运用java的字符串。

四、实验内容

1.使用BigInteger计算超大整数的问题。

2.结合字符串和数组计算超大整数的问题

这里,我模仿Math写了专门计算数组形式存放整数的一个工具类(见后面源代码),只有静态函数,想来挑战Biginteger类。

在循环体计算前后,分别用时间函数获取系统当前时间,作差来近似等价于核心运算时间。

Biginteger类耗时1ms,自己写的方法耗时13ms,相差13倍,惨败。

3.字符串的“==”和equals()方法。

详细结果解释在源代码注释中给出。

4.统计英文单词

5.用StringBuffer模拟扑克牌洗牌

6.数独游戏

Clear:

清除用户输入的所有数据

Restart:

开始新的数独游戏

Check:

检查用户输入数据的正确性

提示框

 

用户输入的数字显示蓝色,已知的数字显示黑色

为增加可判断性,做出了灰色阴影效果

7.财务应用程序,比较不同利率下的贷款。

8.财务应用程序,信用卡号的合法性

实验源代码如下:

1.使用Biginteger计算大数

packageone;

importjava.math.BigInteger;

publicclassBigintegerCal{

publicstaticvoidmain(Stringargs[]){

BigIntegerres=newBigInteger("1");

BigIntegerb=newBigInteger("1");

longstart=System.currentTimeMillis();

for(inti=1;i<=100;i++){

b=b.multiply(newBigInteger("2"));

res=res.multiply(b.add(BigInteger.ONE));

}

longend=System.currentTimeMillis();

System.out.println(res.toString());

System.out.println("After"+(end-start)+"ms");

}

}

2.字符串或数组计算大数

========part1=======

packagetwo;

/*

*thisclassisdesignedtooffertoolstocalculate

*hugeintegernumbersviaplainintegerarray

*/

publicclassArrayCal{

privateArrayCal(){};

/*arrayamultiplyarrayb*/

publicstaticint[]mul(int[]a,int[]b){

int[]res=newint[a.length+b.length];

for(inti=0;i

res[i]=0;

inttmp=0;

for(inti=b.length-1;i>=0;--i){

for(intj=a.length-1;j>=0;--j){

tmp+=(res[j+i+1]+a[j]*b[i]);

res[j+i+1]=tmp%10;

tmp/=10;

}

res[i]=tmp;

tmp=0;

}

returnArrayCal.clean(res);

}

/*arrayaaddasingleinteger*/

publicstaticint[]add(int[]a,intnum){

int[]res=newint[a.length+1];

for(inti=a.length-1;i>=0;i--){

num=a[i]+num;

res[i+1]=num%10;

num/=10;

}

res[0]=num;

returnArrayCal.clean(res);

}

/*arrayamultiplyasingleinteger*/

publicstaticint[]mul(int[]a,intnum){

int[]res=newint[a.length+1];

intc=0;//pre

for(inti=a.length-1;i>=0;i--){

c=a[i]*num+c;

res[i+1]=c%10;

c/=10;

}

res[0]=c;

returnArrayCal.clean(res);

}

/*cleantheredundant0inthefrontofthearray*/

publicstaticint[]clean(int[]a){

if(a[0]!

=0)

returna;

else{

intcount=0;

for(inti=0;i

count++;

int[]res=newint[a.length-count];

System.arraycopy(a,count,res,0,a.length-count);

returnres;

}

}

}

======part2=======

packagetwo;

publicclassCalculate{

publicstaticvoidmain(String[]args){

int[]part2={1};

int[]part1;

int[]res={1};

longstart=System.currentTimeMillis();

for(inti=1;i<=100;++i){

part2=ArrayCal.mul(part2,2);

part1=ArrayCal.add(part2,1);

res=ArrayCal.mul(res,part1);

}

longend=System.currentTimeMillis();

for(intc:

res)

System.out.print(c);

System.out.println("\nAfter"+(end-start)+"ms");

}

}

3.字符串的相等和equals方法

packagethree;

publicclassTestString{

publicstaticvoidmain(String[]args){

Stringone="hello";

Stringtwo="hello";

Stringthree=newString("hello");

//whetheroneandtworeferstoasameobject,obviouslytrue

System.out.println("1."+(one==two));

//first,calculate"1."+one,namely:

"1.hello",anewstring,differentaddress

System.out.println("2."+one==three);

//inStringclass,equals()hasbeenoverride,twoandtreehasthesamecontent,hence,true

System.out.println("3."+one.equals(two));

//0forequal,>0formore,<0forless

System.out.println("4."+pareTo(three));

//sameas2

System.out.println("5."+one.intern()==three.intern());

}

}

4.统计英文单词

packagefour;

importjava.util.ArrayList;

importjava.util.Scanner;

publicclassCountWord{

publicstaticvoidmain(String[]args){

Scannerinput=newScanner(System.in);

ArrayListlist=newArrayList();

System.out.println("PleaseinputanEnglishsentence,seperatedbyblank:

");

Stringdata=input.nextLine();

int[]count=newint[data.length()];

//splitthestringinputtedbytheuserwith''

//makingitString[]

String[]dataNew=data.split("");

for(Stringe:

dataNew){

if(!

list.contains(e)){

list.add(e);

count[list.size()-1]++;

}

else

count[list.indexOf(e)]++;

}

intmax=count[0];

intindex=0;

for(inti=1;i

if(count[i]>max){

max=count[i];

index=i;

}

System.out.println("Thewordwiththemostappearancetime:

"+dataNew[index]);

System.out.println("Thetimesofitsappearance:

"+max);

}

}

5.StringBuffer模拟扑克牌洗牌

======part1=====

packagefive;

/*

*theclassisdesignedtocreatethebasicPokemodel

*inwhichusercanshuffleit,displaythecards

*/

classPoke{

privatechar[]type={'♣','♦','♥','♠'};

privateString[]value={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

privateStringBuffercards=newStringBuffer("");

publicPoke(){

for(charx:

type)

for(Stringy:

value)

this.cards.append(x+y);

}

voidshuffle(inttimes){

for(inti=0;i

//thestartindexofeachside

intlen=cards.length();

intleft,right;

left=right=len/2;

StringBuffertemp=newStringBuffer("");

intnumOfL,numOfR;

while((left-0)/3>=3&&(len-1-right)/3>=3){

//theamountofcardstobeshuffledatrandomoneachside(1~3)

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;

}

//lessthan3cardsineachside,putthemallinthenewstring

if(left!

=0)

temp.append(cards.substring(0,left));

if(right!

=len-1)

temp.append(cards.substring(right,len));

cards=temp;//renewtheoriginalcards

}//shufflefinished

}

voidshowCards(){

for(inti=0;i<52;i++){

if((i+13)%13==0)

System.out.println();

System.out.print(cards.substring(3*i,3*i+3)+"");

}

System.out.println();

}

}

=====part2======

packagefive;

publicclassShuffle{

publicstaticvoidmain(String[]args){

Pokepoke=newPoke();

poke.shuffle(4);//shufflefor4times

poke.showCards();

}

}

6.数独游戏

=====part1=====

packagesix;

importjava.util.Arrays;

/*

thisclassisdesignedtogenerateonesolutionofSudo,

tomaskseveralnumbersatrandominall81numberstoraiseaSudoproblem,

todefinetherulestocheckwhetherthenumbersinputtedbyuserarecorrect

*/

publicclassSudo{

privateint[][]solution;

//initializethesudokuaccordingto

//-theregularityofthesumofiandj

publicSudo(){

solution=newint[9][];

for(inti=0;i<9;i++){

solution[i]=newint[9];

for(intj=1;j<=9;++j)

this.solution[i][j-1]=(i+j>9)?

(i+j-9):

i+j;

}

martrix();

colSwap();

mask();

}

//returnaconcreteandrandomsolution

publicintgetDigit(inti,intj){

returnsolution[i][j];

}

//constructasituationobjecttothesudokurules

//byextractingtheline[147,258,369]fromthesolution

privatevoidmartrix(){

int[][]temp=newint[9][9];

for(inti=0,j=0;i<3;i++){

temp[j++]=Arrays.copyOf(solution[i],solution[i].length);

temp[j++]=Arrays.copyOf(solution[i+3],solution[i+3].length);

temp[j++]=Arrays.copyOf(solution[i+6],solution[i+6].length);

}

solution=temp;

}

//basedonthe[123,456,789]columngroup

//swapeachcolumnvalueamongeachgroup

privatevoidcolSwap(){

intcolForSwap=0;

int[]temp=newint[9];

for(inti=0;i<9;i++){

switch(i){//choosethecolumntoswap

case0:

case1:

case2:

colForSwap=(int)(Math.random()*3);break;//group1

case3:

case4:

case5:

colForSwap=(int)(Math.random()*3+3);break;//group2

case6:

case7:

case8:

colForSwap=(int)(Math.random()*3+6);//group3

}

if(colForSwap!

=i)

for(intj=0;j<9;j++){//jforline

temp[j]=solution[j][colForSwap];//thesamecolumn,diffentline

solution[j][colForSwap]=solution[j][i];

solution[j][i]=temp[j];

}//finishone-columnswap

}//finishall-columnsswap

}

privatevoidmask(){

//choosethevaluemaskedatrandom

for(inti=0;i<9;++i){

for(intj=0;j<9;j++){

intdifficulty=(int)(Math.random()*2);//0or1

if(difficulty==0)

solution[i][j]=0;

}

}

}

privatebooleanlineVlid(inti,intj,intvalue){

for(intm=0;m<9&&m!

=j;m++)

if(solution[i][m]==value)

returnfalse;

returntrue;

}

privatebooleancolValid(inti,intj,intvalue){

for(intn=0;n<9&&n!

=i;n++)

if(solution[n][j]==value)

returnfalse;

returntrue;

}

privatebooleanrecValid(inti,intj,intvalue){

for(intm=3*(i/3);m<=3*(i/3)+2;++m)

for(intn=3*(j/3);n<3*(j/3)+2;++n)

if(i!

=m&&j!

=n&&solution[m][n]==value)

returnfalse;

returntrue;

}

publicbooleanisValid(inti,intj,intvalue){

if(value>0&&value<10)

return(lineVlid(i,j,value)&&colValid(i,j,value)&&recValid(i,j,value));

else

returnfalse;

}

}

=====part2=====

packagesix;

importjavafx.geometry.Insets;

importjavafx.geometry.Pos;

importjavafx.scene.control.Button;

importjavafx.scene.control.TextField;

importjavafx.scene.layout.BorderPane;

importjavafx.scene.layout.GridPane;

importjavafx.scene.text.Font;

importjavafx.scene.text.FontWeight;

/*

thisclassisdesignedtoaddUIelementsoftheplainSudogame

definethesimplerulesoftheusers'operationonui

andthelayoutofalltheseelements

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 自我管理与提升

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1