于达——第七次作业

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

代码:

import java.util.Scanner;

public class Rectangle {//长方形类
    static int length;//长度
    static int width;//宽度
    int area;//面积
    int perimeter;//周长
    public void setarea(int x, int y) {//计算面积方法
        area = x * y;
    }
    public int getarea() {//面积访问器
        return area;
    }
    void setperimeter(int x, int y) {//计算周长方法
        perimeter = (x + y) * 2;
    }
    public int getperimeter() {//周长访问器
        return perimeter;
    }
    public static void main(String[] args) {//主方法

    Rectangle rect = new Rectangle();//创建一个新长方形对象rect
    Cobuid a=new Cobuid(rect);//创建一个新长方体对象a
    Scanner reader = new Scanner(System.in);
    System.out.println("请输入长宽高:");
    length=reader.nextInt();//输入长
    width=reader.nextInt();//输入宽
    rect.setarea(length,width);//调用求面积的方法
    a.volume=a.getvolume(reader.nextInt());//调用求体积的方法
    System.out.println("体积为:"+a.volume);
    Cobuid b = new Cobuid(rect);//创建一个新长方体b
    System.out.println("请输入B的长宽高:");
    length=reader.nextInt();
    width=reader.nextInt();
    rect.setarea(length,width);//调用方法。求b的底面积
    b.volume=b.getvolume(reader.nextInt());//调用方法。求b的体积
    System.out.println("体积为:"+b.volume);
    System.out.println("请输入长宽进行换底");
    b.setA(reader.nextInt(), reader.nextInt());//调用换底方法,修改长宽
    b.volume=b.rect.width*b.rect.length*b.height;//求换底之后,b的体积
    System.out.println("体积为:"+b.volume);//输出b的体积
    }
}
class Cobuid{//长方体类
    Rectangle rect;//成员变量 Rectangle对象
    int height ;//高度
    int volume ;//体积
    Cobuid(Rectangle rect) {//构造方法
        this.rect = rect;
    }
    int getvolume(int j) {//获取体积方法
        height=j;
        volume=height*rect.getarea();
        return volume;
    }
    public void setA(int a,int b) {//Rectangle长宽修改器
        rect.length =a;
        rect.width=b;
    }
}
 运行结果:
 
  
 
 
 题目2:设计名为MyInteger的类,它包括: int型数据域value 一个构造方法,当指定int值时,创建MyInteger对象 数据域value的访问器和修改器 isEven( )和isOdd( )方法,如果当前对象是偶数或奇数,返回true 类方法isPrime(MyInteger i),判断指定的值是否为素数,返回true 在主类中创建MyInteger对象,验证MyInteger类中各方法。 
 
代码:
package src.project;

public class MyInteger {
    static int value;// 类变量value

    void MyInteger(int value) {// 构造方法
        this.value = value;
    }

    public int getValue() {// value访问器
        return value;
    }

    public void setValue(int value) {// value修改器
        this.value = value;
    }

    boolean isEven() {// 判断是否为偶数方法
        if (value % 2 == 0) {
            return true;
        } else
            return false;
    }

    boolean isOdd() {// 判断是否为奇数方法
        if (value % 2 != 0) {
            return true;
        } else
            return false;
    }

    static boolean isPrime() {// 判断是否为素数方法
        int j;
        for (j = 2; j < value; j++) {
            if (value % j == 0) {
                return false;
            }
        }
        return true;

    }

    public static void main(String[] args) {

        MyInteger a = new MyInteger();// 创建对象
        a.setValue(6);// 调用修改器。输入一个6

        System.out.println("是否为偶数:" + a.isEven() + "是否为奇数:" + a.isOdd() + "是否为素数:" + a.isPrime());
//验证方法

    }
}

运行结果:

 

猜你喜欢

转载自www.cnblogs.com/chen4635/p/11568429.html