Results 1 to 9 of 9
  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default calculator with some extra features.

    hello again people. So I was just messing around trying to refine how I write code. Here is a simple calculator with some extra functions. Also if any of my math formulas are wrong please correct me or suggest more I can add. Another also, is it possible to use the switch with case higher then 9? it wouldn't let me use 10,11,12,13,14, or 15. Is that because I have atChar(0) when scanning for choice? Is it possible to change my-scan-for-choice statement so it can accept 1 through 15 while still using switch?

    Java Code:
    package com.blue2525989;
    
    import java.util.*;
    
    public class MathFun {
        
        public static Scanner scan = new Scanner(System.in);
        public static double a,b,a2,b2,res;
        public static int choice;    
        
        public static void prompt(){
            System.out.println("----------------------------------------");
            System.out.println("Welcome to Fun with Math.");
            System.out.println("This program features a collection of");
            System.out.println("different math formulas and features.");
            System.out.println("Please enter a number for your selection.");
            System.out.println("----------------------------------------");
            System.out.println("1. Addition.");
            System.out.println("2. Subtraction.");
            System.out.println("2. Multiply.");
            System.out.println("4. Divide.");
            System.out.println("5. Square Root.");
            System.out.println("6. Square.");
            System.out.println("7. Power of n.");
            System.out.println("8. Find Distance.");
            System.out.println("9. Find Area.");
            System.out.println("10. is Divisible. enter a");
            System.out.println("11. is Single Digit. enter b");
            System.out.println("12. is Triangle. enter c");
            System.out.println("13. Multiply and add. enter d");
            System.out.println("14. Exponent Sum. enter e");
            System.out.println("15. Exit. enter f");
            System.out.println();
        }
        public static void ent1num(){System.out.println("Please enter one number: ");}
        public static void ent2num(){System.out.println("Please enter two numbers: ");}
        public static void ent3num(){System.out.println("Please enter three numbers: ");}
        public static void ent4num(){System.out.println("Please enter four numbers: ");}
        public static void res(){System.out.print("Your result is: ");}
        
        public static double add() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double res = a + b;
            res();
            System.out.println(res);
            return 1;
        }
        
        public static double subtract() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double res = a - b;
            res();
            System.out.println(res);
            return 1;
        }
        
        public static double mult() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double res = a * b;
            res();
            System.out.println(res);
            return 1;
        }
        
        public static double divide() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double res = a / b;
            res();
            System.out.println(res);
            return 1;
        }
        
        public static double sqrt() {
            ent1num();
            a = scan.nextDouble();
            double res = Math.sqrt(a);
            res();
            System.out.println(res);
            return 1;
        }
        
        public static double square(){
            ent1num();
            a = scan.nextDouble();
            double res = Math.pow(a,2);
            res();
            System.out.println(res);
            return 1;
        }
        
        public static double pow(){
            ent2num();
            System.out.println("(raises first number to power of second.)");
            a = scan.nextDouble();
            b = scan.nextInt();
            double res = Math.pow(a, b);
            res();
            System.out.println(res);
            return 1;
        }
        
        public static double dist() {
            ent4num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            a2 = scan.nextDouble();
            b2 = scan.nextDouble();
            double res0 = (a2 * a) + (b2 * b);
            double res = Math.sqrt(res0);
            res();
            System.out.println(res);
            return 1;
        }
        
        public static double area() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double res = a * b;
            res();
            System.out.println(res);
            return 1;
        }
        
        public static double isDivisible() {
        ent2num();
        a = scan.nextDouble();
        b = scan.nextDouble();
        if (a / b == 0) {
            res();
            System.out.println("True");
        }else {
            res();
            System.out.println("False");
        }
        return 1;
        }
        
        public static boolean isSingleDigit() {
            ent1num();
            a = scan.nextDouble();
            if (a > -10 && a < 10) {
                res();
                System.out.println(a + " is single digit.");
                return true;
            } else {
                res();
                System.out.println(a + " is not single digit.");
                return false;
            }
        }
        
        public static boolean isTriangle() {
            ent3num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            a2 = scan.nextDouble();
            if (a > b + a2) {
                res();
                System.out.println(a+" is too large.");
                return false;
            }
            if ( b > a + a2) {
                res();
                System.out.println(b+" is too large.");
                return false;
            }
            if (a2 > a + b) {
                res();
                System.out.println(a2+" is too large.");
                return false;
            }
            res();
            System.out.println(a+"+"+b+"+"+a2+" equals a triangle.");
            return true;
        }
        
        public static double multAdd() {
            ent3num();
            System.out.println("(a * b + c)");
            a = scan.nextDouble();
            b = scan.nextDouble();
            a2 = scan.nextDouble();
            double res = (a * b + a2);
            res();
            System.out.println(res);
            return 1;
        }
        
        public static double multAdd(double a, double b, double a2) {
            double res = (a * b + a2);
            return res;
        }
        
        public static double expSum() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double e = a;
            double c = b + Math.exp(-e);
            double d = Math.sqrt(1 - b + Math.exp(-e));
            double res = multAdd(e,c,d);
            res();
            System.out.println(res);
            return 1;
        }
        
        public MathFun() {        
            do {
                prompt();
                choice = scan.next().charAt(0);
                switch (choice) {
                    case '1' :
                        add();
                        break;
                    case '2' :
                        subtract();
                        break;
                    case '3' :
                        mult();
                        break;
                    case '4' :
                        divide();
                        break;
                    case '5' :
                        sqrt();
                        break;
                    case '6' :
                        square();
                        break;
                    case '7' :
                        pow();
                        break;
                    case '8' :
                        dist();
                        break;
                    case '9' :
                        area();
                        break;
                    case 'a' :
                        isDivisible();
                        break;
                    case 'b' :
                        isSingleDigit();
                        break;
                    case 'c' :
                        isTriangle();
                        break;
                    case 'd' :
                        multAdd();
                        break;
                    case 'e' :
                        expSum();
                        break;
                    case 'f' :
                        System.exit(0);
                }
                
            } while (choice != 9);    
    }
        public static void main(String[] args) {
            new MathFun();
        }
    }

  2. #2
    camel-man is offline Member
    Join Date
    Jan 2016
    Posts
    19
    Rep Power
    0

    Default Re: calculator with some extra features.

    you can make choice an integer and replace that in your case statement. The reason you can't use '12' and other two digit number is because choice is a single character and '11' for example is 2 characters, making it a string. Which would be incompatible with a switch statement.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: calculator with some extra features.

    a string. Which would be incompatible with a switch statement.
    Java now supports Strings in switch statements.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    camel-man is offline Member
    Join Date
    Jan 2016
    Posts
    19
    Rep Power
    0

    Default Re: calculator with some extra features.

    Oh wow haha I didn't even realize that. I'm still using java 6. I guess it's time to update.

  5. #5
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: calculator with some extra features.

    thanks I added String choice2; and used that in switch. Changed scan.next().charAt(0) to scan.next(). Worked good except when using the while (choice2 != "exit") would let me start off normal but after using a function it would print the prompt() twice. so I used while (choice != 1) which is an int and it works properly now. thank you both for your help.
    Last edited by Blue2525989; 11-03-2016 at 09:42 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: calculator with some extra features.

    Java Code:
    while (choice2 != "exit")
    Use the equals() method to compare Strings, not the == or != operators.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: calculator with some extra features.

    nice that works well, thanks norm.
    Java Code:
    while (!choice2.equals("exit"));

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: calculator with some extra features.

    Why is everything static?

    You create an instance of MathFun, so I'm not sure why you don't just strip out all the static keywords.

    Also, I wouldn't use the constructor for launching the app.
    It's job is to just set up the object into a valid state, not to really run anything.
    I would have something like a launch() method instead.
    All that involves is changing the constructor to the launch method...since the constructor doesn't actually initialise anything you can safely get rid of it.
    Your line in main would become:
    Java Code:
    new MathFun().launch();
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: calculator with some extra features.

    Ok I removed all the static keywords and it still works. Also made public void launch(). and changed my main line. thanks for help.

    Java Code:
    package com.blue2525989;
    
    import java.util.*;
    
    public class MathFun {
        
        public  Scanner scan = new Scanner(System.in);
        public  double a,b,a2,b2,res;   
        public  String choice;
        
        public void prompt(){
            System.out.println("----------------------------------------");
            System.out.println("Welcome to Fun with Math.");
            System.out.println("This program features a collection of");
            System.out.println("different math formulas and features.");
            System.out.println("Please enter a number for your selection.");
            System.out.println("----------------------------------------");
            System.out.println("1. Addition.");
            System.out.println("2. Subtraction.");
            System.out.println("3. Multiply.");
            System.out.println("4. Divide.");
            System.out.println("5. Square Root.");
            System.out.println("6. Square.");
            System.out.println("7. Power of n.");
            System.out.println("8. Find Distance.");
            System.out.println("9. Find Area.");
            System.out.println("10. is Divisible.");
            System.out.println("11. is Single Digit.");
            System.out.println("12. is Triangle.");
            System.out.println("13. Multiply and add.");
            System.out.println("14. Exponent Sum.");
            System.out.println("15. Exit.");
            System.out.println();
        }
        public static void ent1num(){System.out.println("Please enter one number: ");}
        public static void ent2num(){System.out.println("Please enter two numbers: ");}
        public static void ent3num(){System.out.println("Please enter three numbers: ");}
        public static void ent4num(){System.out.println("Please enter four numbers: ");}
        public static void res(){System.out.print("Your result is: ");}
        
        public double add() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double res = a + b;
            res();
            System.out.println(res);
            return 1;
        }
        
        public double subtract() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double res = a - b;
            res();
            System.out.println(res);
            return 1;
        }
        
        public double mult() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double res = a * b;
            res();
            System.out.println(res);
            return 1;
        }
        
        public double divide() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double res = a / b;
            res();
            System.out.println(res);
            return 1;
        }
        
        public double sqrt() {
            ent1num();
            a = scan.nextDouble();
            double res = Math.sqrt(a);
            res();
            System.out.println(res);
            return 1;
        }
        
        public double square(){
            ent1num();
            a = scan.nextDouble();
            double res = Math.pow(a,2);
            res();
            System.out.println(res);
            return 1;
        }
        
        public double pow(){
            ent2num();
            System.out.println("(raises first number to power of second.)");
            a = scan.nextDouble();
            b = scan.nextInt();
            double res = Math.pow(a, b);
            res();
            System.out.println(res);
            return 1;
        }
        
        public double dist() {
            ent4num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            a2 = scan.nextDouble();
            b2 = scan.nextDouble();
            double res0 = (a2 * a) + (b2 * b);
            double res = Math.sqrt(res0);
            res();
            System.out.println(res);
            return 1;
        }
        
        public double area() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double res = a * b;
            res();
            System.out.println(res);
            return 1;
        }
        
        public double isDivisible() {
        ent2num();
        a = scan.nextDouble();
        b = scan.nextDouble();
        if (a / b == 0) {
            res();
            System.out.println("True");
        }else {
            res();
            System.out.println("False");
        }
        return 1;
        }
        
        public boolean isSingleDigit() {
            ent1num();
            a = scan.nextDouble();
            if (a > -10 && a < 10) {
                res();
                System.out.println(a + " is single digit.");
                return true;
            } else {
                res();
                System.out.println(a + " is not single digit.");
                return false;
            }
        }
        
        public boolean isTriangle() {
            ent3num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            a2 = scan.nextDouble();
            if (a > b + a2) {
                res();
                System.out.println(a+" is too large.");
                return false;
            }
            if ( b > a + a2) {
                res();
                System.out.println(b+" is too large.");
                return false;
            }
            if (a2 > a + b) {
                res();
                System.out.println(a2+" is too large.");
                return false;
            }
            res();
            System.out.println(a+"+"+b+"+"+a2+" equals a triangle.");
            return true;
        }
        
        public double multAdd() {
            ent3num();
            System.out.println("(a * b + c)");
            a = scan.nextDouble();
            b = scan.nextDouble();
            a2 = scan.nextDouble();
            double res = (a * b + a2);
            res();
            System.out.println(res);
            return 1;
        }
        
        public double multAdd(double a, double b, double a2) {
            double res = (a * b + a2);
            return res;
        }
        
        public double expSum() {
            ent2num();
            a = scan.nextDouble();
            b = scan.nextDouble();
            double e = a;
            double c = b + Math.exp(-e);
            double d = Math.sqrt(1 - b + Math.exp(-e));
            double res = multAdd(e,c,d);
            res();
            System.out.println(res);
            return 1;
        }
        
        public void launch() {
            do {
                prompt();
               choice = scan.next();
                switch (choice) {
                    case "1" :
                        add();
                        break; 
                    case "2" :
                        subtract();
                        break;
                    case "3" :
                        mult();
                        break;
                    case "4" :
                        divide();
                        break;
                    case "5" :
                        sqrt();
                        break;
                    case "6" :
                        square();
                        break;
                    case "7" :
                        pow();
                        break;
                    case "8" :
                        dist();
                        break;
                    case "9" :
                        area();
                        break;
                    case "10" :
                        isDivisible();
                        break;
                    case "11" :
                        isSingleDigit();
                        break;
                    case "12" :
                        isTriangle();
                        break;
                    case "13" :
                        multAdd();
                        break;
                    case "14" :
                        expSum();
                        break;
                    case "15" :
                        System.exit(0);
                    case "exit" :
                        System.exit(0);
                }
            } while  (!choice.equals("exit"));
            
        }
        
        public MathFun() {        
                
    }
        public static void main(String[] args) {
            new MathFun().launch();
        }
    }

Similar Threads

  1. Need help with some ScrollPane() features
    By paulhr in forum JavaFX
    Replies: 0
    Last Post: 08-20-2015, 07:39 PM
  2. Java features
    By jackson123 in forum New To Java
    Replies: 1
    Last Post: 10-15-2013, 09:11 AM
  3. Java 1.7 features
    By Reddi.Java in forum Advanced Java
    Replies: 1
    Last Post: 02-08-2013, 10:01 AM
  4. Java 1.5 Features
    By peiceonly in forum Advanced Java
    Replies: 2
    Last Post: 05-06-2007, 07:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •