calculator program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: calculator program

  1. #1
    Join Date
    Apr 2012
    Posts
    2

    calculator program

    Hi, I am required to create a calculator program that will consist of a Arithmetic calculator, Area calculator, and volume calculator.

    My problem area is the Area Calculator , I made it to only calculate fa square. But now I want to add in a Circle(radius * radius * pi) and Triangle(base * height/2)

    And my second problem is that, every calculator (Area calculator) has to have the following features:
    When a user presses "Y" Area calculator menu must be redrawn
    When a user presses "N" Main menu of the calculator program must be redrawn.


    Your help will be appreciated, please when replying try not to be too complex because I am a beginner and still trying to adjust to java. Thank you



    import java.util.Scanner;

    public class Calculator
    {
    * *public static void main(String[] args)
    * *{

    * ** *Scanner keyboard= new Scanner(System.in);
    * ** *System.out.print("Enter calculator choice: ");

    * ** *int userChoice = keyboard.nextInt();
    * ** *
    * * * * * * * *
    * ** *int num1 = 0;
    * ** *int num2 = 0;
    * ** *String arithmeticOperation = "";
    * ** *
    * * * * * * * * //initialise variables for choices 2 and 3
    * ** *int length = 0;
    * ** *int width = 0;
    * ** *int height = 0;
    * ** *
    * ** *switch(userChoice)
    * ** *{
    * ** *case 1:
    * ** ** *System.out.println("Arithmetic Calculator");
    * ** ** *break;
    * ** *case 2:
    * ** ** *System.out.println("Area Calculator");
    * ** ** *break;
    * ** *case 3:
    * ** ** *System.out.println("Volume Calculator");
    * ** ** *break;
    * ** *}
    * ** *
    * ** *if(userChoice == 1)
    * ** *{
    * ** ** *System.out.print("Enter first number: ");
    * ** ** *num1 = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter second number: ");
    * ** ** *num2 = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Arithmetic Operation: ");
    * ** ** *arithmeticOperation = keyboard.next();
    * ** ** *
    * ** ** *System.out.println("Answer = " + arithmeticCalculator(num1, num2, arithmeticOperation));
    * ** *}
    * ** *else if(userChoice == 2)
    * ** *{
    * ** ** *System.out.print("Enter Length of the square: ");
    * ** ** *length = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Width of the square: ");
    * ** ** *width = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.println("Area = " + areaCalculator(length, width) + " Units^2");
    * ** *}
    * ** *else if(userChoice == 3)
    * ** *{
    * ** ** *System.out.print("Enter Length of the cube: ");
    * ** ** *length = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Width of the cube: ");
    * ** ** *width = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Height of the cube: ");
    * ** ** *height = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.println("Volume = " + volumeCalculator(length, width, height) + " Units^3");
    * ** *}
    * ** *else
    * ** *{
    * ** ** *System.out.println("Error, choose a number between 1 and 3 inclusive.");
    * ** *}
    * *}
    * *
    * *
    * *public static int arithmeticCalculator(int number1, int number2, String arithmeticOperator)
    * *{
    * ** * if(arithmeticOperator.equals("+"))
    * ** * {
    * ** * * * return number1 + number2;
    * ** * }
    * ** * else if(arithmeticOperator.equals("-"))
    * ** * {
    * ** * * * return number1 - number2;
    * ** * }
    * ** * else
    * ** * {
    * ** * * * return 0;
    * ** * }
    * *}
    * *
    * * * *
    * *public static int areaCalculator(int length, int width)
    * *{
    * ** *return length * width;
    * *}
    * *
    * * *
    * *public static int volumeCalculator(int length, int width, int height)
    * *{
    * ** *return length * width * height;
    * *}
    }

  2. #2
    Join Date
    Sep 2011
    Posts
    197

    Re: calculator program

    Without looking at your current code, I understand you need to test for the press of 'N'. I would use keyevent listner to run a continous loop to check for the pressing of N. This is done by using a new thread implementing the runnable class and overriding the run method. Also, you'll want to add a 1 millisecond sleep just so your program doesn't bottleneck all your resources at your loop's speed. Or, you could just add an if statement before every other one. Meaning E.G

    Code:
    if(UserInput.equals("N")) {
            // deal with this however you need to (in your case just send to the top of the code for a 
    // repaint()
    if(addition) {
    you could add that in each time.

  3. #3
    Join Date
    Apr 2012
    Posts
    2

    Re: calculator program

    Thank you. If I may ask I hav the following error it says "Creat a method arithmeticCalculator(int,int,java.lang.String) in calcultor" I n this of my code.
    System.out.println("answer='arithmeticCalculator (num1, num2, arithmeticOperation));

    Please help..

  4. #4
    Join Date
    Sep 2011
    Posts
    197

    Re: calculator program

    Code:
    Thank you. If I may ask I hav the following error it says "Creat a method arithmeticCalculator(int,int,java.lang.String) in calcultor" I n this of my code.
    System.out.println("answer='arithmeticCalculator (num1, num2, arithmeticOperation));
    
    Please help..
    I am not sure what you mean. I can only guess from your code you posted, and what I can make out from here that your method arithmeticCalculator(int, int, String) isn't in your calculator class? Re-post your question, and your code in [/code] [/code] (put your code in between those tags, and remove the '/' slash from the first code tag). Then I'll be able to further help you.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: calculator program

    When posting code please do as kolt007 has said and use code tags and please also remove all those * chars from the front of each line.

    When posting error messages please post the whole error message and stack trace if any by cut and paste and not by retyping it.

    My problem area is the Area Calculator , I made it to only calculate fa square. But now I want to add in a Circle(radius * radius * pi) and Triangle(base * height/2)
    You should really have a separate class for each shape that all either extend a common class or implement a common interface. The root class/interface should define methods for getArea and getVolume.

    And my second problem is that, every calculator (Area calculator) has to have the following features:
    When a user presses "Y" Area calculator menu must be redrawn
    When a user presses "N" Main menu of the calculator program must be redrawn.
    Do you mean whenever they press one of the keys or do you mean if they enter Y or N in response to one or more of your questions? The first option is rather complex for a beginner.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center