题目1:在作业5的基础上,再创建一个柱体类,包含矩形对象、高和体积等三个成员变量,一个构造方法进行成员变量初始化,和计算体积、换底两个功能方法,在主类中输入长、宽、高,计算柱体体积,输入新的长、宽、高,创建新的矩形对象,并利用换底方法换底,再次计算柱体体积。
1 package Test; 2 public class test5 { 3 int count; 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 //建立矩形的类的对象 7 Rectangle rect = new Rectangle(2,3); 8 double area = rect.getArea(); 9 double peri = rect.getPeri(); 10 //System.out.println("矩形的面积是:"+area+"矩形的周长是:"+peri); 11 Pyramid pyr = new Pyramid(rect,4); 12 double volumn = pyr.getVolumn(); 13 //System.out.println("四棱柱的体积是:"+volumn); 14 Podetium pode = new Podetium(rect,5); 15 double volumn1 = pode.getVolumn(); 16 System.out.println("新柱体体积是:"+volumn1); 17 } 18 } 19 class Rectangle{ 20 //矩形的宽 21 private double width = 0.0; 22 //矩形的高 23 private double height = 0.0; 24 //矩形的面积 25 private double area = 0.0; 26 //矩形的周长 27 private double peri = 0.0; 28 Rectangle(double width,double height){ 29 this.width = width; 30 this.height = height; 31 } 32 //计算矩形的面积 33 public double getArea() { 34 area = width*height; 35 return area; 36 } 37 //计算矩形的周长 38 public double getPeri() { 39 peri = (height+width)*2; 40 return peri; 41 } 42 public double getWidth() { 43 return width; 44 } 45 public void setWidth(double width) { 46 this.width = width; 47 } 48 public double getHeight() { 49 return height; 50 } 51 public void setHeight(double height) { 52 this.height = height; 53 } 54 } 55 class Pyramid{ 56 Rectangle rect; 57 double height; 58 double volumn; 59 Pyramid(Rectangle rect,double height){ 60 this.rect = rect; 61 this.height = height; 62 } 63 public double getVolumn() { 64 volumn = rect.getArea()*height; 65 return volumn; 66 } 67 } 68 class Podetium{ 69 Rectangle rect; 70 double height; 71 double volumn; 72 Podetium(Rectangle rect,double height){ 73 this.rect = rect; 74 this.height = height; 75 } 76 public double getArea() { 77 return rect.getHeight()*rect.getWidth(); 78 } 79 public double getVolumn(){ 80 volumn = getArea()*height; 81 return volumn; 82 } 83 }
题目2:设计名为MyInteger的类,它包括: int型数据域value 一个构造方法,当指定int值时,创建MyInteger对象 数据域value的访问器和修改器 isEven( )和isOdd( )方法,如果当前对象是偶数或奇数,返回true 类方法isPrime(MyInteger i),判断指定的值是否为素数,返回true 在主类中创建MyInteger对象,验证MyInteger类中各方法。
package Test; public class MyInteger { int value; public int getValue() { return value; } public void setValue(int value) { this.value = value; } MyInteger(int value){ this.value = value; } public Boolean isEven(){ return value%2==0; } public Boolean isOdd() { return value%2==1; } public static Boolean isPrime(MyInteger i) { Boolean flag = false; for(int j = 2;j<=i.value/2;j++) { if(i.value%j==0) flag = true; } return flag; } public static void main(String[] args) { // TODO Auto-generated method stub MyInteger myinteger = new MyInteger(7); if(myinteger.getValue()<1) { try { throw new MyException("素数必须都大于1"); } catch (MyException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //判断传入的值 else if(MyInteger.isPrime(myinteger)) { System.out.println(myinteger.getValue()+"不是个素数"); } else{ System.out.println(myinteger.getValue()+"是个素数"); } } } //自定义异常 class MyException extends Exception{ /** * */ private static final long serialVersionUID = 1L; MyException(String msg){ //将信息传给父类构造函数 super(msg);