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?

Quote:

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:

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:

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

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

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?

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

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