Results 1 to 6 of 6
- 10-19-2009, 11:39 PM #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.
And this is the second class I use to execute all the "core" code.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; double radius; // All variables that are used in physics. double velocity; double distance; double time; String physicsType; public void doAddProb() { // Does a simple addition problem. System.out.println("Please enter the first number you wish to add."); num1 = console.nextDouble(); System.out.println("Please enter the second number you wish to add."); 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("Please enter the radius."); radius = console.nextDouble(); totalNum = pi * radius * radius; 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("Addition - Does simple addition problems."); 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(); if (type.equalsIgnoreCase("Addition") || type.equalsIgnoreCase("Add")) { doAddProb(); } 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."); } } }
Thanks for all the help. :]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(); } }
- 10-19-2009, 11:51 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Here is my output
When did you get all zeros?Java Code:Which type of problem do you want to do? Addition - Does simple addition problems. 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.
- 10-19-2009, 11:54 PM #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.
- 10-19-2009, 11:55 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 10-19-2009, 11:58 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Another reason why code should not be duplicated. Copy paste errors increase with code duplication.
- 10-19-2009, 11:59 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
what's the wrong in this Code
By the swan in forum AWT / SwingReplies: 1Last Post: 04-04-2009, 04:27 AM -
what's wrong with my code? please help me...
By lovely23 in forum Java AppletsReplies: 2Last Post: 03-01-2009, 01:33 PM -
pls tell wat wrong with my code???
By low224 in forum New To JavaReplies: 13Last Post: 01-11-2009, 07:40 AM -
What's wrong with this code?
By Doctor Cactus in forum New To JavaReplies: 4Last Post: 11-29-2008, 05:44 PM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks