Java's Calculator

Section for submitting new content to Sandbox, such as maps, code, etc.
Kids are also more than welcome to submit their work with parental permission!
java.x.beast
Member
Member
Posts: 194
Joined: August 10th, 2011, 2:35 pm
Name: Addis
IRC Username: javaxbeast
Location: Chicago, IL
Contact:

Java's Calculator

Postby java.x.beast » 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.

-Addition
-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.

Edit: Download link : http://sourceforge.net/projects/javasca ... e/download

-Java
Age of Darkness

Maps: Image
Music: Image
Models: Image
Scripts: Image
Other: Image


Visit the company website: (In progress (Expected to be released in two weeks :uber:) 8-) :D 8-) )
Visit the company page on ModDB: Vulcanis Entertainment


java.x.beast wrote: I got them moves like JAGger!!!

Langdon99
Member
Member
Posts: 3
Joined: December 31st, 2011, 2:06 am
Name: Langdon99
IRC Username: Langdon99

Re: Java's Calculator

Postby Langdon99 » 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");}

java.x.beast
Member
Member
Posts: 194
Joined: August 10th, 2011, 2:35 pm
Name: Addis
IRC Username: javaxbeast
Location: Chicago, IL
Contact:

Re: Java's Calculator

Postby java.x.beast » 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
Age of Darkness

Maps: Image
Music: Image
Models: Image
Scripts: Image
Other: Image


Visit the company website: (In progress (Expected to be released in two weeks :uber:) 8-) :D 8-) )
Visit the company page on ModDB: Vulcanis Entertainment


java.x.beast wrote: I got them moves like JAGger!!!

java.x.beast
Member
Member
Posts: 194
Joined: August 10th, 2011, 2:35 pm
Name: Addis
IRC Username: javaxbeast
Location: Chicago, IL
Contact:

Re: Java's Calculator

Postby java.x.beast » 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
Age of Darkness

Maps: Image
Music: Image
Models: Image
Scripts: Image
Other: Image


Visit the company website: (In progress (Expected to be released in two weeks :uber:) 8-) :D 8-) )
Visit the company page on ModDB: Vulcanis Entertainment


java.x.beast wrote: I got them moves like JAGger!!!

User avatar
Sircameron
Member
Member
Posts: 62
Joined: March 6th, 2012, 9:13 pm
Name: Cameron
Location: Indiana

Re: Java's Calculator

Postby Sircameron » 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. :D

java.x.beast
Member
Member
Posts: 194
Joined: August 10th, 2011, 2:35 pm
Name: Addis
IRC Username: javaxbeast
Location: Chicago, IL
Contact:

Re: Java's Calculator

Postby java.x.beast » April 6th, 2012, 5:36 pm

Thanks :D
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
Age of Darkness

Maps: Image
Music: Image
Models: Image
Scripts: Image
Other: Image


Visit the company website: (In progress (Expected to be released in two weeks :uber:) 8-) :D 8-) )
Visit the company page on ModDB: Vulcanis Entertainment


java.x.beast wrote: I got them moves like JAGger!!!

Jordan Ashby
Member
Member
Posts: 16
Joined: November 15th, 2012, 1:32 pm
Name: Jordan
IRC Username: Jordy

Re: Java's Calculator

Postby Jordan Ashby » 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.


Return to “Donate Content To Sandbox And The Community”

Who is online

Users browsing this forum: Bing [Bot] and 2 guests