Java Exp7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

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/
│ ├── Statistical.java
│ └── convert/
│ └── DecimalConverter.java
└── App.java

Statistical.java:
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;
}
}
DecimalConverter.java:
package math.convert;
public class DecimalConverter {
public String decimalToBinary(int decimal) {
return Integer.toBinaryString(decimal);
}

public String decimalToOctal(int decimal) {


return Integer.toOctalString(decimal);
}

public String decimalToHex(int decimal) {


return Integer.toHexString(decimal);
}

public int binaryToDecimal(String binary) {


return Integer.parseInt(binary, 2);
}

public int octalToDecimal(String octal) {


return Integer.parseInt(octal, 8);
}
Public int hexToDecimal(String hex) {
return Integer.parseInt(hex, 16);}}
App.java:

import math.Statistical;
import math.convert.DecimalConverter;
import java.util.Scanner;

public class App {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Statistical stats = new Statistical();
DecimalConverter converter = new DecimalConverter();

System.out.println("Mathematical and Conversion Operations:");

System.out.print("Enter a number to calculate factorial: ");


int num = scanner.nextInt();
System.out.println("Factorial of " + num + " is: " + stats.factorial(num));

System.out.print("Enter a number to calculate cube: ");


num = scanner.nextInt();
System.out.println("Cube of " + num + " is: " + stats.cube(num));

System.out.print("Enter a decimal number to convert: ");


num = scanner.nextInt();
System.out.println("Binary: " + converter.decimalToBinary(num));
System.out.println("Octal: " + converter.decimalToOctal(num));
System.out.println("Hexadecimal: " + converter.decimalToHex(num));

System.out.print("Enter a binary number to convert to decimal: ");


String binary = scanner.next();
System.out.println("Decimal: " + converter.binaryToDecimal(binary));

System.out.print("Enter an octal number to convert to decimal: ");


String octal = scanner.next();
System.out.println("Decimal: " + converter.octalToDecimal(octal));

System.out.print("Enter a hexadecimal number to convert to decimal: ");


String hex = scanner.next();
System.out.println("Decimal: " + converter.hexToDecimal(hex));

scanner.close();
}
}
OUTPUT:

PS D:\JAVA_PROGRAMMING> javac App.java


math/Statistical.java math/convert/DecimalConverter.java

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/
│ └── Addition.java
└── Test.java

Addition.java
package Operation;

public class Addition {


// Method to add two numbers
public int add(int a, int b) {
return a + b;
}
}

Test.java
import Operation.Addition;
import java.util.Scanner;

public class Test {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create an object of the Addition class
Addition addition = new Addition();

// Take two numbers as input


System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();

// Perform addition and display the result


int result = addition.add(num1, num2);
System.out.println("The sum of " + num1 + " and " + num2 + " is: " + result);

scanner.close();
}
}
OUTPUT

PS D:\JAVA_PROGRAMMING> javac Test.java


Operation/Addition.java

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/
│ ├── Addition.java
│ ├── Subtraction.java
│ ├── Multiply.java
│ └── Divide.java
└── Test.java

Addition.java
package Operations;

public class Addition {


public int add(int a, int b) {
return a + b;
}
}
Subtraction.java
package Operations;

public class Subtraction {


public int subtract(int a, int b) {
return a - b;
}
}

Multiply.java
package Operations;

public class Multiply {


public int multiply(int a, int b) {
return a * b;
}
}
Divide.java

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;
}
}

Test.java
import Operations.Addition;
import Operations.Subtraction;
import Operations.Multiply;
import Operations.Divide;
import java.util.Scanner;

public class Test {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// 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


System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();

// Perform operations and display results


System.out.println("Addition: " + addition.add(num1, num2));
System.out.println("Subtraction: " + subtraction.subtract(num1, num2));
System.out.println("Multiplication: " + multiply.multiply(num1, num2));

try {
System.out.println("Division: " + divide.divide(num1, num2));
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}

scanner.close();
}
}
OUTPUT
PS D:\JAVA_PROGRAMMING> javac Test.java
Operations/Addition.java Operations/Subtraction.java Operations/Multiply.java
Operations/Divide.java

PS D:\JAVA_PROGRAMMING> java Test


Enter the first number: 10
Enter the second number: 19
Addition: 29
Subtraction: -9
Multiplication: 190
Division: 0

You might also like