public class RationalNum {
private int RNum; //有理数
public int getRNum() {
return RNum;
}
public void setRNum(int rNum) {
RNum = rNum;
}
public int add(int addNum) {//相加
int num = RNum + addNum;
return num;
}
public int sub(int subNum) {//相减
int num = RNum - subNum;
return num;
}
public int mul(int mulNum) {//相乘
int num = RNum * mulNum;
return num;
}
public double div(double divNum) {//相除
double num = RNum / divNum;
return num;
}
public int rem(int remNum) {//求余
int num = RNum%remNum;
return num;
}
public double sqrt() {//开方
double num=Math.sqrt(RNum);
return num;
}
}
2.测试代码
import java.util.Scanner;
import test.RationalNum;
public class test2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numb1 = scanner.nextInt();
int num2 = scanner.nextInt();
RationalNum firstNum = new RationalNum();
firstNum.setRNum(numb1);
System.out.println("相加得:" + firstNum.add(num2));
System.out.println("相减得:" + firstNum.sub(num2));
System.out.println("相乘得:" + firstNum.mul(num2));
System.out.println("相除得:" + firstNum.div((double)num2));
System.out.println("除余得:" + firstNum.rem(num2));
System.out.println("开方得:" + firstNum.sqrt());
scanner.close();
}
}