JAVA第七次作业

题目1:

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

1、源代码

 1 package Qwe;
 2 public class Rect{
 3     double a,b,area;
 4     public Rect(double a,double b){
 5     this.a=a;
 6     this.b=b;
 7     }
 8     public double getArea(){//面积方法
 9     return area=a*b;
10     }
11 }
 1 package Qwe;
 2 import Qwe.Rect;
 3 public class Cone {
 4 Rect rect ;//创建对象
 5 double h;
 6 double v;
 7 Cone(double h,Rect rect){//构造方法
 8 this.h=h;
 9 this.rect=rect;
10 }
11 public double getV() {
12 return v=h*rect.getArea();
13 }
14 public void setRect(Rect rect){
15 this.rect=rect;
16 }
17 }
 1 package Qwe;
 2 import java.util.Scanner;
 3 public class Test {
 4 public static void main(String[] args) {
 5 Scanner reader=new Scanner(System.in);//输入长宽高
 6 System.out.println("请输入四棱柱的长");
 7 double a=reader.nextDouble(); 
 8 System.out.println("请输入四棱柱的宽");
 9 double b=reader.nextDouble(); 
10 System.out.println("请输入四棱柱的高");
11 double h=reader.nextDouble(); 
12 Rect rect=new Rect(a,b);//定义矩形类
13 Cone c=new Cone(h,rect);//定义柱体
14 System.out.println("柱体的体积为:"+c.getV());
15 System.out.println("请输入想要换底四棱柱的长");//进行换底
16 double a1 =reader.nextDouble();
17 System.out.println("请输入想要换底四棱柱的宽");
18 double b1 =reader.nextDouble();
19 Rect rect1 = new Rect(a1, b1);
20 c.setRect(rect1);//调用换底方法
21 System.out.println("换底后的矩形面积为"+rect1.getArea());
22 System.out.println("换底后的柱体体积为"+c.getV());
23 }
24 }

2、运行结果

 

 题目2:

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

1、源代码

 

 1 package Wasd;
 2 public class MyInteger {//创建MyInteger类
 3 int value;                         
 4    MyInteger(int value) { //构造方法
 5         this.value=value;
 6     }   
 7    public int setValue(){//构造器
 8        return value;
 9    }
10   public void getValue(int value){//修改器
11       this.value=value;       
12    }
13    public boolean isEven(int value){//创建偶数方法
14        if(value%2==0)
15            return true;
16     return false;
17    }
18    public boolean isOdd(int value){//创建奇数方法
19        if(value%2!=0)
20            return true;
21     return false;
22    }
23   public static  boolean isPrime(MyInteger m){//创建素数方法
24       for(int i=2;i<m.value;i++){
25           if (m.value%i==0){ 
26               return true;              
27           }        
28       }
29     return false;   
30   }    
31 }

 

 1 package Wasd;
 2 import java.util.Scanner;
 3 public class Test2 {//主类
 4     MyInteger myInteger =new MyInteger(0);//创建对象
 5     public static void main(String[] args) {
 6         Scanner reader=new Scanner(System.in);
 7         System.out.println("请输入验证的数");
 8         int n =reader.nextInt();
 9         MyInteger my =new MyInteger(n);
10         System.out.println("是否为偶数"+my.isEven(n));
11         System.out.println("是否为奇数"+my.isOdd(n));
12         System.out.println("是否为素数"+my.isPrime(my));
13     }
14 
15 }

2、运行结果

 

 

posted @ 2019-09-22 21:02  黄晓天20194672  阅读(179)  评论(0编辑  收藏  举报