《Java语言程序设计》Word文件下载.docx
《《Java语言程序设计》Word文件下载.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计》Word文件下载.docx(36页珍藏版)》请在冰豆网上搜索。
publicstaticvoidmain(String[]args){
intx,y,z;
System.out.println("
水仙花数依次为:
"
);
for(inti=100;
i<
=999;
i++){
x=i/100;
y=(i/10)%10;
z=(i%100)%10;
if(i==(x*x*x+y*y*y+z*z*z))
System.out.println(i);
}
}
}
2.publicclassSushu{
publicstaticvoidmain(String[]args){
System.out.println("
1-100以内的素数为"
for(inti=2;
i<
=100;
i++){
for(intk=2;
k<
=i;
k++){
if(i%k==0&
&
i!
=k){
break;
i==k){
System.out.println(i);
(2)程序运行结果
6.实验分析和体会
通过几个Java小程序的训练,我不仅巩固了先前所学的有关编程的东西,还使自己的编程能力有了大的提高。
Java程序的设计过程应该是认真细致的,在完成要求之前,先做做例题,这同时也是自己熟悉Java语言规则及其编译器的过程。
数组和字符串编程
软件1601学号:
实验2数组和字符串编程
(1)通过实验,掌握Java语言中数组、字符串程序设计的基本方法。
(2)较熟练地掌握Java程序数组的应用、字符串处理操作的方法应用。
(1)编写一个程序,在控制台输入10个数,按大小顺序输出。
(2)求一个3*3矩阵对角元素之和。
(3)输入一段字符串,统计其中有多少个单词。
(单词用空格隔开)
(1)将本章所学内容要点进行整理,并认真体会例题中的编程要领。
(2)写出实验内容中第
(2)、(3)题两个程序的源代码,作为报告。
1.publicclasssansanjuzhen{
intA1[][]={{3,8,5},{5,15,7},{24,8,68}};
intsum1=0,sum2=0;
for(inti=0;
A1.length;
for(intj=0;
j<
A1[i].length;
j++)
{if(i==j)
sum1=A1[i][j]+sum1;
if(j==A1.length-1-i)
sum2=A1[i][j]+sum2;
正对角线为:
System.out.println(sum1);
反对角线为:
System.out.println(sum2);
}}
2.importjava.util.*;
publicclassTotalWord{
publicstaticvoidmain(String[]args){
Scannerscan=newScanner(System.in);
Strings1=scan.nextLine();
System.out.println(s1);
Stringspilt[]=s1.split("
"
单词数="
+spilt.length);
(2)程序运行结果
通过实验,掌握了Java语言中数组、字符串程序设计的基本方法,并且较熟练地掌握了Java程序数组的应用、字符串处理操作的方法应用。
提高了自己的编程能力,同时在实验过程中有时会遇到一些因粗心而产生的小错误,在今后
编程中,一定要培养自己细心的习惯。
java面向对象程序设计
2016005
2017年11月16日
实验3Java面向对象程序设计
掌握Java面向对象程序设计中类、继承、包和接口的概念与应用,能熟练应用方法、类、成员变量等元素。
(1)定义一个描述长方体的类Box,类中有三个整型的成员变量length、width和height,分别表示长方体的长、宽和高。
定义setInfo(int,int,int)方法设置这三个变量的值;
定义volumn()方法求长方体的体积并返回整型结果;
定义area()方法求长方体的表面积整型结果;
定义toString()方法把长方体的长、宽和高,以及长方体的体积和表面积转化为字符串并返回字符串。
编写Java应用程序,测试类Box,使用类中定义的各个方法,并将其结果输出。
(2)创建一个复数类complex,以附属进行数学运算、复数具有如下格式:
RealPart+ImaginaryRart*I,其中,I为-1的平方根。
要求如下:
1 利用浮点变量表示此类的私有数据。
提供两个构造方法,一个用于此类声明的对象的初始化;
一个为默认的无参构造方法。
2 提供两复数加、减、乘的运算方法。
3 按格式(a,b)打印复数。
其中a为实部,b为虚部。
(3)创建银行账号Account类,实现存款(balance)的存(deposit)、取(withdraw)和查询(getbalance)等功能。
(4)编程定义一个接口,实现三个数中求最小值和最大值的方法,并将程序存放在mymaths包中。
(5)编程实现以下简单超市管理,具体要求如下:
1 实现描述超市的类Suppermacket类,具有货架属性,记录系统中现有商品,定义盘点商品的函数check(盘点各类商品的数量,并输出盘点结果),查询商品的函数query,并显示查询结果;
2 定义商品类Goods,具有商品名称Name,商品价格Price,商品产地Producer等属性,操作Sale(销售商品,余额不足时给予提示)、Add(商品上架操作)和ShowMe(显示商品信息)。
3 main函数,测试以上所要求的各种功能,即可以根据菜单命令增加、删除、查询商品、盘点商品,以及商品销售和商品上架的操作
提示:
①超市货架可以使用商品数组,数组大小100表明存放100个商品。
②注意实现商品查询的多态性,要求可以根据商品的名称或产地或价格查询商品的数量。
复习本章所学内容,思考以下问题:
(1)什么是对象?
什么是类?
(2)面向对象编程的特性有哪三个?
它们各自有哪些特性?
(3)Java语言在面向对象编程方面有哪些独特的特点?
(4)抽象类与接口有何异同?
(5)写出本实验内容中所有题目的源代码,作为报告。
(6)实验过程中,注意记录以下内容:
1.publicclassBox{
privateintlength;
privateintwidth;
privateintheight;
privateintarea;
privateintvolumn;
publicvoidsetinfo(intlength,intwidth,intheight){
this.height=height;
this.length=length;
this.width=width;
publicintvolumn(){
volumn=height*length*width;
returnvolumn;
publicintarea(){
area=2*(height*length+length*width+height*width);
returnarea;
}
publicStringtoString(){
return"
长方体的长="
+length+"
\t长方体的宽="
+width+"
\t长方体的高="
+height+"
\t面积="
+area+"
\t体积="
+volumn;
Boxb=newBox();
b.setinfo(5,4,5);
b.volumn();
b.area();
System.out.println(b.toString());
2.publicclassComplex{
privatefloatreal;
privatefloatimage;
publicComplex(){}
publicComplex(floatreal,floatimage){
this.real=real;
this.image=image;
publicComplexadd(Complexa1){
returnnewComplex(this.real+a1.real,this.image+a1.image);
publicComplexreduce(Complexa1){
returnnewComplex(this.real-a1.real,this.image-a1.image);
publicComplexcheng(Complexa1){
returnnewComplex(this.real*a1.real-this.image*a1.image,this.real*a1.image+this.image*a1.real);
publicStringOutput1(){
if(image>
0)
returnreal+"
+"
+image+"
i"
;
else
returnreal+"
Complexc1=newComplex(3,5);
Complexc2=newComplex(4,-6);
Complexc3=newComplex();
Complexc4=newComplex();
Complexc5=newComplex();
c3=c1.add(c2);
c4=c1.reduce(c2);
c5=c1.cheng(c2);
第一个复数为3+5i,第二个复数为4-6i"
复数的加法为:
System.out.println(c3.Output1());
复数的减法为:
System.out.println(c4.Output1());
复数的乘法为:
System.out.println(c5.Output1());
3.packageAccount;
publicclassAcount{
publicdoublebalance;
publicAcount(doublebalance){
SetBalance(balance);
publicvoidSetBalance(doublebalance){
this.balance=balance;
publicdoubledeposit(doublebalance1){
this.balance+=balance1;
returnthis.balance;
publicdoublewithdraw(doublebalance1){
this.balance-=balance1;
publicdoubleGetbalance(){
returnbalance;
packageAccount;
importjava.io.*;
publicclassMain{
AcountA=newAcount(0);
doubleIncome=0.0,Outcome=0.0;
inti;
java.text.DecimalFormatdf=newjava.text.DecimalFormat("
#.00"
while(true){
System.out.println("
欢迎光临,请输入你要进行的操作:
\n1,存钱"
\n2,取钱"
\n3,查询"
\n4,退出"
BufferedReaderBf=newBufferedReader(newInputStreamReader(System.in));
while(true){
try{
i=Integer.parseInt(Bf.readLine());
if(i!
=1&
i!
=2&
=3&
=4)
System.out.println("
请重新输入:
else
break;
}catch(Exceptione){
System.out.println("
请输入整数:
}
}
if(i==4){
break;
if(i==1){
System.out.println("
请输入存款额:
while(true){
try{
Income=Double.parseDouble(Bf.readLine());
A.deposit(Income);
存入的钱为:
+Income);
剩余的钱为"
+df.format(A.balance));
请输入浮点数"
}
if(i==2){
请输入取款额:
Outcome=Double.parseDouble(Bf.readLine());
if(A.Getbalance()>
Outcome)
{
A.withdraw(Outcome);
取出的钱为:
+Outcome);
}
else
System.out.println("
余额不足"
if(i==3){
4.packagemymaths;
publicinterfaceQiuzhi{
abstractintMymax();
abstractintMymin();
packagemymaths;
publicclassQiuzhi1implementsQiuzhi{
publicinta,b,c;
publicQiuzhi1(inta,intb,intc){
this.a=a;
this.b=b;
this.c=c;
publicintMymax(){
intd=b>
c?
b:
c;
returna>
d?
a:
d;
publicintMymin(){
intd=b<
returna<
Qiuzhi1q=newQiuzhi1(25,6,108);
最大数:
+q.Mymax());
最小数:
+q.Mymin());
5.packageManageSuppermacket;
publicclassGoods{
publicStringName;
publicdoublePrice;
publicStringProcuder;
publicintNumber;
publicvoidSale(StringName,intnumber){
if(this.Number<
number)
{
没有足够的"
+Name);
剩余"
+this.Number+"
的"
+this.Name);
{System.out.println("
物品销售成功!
this.Number=this.Number-number;
publicvoidAdd(StringName,StringProcuder,doubleprice,intNumber){
this.Name=Name;
this.Procuder=Procuder;
this.Price=price;
this.Number=Number;
物品上架成功!
publicvoidShowMe(){
商品名称:
商品价格:
+Price);
商品产地:
+Procuder);
商品数量:
+Number);
publicGoodsdelect(Goodsgood){
if(good.Number>
0){
good=null;
物品删除成功!
returngood;
packageManageSuppermacket;
publicclassSuppermacket{
GoodsGood[]=newGoods[100];
publicvoidcheck(GoodsGood){
物品"
\t价格"
\t商品产地"
\t数量"
System.out.println(Good.Name+"
+Good.Price+"
+Good.Procuder+"
+Good.Number);
publicvoidQuery(Goodsgood){
good.ShowMe();
packageManageSu