Program 1: A Simple Calculator


  • Assigned On: 09/21/2005
  • Due On: 09/30/2005
  • Description:

    Write a java program that:

    • Prompts the user to enter a number,
    • Prompts the user to enter a symbol: '+', '-', '*', '/', '\', or '%',
    • Propts the user to enter a second number,
    • Outputs the appropriate mathematical result.

    The plus symbol represents the command "perform addition." The minus symbol represents command "perform subtraction." The asterix represents "perform multiplication." The slash represents "perform floating point division." The backslash represents "perform integer division." The percent symbol represents "perform modulo."

    Your code should not crash for any reason reguardless of what the user inputs. If the user inputs inappropriate data, you must tell them what was wrong with their input before telling the program to exit.

    • Hint 1: Division by 0 will crash your program.
    • Hint 2: If the user asks you to perform integer division or modulo operations, but gives you floating point values, your program should tell the user that their input won't work for those operations.
    • Hint 3: System.exit(1); will cause your program to exit (with status "1").


    Here are some examples of "good" inputs your program should be able to handle:
    • 3 + 4
    • 12 - 15
    • 3.2 * 5.2
    • 4 % 2
    • 5 / 3
    • 5 \ 3


    Here are some examples of "bad" inputs your program should be able to handle:
    • 12.3 / 0
    • 12.3 \ 0
    • 12.3 \ 1
    • 12.3 \ 1.2
    • 12 \ 1.2
    • 5 \ 0
    • five + four
    • 5 4 +
    • 4 ? 5
    • 4 plus 5


    Type the honor pledge at the top of your .java file in a comment block.

    Zip 1) your .java file and 2) your .class file into a single file. Name that file <your onion>_pr01.zip. That's the name it should have on your computer. Use blackboard to SEND your .zip file.