JAVA第七次作业

题目1:

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

1、R.java

 1 package com;
 2 
 3 public class R {
 4      static double x;//
 5      static double y;//
 6     
 7     public double getA() {
 8         return x;
 9     }
10     public void setA(double a) {
11         this.x = a;
12     }
13     public double getB() {
14         return y;
15     }
16     public void setB(double b) {
17         this.y = b;
18     }
19     R( double a,double b){ //接受长宽
20         this.x=a;
21         this.y=b;
22     }
23    double getArea(){// 返回底面积
24        return x*y;
25    }
26 
27 }

2、A.java

 1 package com;
 2 
 3 import java.util.Scanner;
 4 
 5 public class A {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args){
11         
12         //修改之前的
13         Scanner reader=new Scanner(System.in);
14         int a=reader.nextInt();
15         int b=reader.nextInt();
16         int high=reader.nextInt();
17         R re1=new R(a,b);
18         C co1=new C(high, re1);
19         System.out.println(co1.getVolume());
20         //修改之后的
21         re1.x=reader.nextInt();
22         re1.y=reader.nextInt();
23         System.out.println(co1.changeArea(re1.x, re1.y)*high);
24 
25 
26     }
27 
28 }

3、C.java

 1 package com;
 2 
 3 public class C {
 4   double hight; //
 5   double volume;//体积
 6   R  rect;//创建底面积
 7   
 8   public double getHight() { // 修改器,访问器
 9     return hight;
10 }
11 public void setHight(double hight) {
12     this.hight = hight;
13 }
14 public R getRect() {
15     return rect;
16 }
17 public void setRect(R rect) {
18     this.rect = rect;
19 }
20 public void setVolume(double volume) {
21     this.volume = volume;
22 }
23 C(double hight,R  rect){
24       this.hight=hight;
25       this.rect=rect;
26   }
27   double getVolume(){ //返回体积
28       return hight*rect.getArea();
29   }
30   
31   public double changeArea(double width,double length){//修改底面积
32       double newArea=width*length;
33       return  newArea;
34   }
35 }

输出显示结果:

题目2

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

MyInteger.java

1.设置数值       2.创建MyInteger对象        3.三个方法判读奇数偶数

 1 package com;
 2 
 3 public class MyInteger {
 4 int value;// 设置数值
 5 MyInteger mi;//创建MyInteger对象
 6 
 7 public int getValue() {// 修改器,访问器
 8     return value;
 9 }
10 
11 public void setValue(int value) {
12     this.value = value;
13 }
14 
15 boolean isEven(){//判读偶数
16     if(value%2==0){
17         return true;
18     }
19     else 
20         return false;
21 }
22 
23 boolean isOdd(){ //判断奇数
24     
25     if(value%2==1){
26         return true;
27     }
28     else 
29         return false;
30 }
31 static boolean isPrime(MyInteger i){// 类方法isPrime(MyInteger i),判断指定的值是否为素数
32     if(i.value%2==0){
33         return true;
34     }
35     else 
36         return false;
37 }
38 }

Test.java

1.创建MyInteger对象     2.设置数值     3.判读奇数偶数

 1 package com;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         Scanner reader=new Scanner(System.in);
12         MyInteger mi=new MyInteger(); //创建MyInteger对象
13         int a=reader.nextInt();
14         mi.value=a;//设置数值
15         System.out.println(mi.isEven());//判断偶数
16         System.out.println(mi.isOdd());//判断奇数
17         System.out.println(mi.isPrime(mi));//判断奇数偶数
18 
19     }
20 
21 }

输出结果:

原文地址:https://www.cnblogs.com/lietian12345/p/11567477.html