• Java第7次作业


    一、作业

    题目一

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

    题目二

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

    二、程序代码:

    题目一

    代码块(一):

    package HomeWork;

    import HomeWork2.CylinderBody;

    public class CylinderBodyMain {

     

    public static void main(String[] args) {            

    CylinderBody p=new CylinderBody();//创建一个名为p的柱形对象

    System.out.println(p.InitialVolume(4,5,6)); //换初始化值(换底)求p体积

    CylinderBody p2=new CylinderBody();//创建一个名为p2的柱形对象

    System.out.println(p2.InitialVolume(5,6,7));//换初始化值(换底)求p2体积

    }

    }

    代码块(二):

    package HomeWork2;

    public class CylinderBody {

    int rectLong,rectWeight,rectArea,height,volume; //定义长、宽、高、体积4个成员变量

    public int Rectangle(int l,int w,int h){        // 定义一个矩形类

    rectLong=l;                                   //初始化长

    rectWeight=w;                                 //初始化宽

    height=h;

    rectArea=rectLong*rectWeight;               //初始化面积

    return rectArea;

    }

    public int area(int x,int y){                //创建一个求面积的构造方法

    rectArea=x*y;

    return rectArea;

    }

    public int InitialVolume(int l,int w,int h){//创建一个换底和求体积构造方法

    volume=area(l,w)*h;

    return volume;

    }

    }

     

    题目二

    代码块(一):

    package HomeWork;

    import HomeWork2.Mylnteger;

    public class MylntegerMain {

    public static void main(String[] args) {

    Integer value= 5;                                             

    if(value instanceof Integer){

    HomeWork2.Mylnteger p=new Mylnteger();

    p.setValue(value);

    System.out.println(p.getValue());

    System.out.println(p.isEven(value));

    System.out.println(p.isOdd(value));

    System.out.println(p.isPrime(p));

    }else{

    System.out.println("你写错了");

    }

    }

    }

     

    代码块(二):

    package HomeWork2;

    public class Mylnteger {

    int value2=5;

    public int getValue(){

    return value2;

    }

    public void setValue(int x){

    this.value2=x;

    }

    public boolean isEven(int x){

    x=this.value2;

    if(x%2==0){

    return true;

    }else{

    return false;

    }

    }

    public boolean isOdd(int x){

    x=this.value2;

    if(x%2==0){

    return false;

    }else{

    } return true;

    }

    public static boolean isPrime(Mylnteger i){

    int z=i.getValue();

    boolean flag =false;

    for(int s=2;s<z;s++){

    if(z%s==0){

    flag = true;

    }

    }

    return flag;

    }

    }

     

    三、运行结果:

    题目一

     

     

     

    题目二

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    HDU 4388 To the moon
    HDU 4757 Tree
    HDU 5816 Hearthstone
    hihocoder 1356 分隔相同整数
    HDU 5726 GCD
    POJ3026 Borg Maze(Prim)(BFS)
    POJ1258 Agri-Net(Prim)
    POJ1751 Highways(Prim)
    POJ2349 Arctic Network(Prim)
    POJ1789 Truck History(Prim)
  • 原文地址:https://www.cnblogs.com/anemone0919/p/11567271.html
Copyright © 2020-2023  润新知