Results 1 to 4 of 4
  1. #1
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Question What is error checking and how can it be used?

    These are the instructions and i need to include error checking? What exactly is that? Did i put it right in my code?
    I need to write a method named speed that will accept two double values. The first value represents the distance covered by a vehicle (in miles)
    -the second is the time that it took the vehicle to cover the distance (in hours).
    The method should calculate and return the speed of the vehicle (in miles per hour).
    Include error checking. If the distance is less than or equal to zero, return zero. If the time is less than or equal to zero, return zero.
    This is my class:
    Java Code:
    public double speed( double miles, double hours) {
    return (0 >= miles || 0 >= hours)? 0 : miles / hours;
    }
    // second shortest form uses implicit else
    public double speed1( double miles, double hours) {
    if (0 >= miles || 0 >= hours) return 0;
    // don't NEED explicit else, since method exited above 
    // for invalid input
    return miles / hours;
    }
    // third shortest form uses explicit else
    public double speed2( double miles, double hours) {
    if (0 >= miles || 0 >= hours) return 0;
    else return miles / hours;
    }
    // longest form uses single exit point
    public double speed3( double miles, double hours) {
    double mph = 0; // assign default for bad input
    // if good input calculate and reassign return value
    if (0 < miles && 0 < hours) mph = miles / hours;
    return mph;
    }
    
    
    }// end class
    This is my driver program:
    Java Code:
       System.out.println("\nThis is problem # 4\n");
       
    Scanner sp = new Scanner(System.in);
    System.out.println("Enter the number of miles: ");
    double miles = sp.nextDouble();
    System.out.println("Enter the duration in hours: ");
    double hours = sp.nextDouble();
    
    Lab6B spe = new Lab6B();
    System.out.println( 
    "using speed(), speed in miles-per-hour is: " + 
    s.speed( miles, hours) + "\n" +
    "using speed()1, speed in miles-per-hour is: " + 
    s.speed1( miles, hours) + "\n" +
    "using speed2(), speed in miles-per-hour is: " + 
    s.speed2( miles, hours) + "\n" +
    "using speed3(), speed in miles-per-hour is: " + 
    s.speed3( miles, hours) + "\n"
    );
    
    }//end of problem 4

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,784
    Rep Power
    5

    Default Re: What is error checking and how can it be used?

    Error checking in this case is validating user input so it makes sense in the context of the program. For example. if you are prompted for the dimensions of a rectangle to calculate area, you would want to ensure that the values for length and width are each > 0.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: What is error checking and how can it be used?

    oh okay, so the code has to show that the numbers cannot be < zero. So in my output if i put anything less then 0 it will return what i need it to return?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,784
    Rep Power
    5

    Default Re: What is error checking and how can it be used?

    Java Code:
    public double speed( double miles, double hours) {
    
    return (0 >= miles || 0 >= hours)? 0 : miles / hours;
    }
    // second shortest form uses implicit else
    public double speed1( double miles, double hours) {
    if (0 >= miles || 0 >= hours) return 0;
    // don't NEED explicit else, since method exited above 
    // for invalid input
    return miles / hours;
    }
    // third shortest form uses explicit else
    public double speed2( double miles, double hours) {
    if (0 >= miles || 0 >= hours) return 0;
    else return miles / hours;
    }
    // longest form uses single exit point
    public double speed3( double miles, double hours) {
    double mph = 0; // assign default for bad input
    // if good input calculate and reassign return value
    if (0 < miles && 0 < hours) mph = miles / hours;
    return mph;
    }
    Ok. Here are some observations.

    First, normally one does tests to check the variable against the constraint, not the constraint against the variable.

    For example,

    if (a > 0)
    do something

    if (0 < a)
    do something.

    I find the first one easier to read. But they both fulfill the function. I had to reverse the inequalities in my head to make certain your conditions were valid (probably my shortcoming).

    Second, you don't need the else in line 15 since you simply return if the test succeeds.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Checking an error message from a website.
    By christopherx in forum Advanced Java
    Replies: 6
    Last Post: 08-14-2012, 09:59 AM
  2. help to checking the error!!
    By vitorloke in forum New To Java
    Replies: 34
    Last Post: 04-26-2012, 06:50 PM
  3. Checking if a Bit is Set.
    By Hypnos in forum New To Java
    Replies: 11
    Last Post: 01-18-2012, 09:27 PM
  4. Error Checking not working correctly
    By RickAintree in forum New To Java
    Replies: 1
    Last Post: 12-15-2010, 01:54 PM
  5. how to do checking value
    By madhuks in forum Java Servlet
    Replies: 3
    Last Post: 07-21-2010, 09:35 AM

Posting Permissions

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