# caculator

Page 1 of 1

## 10 Replies - 893 Views - Last Post: 20 April 2012 - 07:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=275615&amp;s=68ce4b91f58667bbd8d4ba6c0d66337d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Milestone45

• New D.I.C Head

Reputation: -1
• 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

Is This A Good Question/Topic? 0

## Replies To: caculator

### #2 BetaWar

• #include "soul.h"

Reputation: 1509
• Posts: 8,268
• 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.

### #3 pbl

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

Reputation: 8378
• Posts: 31,956
• 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

### #4 Milestone45

• New D.I.C Head

Reputation: -1
• 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

### #5 Milestone45

• New D.I.C Head

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

## Re: caculator

Posted 17 April 2012 - 04:00 PM

Can someone help please..

### #6 pbl

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

Reputation: 8378
• Posts: 31,956
• 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"));

```

### #7 Milestone45

• New D.I.C Head

Reputation: -1
• 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

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

### #8 pbl

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

Reputation: 8378
• Posts: 31,956
• 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

### #9 Milestone45

• New D.I.C Head

Reputation: -1
• 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;

Scanner keyboard=new Scanner(System.in);
System.out.println("1st number");
length1=keyboard.nextInt ();
System.out.println("2nd number");
length2=keyboard.nextInt ();
System.out.println("the area is:\n" + answer + "cm^3");]

### #10 Milestone45

• New D.I.C Head

Reputation: -1
• 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;

Scanner keyboard=new Scanner(System.in);
System.out.println("1st number");
length1=keyboard.nextInt ();
System.out.println("2nd number");
length2=keyboard.nextInt ();
System.out.println("the area is:\n" + answer + "cm^3");]

### #11 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: caculator

Posted 20 April 2012 - 07:29 PM

after 18 posts it is about time your learn
And may be would have been to fix your firts switch() stament problem... will simplify a lot your following code