Answer to Question #235757 in Java | JSP | JSF for kavin

Question #235757

Create two interfaces such as MathsOperable and TrigonometricOperable which

contains the functions to perform the basic arithmetic operations (add, sub, mul, div,

mod) and trigonometric operations (sine, cosine, tan) respectively.Create an abstract class called “Calculator” with details such as no1, no2 and result. Add

necessary constructors.

Implement these interfaces and inherit the class in “Operation” class to perform the

specific operations.

Demonstrate the operations in a menu driven fashion from a Main class. Write logics in

the corresponding methods.


1
Expert's answer
2021-09-10T18:51:24-0400
public interface MathsOperable {
    void add();
    void sub();
    void mul();
    void div();
}


public interface TrigonometricOperable {
    void sine();
    void cosine();
    void tan();
}


abstract public class Calculator {
    private double no1;
    private double no2;
    private double result;

    public Calculator(double no1, double no2) {
        this.no1 = no1;
        this.no2 = no2;
    }

    public double getNo1() {
        return no1;
    }

    public double getNo2() {
        return no2;
    }

    public double getResult() {
        return result;
    }

    public void setResult(double result) {
        this.result = result;
    }
}


public class Operation extends Calculator implements MathsOperable, TrigonometricOperable {

    public Operation(double no1, double no2) {
        super(no1, no2);
    }

    @Override
    public void add() {
        setResult(getNo1() + getNo2());
    }

    @Override
    public void sub() {
        setResult(getNo1() - getNo2());
    }

    @Override
    public void mul() {
        setResult(getNo1() * getNo2());
    }

    @Override
    public void div() {
        setResult(getNo1() / getNo2());
    }

    @Override
    public void sine() {
        setResult(getNo2() / Math.sqrt(Math.pow(getNo1(), 2) + Math.pow(getNo2(), 2)));
    }

    @Override
    public void cosine() {
        setResult(getNo1() / Math.sqrt(Math.pow(getNo1(), 2) + Math.pow(getNo2(), 2)));
    }

    @Override
    public void tan() {
        setResult(getNo2() / getNo1());
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("no1(adjacent): ");
        double no1 = Double.parseDouble(in.nextLine());
        System.out.print("no2(opposite): ");
        double no2 = Double.parseDouble(in.nextLine());
        Operation operation = new Operation(no1, no2);
        String choice;
        while (true) {
            System.out.println("\n1. Add\n2. Sub\n3. Mul\n4. Div\n5. Sine\n6. Cosine\n7. Tan\n0. Exit\n");
            choice = in.nextLine();
            switch (choice) {
                case "1":
                    operation.add();
                    break;
                case "2":
                    operation.sub();
                    break;
                case "3":
                    operation.mul();
                    break;
                case "4":
                    operation.div();
                    break;
                case "5":
                    operation.sine();
                    break;
                case "6":
                    operation.cosine();
                    break;
                case "7":
                    operation.tan();
                    break;
                case "0":
                    System.exit(0);
                    break;
                default:
                    continue;
            }
            System.out.println("Result: " + operation.getResult());
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS