# Help with simple calculator

• September 22nd, 2013, 12:18 PM
GMPoison
Help with simple calculator
First time using the forums and I got this assignment to make a calculator, which I have down and know how to do. It has to loop through asking for an Integer and Double 4 times, which I know how to do. The problem I'm having when ever I enter two numbers , it's supposed to give the sum, difference, and product, quotient and remainder, but when I enter a 0 for the second number, it's supposed to give the sum, difference, and product, but say "cannot divide by 0" for the quotient and remainder. When I enter a 0 for the second number of my Double, it works fine and tells me I cannot divide by 0. When I enter the second number for my Integer, it gives me an error. Please help!
Code :

package methodVersions;

import java.util.Scanner;

public class Method_Void {

public static void main(String[] args) {

System.out.println("This program was written by Daniel Pytlarz");
System.out.println("-----------------------------");
String varName;

for (int loop = 0; loop <5; ++loop){

//Integers

System.out.println("");
System.out.println("Trial " + (loop+1) + " Integers: ");
int fNum, sNum;

System.out.println("");

add(varName, fNum, sNum); //firstNum + secondNum;

sub(varName, fNum, sNum); //firstNum - secondNum;

mult(varName, fNum, sNum); //firstNum * secondNum;

qotremain(varName, fNum, sNum); //firstNum / secondNum & firstNum % secondNum;

//Doubles

System.out.println("");
System.out.println("Trial " + (loop+1) + " Doubles: ");
Double fNumD, sNumD;

System.out.println("");

addD(varName, fNumD, sNumD); //firstNum + secondNum;

subD(varName, fNumD, sNumD); //firstNum - secondNum;

multD(varName, fNumD, sNumD); //firstNum * secondNum;

qotremainD(varName, fNumD, sNumD); //firstNum / secondNum & firstNum % secondNum;

}

System.out.println("-----------------------------");
System.out.println("This program was run by " + varName);
System.out.println("-----------------------------");

}

//Integer Methods

public static void add(String z, int x, int y){
int sum;
sum = x + y;
System.out.println(z + ", the sum of " + x + " and " + y + " is " + sum);

}

public static void sub(String z, int x, int y){
int diff;
diff = x - y;
System.out.println(z + ", the difference of " + x + " and " + y + " is " + diff);

}

public static void mult(String z, int x, int y){
int prod;
prod= x * y;
System.out.println(z + ", the product of " + x + " and " + y + " is " + prod);

}

public static void qotremain(String z, int x, int y){
int qot;
int remain;
qot = x / y;
remain = x % y;
if(y == 0){
System.out.println(z + ", you cannot divide by 0. Remainder and quotent not available. ");
} else{
System.out.println(z + ", the quotient of " + x + " and " + y + " is " + qot);
System.out.println(z + ", the remainder of " + x + " and " + y + " is " + remain);
}

}

//Double Methods

public static void addD(String z, double x, double y){
double total;
total = x + y;
System.out.println(z + ", the sum of " + x + " and " + y + " is " + total);

}

public static void subD(String z, double x, double y){
double diff;
diff = x - y;
System.out.println(z + ", the difference of " + x + " and " + y + " is " + diff);

}

public static void multD(String z, double x, double y){
double prod;
prod = x * y;
System.out.println(z + ", the product of " + x + " and " + y + " is " + prod);

}

public static void qotremainD(String z, double x, double y){
double qot;
double remain;
qot = x / y;
remain = x % y;
if(y == 0){
System.out.println(z + ", you cannot divide by 0. Remainder and quotent not available. ");
} else{
System.out.println(z + ", the quotient of " + x + " and " + y + " is " + qot);
System.out.println(z + ", the remainder of " + x + " and " + y + " is " + remain);
}

}

}

• September 22nd, 2013, 01:27 PM
GregBrannon
Re: Help with simple calculator
For the integer dividing by 0 error, the program attempts the division before checking to see if it's an allowed operation in the method qotremain(). Fix the order of operations to prevent the error from occurring.
• September 22nd, 2013, 01:29 PM
GMPoison
Re: Help with simple calculator
Quote:

Originally Posted by GregBrannon
For the integer dividing by 0 error, the program attempts the division before checking to see if it's an allowed operation in the method qotremain(). Fix the order of operations to prevent the error from occurring.

Thank you, I knew it would be something stupid :P