《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx

上传人:b****4 文档编号:6649352 上传时间:2023-05-07 格式:DOCX 页数:72 大小:36.20KB
下载 相关 举报
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第1页
第1页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第2页
第2页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第3页
第3页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第4页
第4页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第5页
第5页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第6页
第6页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第7页
第7页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第8页
第8页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第9页
第9页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第10页
第10页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第11页
第11页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第12页
第12页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第13页
第13页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第14页
第14页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第15页
第15页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第16页
第16页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第17页
第17页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第18页
第18页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第19页
第19页 / 共72页
《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx_第20页
第20页 / 共72页
亲,该文档总共72页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx

《《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx(72页珍藏版)》请在冰点文库上搜索。

《Java编程语言原理与范例》课后实验源代码Word文档下载推荐.docx

importjavax.swing.JButton;

importjavax.swing.JFrame;

publicclassMagicButtonextendsMouseAdapter{

JFramewin;

JButtonbutton=newJButton("

你点不到我"

Randomrand=newRandom();

voidinitUI(){

win=newJFrame();

win.setLayout(null);

button.setSize(100,40);

button.addMouseListener(this);

win.add(button);

win.setSize(400,300);

win.setResizable(false);

win.setLocationRelativeTo(null);

win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

win.setVisible(true);

MagicButtondemo=newMagicButton();

demo.initUI();

publicvoidmouseEntered(MouseEvente){

intmouseX=button.getX()+e.getX();

intmouseY=button.getY()+e.getY();

intbuttonX=rand.nextInt(win.getWidth()-button.getWidth());

intbuttonY=rand.nextInt(win.getHeight()-button.getHeight());

button.setLocation(buttonX,buttonY);

if(!

button.getBounds().contains(mouseX,mouseY)){

break;

第二章

/*********************************

2.交换两个变量的值(不允许使用中间变量)。

**********************************/

packagech03;

publicclassExp2_2{

inta=2,b=3;

ints=a*b;

a=s/a;

b=s/a;

System.out.println("

a="

+a+"

b="

+b);

3.逆序输出一个7位整数,如8639427输出为7249368(不允许使用循环语句)。

publicclassExp2_3{

longa=8639427;

System.out.print(a%10);

System.out.print(a/10%10);

System.out.print(a/100%10);

System.out.print(a/1000%10);

System.out.print(a/10000%10);

System.out.print(a/100000%10);

System.out.print(a/1000000%10);

实验三

4.对于int型变量a,以最快的速度计算34×

a的值。

publicclassExp2_4{

inta=3;

intb=(a<

<

5)+(a<

1);

System.out.println(a+"

*34="

实验四

5.字符型变量ch中存放着一个大小写未知的英文字母,判断其大小写后,将ch的值转为小写或大写字母(不允许使用加减运算符和if语句)。

publicclassExp2_5{

charch='

E'

;

ch=(char)((ch&

32)==0?

ch|32:

ch&

(Integer.MAX_VALUE-32));

ch1="

+ch);

实验5

6.使用嵌套的条件运算符,求a、b、c中的最大者。

publicclassExp2_6{

inta=2,b=4,c=3;

intmax=(a>

b?

a:

b)>

c?

(a>

b):

c;

max="

+max);

第三章

2.使用循环结构逆序输出任意位数的整数。

packagech04;

importjava.util.Scanner;

publicclassExp3_2{

Scanners=newScanner(System.in);

输入整数:

longn=s.nextLong();

while(n>

0){

System.out.print(n%10);

n/=10;

}

3.输出以下由数字组成的菱形(要求将输出行数存放于变量中以便随时更改)。

1

121

12321

1234321

publicclassExp3_3{

introws;

System.out.print("

输入行数:

rows=s.nextInt();

for(inti=-rows/2;

i<

=rows/2;

i++){

System.out.printf("

%-"

+(3*Math.abs(i)+1)+"

s"

"

for(intj=Math.abs(i)-rows/2;

j<

=rows/2-Math.abs(i);

j++){

System.out.printf("

%-3d"

rows/2+1-Math.abs(i)-Math.abs(j));

}

System.out.println();

4.输出以上由数字组成的三角形(要求将输出行数存放于变量中以便随时更改)。

136101521

2591420

481319

71218

1117

16

publicclassExp3_4{

intfirstNumOfRow=1,nextNumOfRow;

for(inti=1;

=rows;

firstNumOfRow+=i-1;

intfirstStepOfRow=i+1;

nextNumOfRow=firstNumOfRow;

for(intj=1;

=rows+1-i;

%-4d"

nextNumOfRow);

nextNumOfRow+=firstStepOfRow++;

5.计算多项式8+88+888+8888+88888+...的前8项之和。

输出结果:

98765424

publicclassExp3_5{

longsum=0;

=8;

longnum=0;

=i;

num=num*10+8;

sum+=num;

System.out.println(sum);

第四章

1.产生10个100以内的随机整数以填充一维数组,实现以下功能。

①找出最大以及最小值。

②查找给定整数a在数组中最后一次出现的位置,若不存在则提示。

③判断数组是否呈非递减排列。

④将数组元素翻转存放。

packagech05;

publicclassExp4_1{

int[]init(){

int[]a=newint[10];

Randomr=newRandom();

for(inti=0;

a.length;

a[i]=r.nextInt(100);

returna;

voidprint(int[]a){

%-5d"

a[i]);

System.out.println();

intfindMax(int[]a){

intmax=a[0];

if(max<

a[i]){

max=a[i];

returnmax;

intfindMin(int[]a){

intmin=a[0];

if(min>

min=a[i];

returnmin;

intfindLastLocation(int[]a,intx){

for(inti=a.length-1;

i>

=0;

i--){

if(a[i]==x){

returni;

return-1;

booleanisAsc(int[]a){

a.length-1;

if(a[i]>

a[i+1]){

returnfalse;

returntrue;

voidreverse(int[]a){

a.length/2;

inttemp=a[i];

a[i]=a[a.length-i-1];

a[a.length-i-1]=temp;

Exp4_1t=newExp4_1();

int[]a=t.init();

t.print(a);

+t.findMax(a));

min="

+t.findMin(a));

输入要查找的数:

intx=s.nextInt();

inti=t.findLastLocation(a,x);

if(i==-1){

System.out.println(x+"

在数组中不存在。

}else{

Lastlocationof%d:

%d。

\n"

x,i);

if(t.isAsc(a)){

System.out.println("

数组是非递减排列!

数组不是非递减排列!

t.reverse(a);

翻转后的数组:

2.将a插入到一个长度不小于10且元素呈递增排列的一维数组中,并保证插入之后的数组依然递增(若a在插入前的数组中存在,则输出提示并忽略)。

publicclassExp4_2{

inta[]={2,4,5,7,9,11,15,17,20,22,Integer.MAX_VALUE};

voidprint(booleanisAfterInsert){

intend=isAfterInsert?

a.length:

end;

intfindInsertLocation(intx){

inti=0;

for(;

return-1;

}elseif(a[i]>

x){

returni;

voidinsert(inti,intx){

for(intj=a.length-2;

j>

j--){

a[j+1]=a[j];

a[i]=x;

Exp4_2t=newExp4_2();

t.print(false);

输入要插入的数:

inti=t.findInsertLocation(x);

在数组中已经存在,放弃插入!

t.insert(i,x);

t.print(true);

3.找出阶数不小于8的方阵的鞍点值及位置(鞍点值在该行上最大、该列上最小),若无鞍点则提示。

importjava.util.ArrayList;

importjava.util.List;

/**

*鞍点对象类

*/

classAnDian{

privateintrow;

//鞍点所在行下标

privateintcol;

//鞍点所在列下标

privateintvalue;

//鞍点值

//完全构造方法

publicAnDian(introw,intcol,intvalue){

this.row=row;

this.col=col;

this.value=value;

//gettersandsetters

publicintgetRow(){

returnrow;

publicvoidsetRow(introw){

publicintgetCol(){

returncol;

publicvoidsetCol(intcol){

publicintgetValue(){

returnvalue;

publicvoidsetValue(intvalue){

*测试类(整体上是若干个并列的2重循环,时间复杂度较3重循环低)

publicclassExp4_3{

int[][]a;

//矩阵

intmaxOfRows[];

//存放每行的最大值

intminOfCols[];

//存放每列的最小值

finalintLIMIT=3;

//矩阵元素值的上限(为测试方便此处写死,也可在运行时由用户输入)

//初始化矩阵

voidinitArray(){

Scannerscanner=newScanner(System.in);

输入矩阵行数:

intm=scann

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

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2