Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am trying to make a calculator with Java and 3 Classes

I am very new to JAVA Programming and was trying to make a Java Program that consists of 2 classes and an interface. The main class is StartingPoint.java, the other class is Calculate.java and the interface is Equations.java.

So far I have one equation in the Equation.java interface which consists of a simple addition function. I want the program to prompt the user to insert 2 integers and return the added solution. Any help would be greatly appreciated.

This is my main class called StartingPoint.java

import java.util.Scanner;


public class StartingPoint {
public static void main (String Hoda[]){

    System.out.println("Please enter two values");

    Scanner a = new Scanner(System.in);
    Scanner b = new Scanner(System.in);

    Calculate calculator = new Calculate();

    int answer = calculator.add(in.nextInt(a), nextInt(Scanner b));
    System.out.print(answer);
}
}

Here is my second class: Calculate.java

import java.util.Scanner;


public class Calculate implements Equations {



@Override
public int add(Scanner a, Scanner b) {
    // TODO Auto-generated method stub
    return  (a + b);
}

}

And here is my Interface called Equations.java

import java.util.Scanner;



public interface Equations {
int add(Scanner a, Scanner b);
}
like image 851
Mustafa Avatar asked May 29 '13 20:05

Mustafa


People also ask

How do you make a calculator in Java?

Example: Simple Calculator using Java switch Statementresult = number1 * number2; System.out.println(number + " * " + number2 + " = " + result); break; These statements compute the product of two numbers and print the output. Finally, the break statement ends the switch statement.

Can we have multiple packages in Java?

The package statement must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file. However you can define same classes - same name and even same implementation in two packages but package name must be different. Save this answer.


1 Answers

You maybe want to convert the values into integer...something like this:

@Override
public int add(Scanner a, Scanner b) {
    int n1=Integer.parseInt(a.next());
    int n2=Integer.parseInt(b.next());
    return  (n1 + n2);
}

I see you are new to java. Take a look at your function:

public int add(...

java expects this function to return an integer. You are returning a + b, but a and b are instances of the object Scanner, not Integers. So we have to "transform" the input string into and integer object. Java is a very high level language, so almost every object has plenty of methods that will help us to do whatever we want. In this case, the class Integer has a static method called parseInt(String args) that will parse the given string and check if it is numeric. If it is numeric, it will return an integer. If not, it will throw an exception. That's why you should validate the input. If you type a non numeric value it will crash.

like image 54
Sebastian Breit Avatar answered Nov 02 '22 00:11

Sebastian Breit