### Thread: Calculating the mileage statistics for a cab company

Page 1 of 2 12 Last
1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2010
Posts
25
Rep Power
0

#### Calculating the mileage statistics for a cab company

I am very new to java and was wondering if I can get some help with an assignment. Here is the description of what needs to be done.

1. Create a Java program with proper documentation and indentation.
2. Give a brief description to the user.
3. Mary suggests creating a class named ‘CarTracker’ with two String variables (first and last names of the driver), a double variable to track the fares paid to the driver; a double variable to track the gasoline purchased by the driver; a double variable to track the gasoline used.
4. Display for the user the following and enter the data (use the Scanner class) exactly to test your program:

(type the name in exactly as shown):
Please enter the Driver's first name:
john
Please enter the Driver's Last name:
betterDriver
Please enter the amount paid to the Driver (fares):
121.45
Please enter the amount the Driver paid for gas:
36.50
Please enter the distance the Driver travelled:
525
Please enter the amount of gasoline the Driver used:
35.5

5. Create a green panel using a JPanel and JLabels. With the given input (above).

6. Mary suggests using these formulas to determining the output and the client agreed:

mpg = miles-travelled / gas-Used
fares-per-mile = fare / miles-travelled
gas-paid-per-mile = gas-purchased / miles- travelled
profit = fare – gas-purchased;

7. Joe (the IS person) suggests (because of his many years of experience with clients) better looking output by using these Java classes:

DecimalFormat dfmt = new DecimalFormat("0.##");
NumberFormat cfmt = NumberFormat.getCurrencyInstance();

8. Add a driver so you can track two drivers. Use any input for the second driver, however, keep using the same input for the first driver. Create a JPanel for each driver. Have one JPanel be a green color and the other a blue color.

9. Have the name (first and last) appear using the following format: all letters are lower case except for the first letter of the first and last name: (e.g., John Betterdriver). The input for driver one should be exactly as shown above, for example enter in the name: john ‘betterDRIVER’. The output should have the name appear as ‘John Betterdriver’.

10. Also create an icon for each driver. The image you use is up to you, however, use a different image for each driver and place the icon inside the JPanels. Make the output look good.

So those are the guidelines and here is what I have come up with so far. I need help with what i'm mssing and what I still need to have in my program. Any and all help is greatly appreciated.

// This program will Calculate the mileage statistics for a cab company in Allendale
//

import Java.awt.Color;
import Java.awt.Dimension;
import Java.text.NumberFormat;
import Java.text.DecimalFormat;
import Java.util.Scanner;

import Javax.swing.JFrame;
import Javax.swing.JLabel;
import Javax.swing.JPanel;
import Javax.swing.SwingConstrants;

public class CarTracker
{

public static void main(String[ ] args )
{

double fares1;
double gas1;
double distance1;

reader = new Scanner( System.in );

System.out.println("Enter the driver's first name");
firstname1= scan.nextline();

System.out.println("Enter the driver's last name");
lastname1= scan.nextline();

System.out.println("Enter the amount paid to the driver");
fares1= scan.nextdouble();

System.out.println("Enter the amount the driver paid for gas");
gas1= scan.nextdouble();

System.out.println("Enter the distance the driver traveled");
distance1= scan.nextdouble();

// Presents two colored panels nested within a third.

JFrame frame = new JFrame ("Nested Panels");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPane1 subPanel1 = new JPane1();
subPanel1.setPreferredSize (new dimension(175, 175));
subPanel1.setBackground (Color.green);
JLabel label1 = new JLabel ("name " + firstname1 + " " + lastname1);

//Set up primary pane
JPanel1 primary = new JPanel();
primary.setBackground (Color.blue);

frame.pack();
frame.setVisable(true);

}
}
2. Hi and welcome to this forum.

First, please read the sticky: [ALL USERS] - How to post a question

Second, you have to be more specific of what you need help with.
3. I need help with what i'm mssing
How do we know what you are missing if you don't tell us.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2010
Posts
25
Rep Power
0
I guess I am stuck with what I should do next, I know I need to add some calculations in but I am not sure how to do that.
Is there any hints you can give me that would help me out at all. Thanks for your help!
5. I need to add some calculations
Anything specific?
Not many people want to read your assignment and then look at your code and figure out what you need.
You're going to have to do that and then come up with some specific questions about how to write the code to do what your assignment needs done.

For example:
I need to compute the distance traveled. I have the speed and I have the total time but I don't know how to write an expression that multiples the speed by the time.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2010
Posts
25
Rep Power
0
I have these formulas that I know I need to set up an expression for but I am not sure how too.

mpg = miles-travelled / gas-Used
fares-per-mile = fare / miles-travelled
gas-paid-per-mile = gas-purchased / miles- travelled
profit = fare – gas-purchased
7. Those are very simple expressions to code.
The java code is in almost exactly the same format as you show them.
What happens when you put those expressions in your program?
Most of the values should be double vs int.
The variable names should not have - (the arithmetic minus operator).

Cross posted at Another forum
Last edited by NormR; October 1st, 2010 at 08:17 AM.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2010
Posts
25
Rep Power
0
profit = fares – gasPurchased;

I am getting an error on this. Can anyone help
The error says:
illegal caracter: \8211
9. For future reference, you should always use [CODE] or [highlight] tags around code samples, so as to retain the indentation and make the code more readable.

