java实习解答Word文件下载.docx
《java实习解答Word文件下载.docx》由会员分享,可在线阅读,更多相关《java实习解答Word文件下载.docx(12页珍藏版)》请在冰豆网上搜索。
);
}
}
importjava.applet.*;
importjava.awt.Graphics;
publicclassHelloWorldAppletextendsApplet{
publicStrings;
publicvoidinit()
{
s=newString("
HelloWorld!
"
publicvoidpaint(Graphicsg){
g.drawString(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++)
System.out.print("
*"
System.out.print("
\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"
**************.com"
employer[1]=newEmployer("
c0002"
XuQuan"
1688.0f,9.5f,"
ShangHai"
*******************"
employer[2]=newEmployer("
c0003"
XuGuangYang"
980.0f,10.0f,"
BeiJinng"
****************"
for(inti=0;
=employer.length;
{System.out.println("
employer["
+i+"
]:
System.out.println("
cardID:
+employer[i].getcardID());
name:
+employer[i].getname());
cost:
+employer[i].getcost());
discount:
+employer[i].getdiscount());
address:
+employer[i].getaddress());
emial:
+employer[i].getemial());
System.out.println();
{CustomerDemocustomerDemo=newCustomerDemo();
}
classEmployer
privateStringcardID;
privateStringname;
privatefloatcost;
privatefloatdiscount;
privateStringaddress;
privateStringemial;
publicEmployer(){}
publicEmployer(StringcardID,Stringname,floatcost,floatdiscount,Stringaddress,Stringemial)
this.cardID=cardID;
this.name=name;
this.cost=cost;
this.discount=discount;
this.address=address;
this.emial=emial;
publicStringgetcardID()
{returncardID;
publicvoidsetcardID(StringcardID)
{this.cardID=cardID;
publicStringgetname()
{returnname;
publicvoidsetname(Stringstr)
{this.name=str;
publicfloatgetcost()
{returncost;
publicvoidsetcost(floatcost)
{this.cost=cost;
publicfloatgetdiscount()
{returndiscount;
publicvoidsetdiscount(floatdiscount)
{this.discount=discount;
}
publicStringgetaddress()
{returnaddress;
publicvoidsetaddress(Stringaddress)
{this.address=address;
publicStringgetemial()
{returnemial;
publicvoidsetemial(Stringemial)
{this.emial=emial;
商店客户折扣卡”功能,使我进一步了解了面向对象的简单程序的应用在实际生活当中,充分体会面向对象编程的方法,有利于以后学习当中遇到的问题的有力解决。
四:
整形数组和使用switch语句
声明一个整形数组保存学生成绩
通过这个简单实例让学生利用switch语句来实现条件分支,实现对于不同分数段的成绩,输出不同等级标识.
使用switch语句实现条件分支.编程实现运算一个简单的程序,程序可以根据学生的成绩判断学生成绩的等级.声明一个整形数组保存学生成绩.
publicclassGrade
publicGrade()
intstudent[]={95,85,75,65,55};
5;
switch(student[i]/10)
case9:
System.out.println("
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;
=(array.length-2);
for(intj=0;
=(array.length-i-2);
if(array[j]>
array[j+1])
intb=array[j];
array[j]=array[j+1];
array[j+1]=b;
}}}
for(intl=0;
l<
array.length;
l++)
System.out.print("
+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<
k.length;
n++)
System.out.print("
+k[n]);
Afterorder"
IntSorts=newIntSort(g,h);
newIntSort(g,h,j);
newIntSort(k);
}
利用方法调用可以是复杂问题宾得简单了,进一步了解了调用方法的使用,只要有了方法什么都可以了,同时也充分利用了if语句。