# Thread: What's going wrong with this code?

1. Member Join Date
Oct 2009
Posts
4
Rep Power
0

## What's going wrong with this code?

Hi all,
I have a quick question with what is going wrong with my code.

When I try to use my distance solver in my program, all the answers come up as zero, i'm not sure if I just rushed through it, but it seems to all look good, can someone maybe help out and point out the problem, all the rest of the code seems to be working just fine.

This is my class with all the "core" methods which does all the calculations.
You will be looking specifically at the "doVelProb" method, which has the little mistake in it, please.

Java Code:
```/**
* This class is a simple calculator which calculates physics and math problems.
* Eventually, it will have a huge inventory of selections to choose from so you can
* do you math & physics problems simply.
*
* @author Sebastian Wietecha
* @version 1.0
*/

import java.util.*;

public class MPSolverTool
{

static Scanner console = new Scanner(System.in);

// All variables that are used in both physics & mathematics.
double num1;
double num2;
double totalNum;
String type;

// All variables that are used in mathematics.
double pi = 3.1415;

// All variables that are used in physics.
double velocity;
double distance;
double time;
String physicsType;

{
// Does a simple addition problem.
num1 = console.nextDouble();
num2 = console.nextDouble();
totalNum = num1 + num2;
System.out.println("The total of num1 + num2 = " + totalNum + ".");
}

public void doSubProb()
{
// Does a simple subtraction problem.
System.out.println("Please enter the first number you wish to subtract.");
num1 = console.nextDouble();
System.out.println("Please enter the second number you wish to subtract.");
num2 = console.nextDouble();
totalNum = num1 - num2;
System.out.println("The total of num1 - num2 = " + totalNum + ".");
}

public void doDivProb()
{
// Does a simple division problem.
System.out.println("Please enter the first number you wish to divide.");
num1 = console.nextDouble();
if (num1 == 0) // You cannot divide by zero, making a failsafe here.
{
System.out.println("You cannot divide by zero!");
} else
{
System.out.println("Please enter the second number you wish to divide.");
num2 = console.nextDouble();
if (num2 == 0) // You cannot divide by zero, making a failsafe here.
{
System.out.println("You cannot divide by zero!");
} else
{
totalNum = num1 / num2;
System.out.println("The total of num1 / num2 = " +  totalNum + ".");
}
}
}

public void doMulProb()
{
// Does a simple multiplication problem.
System.out.println("Please enter the first number you wish to multiply.");
num1 = console.nextDouble();
System.out.println("Please enter the second number you wish to multiply.");
num2 = console.nextDouble();
totalNum = num1 * num2;
System.out.println("The total of num1 * num2 = " +  totalNum + ".");
}

public void doCirProb()
{
// Finds the circumference with just the radius variable.
// TODO: Make it solves for radius using circumference.
System.out.println("The circumference of a circle is: " + totalNum + ".");
}

public void doVelProb()
{
// Does a physics problem (Velocity = Distance over Time.)
System.out.println("What are you solving for?");
System.out.println("Velocity, Distance, or Time.");
physicsType = console.nextLine();

// Decides which problem to solve for from the information the user has input.
if (physicsType.equalsIgnoreCase("Velocity") || physicsType.equalsIgnoreCase("Vel"))
{
System.out.println("You are solving for velocity.");
System.out.println("Please enter the distance that you have.");
distance = console.nextDouble();
System.out.println("Please enter the time that you have.");
time = console.nextDouble();
velocity = distance / time;
System.out.println("The answer for velocity is: " + velocity + ".");
} else if (physicsType.equalsIgnoreCase("Distance") || physicsType.equalsIgnoreCase("Dis"))
{
System.out.println("You are solving for distance.");
System.out.println("Please enter the velocity that you have.");
distance = console.nextDouble();
System.out.println("Please enter the time that you have.");
time = console.nextDouble();
distance = velocity * time;
System.out.println("The answer for distance is: " + distance + ".");
} else if (physicsType.equalsIgnoreCase("Time") || physicsType.equalsIgnoreCase("Tim"))
{
System.out.println("You are solving for time.");
System.out.println("Please enter the velocity that you have.");
distance = console.nextDouble();
System.out.println("Please enter the distance that you have.");
time = console.nextDouble();
time = distance * velocity;
System.out.println("The answer for time is: " + time + ".");
} else
{
System.out.println("You entered something wrong.");
}
}

public void whichProb()
{
// Figures out which problem you chose, and proceeds to execute it.
System.out.println("Which type of problem do you want to do?");
System.out.println("Subtraction - Does simple subtraction problems.");
System.out.println("Multiplication - Does simple multiplicative problems.");
System.out.println("Division - Does simple division problems.");
System.out.println("Circumference - Finds the circumference using radius.");
System.out.println("Velocity - Finds the velocity, distance, or time in physics.");
type = console.nextLine();

{
} else if (type.equalsIgnoreCase("Subtraction") || type.equalsIgnoreCase("Sub"))
{
doSubProb();
} else if (type.equalsIgnoreCase("Mutiplication") || type.equalsIgnoreCase("Mul"))
{
doMulProb();
} else if (type.equalsIgnoreCase("Divide") || type.equalsIgnoreCase("Div") || type.equalsIgnoreCase("Division"))
{
doDivProb();
} else if (type.equalsIgnoreCase("Circumference") || type.equalsIgnoreCase("Cir"))
{
doCirProb();
} else if (type.equalsIgnoreCase("Velocity") || type.equalsIgnoreCase("Vel"))
{
doVelProb();
} else
{
System.out.println("You mis-typed something, and the program cannot read what command your executing.");
}
}

}```
And this is the second class I use to execute all the "core" code.

