HEY GUYS I'M WORKING ON A CALCULATOR USING THE SCANNER CLASS. HOWEVER THERE IS A CATCH TO IT I CAN ONLY USE ADDITION AND SUBTRACTION TO CALCULATE ANSWERS. I HAVE TO MAKE IT INTERACTIVE AND ASK THE USER WHAT FUNCTION THEY WOULD LIKE TO DO(1.ADDITION,2.SUBTRACTION,3.DIVISION,4,MULTIPLICATION) I'M GOING TO USE A SWITCH CASE I WON'T HAVE ANY PROBLEM WITH THE ADDITION AND SUBTRACTION...BUT THE MULTIPLICATION AND DIVISION IS WHERE I'M GOING TO RUN INTO SOME PROBLEMS. FOR THOSE WHO DON'T GET WHAT I'M SAYING HERES AN EXAMPLE
mULTIPLICATION
2*2
2+2

3*4
4+4+4

DIVISION
20/4
20-4 = 16
20-4 = 12
20-4 = 8
20-4 = 4

SO THE ANSWER WOULD BE 5(COUNT HOW MANY TIMES I SUBTRACTED 4)
SO INSTEAD OF JUST HAVING LIKE THE ANSWER EQUAL SOMETHING LIKE NUM1*NUM2 I HAVE TO HAVE IT SETUP SO THE CODE WOULD HAVE THE ANSWER CALCULATED USING ADDITION INSTEAD OF JUST USING THE * OPERATOR. Could somebody give me some suggestions on how I would go about doing that. Only serious answers pleez no sarcasm.

## All 7 Replies

jwill, please do not use caps-lock. What code do you have so far? What have you tried?

jwill, please do not use caps-lock. What code do you have so far? What have you tried?

I'm sorry I will post what I got asap...it probably won't be until tomorrow though.

jwill, for the multiplication and divison you can do a for loop.

1. For multiplication, notice that;
2 * 2 = 2 + 2
3 * 4 = 3 + 3 + 3 + 3

Notice that you need to add factor 1 factor 2 times.

you can do a for loop like this

