10 Replies - 510 Views - Last Post: 20 April 2012 - 07:29 PM Rate Topic: -----

#1 Milestone45  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 04-April 12

caculator

Posted 17 April 2012 - 11:22 AM

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


Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: caculator

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1105
  • View blog
  • Posts: 6,918
  • Joined: 07-September 06

Re: caculator

Posted 17 April 2012 - 11:57 AM

I am going to go ahead and move this out of the tutorials section for you, and into the programming questions forum.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: caculator

Posted 17 April 2012 - 12:37 PM

Why making a switch() on userChoice
print something
and then start a serie of if/else of userChoice

Simply put the code in your if/else inside the switch() statement will be much clearer
Was This Post Helpful? 0
  • +
  • -

#4 Milestone45  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 04-April 12

Re: caculator

Posted 17 April 2012 - 01:13 PM

I tried that it is just making more difficult for me..thank you
Was This Post Helpful? 0
  • +
  • -

#5 Milestone45  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 04-April 12

Re: caculator

Posted 17 April 2012 - 04:00 PM

Can someone help please..
Was This Post Helpful? -1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: caculator

Posted 17 April 2012 - 04:14 PM

Two areas ? You prompt for square length and with you do not prompt for a diamater

To continue

  do {

..... you actual code


    System.out.print("Do you want to continue ?");
    String ans = keyboard.next();
    } while(ans.equalsIgnoreCase("Y"));


Was This Post Helpful? 0
  • +
  • -

#7 Milestone45  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 04-April 12

Re: caculator

Posted 19 April 2012 - 04:12 PM

Hi sorry for the. Late reply had ran out of data bundles.

Can you please more further.. Try to make it easy this time.
The code I have above only calculates for the length of the Square, I need you to help me make it calculate for a circle and a Triangle. therefore when I open the Calculator program and choose the Area calculator it shud give me options to calculate either the Square, rectangle, Triangle or Circle.
E.g. Area Calculator
1 [S] Square
2 [R] Rectangle
3 [T] Triangle
4 [C] circle

Please help.
This is the code I am using

*Edited: uncorrectly posted/unreadable code removed

This post has been edited by pbl: 20 April 2012 - 07:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: caculator

Posted 19 April 2012 - 08:40 PM

just make another print and scanner.nextInt() in the if part that processes the Area calculation
Was This Post Helpful? 0
  • +
  • -

#9 Milestone45  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 04-April 12

Re: caculator

Posted 19 April 2012 - 10:44 PM

You mean like this?

[ else if(userChoice == 2){
class AreaCalculator
{
Public void Square()
{
While (true){
int length1;
int length2;
int answer

Scanner keyboard=new Scanner(System.in);
System.out.println("1st number");
length1=keyboard.nextInt ();
System.out.println("2nd number");
length2=keyboard.nextInt ();
answer=length1 * length2;
System.out.println("the area is:\n" + answer + "cm^3");]
Was This Post Helpful? 0
  • +
  • -

#10 Milestone45  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 04-April 12

Re: caculator

Posted 20 April 2012 - 02:29 AM

You mean like this?

[ else if(userChoice == 2){
class AreaCalculator
{
Public void Square()
{
While (true){
int length1;
int length2;
int answer

Scanner keyboard=new Scanner(System.in);
System.out.println("1st number");
length1=keyboard.nextInt ();
System.out.println("2nd number");
length2=keyboard.nextInt ();
answer=length1 * length2;
System.out.println("the area is:\n" + answer + "cm^3");]
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: caculator

Posted 20 April 2012 - 07:29 PM

after 18 posts it is about time your learn :code:
And may be would have been to fix your firts switch() stament problem... will simplify a lot your following code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1