Show DevBest Simple Calculator

ArchoCrime

Member
Aug 13, 2014
98
1
Hello,

I'm new to Java and I just wanted to release this useless script I've had in my files for a bit now.
If you know how to work with java, then make it openable, I don't feel like making a tutorial on it.

Code:
import java.util.Scanner;

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 sum: ");

        inputA = input.nextDouble();
        operator = input.next().charAt(0);
        inputB = input.nextDouble();       

        switch (operator) {
            case '+': answer = Maths.add(inputA, inputB);
                      break;
            case '-': answer = Maths.subtract(inputA, inputB);
                      break;
            case '*': answer = Maths.multiply(inputA, inputB);
                      break;
            case '/': answer = Maths.divide(inputA, inputB);
                      break;
            case '^': answer = Maths.power(inputA, inputB);
                      break;
        }

            System.out.println(answer);            
    }      

    input.close();

  }

}

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;
    }

}

I know, I'm a noob. Don't go to be hating, I'm new to Java :)
 

Hindi

System.out.println(" ");
Dec 30, 2012
989
193
You can do the same thing with less lines of code :p
But java is really cool :D

Code:
package Sandbox;

import java.util.Scanner;

public class Sandbox{
    public static void main(String args[])
    {
        lol();
    }
    public static void lol()
    {
        double ans;
        Scanner scans = new Scanner(System.in);
        System.out.println("Enter your operator");
        char op = scans.next().charAt(0);
        System.out.println("Enter number 1 and 2");
        double a = scans.nextDouble();
       
        double b = scans.nextDouble();
       
        if(op == '+'){
            ans = a + b;
        System.out.println(ans);
        }else if(op == '-'){
            ans = a-b;
            System.out.println(ans);
        }else if(op == '*'){
                ans = a*b;
                System.out.println(ans);
        }else if(op == '/'){
            ans = a/b;
            System.out.println(ans);
        }else {
            System.out.println("Enter proper char");
        }
       
       
       
    }
}
 
Aug 8, 2014
43
3
Nicely done (considering that you're a beginner) once u progress I recommend you take this project and make it better with the skills you'll learn.
i.e. Learn arrays and incorporate that so on one line the user can type "1+5" and bam
 

ArchoCrime

Member
Aug 13, 2014
98
1
Nicely done (considering that you're a beginner) once u progress I recommend you take this project and make it better with the skills you'll learn.
i.e. Learn arrays and incorporate that so on one line the user can type "1+5" and bam
Thanks, I'm going to make a browser then go on after with more advanced projects.
 
Aug 8, 2014
43
3
Code:
    public static void run(){
    System.out.println("Input the value which you would like to calculate, include an addition sign with no spaces");
    Scanner user = new Scanner(System.in);
    String userQuestion = user.nextLine();
    int lengthofMaths = userQuestion.length(); //this is the length of what the user put in
    String splitter[] = new String[3]; //assigning array

   
    for (int i = 0; i <=lengthofMaths; i++){ //this loops through the question that they had put in
        int location1 = 0; //setting location at index 0
        location1 = userQuestion.indexOf("+"); //what im doing here is making it loop to find the addition sign
        splitter[1] = userQuestion.substring(0, location1); //once its found the addition it assigns splitter[1] to equal everything before it
        splitter[2] = userQuestion.substring(location1 + 1, lengthofMaths); //and here is everything after it
       
    }
   
    int y = Integer.parseInt(splitter[1]); //i didn't think ahead so i needed to parse my strings into integers so i could display the final answer
    int x = Integer.parseInt(splitter[2]); //same thing for the second value
   
    int finalCalculation = x + y; //adding them up
    System.out.println("The answer is... " + finalCalculation); //display
     
   
}
I talked the talk, but can I walk the walk?

This is a simple calculator only for addition showing how I used arrays. If its inefficent someone do please tell me, but this is also here for others to learn and understand
 

Hindi

System.out.println(" ");
Dec 30, 2012
989
193
Code:
    public static void run(){
    System.out.println("Input the value which you would like to calculate, include an addition sign with no spaces");
    Scanner user = new Scanner(System.in);
    String userQuestion = user.nextLine();
    int lengthofMaths = userQuestion.length(); //this is the length of what the user put in
    String splitter[] = new String[3]; //assigning array

  
    for (int i = 0; i <=lengthofMaths; i++){ //this loops through the question that they had put in
        int location1 = 0; //setting location at index 0
        location1 = userQuestion.indexOf("+"); //what im doing here is making it loop to find the addition sign
        splitter[1] = userQuestion.substring(0, location1); //once its found the addition it assigns splitter[1] to equal everything before it
        splitter[2] = userQuestion.substring(location1 + 1, lengthofMaths); //and here is everything after it
      
    }
  
    int y = Integer.parseInt(splitter[1]); //i didn't think ahead so i needed to parse my strings into integers so i could display the final answer
    int x = Integer.parseInt(splitter[2]); //same thing for the second value
  
    int finalCalculation = x + y; //adding them up
    System.out.println("The answer is... " + finalCalculation); //display
    
  
}
I talked the talk, but can I walk the walk?

This is a simple calculator only for addition showing how I used arrays. If its inefficent someone do please tell me, but this is also here for others to learn and understand
Why the need of string anyway? You could simply take the integer values, No need of finding + sign as well, You can do it easily without that.
 
Aug 8, 2014
43
3
Why the need of string anyway? You could simply take the integer values, No need of finding + sign as well, You can do it easily without that.
I was making this to show how arrays can be implemented in the calculator so I the user can write 5+5 in the console instead of...
5
5
separated in two lines

The plus sign is needed to order to location where the two numbers are.
Everything before the plus sign is assigned to the first array as the first part of the question
And everything after the plus sign is the second part of the question.
 

Users who are viewing this thread

Top