Page 1 of 1

### Java's Calculator

Posted: November 4th, 2011, 8:13 pm
Hey guys. I started working on a calculator about an hour ago. It's not that great at the moment, but it's something. Especially for an hour's worth of coding from a C++ noob. If anybody wants to test and give me some feedback/bugs, I'll be happy to link the DL later. It currently has only four properly working functions.

-Subtraction
-Multiplication
-Division

I plan on adding for stuff like square rooting and multiplying using powers later on. This is a very small, useless project, I know. I'm just doing this so that I can get familiar with C++ in order to make CPPESB and modifications to the PAS engine. Thanks for your time.

-Java

### Re: Java's Calculator

Posted: January 3rd, 2012, 3:57 am
I'm learning Java bymyself and here I am trying to make a calculator but for some reason I'm getting weird values can someone revise me.
By the way I'm using my C++ knowledge as reference I don't know if it helps with java since some things are different.

Code:
import java.util.Scanner;
public class apples {
public static void main (String args[]){

Scanner input = new Scanner (System.in);
int select = 0;
double num1=0, num2=0 , result=0;
num1 = input.nextDouble();
num2 = input.nextDouble();
result = input.nextDouble();
System.out.print("Num1 :"); System.out.print(num1); System.out.print(" Num2: "); System.out.println(num2);

System.out.print("1. Sum, 2. Subtract, 3. Multiply , 4. Divide :");
select = input.nextInt();

System.out.print(select);

if ( select ==1)
{ result = num1 + num2;
System.out.println(result);}

else if ( select ==2)
{ result = num1 - num2;
System.out.println(result); }

else if (select == 3)
{result = num1 * num2;
System.out.println(result);}

else if (select == 4)
{ result = num1 / num2;
System.out.println(result);}

else {System.out.println("Invalid");}

### Re: Java's Calculator

Posted: January 3rd, 2012, 12:32 pm
Instead of using the if else if else statement so many times, you should try using the switch statement.

Code: Select all

`switch ( select ) {case 1: result = num1 + num2;System.out.println(result);break;case 2: result = num1 - num2;System.out.println(result);break;case 3: result = num1 * num2;System.out.println(result);break;case 4: result = num1 / num2;System.out.println(result);break;default: System.out.println("Invalid");break;}`

That should fix the problem. If anything else pops up, just let me know and I'll do my best to help.

-Java

### Re: Java's Calculator

Posted: April 5th, 2012, 8:32 pm
Langdon99 wrote:I'm learning Java bymyself and here I am trying to make a calculator but for some reason I'm getting weird values can someone revise me.
By the way I'm using my C++ knowledge as reference I don't know if it helps with java since some things are different.

Code:
import java.util.Scanner;
public class apples {
public static void main (String args[]){

Scanner input = new Scanner (System.in);
int select = 0;
double num1=0, num2=0 , result=0;
num1 = input.nextDouble();
num2 = input.nextDouble();
result = input.nextDouble();
System.out.print("Num1 :"); System.out.print(num1); System.out.print(" Num2: "); System.out.println(num2);

System.out.print("1. Sum, 2. Subtract, 3. Multiply , 4. Divide :");
select = input.nextInt();

System.out.print(select);

if ( select ==1)
{ result = num1 + num2;
System.out.println(result);}

else if ( select ==2)
{ result = num1 - num2;
System.out.println(result); }

else if (select == 3)
{result = num1 * num2;
System.out.println(result);}

else if (select == 4)
{ result = num1 / num2;
System.out.println(result);}

else {System.out.println("Invalid");}

I also just realized that you asked the user for three values, so the third number they entered would be the result. What you need to do to fix this is simply declare the variable and not assign it a value. The finished code would be like this.

Code: Select all

`import java.util.Scanner;public class apples {   public static void main (String[] args){            Scanner input = new Scanner (System.in);      int select = 0;      double num1=0, num2=0;      num1 = input.nextDouble();      num2 = input.nextDouble();      System.out.print("Enter first number:");                 System.out.print(num1);                 System.out.print("Enter second number: ");                 System.out.println(num2);                     System.out.print("Enter 1 for Addition, 2 for Subtraction, 3 for Multiplication, and 4 for Division");      select = input.nextInt();            switch ( select ) {         case 1: result = num1 + num2;         System.out.println(result);         break;         case 2: result = num1 - num2;         System.out.println(result);         break;         case 3: result = num1 * num2;         System.out.println(result);         break;         case 4: result = num1 / num2;         System.out.println(result);         break;         default: System.out.println("Invalid");         break;      }   }}`

That should be what the final code looks like, although you've already fixed it by now. Sorry for not noticing this earlier, my brain was foggy that day.

-Java

### Re: Java's Calculator

Posted: April 6th, 2012, 12:51 am
Its pretty neat! Password seems out of place, and the program stops working if 11 or more numbers are used in a single number. (Was just seeing how many numbers it could handle at once)... Still looks good though. ### Re: Java's Calculator

Posted: April 6th, 2012, 5:36 pm
Thanks I've updated this project a long time ago and the password protect system is now working, complete with a function that stores the password deep inside the darkest corners of your PC for later use. I'm going to work on adding several other things including quadratic equation solver and Pythagorean Theorem solver.

-Java

### Re: Java's Calculator

Posted: March 15th, 2013, 2:12 am
Just in case you're interested. I'm learning c++ and Ruby through Thenewboston YouTube channel. he covers a lot of different languages and the videos are well organized so it's easy to find a specific one.