Results 1 to 6 of 6
  1. #1
    Suurisa is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default 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;
        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.");
            }
        }
        
    }
    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. :]

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Here is my output
    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.
    When did you get all zeros?

  3. #3
    Suurisa is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Another reason why code should not be duplicated. Copy paste errors increase with code duplication.

  6. #6
    Suurisa is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    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. :]

Similar Threads

  1. what's the wrong in this Code
    By the swan in forum AWT / Swing
    Replies: 1
    Last Post: 04-04-2009, 04:27 AM
  2. what's wrong with my code? please help me...
    By lovely23 in forum Java Applets
    Replies: 2
    Last Post: 03-01-2009, 01:33 PM
  3. pls tell wat wrong with my code???
    By low224 in forum New To Java
    Replies: 13
    Last Post: 01-11-2009, 07:40 AM
  4. What's wrong with this code?
    By Doctor Cactus in forum New To Java
    Replies: 4
    Last Post: 11-29-2008, 05:44 PM
  5. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-22-2008, 11:55 PM

Posting Permissions

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