Assignment 9 Java
Assignment 9 Java
• 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:
Addition.java
package mathOperations;
return a + b;
Subtraction.java
package mathOperations;
return a - b;
Multiplication.java
package mathOperations;
return a * b;
}
}
Division.java
package mathOperations;
if (b == 0) {
return Double.POSITIVE_INFINITY;
return (double) a / b;
MathOperationsDemo.java
import mathOperations.Addition;
import mathOperations.Subtraction;
import mathOperations.Multiplication;
import mathOperations.Division;
int a = 20;
int b = 5;
// Perform addition
// Perform subtraction
int difference = Subtraction.subtract(a, b);
// Perform multiplication
// Perform division
Output:
java MathOperationsDemo
20 + 5 = 25
20 - 5 = 15
20 * 5 = 100
20 / 5 = 4.0
20 / 0 = Infinity