0% found this document useful (0 votes)
12 views3 pages

Java Code for Mixed Math Operations

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)
12 views3 pages

Java Code for Mixed Math Operations

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

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

You might also like