Sourabh Suresh Bandgar
Roll No: 10
Experiment 7
Q1. Develop a mathematical package for Statistical operations like factorial, cube. Create
a sub package in the math package -convert. In “convert” package provide classes to
convert decimal to octal, binary, hex and vice-versa. Develop application program to use
this package
MathApp/
├── math/
│ ├── [Link]
│ └── convert/
│ └── [Link]
└── [Link]
[Link]:
package math;
public class Statistical {
public int factorial(int n) {
if (n <= 1) return 1;
else return n * factorial(n - 1);
}
public int cube(int n) {
return n * n * n;
}
}
[Link]:
package [Link];
public class DecimalConverter {
public String decimalToBinary(int decimal) {
return [Link](decimal);
}
public String decimalToOctal(int decimal) {
return [Link](decimal);
}
public String decimalToHex(int decimal) {
return [Link](decimal);
}
public int binaryToDecimal(String binary) {
return [Link](binary, 2);
}
public int octalToDecimal(String octal) {
return [Link](octal, 8);
}
Public int hexToDecimal(String hex) {
return [Link](hex, 16);}}
[Link]:
import [Link];
import [Link];
import [Link];
public class App {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Statistical stats = new Statistical();
DecimalConverter converter = new DecimalConverter();
[Link]("Mathematical and Conversion Operations:");
[Link]("Enter a number to calculate factorial: ");
int num = [Link]();
[Link]("Factorial of " + num + " is: " + [Link](num));
[Link]("Enter a number to calculate cube: ");
num = [Link]();
[Link]("Cube of " + num + " is: " + [Link](num));
[Link]("Enter a decimal number to convert: ");
num = [Link]();
[Link]("Binary: " + [Link](num));
[Link]("Octal: " + [Link](num));
[Link]("Hexadecimal: " + [Link](num));
[Link]("Enter a binary number to convert to decimal: ");
String binary = [Link]();
[Link]("Decimal: " + [Link](binary));
[Link]("Enter an octal number to convert to decimal: ");
String octal = [Link]();
[Link]("Decimal: " + [Link](octal));
[Link]("Enter a hexadecimal number to convert to decimal: ");
String hex = [Link]();
[Link]("Decimal: " + [Link](hex));
[Link]();
}
}
OUTPUT:
PS D:\JAVA_PROGRAMMING> javac [Link]
math/[Link] math/convert/[Link]
PS D:\JAVA_PROGRAMMING> java App
Mathematical and Conversion Operations:
Enter a number to calculate factorial: 10
Factorial of 10 is: 3628800
Enter a number to calculate cube: 9
Cube of 9 is: 729
Enter a decimal number to convert: 10
Binary: 1010
Octal: 12
Hexadecimal: a
Enter a binary number to convert to decimal: 1010
Decimal: 10
Enter an octal number to convert to decimal: 12
Decimal: 10
Enter a hexadecimal number to convert to decimal: a
Decimal: 10
Q2. Develop a package Operation consisting Addition class to perform addition of two
numbers. Create class test and use the package addition to get addition of two numbers.
OperationApp/
├── Operation/
│ └── [Link]
└── [Link]
[Link]
package Operation;
public class Addition {
// Method to add two numbers
public int add(int a, int b) {
return a + b;
}
}
[Link]
import [Link];
import [Link];
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Create an object of the Addition class
Addition addition = new Addition();
// Take two numbers as input
[Link]("Enter the first number: ");
int num1 = [Link]();
[Link]("Enter the second number: ");
int num2 = [Link]();
// Perform addition and display the result
int result = [Link](num1, num2);
[Link]("The sum of " + num1 + " and " + num2 + " is: " + result);
[Link]();
}
}
OUTPUT
PS D:\JAVA_PROGRAMMING> javac [Link]
Operation/[Link]
PS D:\JAVA_PROGRAMMING> java Test
Enter the first number: 10
Enter the second number: 19
The sum of 10 and 19 is: 29
Q3. Develop a package Operations consisting of classes addition, subtraction,
multiply, divide to provide the respective functionality. Create class test and use the
package Operations to perform various operations on two numbers.
OperationsApp/
├── Operations/
│ ├── [Link]
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
└── [Link]
[Link]
package Operations;
public class Addition {
public int add(int a, int b) {
return a + b;
}
}
[Link]
package Operations;
public class Subtraction {
public int subtract(int a, int b) {
return a - b;
}
}
[Link]
package Operations;
public class Multiply {
public int multiply(int a, int b) {
return a * b;
}
}
[Link]
package Operations;
public class Divide {
public int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return a / b;
}
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Create objects for each operation class
Addition addition = new Addition();
Subtraction subtraction = new Subtraction();
Multiply multiply = new Multiply();
Divide divide = new Divide();
// Take two numbers as input
[Link]("Enter the first number: ");
int num1 = [Link]();
[Link]("Enter the second number: ");
int num2 = [Link]();
// Perform operations and display results
[Link]("Addition: " + [Link](num1, num2));
[Link]("Subtraction: " + [Link](num1, num2));
[Link]("Multiplication: " + [Link](num1, num2));
try {
[Link]("Division: " + [Link](num1, num2));
} catch (ArithmeticException e) {
[Link]("Error: " + [Link]());
}
[Link]();
}
}
OUTPUT
PS D:\JAVA_PROGRAMMING> javac [Link]
Operations/[Link] Operations/[Link] Operations/[Link]
Operations/[Link]
PS D:\JAVA_PROGRAMMING> java Test
Enter the first number: 10
Enter the second number: 19
Addition: 29
Subtraction: -9
Multiplication: 190
Division: 0