Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Simple Java Calculator

This is a simple Java calculator I wrote. It's my first program in Java so let me know how i did. I'm aware I could have used functions, but I haven't learned that in java yet.

Code
import java.util.Scanner;


public class Calculator
{
    public static void main(String args[])
        throws java.io.IOException
        {
            char choice;
            
            // The calculator menu
            System.out.println("\t*Calculator*");
            System.out.println("Menu:");
            System.out.println("\t1. Addition");
            System.out.println("\t2. Subtraction");
            System.out.println("\t3. Multiplication");
            System.out.println("\t4. Division");
            System.out.println("\t5. Raise to Power");
            System.out.println("\t0. Quit");
            
            System.out.print("Choice: "); // prompts for choice
            choice = (char) System.in.read(); // reads in choice
            System.out.println(""); // blank line to seperate menu
            
            Scanner sc;
            double A, B, C;
            
            switch (choice)
            {
                case '1':
                    // Description of addition
                    System.out.println("Adds two numbers:");
                    System.out.println("\tA + B = C");
                    
                    // Inputs A and B
                    System.out.print("Enter A: ");
                    sc = new Scanner(System.in);
                    A = sc.nextDouble();
                    System.out.print("Enter B: ");
                    sc = new Scanner(System.in);
                    B = sc.nextDouble();
                    sc.close();
                    
                    // Adds A and B to find C and shows the answer
                    C = A + B;
                    System.out.println(A + " + " + B + " = " + C);
                    break;
                    
                case '2':
                    // Description of subtraction
                    System.out.println("Subtracts one number from another");
                    System.out.println("\tA - B = C");
                    
                    // Inputs A and B
                    System.out.print("Enter A: ");
                    sc = new Scanner(System.in);
                    A = sc.nextDouble();
                    System.out.print("Enter B: ");
                    sc = new Scanner(System.in);
                    B = sc.nextDouble();
                    sc.close();
                    
                    // Subtracs B from A to find C and shows the answer
                    C = A - B;
                    System.out.println(A + " - " + B + " = " + C);
                    break;
                    
                case '3':
                    // Description of Multiplication
                    System.out.println("Multiplies two numbers");
                    System.out.println("\tA * B = C");
                    
                    // Inputs A and B
                    System.out.print("Enter A: ");
                    sc = new Scanner(System.in);
                    A = sc.nextDouble();
                    System.out.print("Enter B: ");
                    sc = new Scanner(System.in);
                    B = sc.nextDouble();
                    sc.close();
                    
                    // Multiplies A and B to make C and shows answer
                    C = A * B;
                    System.out.println(A + " * " + B + " = " + C);
                    break;
                
                case '4':
                    // Description of Division
                    System.out.println("Divides one number by another");
                    System.out.println("\tA / B = C");
                    
                    // Inputs A and B
                    System.out.print("Enter A: ");
                    sc = new Scanner(System.in);
                    A = sc.nextDouble();
                    System.out.print("Enter B: ");
                    sc = new Scanner(System.in);
                    B = sc.nextDouble();
                    sc.close();
                    
                    // Divides A by B to make C and shows answer
                    C = A / B;
                    System.out.println(A + " / " + B + " = " + C);
                    break;
                    
                case '5':
                    // Description of Raise to a Power
                    System.out.println("Gives A to the B power");
                    System.out.println("\t A^B = C");
                    
                    // Inputs A and B
                    System.out.print("Enter A: ");
                    sc = new Scanner(System.in);
                    A = sc.nextDouble();
                    System.out.print("Enter B: ");
                    sc = new Scanner(System.in);
                    B = sc.nextDouble();
                    sc.close();
                    
                    C = Math.pow(A, B);
                    System.out.println(A + "^" + B + " = " + C);
                    break;
                    
                default:
                    System.out.println("Quitting...");
                    
            
            }
            
        }

}
For a beginner, I will say that I see some good habits and logic, although it's not perfect. A little tired ATM so if no one gets back to you in detail, I will when I wake up.
Don't call System.out.println() ten times in a row if you want to print out options on a console, just format the lines with "\n" in one method call. And as for the rest of it, well it works lol. Next try a calculator where you can enter a single expression like "4+5" or "12/3" and it will automatically detect what operations to do on the numbers.
(Mar 8, 2015 10:59 PM)S. Goodman Wrote:  For a beginner, I will say that I see some good habits and logic, although it's not perfect. A little tired ATM so if no one gets back to you in detail, I will when I wake up.

Thanks, this isn't my first language, I know python fairly well.


(Mar 9, 2015 01:37 PM)Trisomy 21 Wrote:  Don't call System.out.println() ten times in a row if you want to print out options on a console, just format the lines with "\n" in one method call. And as for the rest of it, well it works lol. Next try a calculator where you can enter a single expression like "4+5" or "12/3" and it will automatically detect what operations to do on the numbers.

Would only one call make it faster or save memory? And making a calculator like that would be hard, but I’m sure I will be able to do so eventually.
(Mar 9, 2015 04:19 PM)CyberPunkw Wrote:  Thanks, this isn't my first language, I know python fairly well.



Would only one call make it faster or save memory? And making a calculator like that would be hard, but I’m sure I will be able to do so eventually.

Actually it's not that hard, but for a beginner it's the idead challange! It looks difficult but once you did 'one' of the methods the calculator should have the rest will be easier.
The hardest part would be the parsing, reading a string (your calculation) as numbers and operators.
I really encourage you to create such a calculator, you will learn loads. :)
(Mar 9, 2015 04:19 PM)CyberPunkw Wrote:  Would only one call make it faster or save memory? .

