Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## New user needs help badly!!!

I am fairly new to Java and have been working on a problem for over a week and have hit a wall. I have been working on it for the past six hours to no avail and was hoping that somebody could help me out.

The problem I have been assigned to do is making a taxi fare calculator using a floating point value to represent the number of miles a taxi has transported a passenger. The minimum fair for a ride is \$2.00 for anything less than or equal to 1 mile. It is 15 cents for each additional complete tenth of a mile beyond the first one. Mileage is never entered for anything more than one hundredth of a mile, but is always displayed in hundredths. I need to use system.out.printf as my output mechanism.

I know it is completely wrong but here is what I came up with:

import java.util.Scanner;
public class cab {

public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);

System.out.print("Please enter the mileage traveled - ");
//prompt user to type in mileage travelled
double Mileage = input.nextInt();

double Fare = (Mileage - 1) * 10 * .15 + 2.00;

if (Mileage < 1.10)
System.out.println("The fare for " + Mileage + " miles is \$2.00.");
else if (Mileage >= 1.10);
System.out.printf("The fare for " + Mileage + " miles is \$" + Fare);

}

}

Any help I can get would be much appreciated. Thanks in advance!

• ## Mileage

Mileage.java

Code:
```import java.util.InputMismatchException;
import java.util.Scanner;

public class Mileage {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
double mileage = new Double(0);

System.out.print("Enter mileage: ");
try {
mileage = in.nextDouble();
} catch (InputMismatchException ex) {
System.out.println("Mileage has to be an int or double");
}

System.out.printf("The fare for %.3f miles is: \$%.2f", mileage, getFare(mileage));

}

static double getFare(double miles) {
if (miles <= 0) {
return 0;
}

if (miles <= 1) {
return 2;
}

double fare = 2;

for (int m = (int) ((miles - 1) * 100); m > 0; m -= 10) {
fare += 0.15;
}

return fare;
}
}```

• ## Mileage

Wow! I am impressed with how fast you did that! You know your stuff. It really helps me figure it out by seeing what you did with the coding.

The only problem I am having with it (and I apologize, I should have told you this in the first post) is that the math is not coming out correctly with a few examples. I was given these numbers to work with for the test to make sure I get the correct results: 1.05 miles should result in \$2.00 and 3.14 miles should result in \$5.15. I am guessing that the difference is because in the calculator the cost should only rise in 15 cent increments every time a .1 mile is complete. But I see in your coding that you specified for that.

Any idea why the math is coming out differently? Again, I thank you for all your help with this!

• ## Correction

Appreciate the thanks. The wrong assumption I made was rounding up the fare, anyways now I have made the corrections. Check if it is working the way you wanted.

Code:
```import java.util.InputMismatchException;
import java.util.Scanner;

public class Mileage {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
double mileage = new Double(0);

System.out.print("Enter mileage: ");
try {
mileage = in.nextDouble();
} catch (InputMismatchException ex) {
System.out.println("Mileage has to be an int or double");
System.exit(-1);
}

System.out.printf("The fare for %.3f miles is: \$%.2f", mileage, getFare(mileage));

}

static double getFare(double miles) {
if (miles <= 0) {
return 0;
}

//change 01
if (miles < 1.10) {
return 2;
}

double fare = 2;

//change 02
for (int m = (int) ((miles - 1) * 100); m >= 9; m -= 10) {
fare += 0.15;
}

return fare;
}
}```

• ## Users who have thanked parrygill for this post:

Mustan7012 (02-14-2011)

