java实习解答Word文档格式.docx

上传人:b****6 文档编号:17493394 上传时间:2022-12-06 格式:DOCX 页数:12 大小:120.03KB
下载 相关 举报
java实习解答Word文档格式.docx_第1页
第1页 / 共12页
java实习解答Word文档格式.docx_第2页
第2页 / 共12页
java实习解答Word文档格式.docx_第3页
第3页 / 共12页
java实习解答Word文档格式.docx_第4页
第4页 / 共12页
java实习解答Word文档格式.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

java实习解答Word文档格式.docx

《java实习解答Word文档格式.docx》由会员分享,可在线阅读,更多相关《java实习解答Word文档格式.docx(12页珍藏版)》请在冰豆网上搜索。

java实习解答Word文档格式.docx

HelloWorld"

);

}

}

import.*;

importclassHelloWorldAppletextendsApplet{

publicStrings;

publicvoidinit()

{

s=newString("

HelloWorld!

"

publicvoidpaint(Graphicsg){

(s,10,25);

 

运行结果:

实验心得:

了解了如何安装java开发工具软件,成功的设置了环境变量,并用java程序编译和运行了几个小程序,对java做了进一步的了解。

二.for循环

多重for循环绘制图形

通过简单的实例了解for循环的使用。

编程实现运算一个简单图形的绘制,利用符号“*”的个数,实现不同的图形。

程序结构(程序流程图):

分析得:

运行结果是由两个直三角形组成的,所以分别定义两个变量I,J分别控制三角形的行和列,再用两组FOR语句变可得结果.

核心算法及源代码:

publicclassMultiplyLoop2

publicstaticvoidmain(String[]args)

{

inti;

intj;

for(i=1;

i<

=10;

i++)

{for(j=1;

j<

=i;

j++)

"

*"

"

\n"

}

for(i=1;

{for(j=1;

=10-i;

}

实验心得:

利用for语句可以解决一些循环和排序问题,经过排序可以得到我们想要的东西,进一步了解了for语句的使用,为以后的使用打下基础。

三.商店客户折扣卡

商店客户折扣卡”功能,

在前几节介绍了面向对象的基本概念,包括类、对象、属性以及方法。

在这个基础上,用两个面向对象的简单程序,继续让学生充分体会面向对象编程的方法。

使用对象模拟“商店客户折扣卡”功能,编程实现现一个模拟的用户消费信用卡的示例,在其中用户Customer类具有用户名、地址、卡号、消费额度、可以享受的打折度等多种个人信息;

而CustomerDemo类中建立3个消费者,对其分别进行操作,模拟其行为。

运行后效果如图所示。

publicclassCustomerDemo

{

Employeremployer[];

publicCustomerDemo()

employer=newEmployer[3];

employer[0]=newEmployer("

c0001"

"

wangxyw"

2800.0f,9.0f,"

BeiJing"

employer[1]=newEmployer("

c0002"

XuQuan"

1688.0f,9.5f,"

ShangHai"

employer[2]=newEmployer("

c0003"

XuGuangYang"

980.0f,10.0f,"

BeiJinng"

for(inti=0;

=;

{"

employer["

+i+"

]:

"

cardID:

+employer[i].getcardID());

name:

+employer[i].getname());

cost:

+employer[i].getcost());

discount:

+employer[i].getdiscount());

address:

+employer[i].getaddress());

emial:

+employer[i].getemial());

{CustomerDemocustomerDemo=newCustomerDemo();

}

classEmployer

privateStringcardID;

privateStringname;

privatefloatcost;

privatefloatdiscount;

privateStringaddress;

privateStringemial;

publicEmployer(){}

publicEmployer(StringcardID,Stringname,floatcost,floatdiscount,Stringaddress,Stringemial)

=cardID;

=name;

=cost;

=discount;

=address;

=emial;

publicStringgetcardID()

{returncardID;

publicvoidsetcardID(StringcardID)

{=cardID;

publicStringgetname()

{returnname;

publicvoidsetname(Stringstr)

{=str;

publicfloatgetcost()

{returncost;

publicvoidsetcost(floatcost)

{=cost;

publicfloatgetdiscount()

{returndiscount;

publicvoidsetdiscount(floatdiscount)

{=discount;

}

publicStringgetaddress()

{returnaddress;

publicvoidsetaddress(Stringaddress)

{=address;

publicStringgetemial()

{returnemial;

publicvoidsetemial(Stringemial)

{=emial;

商店客户折扣卡”功能,使我进一步了解了面向对象的简单程序的应用在实际生活当中,充分体会面向对象编程的方法,有利于以后学习当中遇到的问题的有力解决。

四:

整形数组和使用switch语句

声明一个整形数组保存学生成绩

通过这个简单实例让学生利用switch语句来实现条件分支,实现对于不同分数段的成绩,输出不同等级标识.

使用switch语句实现条件分支.编程实现运算一个简单的程序,程序可以根据学生的成绩判断学生成绩的等级.声明一个整形数组保存学生成绩.

publicclassGrade

publicGrade()

intstudent[]={95,85,75,65,55};

5;

switch(student[i]/10)

case9:

student"

'

sgradeisA!

break;

case8:

sgradeisB!

case7:

sgradeisC!

case6:

sgradeisD!

case5:

sgradeisF!

}}

publicstaticvoidmain(String[]args)

newGrade();

知道了如何在java语言中创建并初始化数组,然后将数组中的元素一一输出,最后利用了switch语句,了解到switch语句在什么情况下永如何利用。

五.方法重载

方法的重载

在OOP中,当程序要实现多个相近的功能时,就给相应的方法起一个共同的名字,用不同的参数代表不同的功能。

这样,在使用方法时不论传递什么参数,只要能被程序识别就可以得到确定的结果。

学生通过此试验可以掌握重载的用法。

方法的重载,编程实现对不同的数进行排序输出。

在IntSort类中定义3个同名的方法sort。

利用重载此方法可以处理两个数的排序,三个数的排序以及数组的排序。

classIntSort

publicIntSort(inti,intj)

if(i>

j)

{inta=i;

i=j;

j=a;

+j);

publicIntSort(inti,intj,intt){

if(i>

{intm=i;

j=m;

t)

intn=i;

i=t;

t=n;

if(j>

intp=j;

j=t;

t=p;

+j+"

+t);

publicIntSort(intarray[])

{

for(inti=0;

for(intj=0;

if(array[j]>

array[j+1])

intb=array[j];

array[j]=array[j+1];

array[j+1]=b;

}}}

for(intl=0;

l<

;

l++)

+array[l]);

publicclassSort{

publicstaticvoidmain(String[]args)

intg=10;

inth=20;

intj=30;

intk[]={7,77,777,9,99,999,5};

\nBeforeorder"

for(intn=0;

n<

n++)

+k[n]);

Afterorder"

IntSorts=newIntSort(g,h);

newIntSort(g,h,j);

newIntSort(k);

}

利用方法调用可以是复杂问题宾得简单了,进一步了解了调用方法的使用,只要有了方法什么都可以了,同时也充分利用了if语句。

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

当前位置:首页 > 表格模板 > 书信模板

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

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