第七次作业

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

1.代码

                       //矩形类

package QQQ;


public class Rect{


double a,b,area;
public Rect(double a,double b){
this.a=a;
this.b=b;
}

public double getArea(){                    //面积方法
return area=a*b;
}
}

 

package QQQ;                                     //四棱柱类


import QQQ.Rect;


public class Cone {

Rect rect ;                                  //创建对象
double h;
double v;
Cone(double h,Rect rect){                    //构造方法
this.h=h;
this.rect=rect;
}


public double getV() {
return v=h*rect.getArea();
}
public void setRect(Rect rect){
this.rect=rect;

}

}


package QQQ;                                                    //主类

import java.io.Reader;
import java.util.Scanner;

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub


Scanner reader=new Scanner(System.in);              //将长宽高进行输入操作
System.out.println("请输入四棱柱的长");
double a=reader.nextDouble();
System.out.println("请输入四棱柱的宽");
double b=reader.nextDouble();
System.out.println("请输入四棱柱的高");
double h=reader.nextDouble();
Rect rect=new Rect(a,b);                            //定义矩形类对象
Cone c=new Cone(h,rect);                             //定义柱体类对象
System.out.println("柱体的体积为:"+c.getV());

System.out.println("请输入想要换底四棱柱的长");               //换底
double a1 =reader.nextDouble();
System.out.println("请输入想要换底四棱柱的宽");
double b1 =reader.nextDouble();
Rect rect1 = new Rect(a1, b1);
c.setRect(rect1);                                       //调用换底方法
System.out.println("换底后的矩形面积为"+rect1.getArea());
System.out.println("换底后的柱体体积为"+c.getV());

}

}

2.输出结果

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

1.代码

 1 package WWW;
 2 
 3 public class MyInteger {                   //MyInteger类
 4 int value;                         
 5 
 6    MyInteger(int value) {                  //构造方法
 7         this.value=value;
 8     }
 9 
10    
11    public int setValue(){                  //构造器
12        return value;
13    }
14   public void getValue(int value){         //修改器
15       this.value=value;       
16    }
17    public boolean isEven(int value){       //偶数方法
18        if(value%2==0)
19            return true;
20     return false;
21    }
22    public boolean isOdd(int value){        //奇数方法
23        if(value%2!=0)
24            return true;
25     return false;
26    }
27   public static  boolean isPrime(MyInteger m){     //素数方法
28       for(int i=2;i<m.value;i++){
29           if (m.value%i==0){ 
30               return true;
31               
32           }
33          
34       }
35     return false;
36     
37   }
38     public static void main(String[] args) {
39         // TODO Auto-generated method stub
40 
41     }
42 
43 }
 1 package WWW;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test {                                      //主类
 6 
 7     MyInteger myInteger =new MyInteger(0);               //创建对象
 8     public static void main(String[] args) {
 9         Scanner reader=new Scanner(System.in);
10         System.out.println("请输入验证的数");
11         int n =reader.nextInt();
12         MyInteger my =new MyInteger(n);
13         System.out.println("是否为偶数"+my.isEven(n));
14         System.out.println("是否为奇数"+my.isOdd(n));
15         System.out.println("是否为素数"+my.isPrime(my));
16 
17     }
18 
19 }

2.运行结果

原文地址:https://www.cnblogs.com/changheng/p/11565028.html