ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:98.92KB ,
资源ID:14283976      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-14283976.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java程序.docx)为本站会员(b****5)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

java程序.docx

1、java程序第三周例6.1 定义一个圆柱体类Cylinder,并创建相应的对象,然后计算圆柱体的底面积与体积。/class Cylinder double radius; int height; double pi=3.14; void area() System.out.println(底面积=+pi*radius*radius); double volume() return(pi*radius*radius)*height; public class App6_1 public static void main(String args) Cylinder volu; volu=new C

2、ylinder(); volu.radius=2.8; volu.height=5; System.out.println(底圆半径=+volu.radius); System.out.println(圆柱的高=+volu.height); System.out.print(圆柱); volu.area(); System.out.println(圆柱体体积=+volu.volume(); 程序运行结果及其分析:定义Cylinder类,定义相关成员变量radius及height。定义无返回值的方法area(),用来计算圆柱底面积。定义返回值为double型的方法volume(),计算体积。定义

3、公共类创建新的对象。对radius,height赋值后输出圆的半径及圆柱的高,最后输出圆柱的底面积及体积。例6.2 同时创建多个圆柱体类Cylinder的对象,并修改其中一个对象的成员变量pi的值。/ App6_2.javaclass Cylinder double radius; int height; double pi=3.14; void area() System.out.println(底面积=+pi*radius*radius); double volume() return(pi*radius*radius)*height; public class App6_2 public

4、 static void main(String args) Cylinder volu1,volu2; volu1=new Cylinder(); volu2=new Cylinder(); volu1.radius=volu2.radius=2.5; volu2.pi=3; System.out.println(圆柱1底半径=+volu1.radius); System.out.println(圆柱2底半径=+volu2.radius); System.out.println(圆柱1的pi值=+volu1.pi); System.out.println(圆柱2的pi值=+volu2.pi)

5、; System.out.print(圆柱1); volu1.area(); System.out.print(圆柱2); volu2.area(); 程序运行结果及其分析:定义Cylinder类,及相关成员变量radius和height。定义无返回值的方法void area()计算圆柱的底面积。定义double型的方法volume()计算体积。声明指向对象的变量volu1和volu2。分别创建对象圆柱1:volu1和圆柱2:volu2。对圆柱1和圆柱2的radius赋值。然后修改volu2的pi值。最后计算圆柱1和圆柱2的底面积并输出。在本题中,定义两个对象volu1和volu2,并将对象v

6、olu1和volu2的radius均设置为2.5,然后将volu2的pi值重新赋值为3,由于pi是double型的变量,所以系统自动将pi值置为3.0。因为每一个新创建的对象的成员变量均有其固定的存放空间,因此修改volu2的pi值并不影响volu1原来的pi值。例6.3 以圆柱体类Cylinder为例来介绍在类内部调用自己的方法。/ App6_3.javaclass Cylinder double radius; int height; double pi=3.14; double area() return pi*radius*radius; double volume() return

7、area()*height; public class App6_3 public static void main(String args) Cylinder volu; volu=new Cylinder(); volu.radius=2.8; volu.height=5; System.out.println(底圆半径=+volu.radius); System.out.println(圆柱的高=+volu.height); System.out.print(圆柱); System.out.println(底面积=+volu.area(); System.out.println(圆柱体体

8、积=+volu.volume(); 程序运行结果及其分析:定义Cylinder类,定义相关成员变量radius及height。定义无返回值的方法area(),用来计算圆柱底面积。定义返回值为double型的方法volume(),计算体积。在类内调用area()方法。定义公共类。创建新对象。分别对圆柱的半径及高赋值。计算圆柱的底面积及体积并输出。例6.4 以圆柱体类Cylinder为例介绍变量调用方法。/ App6_4.javaclass Cylinder double radius; int height; double pi; void setCylinder(double r,int h,

9、double p) /具有三个参数的方法 pi=p; radius=r; height=h; double area() return pi*radius*radius; double volume() return area()*height; public class App6_4 public static void main(String args) Cylinder volu=new Cylinder(); volu.setCylinder(2.5,5,3.14); /调用并传递参数到setCylinder()方法 System.out.println(底圆半径=+volu.radi

10、us); System.out.println(圆柱的高=+volu.height); System.out.println(圆周率pi=+volu.pi); System.out.print(圆柱); System.out.println(底圆面积=+volu.area(); System.out.println(圆柱体体积=+volu.volume(); 程序运行结果及其分析:本题中Cylinder()类内定义了带参的方法setCylinder(),该方法可接收三个参数r,h,p。其中r和p为double型,h为int。这三个参数是用来给对象的成员变量进行赋值。但r,h,p是局部变量,即其

11、作用范围仅限于setCylinder()方法的内部。例6.5 以一维数组为参数的方法调用,求若干数的最小值。/ App6_5.javapublic class App6_5 public static void main(String args) int a=8,3,7,88,9,23; LeastNumb minNumber=new LeastNumb(); minNumber.least(a); class LeastNumb public void least(int Array) int temp=Array0; for(int i=1;iArrayi) temp=Arrayi; Sy

12、stem.out.println(最小的数为:+temp); 程序运行结果及其分析:定义一维数组a,创建新的对象。再将一维数组a传入least()方法。定义另一个类。参数array接收一维整型数组。利用for循环及if选择出最小的数。例6.6 将一个矩阵转置后输出。/ App6_6.javapublic class App6_6 public static void main(String args) int a=1,2,3,4,5,6,7,8,9; int b=new int33; Trans pose=new Trans(); b=pose.transpose(a); for(int i=

13、0;ib.length;i+) for(int j=0;jbi.length;j+) System.out.print(bij+ ); System.out.print(n); class Trans int temp; int transpose(int Array) for(int i=0;iArray.length;i+) for(int j=i+1;jArrayi.length;j+) temp=Arrayij; Arrayij=Arrayji; Arrayji=temp; return Array; 程序运行结果及其分析:定义二维数组,创建Trans类的对象pose。用数组a调用方法

14、,将返回值赋给数组b利用for循环输出数组的内容定义另一个类,定义返回值和参数均为二维整型数组的方法利用for循环将二维数组的行与列互换。最后返回二维数组实验19 编写一个程序,分别按两种方式定义字符串。用运算符“=”与字符串equals()方法对这些字符串进行比较。/StringA.javapublic class StringA public static void main(String args) String str1=Hello; String str2=Hello; String str3=new String(Hello); String str4=new String(Hel

15、lo); System.out.println(运算符=); if(str1=str2) System.out.println(str1与str2相等); else System.out.println(str1与str2不相等); if(str3=str4)System.out.println(str3与str4相等); else System.out.println(str3与str4不相等); if(str2=str3) System.out.println(str2与str3相等); else System.out.println(str2与str3不相等); System.out.p

16、rintln(equals方法); if(str1.equals(str2) System.out.println(str1与str2相等); else System.out.println(str1与str2不相等); if(str3.equals(str4) System.out.println(str3与str4相等); else System.out.println(str3与str4不相等); if(str2.equals(str3) System.out.println(str2与str3相等); else System.out.println(str2与str3不相等); 实验2

17、0 编写一个java程序,对两个字符串进行连接、比较及显示其长度等操作。/StringB.javapublic class StringB public static void main(String args) String str1=Hello; String str2=World!; String str=str1+str2; System.out.println(str=+str); System.out.println(str的长度是+str.length(); System.out.println(str的第三个字符是+str.charAt(3); System.out.print

18、ln(str中字符串or第一次出现的位置+str.indexOf(or); System.out.println(str.toLowerCase(); System.out.println(str.toUpperCase(); 实验21 编写一个java程序,如果没有命令行参数,显示“没有输入参数”;否则,显示用户共输入多少个参数,并显示各个参数内容。/CommandInput.javapublic class CommandInput public static void main(String args) if(args.length=0) System.out.println(没有输入参数); else System.out.println(共输入了+args.length+个参数); for(String arg:args) System.out.print(arg);

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

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