PDA

View Full Version : Created a very Simple Calculator.


Phonecheck
06-07-2007, 01:39 AM
Yes i just created a very simple calculator using what i learned so far(Only been studying Java for about 2 weeks).Im not sure if its in the correct section so correct me if it isnt.Comment on it please and give your opinions but please dont flame.
import java.util.*;
/*Calculator V.1.1*/

public class Calculator
{
public static void main(String[] args)
{
/*Scanner input = new Scanner(System.in);
System.out.println("What are you doing?");
if (answer.equalsIgnorecase("multiplication"))
{
Multiplication();
}*/
System.out.println("Hello, If you are doing Multiplication Type 1.");
System.out.println("If you are doing Addition, Type 2.");
System.out.println("If you are doing Subtraction, Type 3");
System.out.println("If you are doing Division, Type 4");
Scanner input = new Scanner(System.in);
int decision;
decision = input.nextInt();
if (decision == 1)
{
Multiplication();
}
if (decision == 2)
{
Addition();
}
if (decision == 3)
{
Subtraction();
}
if (decision == 4)
{
Division();
}
else {
System.out.println("Those aren't valid numbers.");
wrongInput();
}
}

public static void wrongInput()
{
System.out.println("If you are doing Multiplication Type 1.");
System.out.println("If you are doing Addition, Type 2.");
System.out.println("If you are doing Subtraction, Type 3");
System.out.println("If you are doing Division, Type 4");
Scanner input = new Scanner(System.in);
int decision;
decision = input.nextInt();
if (decision == 1)
{
Multiplication();
}
if (decision == 2)
{
Addition();
}
if (decision == 3)
{
Subtraction();
}
if (decision == 4)
{
Division();
}
else {
System.out.println("Those aren't valid numbers.");
wrongInput();
}
}
public static void Multiplication()
{
int mInput1;
int mInput2;

Scanner input = new Scanner(System.in);
System.out.print("What is the first number? ");
mInput1 = input.nextInt();

System.out.print("What is the second number? ");
mInput2 = input.nextInt();

System.out.println(mInput1 + " times " + mInput2 + " equals " + mInput1 * mInput2);
}
public static void Addition()
{
int aInput1;
int aInput2;
int aAnswer;

Scanner input = new Scanner(System.in);
System.out.println("What is the first number?");
aInput1 = input.nextInt();

System.out.println("What is the second number?");
aInput2 = input.nextInt();

aAnswer = aInput1 + aInput2;
System.out.println(aInput1 + " plus " + aInput2 + " equals " + aAnswer);
}
public static void Subtraction()
{
int sInput1;
int sInput2;
int sAnswer;

Scanner input = new Scanner(System.in);
System.out.println("What is the first number?");
sInput1 = input.nextInt();

System.out.println("What is the second number?");
sInput2 = input.nextInt();

sAnswer = sInput1 - sInput2;
System.out.println(sInput1 + " minus " + sInput2 + " equals " + sAnswer);
}
public static void Division()
{
int dInput1;
int dInput2;
int dAnswer;

Scanner input = new Scanner(System.in);
System.out.println("What is the first number?");
dInput1 = input.nextInt();

System.out.println("What is the second number?");
dInput2 = input.nextInt();

dAnswer = dInput2 / dInput1;
System.out.println(dInput1 + " divided by " + dInput2 + " equals " + dAnswer);
}
}

brad211987
06-07-2007, 02:55 AM
To trim down the code a bit, you can eliminate your wrongInput method, by using a loop in the main method of your code, along with another question like "Do you want to do more calculations?"

The basic idea is:

answer = ""

While answer does not equal "no"
Calculations logic(if statements, method calls etc...)
answer = result of question to continue


Just an idea to continue on what you've learned thus far.

You can also look into how to pass parameters, and use only one method called doCalculation() and pass it a value that tells which type of calculation to do. That would take out all of the repetition of code.

Gox
06-07-2007, 03:52 AM
Congrats on your first Java program!

Brad has made some good suggestions. If you're looking to forward your knowledge take them into consideration, they will help teach you good programming style.