Assignment 9
Create a package named mathOperations that contains the following classes:
• Addition: Contains a method add(int a, int b) that returns the sum of a and b.
• Subtraction: Contains a method subtract(int a, int b) that returns the difference between a and b.
• Multiplication: Contains a method multiply(int a, int b) that returns the product of a and b.
• Division: Contains a method divide(int a, int b) that returns the quotient of a and b. If division by
zero is attempted, return Infinity.
Write a main class outside the package that imports this package and uses these classes to perform
all four operations.
Source code:
[Link]
package mathOperations;
public class Addition {
public static int add(int a, int b) {
return a + b;
[Link]
package mathOperations;
public class Subtraction {
public static int subtract(int a, int b) {
return a - b;
[Link]
package mathOperations;
public class Multiplication {
public static int multiply(int a, int b) {
return a * b;
}
}
[Link]
package mathOperations;
public class Division {
public static double divide(int a, int b) {
if (b == 0) {
return Double.POSITIVE_INFINITY;
return (double) a / b;
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
public class MathOperationsDemo {
public static void main(String[] args) {
int a = 20;
int b = 5;
// Perform addition
int sum = [Link](a, b);
[Link](a + " + " + b + " = " + sum);
// Perform subtraction
int difference = [Link](a, b);
[Link](a + " - " + b + " = " + difference);
// Perform multiplication
int product = [Link](a, b);
[Link](a + " * " + b + " = " + product);
// Perform division
double quotient = [Link](a, b);
[Link](a + " / " + b + " = " + quotient);
// Test division by zero
double infinityTest = [Link](a, 0);
[Link](a + " / 0 = " + infinityTest);
Output:
javac mathOperations/*.java [Link]
java MathOperationsDemo
20 + 5 = 25
20 - 5 = 15
20 * 5 = 100
20 / 5 = 4.0
20 / 0 = Infinity