I am having a problem getting my calculations to work here. I managed to get the objects in place. Any suggestions??

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class TravelCostEstimator Applet implements ActionListener
{
//This part defines how the program will look
           private Choice choice;
           Label companyLabel = new Label("Traveler's Gasoline Calculator" );
           Label vehicleLabel = new Label("Enter Your Type of Vehicle");
           Label fuelLabel = new Label("Enter Your Type / Grade of Fuel");
           Label milesLabel = new Label("Enter the Number of Miles You Will Travel");
           TextField milesEntered = new TextField(9);
           Button calcButton =  new Button("Calculate");
           Label outputLabel = new Label("Click to calculate how much your trip will cost.");
           TextArea outputArea = new TextArea("Your trip costs are",5,80, TextArea.SCROLLBARS_NONE);

//This serves as the main method used by the main class
   public static void main(String[] args)
   {
   double totalOilChangeCost, totalFuelCost, totalTravelExpense, fuelPrice;
   double oilChangeCost = 30.00;
   double fuelSupeUnleadedCost = 3.00;
   double fuelUnleadedCost = 2.90;
   double fuelLeadedCost = 2.50;
   double fuelDieselCost = 4.00;
   int distanceTravel, milesPerTank, tankCapacity, milesEntered;
   int milesPerGallon = 15;
   int oilChangeRequired = 3000;
   int vehicleCompactTankCapacity = 13;
   int vehicleMidSizeTankCapacity = 18;
   int vehicleLuxuryTankCapacity = 15;
   int vehicleSUVTankCapacity = 23;
   char vehicleLabel, fuelLabel, vehicleLabelSelected, fuelLabelSelected;


//The following calls the methods
   totalOilChangeCost = getTotalOilChangeCost() ; 
   totalFuelCost = getTotalFuelCost() ; 
   totalTravelExpense = getTotalTravelExpense() ; 
   fuelPrice = getfuelPrice () ; 
   distanceTravel = getDistanceTravel() ; 
   milesPerTank = getMilesPerTank() ;
   tankCapacity = getTankCapacity() ; 
   totalTravelExpense = getTotalTravelExpense();
   totalOilChangeCost = getTotalOilChangeCost();
   }




//The following defines the methods from above

public static double getTotalOilChangeCost()
   {
           if(milesEntered <3000) totalOilChange = 0;
               else totalOilChange = milesEntered/3000 *30;
       double totalOilChangeCost = (distanceTravel/3000)*30;
       totalOilChangeCost = Double.parseDouble();
       return totalOilChangeCost;
   }
public static double getTotalFuelCost()
   {
       double totalGasCost = (distanceTravel/milesPerTank)*gasPrice;
       totalFuelCost = Double.parseDouble();
       return totalFuelCost;
   }
public static double getTotalTravelExpense()
   {
       double totalTravelExpense = totalFuelCost + totalOilChange;
       totalTravelExpense = Double.parseDouble();
       return totalTravelExpense;
   }
public static double getFuelPrice ()
   {
           if (fuelLabel.choice == "Super Unleaded") fuelLabelSelected = 3.00;
               if (fuelLabel.choice == "Unleaded") fuelLabelSelected = 2.90;
                   if (fuelLabel.choice == "Leaded") fuelLabelSelected = 2.50;
                       if (fuelLabel.choice == "Diesel") fuelLabelSelected = 4.00;
       double fuelPrice = fuelLabelSelected;

   }
public static int getDistanceTravel()
   {
double milesEntered = Double.parseDouble(milesEntered.getText());
           if (milesEntered >=0) return distanceTravel;
       int distanceTravel = milesEntered;
   }
public static int getMilesPerTank()
   {
       int milesPerTank = tankCapacity * 15;
   }
public static int getTankCapacity()
   {
           if (vehicleLabel.choice == "Compact") vehicleLabelSelected = 13;
               if (vehicleLabel.choice == "Mid-Size") vehicleLabelSelected = 18;
                   if (vehicleLabel.choice == "Luxury") vehicleLabelSelected = 15;
                       if (vehicleLabel.choice == "SUV") vehicleLabelSelected = 23;
       int tankCapacity = vehicleTypeSelected;
   }

//This part makes room for the input/output of the form
public void init()
   {
           setSize(600, 400);
           setLayout(new FlowLayout(FlowLayout.LEFT, 45, 70));
           setForeground(Color.black);
           setFont(new Font("sansserif", Font.PLAIN, 14));
           add(companyLabel);
           add(vehicleLabel);
           choice = new Choice();
           choice.addItem("Compact");
           choice.addItem("Mid-Size");
           choice.addItem("Luxury");
           choice.addItem("SUV");
           add(choice);
           add(fuelLabel);
           choice = new Choice();
           choice.addItem("Super Unleaded");
           choice.addItem("Unleaded");
           choice.addItem("Leaded");
           choice.addItem("Diesel");
           add(choice);
           add(milesLabel);
           add(milesEntered);
           add(calcButton);
           calcButton.addActionListener(this);
           add(outputLabel);
           add(outputArea);

   }

public void output(double totalOilChangeCost, double totalTravelExpense)
{
   DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
   outputArea.setText("Your total oil changes will cost " + twoDigits.format(totalOilChangeCost) + "Your entire trip will cost" + twoDigits.format(totalTravelExpense));
}

public void actionPerformed(ActionEvent buttons)
   {

if (buttons.getSource() == calcButton)
   {

   }

}

Recommended Answers

All 7 Replies

Repost using code tags:

[code=JAVA] // paste code here

[/code]

Need more details too. What isn't calculating right? What's the input? What's the output? What should the correct output be?

It is a calculator used to figure out how expensive a traveler's trip will be based on vehicle entered, gas mileage entered, type of fuel entered, and whether or not an oil change will be needed. I am not sure what calculations aren't working correctly as for some reason my computer isn't letting me run the compiled applet.

code=java

/import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class TravelCostEstimator Applet implements ActionListener
{
//This part defines how the program will look
private Choice choice;
Label companyLabel = new Label("Traveler's Gasoline Calculator" );
Label vehicleLabel = new Label("Enter Your Type of Vehicle");
Label fuelLabel = new Label("Enter Your Type / Grade of Fuel");
Label milesLabel = new Label("Enter the Number of Miles You Will Travel");
TextField milesEntered = new TextField(9);
Button calcButton = new Button("Calculate");
Label outputLabel = new Label("Click to calculate how much your trip will cost.");
TextArea outputArea = new TextArea("Your trip costs are",5,80, TextArea.SCROLLBARS_NONE);

//This serves as the main method used by the main class
public static void main(String[] args)
{
double totalOilChangeCost, totalFuelCost, totalTravelExpense, fuelPrice;
double oilChangeCost = 30.00;
double fuelSupeUnleadedCost = 3.00;
double fuelUnleadedCost = 2.90;
double fuelLeadedCost = 2.50;
double fuelDieselCost = 4.00;
int distanceTravel, milesPerTank, tankCapacity, milesEntered;
int milesPerGallon = 15;
int oilChangeRequired = 3000;
int vehicleCompactTankCapacity = 13;
int vehicleMidSizeTankCapacity = 18;
int vehicleLuxuryTankCapacity = 15;
int vehicleSUVTankCapacity = 23;
char vehicleLabel, fuelLabel, vehicleLabelSelected, fuelLabelSelected;


//The following calls the methods
totalOilChangeCost = getTotalOilChangeCost() ;
totalFuelCost = getTotalFuelCost() ;
totalTravelExpense = getTotalTravelExpense() ;
fuelPrice = getfuelPrice () ;
distanceTravel = getDistanceTravel() ;
milesPerTank = getMilesPerTank() ;
tankCapacity = getTankCapacity() ;
totalTravelExpense = getTotalTravelExpense();
totalOilChangeCost = getTotalOilChangeCost();
}


//The following defines the methods from above

public static double getTotalOilChangeCost()
{
if(milesEntered <3000) totalOilChange = 0;
else totalOilChange = milesEntered/3000 *30;
double totalOilChangeCost = (distanceTravel/3000)*30;
totalOilChangeCost = Double.parseDouble();
return totalOilChangeCost;
}
public static double getTotalFuelCost()
{
double totalGasCost = (distanceTravel/milesPerTank)*gasPrice;
totalFuelCost = Double.parseDouble();
return totalFuelCost;
}
public static double getTotalTravelExpense()
{
double totalTravelExpense = totalFuelCost + totalOilChange;
totalTravelExpense = Double.parseDouble();
return totalTravelExpense;
}
public static double getFuelPrice ()
{
if (fuelLabel.choice == "Super Unleaded") fuelLabelSelected = 3.00;
if (fuelLabel.choice == "Unleaded") fuelLabelSelected = 2.90;
if (fuelLabel.choice == "Leaded") fuelLabelSelected = 2.50;
if (fuelLabel.choice == "Diesel") fuelLabelSelected = 4.00;
double fuelPrice = fuelLabelSelected;

}
public static int getDistanceTravel()
{
double milesEntered = Double.parseDouble(milesEntered.getText());
if (milesEntered >=0) return distanceTravel;
int distanceTravel = milesEntered;
}
public static int getMilesPerTank()
{
int milesPerTank = tankCapacity * 15;
}
public static int getTankCapacity()
{
if (vehicleLabel.choice == "Compact") vehicleLabelSelected = 13;
if (vehicleLabel.choice == "Mid-Size") vehicleLabelSelected = 18;
if (vehicleLabel.choice == "Luxury") vehicleLabelSelected = 15;
if (vehicleLabel.choice == "SUV") vehicleLabelSelected = 23;
int tankCapacity = vehicleTypeSelected;
}

//This part makes room for the input/output of the form
public void init()
{
setSize(600, 400);
setLayout(new FlowLayout(FlowLayout.LEFT, 45, 70));
setForeground(Color.black);
setFont(new Font("sansserif", Font.PLAIN, 14));
add(companyLabel);
add(vehicleLabel);
choice = new Choice();
choice.addItem("Compact");
choice.addItem("Mid-Size");
choice.addItem("Luxury");
choice.addItem("SUV");
add(choice);
add(fuelLabel);
choice = new Choice();
choice.addItem("Super Unleaded");
choice.addItem("Unleaded");
choice.addItem("Leaded");
choice.addItem("Diesel");
add(choice);
add(milesLabel);
add(milesEntered);
add(calcButton);
calcButton.addActionListener(this);
add(outputLabel);
add(outputArea);

}

public void output(double totalOilChangeCost, double totalTravelExpense)
{
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
outputArea.setText("Your total oil changes will cost " + twoDigits.format(totalOilChangeCost) + "Your entire trip will cost" + twoDigits.format(totalTravelExpense));
}

public void actionPerformed(ActionEvent buttons)
{

if (buttons.getSource() == calcButton)

}

/code

Code tags. Like this. And you need some formatting/indentation. The program you posted doesn't come close to compiling. At minimum, you have a stray mark and your missing the word extends:

Here's your code, formatted and with code tags, with the slash removed and "extends" added. You have a bunch of variables that aren't defined and at least one class that you use, but do not import.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class TravelCostEstimator extends Applet implements ActionListener
{
//This part defines how the program will look
    private Choice choice;
    Label companyLabel = new Label("Traveler's Gasoline Calculator");
    Label vehicleLabel = new Label("Enter Your Type of Vehicle");
    Label fuelLabel = new Label("Enter Your Type / Grade of Fuel");
    Label milesLabel = new Label("Enter the Number of Miles You Will Travel");
    TextField milesEntered = new TextField(9);
    Button calcButton = new Button("Calculate");
    Label outputLabel = new Label("Click to calculate how much your trip will cost.");
    TextArea outputArea = new TextArea("Your trip costs are", 5, 80, TextArea.SCROLLBARS_NONE);

//This serves as the main method used by the main class
    public static void main(String[] args)
    {
        double totalOilChangeCost, totalFuelCost, totalTravelExpense, fuelPrice;
        double oilChangeCost = 30.00;
        double fuelSupeUnleadedCost = 3.00;
        double fuelUnleadedCost = 2.90;
        double fuelLeadedCost = 2.50;
        double fuelDieselCost = 4.00;
        int distanceTravel, milesPerTank, tankCapacity, milesEntered;
        int milesPerGallon = 15;
        int oilChangeRequired = 3000;
        int vehicleCompactTankCapacity = 13;
        int vehicleMidSizeTankCapacity = 18;
        int vehicleLuxuryTankCapacity = 15;
        int vehicleSUVTankCapacity = 23;
        char vehicleLabel, fuelLabel, vehicleLabelSelected, fuelLabelSelected;


//The following calls the methods
        totalOilChangeCost = getTotalOilChangeCost();
        totalFuelCost = getTotalFuelCost();
        totalTravelExpense = getTotalTravelExpense();
        fuelPrice = getfuelPrice();
        distanceTravel = getDistanceTravel();
        milesPerTank = getMilesPerTank();
        tankCapacity = getTankCapacity();
        totalTravelExpense = getTotalTravelExpense();
        totalOilChangeCost = getTotalOilChangeCost();
    }

//The following defines the methods from above
    public static double getTotalOilChangeCost()
    {
        if (milesEntered < 3000)
        {
            totalOilChange = 0;
        }
        else
        {
            totalOilChange = milesEntered / 3000 * 30;
        }
        double totalOilChangeCost = (distanceTravel / 3000) * 30;
        totalOilChangeCost = Double.parseDouble();
        return totalOilChangeCost;
    }

    public static double getTotalFuelCost()
    {
        double totalGasCost = (distanceTravel / milesPerTank) * gasPrice;
        totalFuelCost = Double.parseDouble();
        return totalFuelCost;
    }

    public static double getTotalTravelExpense()
    {
        double totalTravelExpense = totalFuelCost + totalOilChange;
        totalTravelExpense = Double.parseDouble();
        return totalTravelExpense;
    }

    public static double getFuelPrice()
    {
        if (fuelLabel.choice == "Super Unleaded")
        {
            fuelLabelSelected = 3.00;
        }
        if (fuelLabel.choice == "Unleaded")
        {
            fuelLabelSelected = 2.90;
        }
        if (fuelLabel.choice == "Leaded")
        {
            fuelLabelSelected = 2.50;
        }
        if (fuelLabel.choice == "Diesel")
        {
            fuelLabelSelected = 4.00;
        }
        double fuelPrice = fuelLabelSelected;

    }

    public static int getDistanceTravel()
    {
        double milesEntered = Double.parseDouble(milesEntered.getText());
        if (milesEntered >= 0)
        {
            return distanceTravel;
        }
        int distanceTravel = milesEntered;
    }

    public static int getMilesPerTank()
    {
        int milesPerTank = tankCapacity * 15;
    }

    public static int getTankCapacity()
    {
        if (vehicleLabel.choice == "Compact")
        {
            vehicleLabelSelected = 13;
        }
        if (vehicleLabel.choice == "Mid-Size")
        {
            vehicleLabelSelected = 18;
        }
        if (vehicleLabel.choice == "Luxury")
        {
            vehicleLabelSelected = 15;
        }
        if (vehicleLabel.choice == "SUV")
        {
            vehicleLabelSelected = 23;
        }
        int tankCapacity = vehicleTypeSelected;
    }

//This part makes room for the input/output of the form
    public void init()
    {
        setSize(600, 400);
        setLayout(new FlowLayout(FlowLayout.LEFT, 45, 70));
        setForeground(Color.black);
        setFont(new Font("sansserif", Font.PLAIN, 14));
        add(companyLabel);
        add(vehicleLabel);
        choice = new Choice();
        choice.addItem("Compact");
        choice.addItem("Mid-Size");
        choice.addItem("Luxury");
        choice.addItem("SUV");
        add(choice);
        add(fuelLabel);
        choice = new Choice();
        choice.addItem("Super Unleaded");
        choice.addItem("Unleaded");
        choice.addItem("Leaded");
        choice.addItem("Diesel");
        add(choice);
        add(milesLabel);
        add(milesEntered);
        add(calcButton);
        calcButton.addActionListener(this);
        add(outputLabel);
        add(outputArea);

    }

    public void output(double totalOilChangeCost, double totalTravelExpense)
    {
        DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
        outputArea.setText("Your total oil changes will cost " + twoDigits.format(totalOilChangeCost) + "Your entire trip will cost" + twoDigits.format(totalTravelExpense));
    }

    public void actionPerformed(ActionEvent buttons)
    {

        if (buttons.getSource() == calcButton)
        {

        }

}

So, you shouldn't be getting incorrect results. You should get no results at all because what you posted shouldn't compile. Perhaps there is code that you didn't post?

I will work on what you have mentioned and repost. Thanks for the direction, as you can see I am very new to programming.

I made some changes but still can't seem to get anything going here. Any suggestions on what I am doing wrong?

//import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 
public class TravelCostEstimator extends Applet implements ActionListener
{

    private Choice choice;
    Label companyLabel = new Label("Traveler's Gasoline Calculator");
    Label decribeLabel = new Label("This program calculates fuel costs for trips based on vehicle and fuel types");
    Label vehicleLabel = new Label("Enter Your Type of Vehicle");
    Label fuelLabel = new Label("Enter Your Type / Grade of Fuel");
    Label milesLabel = new Label("Enter the Number of Miles You Will Travel");
    TextField milesEntered = new TextField(9);
    Button calcButton = new Button("Calculate");
    Label outputLabel = new Label("Click to calculate how much your trip will cost.");
    TextArea outputArea = new TextArea("Your trip costs are", 5, 80, TextArea.SCROLLBARS_NONE);
 

    public static void main(String[] args)
    {
        int distanceTravel, milesPerTank, tankCapacity, milesEntered;
        int milesPerGallon = 15;
        int oilChangeRequired = 3000;
        int vehicleCompactTankCapacity = 13;
        int vehicleMidSizeTankCapacity = 18;
        int vehicleLuxuryTankCapacity = 15;
        int vehicleSUVTankCapacity = 23;
        double totalOilChangeCost, totalFuelCost, totalTravelExpense, fuelPrice;
        double fuelLeadedCost = 2.50;
        double fuelUnleadedCost = 2.90;
	double fuelSuperUnleadedCost = 3.00;
        double fuelDieselCost = 4.00;
        double oilChangeCost = 30.00;
 
 

        totalOilChangeCost = getTotalOilChangeCost();
        totalFuelCost = getTotalFuelCost();
        totalTravelExpense = getTotalTravelExpense();
        fuelPrice = getfuelPrice();
        distanceTravel = getDistanceTravel();
        milesPerTank = getMilesPerTank();
        tankCapacity = getTankCapacity();
    }
 

    public static double getTotalOilChangeCost()
    {
        if (milesEntered < 3000) then
            totalOilChange = 0;
        
        else
           totalOilChange = milesEntered / 3000 * 30;
        
        double totalOilChangeCost = (distanceTravel / 3000) * 30;
        totalOilChangeCost = Double.parseDouble();
        return totalOilChangeCost;
    }
 
    public static double getTotalFuelCost()
    {
        double totalFuelCost = (distanceTravel / milesPerTank) * fuelPrice;
        totalFuelCost = Double.parseDouble();
        return totalFuelCost;
    }
 
    public static double getTotalTravelExpense()
    {
        double totalTravelExpense = totalFuelCost + totalOilChangeCost;
        totalTravelExpense = Double.parseDouble();
        return totalTravelExpense;
    }
 
    public static double getFuelPrice()
    {
        if (fuelLabel.choice == "Super Unleaded") then
            fuelLabelSelected = 3.00;
        
        if (fuelLabel.choice == "Unleaded") then
            fuelLabelSelected = 2.90;
        
        if (fuelLabel.choice == "Leaded") then
            fuelLabelSelected = 2.50;
        
        if (fuelLabel.choice == "Diesel") then
            fuelLabelSelected = 4.00;
        
        double fuelPrice = fuelLabelSelected;
    }
 
    public static int getDistanceTravel()
    {
        double milesEntered = Double.parseDouble(milesEntered.getText());
        if (milesEntered >= 0) then
            return distanceTravel;
        
        int distanceTravel = milesEntered;
    }
 
    public static int getMilesPerTank()
    {
        int milesPerTank = tankCapacity * 15;
    }
 
    public static int getTankCapacity()
    {
        if (vehicleLabel.choice == "Compact") then
            vehicleLabelSelected = 13;
        
        if (vehicleLabel.choice == "Mid-Size") then
            vehicleLabelSelected = 18;
        
        if (vehicleLabel.choice == "Luxury") then
            vehicleLabelSelected = 15;
        
        if (vehicleLabel.choice == "SUV") then
            vehicleLabelSelected = 23;
        
        int tankCapacity = vehicleTypeSelected;
    }
 
    public void init()
    {
        setSize(600, 400);
        setLayout(new FlowLayout(FlowLayout.LEFT, 45, 70));
        setForeground(Color.black);
        setFont(new Font("sansserif", Font.PLAIN, 14));
        add(companyLabel);
        add(describeLabel);
        add(vehicleLabel);
        choice = new Choice();
        choice.addItem("Compact");
        choice.addItem("Mid-Size");
        choice.addItem("Luxury");
        choice.addItem("SUV");
        add(choice);
        add(fuelLabel);
        choice = new Choice();
        choice.addItem("Leaded");
        choice.addItem("Unleaded");
        choice.addItem("Super Unleaded");
        choice.addItem("Diesel");
        add(choice);
        add(milesLabel);
        add(milesEntered);
        add(calcButton);
        calcButton.addActionListener(this);
        add(outputLabel);
        add(outputArea);
 
    }
 
    public void output(double totalOilChangeCost, double totalTravelExpense)
    {
        DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
        outputArea.setText("Your total oil changes will cost " + twoDigits.format(totalOilChangeCost) + .""Your entire trip will cost" + twoDigits.format(totalTravelExpense));
    }
 
    public void actionPerformed(ActionEvent buttons)
    {
 
        if (buttons.getSource() == calcButton)
    }

Lots of problems. Start commenting things out, one function at a time till everything compiles. Comment out the "implements ActionListener" for now. Add an ending bracket.

A lot of the problems come from improper use of the word "static". If you don't know the difference between static and non-static variables, google "java static variable" and "java static function". If you try to use a non-static variable inside of a static function, you're going to have problems.

Another problem is confusing a TextField with a number. You can't assign a double to equal a TextField. You have to retrieve the contents of the text field using getText () , then you have to parse that string into a double using the parseDouble function. You can't just use it like a number like you try to do in lines 50 and 54.

There is no "then" in an "if" statement (line 50).

Go through the code, comment things out, fix an error at a time, then uncomment code, fix the errors, recompile. If it works, uncomment a little more, etc. You have too many errors to deal with at once.

Vernon already gave you great advice. . I noticed you have the following line of code:

double totalOilChangeCost = (distanceTravel / 3000) * 30;

Since distanceTravel is an integer, and 3000 and 30 are integers, the whole expression will, I think, result in an integer. You can use a println to test if that is true. Is that what you wanted?

In any case, you should use constants where applicable. You have a constant for 3000 - oilChangeRequired, and you have one for 30 also - oilChangeCost. Use them because it makes your code more clear and more understandable, and if the constant was to change in the future (lets say oil changes go up to $45) it would be easier to change in the code:

double totalOilChangeCost = (distanceTravel / oilChangeRequired) * oilChangeCost;

which would result in a double because oilChangeCost and oilChangeRequired were declared as doubles to begin with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.