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