Reputation: 1829
I am a very young and inexperienced Java programmer trying to understand some of the concepts discussed in my high school computer science class.
Currently, I am trying to make a calculator using Java, but have come across an error in my code...I have been trying to figure out the cause of the problem but I am absolutely stuck. I am trying to create a calculator that has users enter an equation on one line, and through parsing, the program should be able to give me a correct answer. Currently I have only programmed the math to calculate equations with two operands (+,-,*,/,^), but I also want my program to calculate equations with single operands (absolute value, sin, cos, tan).
Here is what I have so far:
package calculator;
import java.util.Scanner;
public class Calculator {
public class Maths {
double add(double a, double b) {
double answer = a+b;
return answer;
}
double subtract(double a, double b) {
double answer = a-b;
return answer;
}
double multiply(double a, double b) {
double answer = a*b;
return answer;
}
double divide(double a, double b) {
double answer = a/b;
return answer;
}
double power(double a, double b){
double answer =a;
for (int x=2; x<=b; x++){
answer *= a;
}
return answer;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Maths maths = Maths();
double answer = 0;
double inputA, inputB;
char operator;
boolean done = false;
while (done == false) {
System.out.print("Please enter your equation: ");
inputA = input.nextDouble();
operator = input.next().charAt(0);
inputB = input.nextDouble();
if (operator == '+') {
answer = maths.add(inputA, inputB);
}
if (operator == '-') {
answer = maths.subtract(inputA, inputB);
}
if (operator == '*') {
answer = maths.multiply(inputA, inputB);
}
if (operator == '/') {
answer = maths.divide(inputA, inputB);
}
if (operator == '^') {
answer = maths.power(inputA, inputB);
}
System.out.println(answer);
}
input.close();
}
}
As of right now, I feel as though my code is headed in the right direction: except there is a significant error, the cause of which I am unsure. public class Maths
is not working in my main block of code. When I was creating this, I had the idea that I would use procedural decomposition and make a separate method where the computation would take place, while the actual collection of variables would happen in the main class.
My IDE is giving me an error at Maths maths = Maths();
located in the main class.
I would really appreciate it if I could get some help on figuring out and understanding the root of the problem at hand so I can fix my program and can avoid such mistakes in the future!
Thank's in advance
Upvotes: 0
Views: 328
Reputation: 799
use this code it will work
package calculator;
import java.util.Scanner;
class Maths {
double add(double a, double b) {
double answer = a+b;
return answer;
}
double subtract(double a, double b) {
double answer = a-b;
return answer;
}
double multiply(double a, double b) {
double answer = a*b;
return answer;
}
double divide(double a, double b) {
double answer = a/b;
return answer;
}
double power(double a, double b){
double answer =a;
for (int x=2; x<=b; x++){
answer *= a;
}
return answer;
}
}
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Maths maths = new Maths();
double answer = 0;
double inputA, inputB;
char operator;
boolean done = false;
while (done == false) {
System.out.print("Please enter your equation: ");
inputA = input.nextDouble();
operator = input.next().charAt(0);
inputB = input.nextDouble();
if (operator == '+') {
answer = maths.add(inputA, inputB);
}
if (operator == '-') {
answer = maths.subtract(inputA, inputB);
}
if (operator == '*') {
answer = maths.multiply(inputA, inputB);
}
if (operator == '/') {
answer = maths.divide(inputA, inputB);
}
if (operator == '^') {
answer = maths.power(inputA, inputB);
}
System.out.println(answer);
}
input.close();
}
}
Upvotes: 2
Reputation: 35557
You missed the new
here
Maths maths = Maths();
It should be
Maths maths = new Maths();
This Maths
is an inner class. So you needs to change the instantiation as follows to corrected all errors.
Maths maths =new Calculator().new Maths();
Upvotes: 3