``````product = 0;
for ( int i = 0; i < factor2; i++) {
product = product + factor 1;``````

2. For division, juts follow same steps but do a subtraction and do some tweaking.

Hope this helps (",)

*WHIPES EYEBROW* Ok here's what I got

``````import java.util.*;

public class Calculator2
{
public static void main(String[] args)
{

System.out.println("To do Addition  Type 1.");
System.out.println("To do subtraction Type 2.");
System.out.println("To do Multiplication, Type 3");
System.out.println("To do division, Type 4");
Scanner enterSomething = new Scanner(System.in);
int decision;
decision = enterSomething.nextInt();

switch(decision)
{
break;

case 2: Subtraction();
break;

case 3: Multiplication();
break;

case 4: Division();
break;

}

}public static void Addition()
{
double result;

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

System.out.println("What is the second number?");

System.out.println(addend1 + " plus " + addend2 + " equals " + result);
}

public static void Subtraction()
{
double num1;
double num2;
double result;

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

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

result = num1 - num2;
System.out.println(num1 + " minus " + num2 + " equals " + result);
}

public static void Multiplication()
{
double factor1;
double factor2;
double result;

Scanner input = new Scanner(System.in);
System.out.print("What is Your First Number? ");
factor1 = input.nextDouble();

System.out.print("What is your second number? ");
factor2 = input.nextDouble();
result = factor1 * factor2;

System.out.println(factor1 + " times " + factor2 + " equals " + result );
}

public static void Division()
{
double dnum1;
double dnum2;
double quotient;

Scanner input = new Scanner(System.in);
System.out.println("What is your first number?");
dnum1 = input.nextDouble();

System.out.println("What is your second number");
dnum2 = input.nextDouble();

quotient = dnum1 / dnum2;
System.out.println(dnum1 + " divided by " + dnum2 + " equals " + quotient);
}
}``````

I haven't tried @Eric Cute's idea yet I'm just trying to get it run but, yeah this is what I got for now I'm about to try his idea right now

Ok I'm doing terrible with the division part this is what I have

``````import java.util.*;
/* I need to change some variables..lol note to self*/
public class Calculator2
{
public static void main(String[] args)
{

System.out.println("To do Addition  Type 1.");
System.out.println("To do subtraction Type 2.");
System.out.println("To do Multiplication, Type 3");
System.out.println("To do division, Type 4");
Scanner enterSomething = new Scanner(System.in);
int decision;
decision = enterSomething.nextInt();

switch(decision)
{
break;

case 2: Subtraction();
break;

case 3: Multiplication();
break;

case 4: Division();
break;

}

}public static void Addition()
{
double result;

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

System.out.println("What is the second number?");

System.out.println(addend1 + " plus " + addend2 + " equals " + result);
}

public static void Subtraction()
{
double num1;
double num2;
double result;

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

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

result = num1 - num2;
System.out.println(num1 + " minus " + num2 + " equals " + result);
}

public static void Multiplication()
{
double factor1;
double factor2;
double result;
double product;

Scanner input = new Scanner(System.in);
System.out.print("What is Your First Number? ");
factor1 = input.nextDouble();

System.out.print("What is your second number? ");
factor2 = input.nextDouble();
product = 0;
for(  double i = 0;i <factor2;i++)
{
product += factor1;
}

//result = factor1 * factor2;

System.out.println(factor1 + " times " + factor2 + " equals " + product );
}

public static void Division()
{
double dnum1;
double dnum2;
double quotient = 0;

Scanner input = new Scanner(System.in);
System.out.println("What is your first number?");
dnum1 = input.nextDouble();

System.out.println("What is your second number");
dnum2 = input.nextDouble();

for( double i = 0;i>dnum1;i++)
{
quotient=+i;

}

//quotient = dnum1 / dnum2;
System.out.println(dnum1 + " divided by " + dnum2 + " equals " + quotient);
}
}``````

Not looking to good for me

Not looking to good for me

Actually it seems that you are progressing so don't give up.

``````for(double i = 0; i <factor2; i++)
{
product += factor1;
}``````

This code will work - it states that if you want to calculate product*factor you simply add up product factor amount of times.
The division should work just like the multiplication, meaning that if you get var1 and var2 then var1/var2 is basically the question is how many times var2 can be contained inside var1 (6/3 means how many times 3 is contained inside 6, and the answer is 2).
In your division code you ask for dnum1 and dnum2, and then you do

``````for( double i = 0;i>dnum1;i++)
{
quotient=+i;
}``````

Where did dnum2 go? remember - dnum1/dnum2 simply states how many times can you contain dnum2 inside dnum1. One way to solve this problem is to subtract dnum2 from dnum1 as many times as you can, which is the number of times until dnum1 is smaller than dnum2 and then you can't subtract dnum2 from it anymore. What we got so far:

``````while(dnum1 >= dnum2)
{
dnum1 -= dnum2;
++result;
}``````

Now, two more comments about your code if I may - first you should stick to Java's naming conventions. Class names should start with a capital letter and methods should start with lower case letters. Addition() should be addition() and so on. This is not a must but will make your code more readable (for example, when you post questions and code on a forum :))
Second, you have a lot of code repetition - Your Addittion(), Subtraction(), Multiplication(), and Division() all have the same starting code:

``````double addend1;
double result;
Scanner input = new Scanner(System.in);
System.out.println("What is the first number?");
System.out.println("What is the second number?");

I now you decide that you want to get 3 numbers from the user, you will have to change this code in any of your methods. Why not extract this code from all the methods and put it in one place? Then you can pass the two numbers taken from the user as a parameter to the appropriate method - Something of the sort of:

``````public static void main(String[] args)
{
System.out.println("To do Addition  Type 1.");
System.out.println("To do subtraction Type 2.");
System.out.println("To do Multiplication, Type 3");
System.out.println("To do division, Type 4");
Scanner enterSomething = new Scanner(System.in);
int decision;
decision = enterSomething.nextInt();

//this is the code for prompting the user for the numbers
//extracted from all the other methods to one central place
double var1;
double var2;

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

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

//now send those parameters to the appropriate method
switch(decision)
{
case 1: Addition(var1, var2); //var1 and var2 are sent as parameters
break;

case 2: Subtraction(var1, var2);
break;

case 3: Multiplication(var1, var2);
break;

case 4: Division(var1, var2);
break;
}``````

Now you can use those variables in the methods. For example, your Addition method will look:

``````public static void Addition(double addend1, double addend2)
{
System.out.println(addend1 + " plus " + addend2 + " equals " + result);
}``````

More readable this way don't you think?

Why yes I agree....I'll definitely keep that in mind for some more problems in the future.Here's the final product though nonetheless Thanks to everyone..you guys really helped me out.....

``````import java.util.Scanner;

public class Calculator2
{
public static void main(String[] args)
{

/*******************************************************************************************************************
DISPLAY THE MENU AND DECLARE SCANNER
/*******************************************************************************************************************/
int decision;
do { //Do...while loop to bring menu up again after calculation is complete
System.out.println("To do Addition  Type 1.");
System.out.println("To do subtraction Type 2.");
System.out.println("To do Multiplication, Type 3");
System.out.println("To do division, Type 4");
Scanner enterSomething = new Scanner(System.in);
//int decision;
decision = enterSomething.nextInt();
/*****************************************************************************************************************
SWITCH STATEMENT....1 FOR ADDITION METHOD....2 FOR SUBTRACTION METHOD...3 FOR MULTIPLICATION METHOD....AND....
4 FOR DIVISION METHOD.....
/*******************************************************************************************************************/
switch(decision)
{
break;

case 2: Subtraction();
break;

case 3: Multiplication();
break;

case 4: Division();
break;
}

}while(decision!=5);

{
double result;

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

System.out.println("What is the second number?");

System.out.println(addend1 + " plus " + addend2 + " equals " + result);
}

public static void Subtraction()		//Subtraction method
{
double num1;
double num2;
double result;

Scanner tuna = new Scanner(System.in);
System.out.println("What is the first number?");
num1 = tuna.nextDouble();

System.out.println("What is the second number?");
num2 = tuna.nextDouble();

result = num1 - num2;
System.out.println(num1 + " minus " + num2 + " equals " + result);
}

public static void Multiplication()			//Multiplication method
{
double factor1;
double factor2;
double result;
double product;

Scanner tuna = new Scanner(System.in);
System.out.print("What is Your First Number? ");
factor1 = tuna.nextDouble();

System.out.print("What is your second number? ");
factor2 = tuna.nextDouble();
product = 0;
/***********************************************************************************************************************
MULTIPLICATION LOOP
/***********************************************************************************************************************/
for(  double i = 0;i <factor2;i++)
{
product += factor1;
}

//result = factor1 * factor2;

System.out.println(factor1 + " times " + factor2 + " equals " + product );
}

public static void Division()		//Divison method
{
double dnum1;
double dnum2;
double quotient = 0;

Scanner tuna = new Scanner(System.in);
System.out.println("What is your first number?");
dnum1 = tuna.nextDouble();

System.out.println("What is your second number");
dnum2 = tuna.nextDouble();
int result = 0;

double temp = 0;
int i = 0; //define outside because you'll need it later
/*******************************************************************************************************************
DIVISION LOOP
/********************************************************************************************************************/
for (i = 0; temp < dnum1; i++)
{
temp += dnum2;
}

System.out.println(dnum1 + " divided by " + dnum2 + " equals: " + i);

}
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.