第7次作业--访问权限、对象使用
题目一
在作业5的基础上,再创建一个柱体类,包含矩形对象、高和体积等三个成员变量,一个构造方法进行成员变量初始化,和计算体积、换底两个功能方法,
在主类中输入长、宽、高,计算柱体体积,输入新的长、宽、高,创建新的矩形对象,并利用换底方法换底,再次计算柱体体积。
Rect 类
package com.tomotoes.probleam.seven;
/**
* @author Simon
* @project practice
* @package com.tomotoes.probleam
* @date 2019/9/18 17:42
*/
public class Rect {
double length;
double width;
double perimeter;
double area;
Rect(double length, double width) {
this.length = length;
this.width = width;
this.perimeter = (length + width) * 2;
this.area = length * width;
}
public double getPerimeter() {
return perimeter;
}
public double getArea() {
return area;
}
}
Column 类
package com.tomotoes.probleam.seven;
/**
* @author Simon
* @project practice
* @package com.tomotoes.probleam.seven
* @date 2019/9/20 16:49
*/
public class Column {
public Rect rect;
public double height;
public double volume;
public Column(Rect rect, double height) {
this.rect = rect;
this.height = height;
this.volume = this.rect.getArea() * height;
}
public double getVolume() {
return this.volume;
}
public void setRect(Rect rect) {
this.rect = rect;
this.volume = this.rect.getArea() * height;
}
}
主类
package com.tomotoes.probleam.seven;
import java.util.Scanner;
/**
* @author Simon
* @project practice
* @package com.tomotoes.probleam.seven
* @date 2019/9/20 16:46
*/
public class One {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final double length = scanner.nextDouble();
final double width = scanner.nextDouble();
final double height = scanner.nextDouble();
if (length <= 0 || width <= 0 || height <= 0) {
throw new Error("Digital input must be positive.");
}
Column column = new Column(new Rect(length, width), height);
final double volume = column.getVolume();
System.out.println(volume);
final double newLength = scanner.nextDouble();
final double newWidth = scanner.nextDouble();
final double newHeight = scanner.nextDouble();
if (newLength <= 0 || newWidth <= 0 || newHeight <= 0) {
throw new Error("Digital input must be positive.");
}
column.setRect(new Rect(newLength, newWidth));
final double newVolume = column.getVolume();
System.out.println(newVolume);
}
}
运行结果

题目二
设计名为MyInteger的类,它包括:int型数据域value 一个构造方法,当指定int值时,创建MyInteger对象 ,数据域value的访问器和修改器 isEven( )和isOdd( )方法,
如果当前对象是偶数或奇数,返回true ,类方法isPrime(MyInteger i),判断指定的值是否为素数,返回true ,在主类中创建MyInteger对象,验证MyInteger类中各方法
MyInteger 类
package com.tomotoes.probleam.seven;
/**
* @author Simon
* @project practice
* @package com.tomotoes.probleam.seven
* @date 2019/9/20 17:47
*/
public class MyInteger {
private int value;
public MyInteger(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isEven() {
return this.value % 2 == 0;
}
public boolean isOdd() {
return !isEven();
}
public static boolean isPrime(MyInteger i) {
final int number = i.getValue();
if (number < 2) {
return false;
}
if (number == 2) {
return true;
}
final int sqrt = (int) Math.sqrt(number);
for (int j = 2; j <= sqrt; j++) {
if (number % j == 0) {
return false;
}
}
return true;
}
}
主类
package com.tomotoes.probleam.seven;
import java.util.Scanner;
/**
* @author Simon
* @project practice
* @package com.tomotoes.probleam.seven
* @date 2019/9/20 17:53
*/
public class Two {
public static void main(String[] args) {
System.out.println("Enter a negative number, exit the program.");
Scanner scanner = new Scanner(System.in);
while (true) {
final int number = scanner.nextInt();
if (number < 0) {
return;
}
MyInteger myInteger = new MyInteger(number);
System.out.println(MyInteger.isPrime(myInteger));
}
}
}
运行结果


浙公网安备 33010602011771号