Challenge

Project

import java.util.Scanner;

public class Calculator {
private double memory;

public Calculator() {
memory = 0;
}

public double add(double x, double y) {
return x + y;
}

public double subtract(double x, double y) {
return x - y;
}

public double multiply(double x, double y) {
return x * y;
}

public double divide(double x, double y) {
if (y == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return x / y;
}

public double power(double x, double y) {
return Math.pow(x, y);
}

public double squareRoot(double x) {
if (x < 0) {
throw new IllegalArgumentException("Cannot calculate square root of negative number");
}
return Math.sqrt(x);
}

public double nthRoot(double x, double n) {
if (x < 0 && n % 2 == 0) {
throw new IllegalArgumentException("Cannot calculate even root of negative number");
}
return Math.pow(x, 1.0 / n);
}

public double log(double x) {
if (x <= 0) {
throw new IllegalArgumentException("Cannot calculate logarithm of non-positive number");
}
return Math.log10(x);
}

public double naturalLog(double x) {
if (x <= 0) {
throw new IllegalArgumentException("Cannot calculate natural logarithm of non-positive number");
}
return Math.log(x);
}

public double sin(double x) {
return Math.sin(x);
}

public double inverseSin(double x) {
if (x < -1 || x > 1) {
throw new IllegalArgumentException("Cannot calculate inverse sine of value outside [-1, 1]");
}
return Math.asin(x);
}

public double cos(double x) {
return Math.cos(x);
}

public double inverseCos(double x) {
if (x < -1 || x > 1) {
throw new IllegalArgumentException("Cannot calculate inverse cosine of value outside [-1, 1]");
}
return Math.acos(x);
}

public double tan(double x) {
return Math.tan(x);
}

public double inverseTan(double x) {
return Math.atan(x);
}

public void store(double x) {
memory = x;
}

public double recall() {
return memory;
}

public String decimalToBinary(int x) {
return Integer.toBinaryString(x);
}

public int binaryToDecimal(String x) {
return Integer.parseInt(x, 2);
}

public double sip(double p, double r, int n, int t) {
double rPerPeriod = r / n;
int periods = n * t;
return p * Math.pow(1 + rPerPeriod, periods);
}
}

Main class

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
private static final String HISTORY_FILE = "CalculationsHistory.txt";
private static final String SEPARATOR

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.