Java Code:
```// This program will Calculate the mileage statistics for a cab company in Allendale
//

import Java.awt.Color;
import Java.awt.Dimension;
import Java.text.NumberFormat;
import Java.text.DecimalFormat;
import Java.util.Scanner;

import Javax.swing.JFrame;
import Javax.swing.JLabel;
import Javax.swing.JPanel;
import Javax.swing.SwingConstrants;

public class CarTracker
{

public static void main(String[ ] args )
{

double fares1;
double gas1;
double distance1;

reader = new Scanner( System.in );

System.out.println("Enter the driver's first name");
firstname1= scan.nextline();

System.out.println("Enter the driver's last name");
lastname1= scan.nextline();

System.out.println("Enter the amount paid to the driver");
fares1= scan.nextdouble();

System.out.println("Enter the amount the driver paid for gas");
gas1= scan.nextdouble();

System.out.println("Enter the distance the driver traveled");
distance1= scan.nextdouble();

// Presents two colored panels nested within a third.

JFrame frame = new JFrame ("Nested Panels");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPane1 subPanel1 = new JPane1();
subPanel1.setPreferredSize (new dimension(175, 175));
subPanel1.setBackground (Color.green);
JLabel label1 = new JLabel ("name " + firstname1 + " " + lastname1);

//Set up primary pane
JPanel1 primary = new JPanel();
primary.setBackground (Color.blue);

frame.pack();
frame.setVisable(true);

}
}```

I would also consider whether the professor intended you to have the variables fares, gas, distance, etc. as instance variables of the class rather than local variables of the main() method.
Last edited by Schol-R-LEA; October 1st, 2010 at 04:02 PM.
10. Looking a bit closer, I'm finding a whole lot of reasons why this code won't compile as given.
• All of the imports start with a capitalized 'Java'. In imports from the standard library, only the class names will have capitalization, in almost all cases.
• Conversely, all of the Scanner methods are all lowercase, rather than camelCase. Since Java is case-sensitive, this is a showstopper.
• You substituted '1' for lowercase 'l' in several places, an old typists trick but one that simply won't work in programming.
• You never declared the types for firstname1 and lastname1.

The version of the code given below rewrites the existing code so that it compiles; it defines the variables as instance variables of the class, and adds several methods which you can use to encapsulate the various formulae you need, but other than that doesn't add any new behavior beyond what you'd written so far:
Java Code:
```// This program will Calculate the mileage statistics
// for a cab company in Allendale

import java.awt.Color;
import java.awt.Dimension;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class CarTracker {

private String firstName, lastName;
private double fares;
private double fuelUsed, distanceDriven;

public CarTracker(String first, String last) {
firstName = first;
lastName = last;
fares = 0.0;
fuelUsed = 0.0;
distanceDriven = 0.0;
}

public String driversName() {
return (firstName + ' ' + lastName);
}

fares += Math.abs(fare);
}

fuelUsed += Math.abs(fuel);
}

public void drive(double distance) {
distanceDriven += Math.abs(distance);
}

return fares;
}

return fuelUsed;
}

return distanceDriven;
}

public double fuelEfficency() {
return (fuelUsed / distanceDriven);
}

public double fareRate() {
return (fares / distanceDriven);
}

public double mileageCost(double fuelPurchased) {
return (fuelPurchased / fuelUsed);
}

public double Profit() {
return (fares - fuelUsed);
}

public static void main(String[ ] args )  {

reader = new Scanner( System.in );

System.out.println("Enter the driver's first name");

System.out.println("Enter the driver's last name");

CarTracker driver = new CarTracker(firstName, lastName);

System.out.println("Enter the amount paid to the driver");

System.out.println("Enter the amount the driver paid for gas");

System.out.println("Enter the distance the driver traveled");
driver.drive(distance);

System.out.println("Please enter the amount of gasoline the Driver used:");

// Presents two colored panels nested within a third.

JFrame frame = new JFrame ("Nested Panels");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPanel subPanel1 = new JPanel();
subPanel1.setPreferredSize (new Dimension(175, 175));
subPanel1.setBackground (Color.green);
JLabel label1 = new JLabel ("name " + driver.driversName());

//Set up primary pane
JPanel primary = new JPanel();
primary.setBackground (Color.blue);

frame.pack();
frame.setVisible(true);
}
}```

I'm being extremely generous with this; hopefully, it will point you in the right direction with the remainder of the program. To get the code without the line numbers and highlighting, click on the double-chevron button on the upper right hand side of the highlighting window.

• MrFujin agrees
Last edited by Schol-R-LEA; October 1st, 2010 at 05:21 PM.
11. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2010
Posts
25
Rep Power
0
Ok thanks for all the help that everyone has given me, I appreciate it a lot.
12. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2010
Posts
25
Rep Power
0
Ok so how do I get these to do calculations for me and then display it in my panel?

JLabel label1 = new JLabel ("Name: " + driver.driversName());
JLabel label2 = new JLabel ("MPG: " + distance + " " + fuel);
JLabel label3 = new JLabel ("Fares per mile: " + fares + " " + distance);
JLabel label4 = new JLabel ("Gas Paid Per Mile: " + gasPrice + " " + distance);
JLabel label5 = new JLabel ("Total Profit: " + fares + " " + gasPrice);

For instance-
MPG = distance/fuel
Fares per mile= fares/distance
Gas paid per mile= price/distance
profit= fares/price

• Schol-R-LEA disagrees : Whoa, I think I just got some real insight into how DaWei became the bitter SOB he is today...
13. take a look at the code posted by Schol-R-LEA, especially the functions he has provided.
When you want to make a specific calculation, you have to call the related function.
14. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2009
Posts
25
Rep Power
0

#### test

ddewrwerwe mw