0% found this document useful (0 votes)
6 views6 pages

Code

The document contains a Java program for a calculator that performs various mathematical operations such as sine, cosine, tangent, absolute value, square root, power, maximum, minimum, random number generation, addition, subtraction, multiplication, division, and hyperbolic functions. It includes a main class that prompts the user to select an operation and then calls the corresponding method from the Calculator class. The program handles user input and displays the results of the calculations.

Uploaded by

makan56312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Code

The document contains a Java program for a calculator that performs various mathematical operations such as sine, cosine, tangent, absolute value, square root, power, maximum, minimum, random number generation, addition, subtraction, multiplication, division, and hyperbolic functions. It includes a main class that prompts the user to select an operation and then calls the corresponding method from the Calculator class. The program handles user input and displays the results of the calculations.

Uploaded by

makan56312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name : Muhammad Uzair ali

Id no : 17388

Code:

package uzair;

import java.util.Scanner;

class Calculator {
Scanner input = new Scanner(System.in);
double num1, num2, result;

// Constructor to initialize values


Calculator() {
num1 = 0;
num2 = 0;
result = 0;
}

// Method to calculate sine


double getSin() {
System.out.print("Enter a number for sin: ");
num1 = input.nextDouble();
result = Math.sin(num1);
return result;
}

// Method to calculate cosine


double getCos() {
System.out.print("Enter a number for cos: ");
num1 = input.nextDouble();
result = Math.cos(num1);
return result;
}

// Method to calculate tangent


double getTan() {
System.out.print("Enter a number for tan: ");
num1 = input.nextDouble();
result = Math.tan(num1);
return result;
}

// Method to calculate absolute value


double getAbsolute() {
System.out.print("Enter a number for absolute: ");
num1 = input.nextDouble();
Name : Muhammad Uzair ali
Id no : 17388
result = Math.abs(num1);
return result;
}

// Method to calculate square root


double getSquareRoot() {
System.out.print("Enter a number for square root: ");
num1 = input.nextDouble();
result = Math.sqrt(num1);
return result;
}

// Method to calculate power


double getPower() {
System.out.print("Enter base: ");
num1 = input.nextDouble();
System.out.print("Enter exponent: ");
num2 = input.nextDouble();
result = Math.pow(num1, num2);
return result;
}

// Method to find maximum of two numbers


double getMaximum() {
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter second number: ");
num2 = input.nextDouble();
result = Math.max(num1, num2);
return result;
}

// Method to find minimum of two numbers


double getMinimum() {
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter second number: ");
num2 = input.nextDouble();
result = Math.min(num1, num2);
return result;
}

// Method to generate a random number


double getRandom() {
result = Math.random();
return result;
Name : Muhammad Uzair ali
Id no : 17388
}

// Method to add two numbers


double add() {
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter second number: ");
num2 = input.nextDouble();
result = num1 + num2;
return result;
}

// Method to subtract two numbers


double subtract() {
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter second number: ");
num2 = input.nextDouble();
result = num1 - num2;
return result;
}

// Method to multiply two numbers


double multiply() {
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter second number: ");
num2 = input.nextDouble();
result = num1 * num2;
return result;
}

// Method to divide two numbers


double divide() {
System.out.print("Enter numerator: ");
num1 = input.nextDouble();
System.out.print("Enter denominator: ");
num2 = input.nextDouble();
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division by zero.");
result = Double.NaN; // Not a Number
}
return result;
}
Name : Muhammad Uzair ali
Id no : 17388

// Method to calculate hyperbolic sine


double getSinh() {
System.out.print("Enter a number for sinh: ");
num1 = input.nextDouble();
result = Math.sinh(num1);
return result;
}

// Method to calculate hyperbolic cosine


double getCosh() {
System.out.print("Enter a number for cosh: ");
num1 = input.nextDouble();
result = Math.cosh(num1);
return result;
}

// Method to calculate hyperbolic tangent


double getTanh () {
System.out.print("Enter a number for tanh: ");
num1 = input.nextDouble();
result = Math.tanh(num1);
return result;
}
}

public class Uzair {


public static void main(String[] args) {
Calculator myOperations = new Calculator();
Scanner input = new Scanner(System.in);
System.out.println("Enter your choice: \n1. Sin \n2. Cos \n3. Tan \n4. Absolute \n5. Square
Root"
+ " \n6. Power \n7. Maximum \n8. Minimum \n9. Random \n10. Add \n11.
Subtract \n12. Multiply"
+ " \n13. Divide \n14. Sinh \n15. Cosh \n16. Tanh");
int choice = input.nextInt();

switch (choice) {
case 1:
double sinResult = myOperations.getSin();
System.out.println("The value of sin is: " + sinResult);
break;
case 2:
double cosResult = myOperations.getCos();
System.out.println("The value of cos is: " + cosResult);
break;
Name : Muhammad Uzair ali
Id no : 17388
case 3:
double tanResult = myOperations.getTan();
System.out.println("The value of tan is: " + tanResult);
break;
case 4:
double absoluteResult = myOperations.getAbsolute();
System.out.println("Absolute is: " + absoluteResult);
break;
case 5:
double squareRootResult = myOperations.getSquareRoot();
System.out.println("Square root is: " + squareRootResult);
break;
case 6:
double powerResult = myOperations.getPower();
System.out.println("Power is: " + powerResult);
break;
case 7:
double maximumResult = myOperations.getMaximum();
System.out.println("Maximum Number is: " + maximumResult);
break;
case 8:
double minimumResult = myOperations.getMinimum();
System.out.println("Minimum Number is: " + minimumResult);
break;
case 9:
double randomResult = myOperations.getRandom();
System.out.println("Random number is: " + randomResult);
break;
case 10:
double addResult = myOperations.add();
System.out.println("Addition result is: " + addResult);
break;
case 11:
double subtractResult = myOperations.subtract();
System.out.println("Subtraction result is: " + subtractResult);
break;
case 12:
double multiplyResult = myOperations.multiply();
System.out.println("Multiplication result is: " + multiplyResult);
break;
case 13:
double divideResult = myOperations.divide();
if (!Double.isNaN(divideResult)) {
System.out.println("Division result is: " + divideResult);
}
break;
Name : Muhammad Uzair ali
Id no : 17388
case 14:
double sinhResult = myOperations.getSinh();
System.out.println("The value of sinh is: " + sinhResult);
break;
case 15:
double coshResult = myOperations.getCosh();
System.out.println("The value of cosh is: " + coshResult);
break;
case 16:
double tanhResult = myOperations.getTanh();
System.out.println("The value of tanh is: " + tanhResult);
break;
default:
System.out.println("Invalid choice");
break;
}
input.close();
}
}

You might also like