Java Code:
```/**
* This class uses the MPSolverTool to execute the tool.
*
* @author Sebastian Wietecha
* @version 1.0
*/

public class ClientSolver
{

public static void main(String[] args)
{
MPSolverTool m = new MPSolverTool();

m.whichProb();
}
}```
Thanks for all the help. :]  Reply With Quote

2. Senior Member Join Date
Aug 2009
Posts
2,388
Rep Power
14

## Here is my output
Java Code:
```Which type of problem do you want to do?
Subtraction - Does simple subtraction problems.
Multiplication - Does simple multiplicative problems.
Division - Does simple division problems.
Circumference - Finds the circumference using radius.
Velocity - Finds the velocity, distance, or time in physics.
vel
What are you solving for?
Velocity, Distance, or Time.
vel
You are solving for velocity.
Please enter the distance that you have.
34
Please enter the time that you have.
3
The answer for velocity is: 11.333333333333334.```
When did you get all zeros?  Reply With Quote

3. Member Join Date
Oct 2009
Posts
4
Rep Power
0

## When you try to solve for distance, the velocity and time works fine. Sorry if I confused you on that part.  Reply With Quote

4. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## The velocity case looks OK, but check the other two.

When calculating distance where does the value of velocity come from? Same when you are calculating time (and check the formula you use.)

Edit:

Further to this: it is a good idea to declare variables right where they are used. If variables are local to a method you will get a warning if they have not been initialised.
Last edited by pbrockway2; 10-19-2009 at 11:57 PM.  Reply With Quote

5. Senior Member Join Date
Aug 2009
Posts
2,388
Rep Power
14

## Another reason why code should not be duplicated. Copy paste errors increase with code duplication.  Reply With Quote

6. Member Join Date
Oct 2009
Posts
4
Rep Power
0

## So for example:

Java Code:
`doVelProb(int distance)`
?

and I have been going over it a couple of times already, and I am positive that the formula to calculate it is correct.

EDIT: Oh, I found it, you were right I didn't change all the C&P code that I had.

Thanks guys/gals. :]  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•