Skip to main content

Write a java program to create a calculator which performs addition, subtraction and multiplication of numbers for different types like integer, float and complex numbers using single function add(),sub() and multi().

 CODE:


class Calculator {

    // Addition for integers

    public int add(int a, int b) {

        return a + b;

    }


    // Addition for floats

    public float add(float a, float b) {

        return a + b;

    }


    // Addition for complex numbers

    public ComplexNumber add(ComplexNumber a, ComplexNumber b) {

        return new ComplexNumber(a.getReal() + b.getReal(), a.getImaginary() + b.getImaginary());

    }


    // Subtraction for integers

    public int sub(int a, int b) {

        return a - b;

    }


    // Subtraction for floats

    public float sub(float a, float b) {

        return a - b;

    }


    // Subtraction for complex numbers

    public ComplexNumber sub(ComplexNumber a, ComplexNumber b) {

        return new ComplexNumber(a.getReal() - b.getReal(), a.getImaginary() - b.getImaginary());

    }


    // Multiplication for integers

    public int multi(int a, int b) {

        return a * b;

    }


    // Multiplication for floats

    public float multi(float a, float b) {

        return a * b;

    }


    // Multiplication for complex numbers

    public ComplexNumber multi(ComplexNumber a, ComplexNumber b) {

        int real = a.getReal() * b.getReal() - a.getImaginary() * b.getImaginary();

        int imaginary = a.getReal() * b.getImaginary() + a.getImaginary() * b.getReal();

        return new ComplexNumber(real, imaginary);

    }

}


class ComplexNumber {

    private int real;

    private int imaginary;


    public ComplexNumber(int real, int imaginary) {

        this.real = real;

        this.imaginary = imaginary;

    }


    public int getReal() {

        return real;

    }


    public int getImaginary() {

        return imaginary;

    }

}


public class CalculatorTest {

    public static void main(String[] args) {

        Calculator calculator = new Calculator();


        // Addition

        System.out.println("Addition:");

        System.out.println("Integer Addition: " + calculator.add(5, 3));

        System.out.println("Float Addition: " + calculator.add(5.5f, 3.3f));

        ComplexNumber complex1 = new ComplexNumber(2, 3);

        ComplexNumber complex2 = new ComplexNumber(4, 5);

        ComplexNumber complexSum = calculator.add(complex1, complex2);

        System.out.println("Complex Addition: " + complexSum.getReal() + " + " + complexSum.getImaginary() + "i");


        // Subtraction

        System.out.println("\nSubtraction:");

        System.out.println("Integer Subtraction: " + calculator.sub(5, 3));

        System.out.println("Float Subtraction: " + calculator.sub(5.5f, 3.3f));

        ComplexNumber complexDiff = calculator.sub(complex1, complex2);

        System.out.println("Complex Subtraction: " + complexDiff.getReal() + " + " + complexDiff.getImaginary() + "i");


        // Multiplication

        System.out.println("\nMultiplication:");

        System.out.println("Integer Multiplication: " + calculator.multi(5, 3));

        System.out.println("Float Multiplication: " + calculator.multi(5.5f, 3.3f));

        ComplexNumber complexProduct = calculator.multi(complex1, complex2);

        System.out.println("Complex Multiplication: " + complexProduct.getReal() + " + " + complexProduct.getImaginary() + "i");

    }

}


Algorithm:

  1. Define a class Calculator with methods to perform addition, subtraction, and multiplication for different types of numbers.
  2. Define overloaded methods for each operation (add, sub, and multi) to handle different types of input parameters (integers, floats, and complex numbers).
  3. For addition: Implement methods to add two integers, two floats, and two complex numbers separately.
  4. For subtraction: Implement methods to subtract two integers, two floats, and two complex numbers separately.
  5. For multiplication: Implement methods to multiply two integers, two floats, and two complex numbers separately.
  6. Define a class ComplexNumber to represent complex numbers with real and imaginary parts.
  7. Inside the CalculatorTest class: Create an instance of the Calculator class. Test each operation (addition, subtraction, and multiplication) for different types of numbers (integer, float, and complex).
  8. Print the results of each operation.
  9. End of the program.














Popular posts from this blog

krushnaaa

  import java . awt .* ; import java . awt . event .* ; public class SimpleAWTExample {     public static void main ( String [] args ) {         // Create a Frame         Frame frame = new Frame ( "Simple AWT Example" );         // Create a Button         Button button = new Button ( "Click Me!" );         // Set layout for the Frame         frame . setLayout ( new FlowLayout ());         // Add the Button to the Frame         frame . add ( button );         // Set size of the Frame         frame . setSize ( 300 , 200 ); // Width: 300 pixels, Height: 200 pixels         // Make the Frame visible         frame . setVisible ( true );         // Add a WindowListener to handle closing event     ...

Write a program to create a class Student2 along with two method getData (), printData () to get the value through argument and display the data in printData. Create the two objects s1, s2 to declare and access the values from class STtest.

 CODE: import java.util.Scanner; class Student2 {     private String name;     private int age;          // Method to set data     public void getData(String name, int age) {         this.name = name;         this.age = age;     }          // Method to print data     public void printData() {         System.out.println("Name: " + name);         System.out.println("Age: " + age);     } } public class STtest {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);                  // Creating objects of Student2 class         Student2 s1 = new Student2();         Student2 s2 = new Student2();              ...

Create an in-order threaded binary search tree and perform the traversals.(Double Thread)

 CODE: #include <iostream> using namespace std; struct Node {     int data;     Node* left;     Node* right;     bool leftThread;     bool rightThread; }; Node* createThreadedBST(int arr[], int n) {     Node* root = nullptr;     for (int i = 0; i < n; ++i) {         Node* newNode = new Node;         newNode->data = arr[i];         newNode->left = newNode->right = nullptr;         newNode->leftThread = newNode->rightThread = true;         if (root == nullptr) {             root = newNode;         } else {             Node* current = root;             Node* parent = nullptr;             while (true) {                ...