一类
package experiment;
public class MyInteger {
private int value;
public MyInteger(int value) {
this.value = value;
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
//无参判断是否为偶数
public String isEven() {
if(this.value%2==0) {
return "true";
}else {
return "false";
}
}
//无参判断是否为奇数
public String isOdd() {
if(this.value%2==1) {
return "true";
}else {
return "false";
}
}
//无参判断是否为素数
public String isPrime() {
int i;
for(i = 2;i<this.value;i++) {
if(this.value%i==0) {
break;
}
}
if(i == this.value) {
return "true";
}else {
return "false";
}
}
//静态传整形判断是否为偶数
public static String isEven(int value) {
if(value%2==0) {
return "true";
}else {
return "false";
}
}
//静态传整形判断是否为奇数
public static String isOdd(int value) {
if(value%2==1) {
return "true";
}else {
return "false";
}
}
//静态传整形判断是否为素数
public static String isPrime(int value) {
int i;
for(i = 2;i<value;i++) {
if(value%i==0) {
break;
}
}
if(i == value) {
return "true";
}else {
return "false";
}
}
//静态传对象判断是否为偶数
public static String isEven(MyInteger myInteger) {
if(myInteger.value%2==0) {
return "true";
}else {
return "false";
}
}
//静态传对象判断是否为奇数
public static String isOdd(MyInteger myInteger) {
if(myInteger.value%2==1) {
return "true";
}else {
return "false";
}
}
//静态传对象判断是否为素数
public static String isPrime(MyInteger myInteger) {
int i;
for(i = 2;i<myInteger.value;i++) {
if(myInteger.value%i==0) {
break;
}
}
if(i == myInteger.value) {
return "true";
}else {
return "false";
}
}
//传整形判断是否相等
public String equals(int value) {
if(this.value == value) {
return "true";
}else {
return "false";
}
}
//传对象判断是否等值
public String equals(MyInteger myInteger) {
if(this.value == myInteger.value) {
return "true";
}else {
return "false";
}
}
}
测试类
package experiment;
import java.util.Scanner;
public class MyIntegerTest {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
MyInteger n1 = new MyInteger(input.nextInt());
System.out.println("n1是偶数吗?"+"\t"+n1.isEven());
System.out.println("n1是素数吗?"+"\t"+n1.isPrime());
System.out.println("n1是素数吗?"+"\t"+MyInteger.isPrime(n1));
MyInteger n2 = new MyInteger(input.nextInt());
System.out.println("n2是奇数吗?"+"\t"+n2.isOdd());
System.out.println("45是奇数吗?"+"\t"+MyInteger.isOdd(45));
System.out.println("n1与n2相等吗?"+"\t"+n1.equals(n2));
System.out.println("n1等于5吗?"+"\t"+n1.equals(5));
input.close();
}
}
结果











