题目1:在作业5的基础上,再创建一个柱体类,包含矩形对象、高和体积等三个成员变量,一个构造方法进行成员变量初始化,和计算体积、换底两个功能方法,在主类中输入长、宽、高,计算柱体体积,输入新的长、宽、高,创建新的矩形对象,并利用换底方法换底,再次计算柱体体积。
Rectangle.java
1.设置矩形边长
2.获取矩形面积
1 package com; 2 3 public class Rectangle { 4 static double a;//长 5 static double b;//宽 6 7 public double getA() { 8 return a; 9 } 10 public void setA(double a) { 11 this.a = a; 12 } 13 public double getB() { 14 return b; 15 } 16 public void setB(double b) { 17 this.b = b; 18 } 19 Rectangle( double a,double b){ //接受长宽 20 this.a=a; 21 this.b=b; 22 } 23 double getArea(){// 返回底面积 24 return a*b; 25 } 26 27 }
Cone.java
1.设置高
2.创建底面积,设置修改器,访问器
3.返回体积
4.更改地面积
1 package com; 2 3 public class Cone { 4 double hight; //高 5 double volume;//体积 6 Rectangle rect;//创建底面积 7 8 public double getHight() { // 修改器,访问器 9 return hight; 10 } 11 public void setHight(double hight) { 12 this.hight = hight; 13 } 14 public Rectangle getRect() { 15 return rect; 16 } 17 public void setRect(Rectangle rect) { 18 this.rect = rect; 19 } 20 public void setVolume(double volume) { 21 this.volume = volume; 22 } 23 Cone(double hight,Rectangle 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 }
App.java
1.创建高的对象
2.创建体积对象
3.设置变量
4.保持高不变,设置底面积
1 package com; 2 3 import java.util.Scanner; 4 5 public class App { 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 Rectangle re1=new Rectangle(a,b); 18 Cone co1=new Cone(high, re1); 19 System.out.println(co1.getVolume()); 20 //修改之后的 21 re1.a=reader.nextInt(); 22 re1.b=reader.nextInt(); 23 System.out.println(co1.changeArea(re1.a, re1.b)*high); 24 25 26 } 27 28 }
题目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 m;//创建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 m=new MyInteger(); //创建MyInteger对象 13 int a=reader.nextInt(); 14 m.value=a;//设置数值 15 System.out.println(m.isEven());//判断偶数 16 System.out.println(m.isOdd());//判断奇数 17 System.out.println(m.isPrime(m));//判断奇数偶数 18 19 } 20 21 }