Every time you call a method, it takes time. For something like println it is literally fractions of a millisecond, but there is no reason not to just use one method call.
(Mar 9, 2015 01:37 PM)Trisomy 21 Wrote:  Don't call System.out.println() ten times in a row if you want to print out options on a console, just format the lines with "\n" in one method call. And as for the rest of it, well it works lol. Next try a calculator where you can enter a single expression like "4+5" or "12/3" and it will automatically detect what operations to do on the numbers.

Why not? You're encouraging a beginner to reduce the readability of their program. Code is made for people to read, it's a means of communication with yourself at a later date and with other programmers. What he learns now will become deep-rooted habits.
Thats honestly pretty well done for being a beginner. We're currently working on stuff similar to this in my computer science class.
(Mar 10, 2015 04:40 PM)Entityy Wrote:  Thats honestly pretty well done for being a beginner. We're currently working on stuff similar to this in my computer science class.

Cool. That makes me feel like more than just a highschooler who likes to code.


(Mar 10, 2015 04:40 PM)Entityy Wrote:  Thats honestly pretty well done for being a beginner. We're currently working on stuff similar to this in my computer science class.

So it is a good habit? I'm confused as hell now.
(Mar 10, 2015 04:39 PM)Han. Wrote:  Why not? You're encouraging a beginner to reduce the readability of their program. Code is made for people to read, it's a means of communication with yourself at a later date and with other programmers. What he learns now will become deep-rooted habits.

I explained why above, if you don't understand why calling the same method 10 times at the moment when you have all the information needed to call it once, you should hit the books. And I couldn't agree with you more about what he learns now will become deep-rooted habits, so he should start doing things the right way now.
Why you wrote this "throws java.io.IOException" ?

Btw i'm also going to make this today for the first time without copying from others.
[Image: ebisfS8.png]
(Mar 12, 2015 10:45 PM)Captain Steve Rogers Wrote:  Why you wrote this "throws java.io.IOException" ?

Btw i'm also going to make this today for the first time without copying from others.
At first is was going to use System.in.read(), but I realized that wouldn't work as well as Scanner. I guess i forgot to take it out.
(Mar 13, 2015 03:42 PM)CyberPunkw Wrote:  At first is was going to use System.in.read(), but I realized that wouldn't work as well as Scanner. I guess i forgot to take it out.

Frankly specking no idea what is "System.in.read()".
Why scanner won't work? you have even used it.
Your code looks damn complicated to me.

Here is mine : https://hackforums.net/showthread.php?tid=4730078
[Image: ebisfS8.png]
No need to lengthen the program by taking inputs in every Case.You can take 2 numbers as inputs before entering the switch block.

Code
import java.util.Scanner;

public class Calculator
{
    public static void main(String args[])
      {
      char choice;

      // The calculator menu
      System.out.println("\t*Calculator*");
      System.out.println("Menu:");
      System.out.println("\t1. Addition");
      System.out.println("\t2. Subtraction");
      System.out.println("\t3. Multiplication");
      System.out.println("\t4. Division");
      System.out.println("\t5. Raise to Power");
      System.out.println("\t0. Quit");

      System.out.print("Choice: "); // prompts for choice
      choice = (char) System.in.read(); // reads in choice
      System.out.println(""); // blank line to seperate menu
      if(choice=='0')
                System.exit(1);
      Scanner sc=new Scanner(System.in);
      double A, B, C;
            System.out.println("\n First number:");//input first number
            A=sc.nextDouble();
      System.out.println("\n Second number:");//input second number
            B=sc.nextDouble();
      switch (choice)
      {
      case '1':
      // Description of addition
      System.out.println("Adds two numbers:");
      System.out.println("\tA + B = C");

      // Adds A and B to find C and shows the answer
      C = A + B;
      System.out.println(A + " + " + B + " = " + C);
      break;

      case '2':
      // Description of subtraction
      System.out.println("Subtracts one number from another");
      System.out.println("\tA - B = C");

      // Subtracts B from A to find C and shows the answer
      C = A - B;
      System.out.println(A + " - " + B + " = " + C);
      break;

      case '3':
      // Description of Multiplication
      System.out.println("Multiplies two numbers");
      System.out.println("\tA * B = C");

      // Multiplies A and B to make C and shows answer
      C = A * B;
      System.out.println(A + " * " + B + " = " + C);
      break;

      case '4':
      // Description of Division
      System.out.println("Divides one number by another");
      System.out.println("\tA / B = C");

      // Divides A by B to make C and shows answer
      C = A / B;
      System.out.println(A + " / " + B + " = " + C);
      break;

      case '5':
      // Description of Raise to a Power
      System.out.println("Gives A to the B power");
      System.out.println("\t A^B = C");

      C = Math.pow(A, B);
      System.out.println(A + "^" + B + " = " + C);
      break;
      }

      }

}
(Mar 9, 2015 01:37 PM)Trisomy 21 Wrote:  Don't call System.out.println() ten times in a row if you want to print out options on a console, just format the lines with "\n" in one method call. And as for the rest of it, well it works lol. Next try a calculator where you can enter a single expression like "4+5" or "12/3" and it will automatically detect what operations to do on the numbers.

Hi there,
I would not recommend doing this. If you're code is going to make a million calls to the System.out.println(String) method, then the JIT will come with a solution on it's own. If you really want to do it yourself, use a StringBuilder(docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html) to concat Strings in Java.
Bertha.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by MordiciaNov 22, 2018 05:41 PM 5 274 Nov 22, 2018 05:41 PM
Last Post: Minesweeper
[.NET] by Dir50Jun 5, 2018 08:56 AM 9 316 Jun 5, 2018 08:56 AM
Last Post: 0xDEAD10CC
[Shell] by V for vandettaMay 26, 2017 08:42 PM 2 145 May 26, 2017 08:42 PM
Last Post: V for vandetta