0% found this document useful (0 votes)
2 views

Assignment 9 Java

The document outlines the creation of a Java package named 'mathOperations' that includes classes for basic arithmetic operations: Addition, Subtraction, Multiplication, and Division. Each class contains a method to perform its respective operation, with the Division class handling division by zero by returning Infinity. A main class, MathOperationsDemo, demonstrates the use of these classes by performing and printing the results of all four operations.

Uploaded by

Monojit Kar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 9 Java

The document outlines the creation of a Java package named 'mathOperations' that includes classes for basic arithmetic operations: Addition, Subtraction, Multiplication, and Division. Each class contains a method to perform its respective operation, with the Division class handling division by zero by returning Infinity. A main class, MathOperationsDemo, demonstrates the use of these classes by performing and printing the results of all four operations.

Uploaded by

Monojit Kar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

Addition.java

package mathOperations;

public class Addition {

public static int add(int a, int b) {

return a + b;

Subtraction.java

package mathOperations;

public class Subtraction {

public static int subtract(int a, int b) {

return a - b;

Multiplication.java

package mathOperations;

public class Multiplication {

public static int multiply(int a, int b) {

return a * b;

}
}

Division.java

package mathOperations;

public class Division {

public static double divide(int a, int b) {

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;

public class MathOperationsDemo {

public static void main(String[] args) {

int a = 20;

int b = 5;

// Perform addition

int sum = Addition.add(a, b);

System.out.println(a + " + " + b + " = " + sum);

// Perform subtraction
int difference = Subtraction.subtract(a, b);

System.out.println(a + " - " + b + " = " + difference);

// Perform multiplication

int product = Multiplication.multiply(a, b);

System.out.println(a + " * " + b + " = " + product);

// Perform division

double quotient = Division.divide(a, b);

System.out.println(a + " / " + b + " = " + quotient);

// Test division by zero

double infinityTest = Division.divide(a, 0);

System.out.println(a + " / 0 = " + infinityTest);

Output:

javac mathOperations/*.java MathOperationsDemo.java

java MathOperationsDemo

20 + 5 = 25

20 - 5 = 15

20 * 5 = 100

20 / 5 = 4.0

20 / 0 = Infinity

You might also like