• This doesn't warrant having an iterative solution.
PHP Code:
```     private static final double MIN_MILES = 1.0;    private static final double MIN_COST = 2.0;        private static final double MILE_MULTIPLIER = 0.10;    private static final double COST_PER_MILE = 0.15;    private static final double MAX_COST = Double.POSITIVE_INFINITY;            static double getFare(double miles)    {        double dResult = 0;                if (miles > 0.0)        {            dResult = MIN_COST;            if (miles > MIN_MILES)            {                dResult += ((int)((miles - MIN_MILES) / MILE_MULTIPLIER)) * COST_PER_MILE;                            }        }                dResult = dResult > MAX_COST ? MAX_COST : dResult;                return dResult;    }  ```
Used with:
PHP Code:
```         for (double d = 0.0; d <= 3.25; d += 0.04)        {            System.out.printf("Fare on %1.2f is %1.2f%n", d, getFare(d));        }  ```
Resulted in:
Code:
```Fare on 0.00 is 0.00
Fare on 0.04 is 2.00
Fare on 0.08 is 2.00
Fare on 0.12 is 2.00
Fare on 0.16 is 2.00
Fare on 0.20 is 2.00
Fare on 0.24 is 2.00
Fare on 0.28 is 2.00
Fare on 0.32 is 2.00
Fare on 0.36 is 2.00
Fare on 0.40 is 2.00
Fare on 0.44 is 2.00
Fare on 0.48 is 2.00
Fare on 0.52 is 2.00
Fare on 0.56 is 2.00
Fare on 0.60 is 2.00
Fare on 0.64 is 2.00
Fare on 0.68 is 2.00
Fare on 0.72 is 2.00
Fare on 0.76 is 2.00
Fare on 0.80 is 2.00
Fare on 0.84 is 2.00
Fare on 0.88 is 2.00
Fare on 0.92 is 2.00
Fare on 0.96 is 2.00
Fare on 1.00 is 2.00
Fare on 1.04 is 2.00
Fare on 1.08 is 2.00
Fare on 1.12 is 2.15
Fare on 1.16 is 2.15
Fare on 1.20 is 2.30
Fare on 1.24 is 2.30
Fare on 1.28 is 2.30
Fare on 1.32 is 2.45
Fare on 1.36 is 2.45
Fare on 1.40 is 2.60
Fare on 1.44 is 2.60
Fare on 1.48 is 2.60
Fare on 1.52 is 2.75
Fare on 1.56 is 2.75
Fare on 1.60 is 2.90
Fare on 1.64 is 2.90
Fare on 1.68 is 2.90
Fare on 1.72 is 3.05
Fare on 1.76 is 3.05
Fare on 1.80 is 3.20
Fare on 1.84 is 3.20
Fare on 1.88 is 3.20
Fare on 1.92 is 3.35
Fare on 1.96 is 3.35
Fare on 2.00 is 3.50
Fare on 2.04 is 3.50
Fare on 2.08 is 3.50
Fare on 2.12 is 3.65
Fare on 2.16 is 3.65
Fare on 2.20 is 3.80
Fare on 2.24 is 3.80
Fare on 2.28 is 3.80
Fare on 2.32 is 3.95
Fare on 2.36 is 3.95
Fare on 2.40 is 4.10
Fare on 2.44 is 4.10
Fare on 2.48 is 4.10
Fare on 2.52 is 4.25
Fare on 2.56 is 4.25
Fare on 2.60 is 4.40
Fare on 2.64 is 4.40
Fare on 2.68 is 4.40
Fare on 2.72 is 4.55
Fare on 2.76 is 4.55
Fare on 2.80 is 4.70
Fare on 2.84 is 4.70
Fare on 2.88 is 4.70
Fare on 2.92 is 4.85
Fare on 2.96 is 4.85
Fare on 3.00 is 5.00
Fare on 3.04 is 5.00
Fare on 3.08 is 5.00
Fare on 3.12 is 5.15
Fare on 3.16 is 5.15
Fare on 3.20 is 5.30
Fare on 3.24 is 5.30```
Which looks to match your criteria. When dealing with doubles, its up to you to format them in some way that provides the necessary display rounding for you. Computation mathematics and doubles often throw your numbers into a state of 'close enough', which when directly printed are incorrect (ie: 7.849999999999999 instead of 7.85) since computers have a hard time dividing by 10s. Use a NumberFormatter, StringFormat, or printf to get around these limitations.

I've provided some final variables (java 'constants') as well in order to make reconfiguration easy.

Edit:
Oh, btw, notice the calculations as well:
PHP Code:
``` double Fare = (Mileage - 1) * 10 * .15 + 2.00;  ```
And
PHP Code:
``` dResult += ((int)((miles - MIN_MILES) / MILE_MULTIPLIER)) * COST_PER_MILE;  ```
Your initial calculation was almost correct, but you would have to have divided again to calculate the correct remaining value. I just ignored that and went straight through the divide.

• ## Users who have thanked Fou-Lu for this post:

Mustan7012 (02-14-2011)

• ## Many thanks!

Thank you both for your help! You were both extremely helpful! You have no idea how long I was running in circles with this problem for. You guys are amazing at this stuff. I learned more from examining your codes than I did reading the textbook on the subject. Thanks again, I really appreciate your help!

•

#### Posting Permissions

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