In: Computer Science
Write a method with the signature
public static NaturalNumber divRemainder(NaturalNumber m, NaturalNumber n)
The method takes two NaturalNumbers as arguments and should return a new NaturalNumber whose value is the sum of m divided by n and the remainder of m divided by n.
For example, if m = 23 and n = 6, the method should return a NaturalNumber with the value 8, since 23/6 + 23 rem 6 = 3 + 5 = 8.
Do not use toInt() in your method.
Do not use any constructors in your method. Instead use newInstance().
//create one class name with NaturalNumber.java
public class NaturalNumber {
//The method takes two NaturalNumbers as arguments and should
return a new NaturalNumber whose
//value is the sum of m divided by n and the remainder of m divided
by n.
public static int naturalNumberDivRemainder(int m, int n) {
int dividend = m / n;
int remainder = m % n;
return dividend + remainder;
}
}
//create test class
import java.util.Scanner;
public class TestDemo {
public static void main(String[] args) {
try {
NaturalNumber naturalNumber =
NaturalNumber.class.newInstance();
Scanner sc = new Scanner(System.in);
System.out.println("Enter Natural Number (m) :");
int m = sc.nextInt();
System.out.println("Enter Natural Number (n) :");
int n = sc.nextInt();
int anwser= naturalNumber.naturalNumberDivRemainder(m, n);
System.out.println("Value is: "+anwser);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//output
Enter Natural Number (m) :
23
Enter Natural Number (n) :
6
Value is: 8