Java.work7 访问权限、对象使用 20194648

题目1:

  在作业5的基础上,再创建一个柱体类,包含矩形对象、高和体积等三个成员变量,一个构造方法进行成员变量初始化,和计算体积、换底两个功能方法,在主类中输入长、宽、高,计算柱体体积,输入新的长、宽、高,创建新的矩形对象,并利用换底方法换底,再次计算柱体体积。

代码:

1、Test.java

复制代码
 1 /**
 2  * 主类
 3  * 输出体积的数值
 4  */
 5 public class App {
6 public static void main(String[] args) { 7 Cylinder cyl = new Cylinder(7,new Rectangle(4,5)); 8 /* 9 * 创建柱对象 10 */ 11 System.out.println("体积:"+qua.get_V()); 12 qua.setr(new Rec(5,3)); 13 System.out.print("换底体积:"+qua.get_V()); 14 } 15 16 }
复制代码

2、Rectangle.java

复制代码
 1 /**
 2  * 矩形类
 3  * 
 4  * 
 5  */
 6 public class Rectangle {
 7 
 8     double a;
 9     double b;
10 Recrangle(double a, double b){ 11 this.a = a; 12 this.b = b; 13 } 14 public double get_area(){ 15 //返回面积值 16 return a*b; 17 } 18 }
复制代码

3、Cylinder.java

复制代码
 1 /** 
 2  * 柱体类
 3  * r为底,h为高,v为体积;
 4  * 
 5  */
 6 public class Cylinder {
 7     //矩形对象,柱体的底
 8     Rec r ; 
 9     double h;
10     double v;
11     Cylinder(double h, Rec r){
12       
13         this.h = h;
14         this.r = r;
15     }
16     public void setr(Rec r){
17         //修改器
18         this.r = r;
19     }
20     public double get_V( ){
21         //计算体积
22         return r.get_area() *h;
23     }
24 }
复制代码

运行结果:

扫描二维码关注公众号,回复: 7327328 查看本文章

题目二:

  设计名为MyInteger的类,它包括: int型数据域value 一个构造方法,当指定int值时,创建MyInteger对象 数据域value的访问器和修改器 isEven( )和isOdd( )方法,如果当前对象是偶数或奇数,返回true 类方法isPrime(MyInteger i),判断指定的值是否为素数,返回true 在主类中创建MyInteger对象,验证MyInteger类中各方法。

代码:

1、Test.java

复制代码
 1 /**
 2  * 主类
 3  */
 4 package cn.edu.ccut;
 5 
 6 public class Test {
 7 
 8     public static void main(String[] args) {
 9         MyInteger obj = new MyInteger(5);//创建对象        
10         System.out.println(""+obj.getvalue()+"是否为奇数:"+obj.isOdd());
11         System.out.println(""+obj.getvalue()+"是否为偶数:"+obj.isEven());
12         System.out.println(""+obj.getvalue()+"是否为素数:"+MyInteger.isPrime(obj));
13         obj.setvalue(8);//调用修改器
14         System.out.println(""+obj.getvalue()+"是否为奇数:"+obj.isOdd());
15         System.out.println(""+obj.getvalue()+"是否为偶数:"+obj.isEven());
16         System.out.println(""+obj.getvalue()+"是否为素数:"+MyInteger.isPrime(obj));
17     }
18 }
复制代码

2、MyInteger.java

复制代码
 1 /**
 2  * MyInteger类
 3  * value存输入的数据
 4  * isEven判断是否为偶数,isOdd判断是否为奇数,isPrime判断是否为素数
 5  */
 6 package cn.edu.ccut;
 7 
 8 public class MyInteger {
 9      static int value ;
10     
11     MyInteger(int value){ //构造方法
12         MyInteger.value = value; 
13     }
14     
15     public int  getvalue() {//访问器
16         return value;
17     }
18     
19     public void setvalue(int value) {//修改器
20         MyInteger.value = value;
21     }
22     
23     public boolean isEven(){//判断是否为偶数
24         if(value%2 == 0){
25             return true;
26         }
27         else
28             return false;
29     }
30     
31     public boolean isOdd(){//判断是否为奇数
32         if(value%2 == 0){
33             return false;
34         }
35         else
36             return true;
37     }
38     
39     public static boolean isPrime(MyInteger i){//判断是否为素数
40         int i;
41         for(i=2;i<value/2;i++){
42             if(value % i ==0){
43                 return false;
44             }
45         }
46         return true;
47     }
48 }
复制代码

运行结果:

猜你喜欢

转载自www.cnblogs.com/iroywang/p/11565154.html