Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create an interface with the motif look and feel that allows the user to enter a

ID: 3572361 • Letter: C

Question

Create an interface with the motif look and feel that allows the user to enter and record trips taken and store them in a data file on a floppy disk. Fields should include: (starting code listed below) but needs Trip date, Start destination, Ending destination, Mileage, Cost added to the end of this code

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.text.*;

import java.util.*;

public class TravelerGasolineCalculator extends JFrame implements ActionListener, EventListener

{

    //declare variables

    int totalMiles, mpg;

    double oilChange = 30.00, fuelCost, totalCost, costPerMile, fuel, total, numOfGallons;

    double superUnleadedCost=3.00, unleadedCost=2.90, leadedCost=2.50, dieselCost=4.00;

    int compactTank=13,midSizeTank=18, luxuryTank=15,suvTank=23;

    String location[] = {"New York, NY","Minneapolis, MN", "Boise, ID", "Dallas, TX", "Miami, FL", "Las Vegas, NV"};

    String locationTwo[] = {"New York, NY","Minneapolis, MN", "Boise, ID", "Dallas, TX", "Miami, FL", "Las Vegas, NV"};

    String vehicle[] = {"Compact", "Mid-Size", "Luxury", "SUV"};

    String fuelPrice[] = {"Leaded", "Unleaded", "Super Unleaded", "Diesel"};

    //COnstruct a panel for each row

    JPanel firstRow = new JPanel();

    JPanel secondRow = new JPanel();

    JPanel thirdRow = new JPanel();

    JPanel fourthRow = new JPanel();

    JPanel fifthRow = new JPanel();

    JPanel sixthRow = new JPanel();

    JPanel seventhRow = new JPanel();

    JPanel eighthRow = new JPanel();

    JPanel ninthRow = new JPanel();

    JPanel tenthRow = new JPanel();

    //Construct a panel for the fields and button

    JPanel fieldPanelOne = new JPanel();

    JPanel fieldPanelTwo = new JPanel();

    JPanel buttonPanelOne = new JPanel();

    JPanel buttonPanelTwo = new JPanel();

    //Construct labels and texts boxes

    JLabel milesLabel = new JLabel("Anticipated Miles:");

        JTextField miles = new JTextField(10);

    JLabel costPerGallonLabel = new JLabel("Cost Per Gallon");

        JTextField costPerGallon = new JTextField(10);

    JLabel milesPerGallonLabel = new JLabel("Miles Per Gallon");

        JTextField milesPerGallon = new JTextField(10);

    JLabel calcLabel = new JLabel("           Press the Calculate button");

    JLabel beginningLocationLabel = new JLabel("Beginning Location");

        JComboBox cityBox = new JComboBox(location);

    JLabel destinationLabel = new JLabel("Destination");

        JComboBox cityBoxTwo = new JComboBox(locationTwo);

    JLabel vehicleSizeLabel = new JLabel("Vehicle Size");

        JComboBox vehicleBox = new JComboBox(vehicle);

    JLabel fuelLabel = new JLabel("Fuel Type");

        JComboBox fuelBox = new JComboBox(fuelPrice);

    JLabel submitLabel = new JLabel("Press the Submit Button");

    // Construct button

    JButton calcButton = new JButton("Calculate");

    JButton submitButton = new JButton("Submit");

    JButton clearButton = new JButton("Clear");

    public static void main(String[] args)

    {

        //set the look of the interface

        try

        {

            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

        }

        catch(Exception e)

        {

            JOptionPane.showMessageDialog(null,"The Traveler's Gasoline Calculator could not set the Look and Feel.","Error", JOptionPane.INFORMATION_MESSAGE);

        }

        TravelerGasolineCalculator f = new TravelerGasolineCalculator();

        f.setSize(450,400);

        f.setTitle("Traveler's Gasoline Calculator");

        f.setResizable(true);

        f.setLocation(200,200);

        f.setVisible(true);

    }

    public TravelerGasolineCalculator()

    {

        Container c = getContentPane();

        c.setLayout((new BorderLayout()));

        fieldPanelOne.setLayout(new GridLayout(4,2));

        FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);

            firstRow.setLayout(rowSetup);

            secondRow.setLayout(rowSetup);

            thirdRow.setLayout(rowSetup);

            fourthRow.setLayout(rowSetup);

        fieldPanelTwo.setLayout(new GridLayout(5,2));

            fifthRow.setLayout(rowSetup);

            sixthRow.setLayout(rowSetup);

            seventhRow.setLayout(rowSetup);

            eighthRow.setLayout(rowSetup);

            ninthRow.setLayout(rowSetup);

            tenthRow.setLayout(rowSetup);

        buttonPanelOne.setLayout(new FlowLayout(FlowLayout.CENTER));

        //Add Fields to rows

        firstRow.add(milesLabel);

        firstRow.add(miles);

        secondRow.add(costPerGallonLabel);

        secondRow.add(costPerGallon);

        thirdRow.add(milesPerGallonLabel);

        thirdRow.add(milesPerGallon);

        fourthRow.add(calcLabel);

        fifthRow.add(beginningLocationLabel);

        fifthRow.add(cityBox);

        sixthRow.add(destinationLabel);

        sixthRow.add(cityBoxTwo);

        seventhRow.add(vehicleSizeLabel);

        seventhRow.add(vehicleBox);

        eighthRow.add(fuelLabel);

        eighthRow.add(fuelBox);

        ninthRow.add(submitLabel);

        //Adds rows to panel

        fieldPanelOne.add(firstRow);

        fieldPanelOne.add(secondRow);

        fieldPanelOne.add(thirdRow);

        fieldPanelOne.add(fourthRow);

        fieldPanelTwo.add(fifthRow);

        fieldPanelTwo.add(sixthRow);

        fieldPanelTwo.add(seventhRow);

        fieldPanelTwo.add(eighthRow);

        fieldPanelTwo.add(ninthRow);

        //adds button to panel

        buttonPanelOne.add(calcButton);

        buttonPanelOne.add(submitButton);

        buttonPanelOne.add(clearButton);

        //Adds panels to frame

        c.add(fieldPanelOne, BorderLayout.NORTH);

        c.add(fieldPanelTwo, BorderLayout.CENTER);

        c.add(buttonPanelOne, BorderLayout.SOUTH);

        //adds funtion to buttons

        calcButton.addActionListener(this);

        submitButton.addActionListener(this);

        clearButton.addActionListener(this);

        //adds function to dropdown lists

        cityBox.addActionListener(this);

        cityBoxTwo.addActionListener(this);

        vehicleBox.addActionListener(this);

        fuelBox.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e)

    {

        //Calculates total cost with or without an oil change

        fuel = Double.parseDouble (costPerGallon.getText());

        mpg = Integer.parseInt (milesPerGallon.getText());

        total = Double.parseDouble (miles.getText());

        numOfGallons = total/mpg;

        totalCost = numOfGallons*fuel;

        costPerMile = totalCost/total;

        DecimalFormat twoDigit = new DecimalFormat("$#,###.##");

                if (total < 3000)

                {

                    JOptionPane.showMessageDialog(null,"Your approximate cost of the trip is " + twoDigit.format(totalCost) + ".","Approximate Cost",JOptionPane.INFORMATION_MESSAGE);

                }

                else

                {

                    totalCost = numOfGallons * fuel + oilChange;

                    costPerMile = totalCost/total;

                    JOptionPane.showMessageDialog(null,"Your total cost of the trip is " + twoDigit.format(totalCost) + ".","Cost With Oil Change", JOptionPane.INFORMATION_MESSAGE);

                }

    }

     public void itemStateChanged(ItemEvent ie)

     {

     }

}

Explanation / Answer

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;

public class TravelerGasolineCalculator extends JFrame implements ActionListener, EventListener
{
//declare variables
int totalMiles, mpg;
double oilChange = 30.00, fuelCost, totalCost, costPerMile, fuel, total, numOfGallons;
double superUnleadedCost=3.00, unleadedCost=2.90, leadedCost=2.50, dieselCost=4.00;
int compactTank=13,midSizeTank=18, luxuryTank=15,suvTank=23;
String location[] = {"New York, NY","Minneapolis, MN", "Boise, ID", "Dallas, TX", "Miami, FL", "Las Vegas, NV"};
String locationTwo[] = {"New York, NY","Minneapolis, MN", "Boise, ID", "Dallas, TX", "Miami, FL", "Las Vegas, NV"};
String vehicle[] = {"Compact", "Mid-Size", "Luxury", "SUV"};
String fuelPrice[] = {"Leaded", "Unleaded", "Super Unleaded", "Diesel"};

//COnstruct a panel for each row
JPanel firstRow = new JPanel();
JPanel secondRow = new JPanel();
JPanel thirdRow = new JPanel();
JPanel fourthRow = new JPanel();
JPanel fifthRow = new JPanel();
JPanel sixthRow = new JPanel();
JPanel seventhRow = new JPanel();
JPanel eighthRow = new JPanel();
JPanel ninthRow = new JPanel();
JPanel tenthRow = new JPanel();


//Construct a panel for the fields and button
JPanel fieldPanelOne = new JPanel();
JPanel fieldPanelTwo = new JPanel();
JPanel buttonPanelOne = new JPanel();
JPanel buttonPanelTwo = new JPanel();

//Construct labels and texts boxes
JLabel milesLabel = new JLabel("Anticipated Miles:");
JTextField miles = new JTextField(10);
JLabel costPerGallonLabel = new JLabel("Cost Per Gallon");
JTextField costPerGallon = new JTextField(10);
JLabel milesPerGallonLabel = new JLabel("Miles Per Gallon");
JTextField milesPerGallon = new JTextField(10);
JLabel calcLabel = new JLabel(" Press the Calculate button");
JLabel beginningLocationLabel = new JLabel("Beginning Location");
JComboBox cityBox = new JComboBox(location);
JLabel destinationLabel = new JLabel("Destination");
JComboBox cityBoxTwo = new JComboBox(locationTwo);
JLabel vehicleSizeLabel = new JLabel("Vehicle Size");
JComboBox vehicleBox = new JComboBox(vehicle);
JLabel fuelLabel = new JLabel("Fuel Type");
JComboBox fuelBox = new JComboBox(fuelPrice);
JLabel submitLabel = new JLabel("Press the Submit Button");



// Construct button
JButton calcButton = new JButton("Calculate");
JButton submitButton = new JButton("Submit");
JButton clearButton = new JButton("Clear");

public static void main(String[] args)
{
//set the look of the interface
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"The Traveler's Gasoline Calculator could not set the Look and Feel.","Error", JOptionPane.INFORMATION_MESSAGE);
}

TravelerGasolineCalculator f = new TravelerGasolineCalculator();
f.setSize(450,400);
f.setTitle("Traveler's Gasoline Calculator");
f.setResizable(true);
f.setLocation(200,200);
f.setVisible(true);
}

public TravelerGasolineCalculator()
{
Container c = getContentPane();
c.setLayout((new BorderLayout()));
fieldPanelOne.setLayout(new GridLayout(4,2));
FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
firstRow.setLayout(rowSetup);
secondRow.setLayout(rowSetup);
thirdRow.setLayout(rowSetup);
fourthRow.setLayout(rowSetup);
fieldPanelTwo.setLayout(new GridLayout(5,2));
fifthRow.setLayout(rowSetup);
sixthRow.setLayout(rowSetup);
seventhRow.setLayout(rowSetup);
eighthRow.setLayout(rowSetup);
ninthRow.setLayout(rowSetup);
tenthRow.setLayout(rowSetup);
buttonPanelOne.setLayout(new FlowLayout(FlowLayout.CENTER));

//Add Fields to rows
firstRow.add(milesLabel);
firstRow.add(miles);

secondRow.add(costPerGallonLabel);
secondRow.add(costPerGallon);

thirdRow.add(milesPerGallonLabel);
thirdRow.add(milesPerGallon);

fourthRow.add(calcLabel);

fifthRow.add(beginningLocationLabel);
fifthRow.add(cityBox);

sixthRow.add(destinationLabel);
sixthRow.add(cityBoxTwo);

seventhRow.add(vehicleSizeLabel);
seventhRow.add(vehicleBox);

eighthRow.add(fuelLabel);
eighthRow.add(fuelBox);

ninthRow.add(submitLabel);


//Adds rows to panel
fieldPanelOne.add(firstRow);
fieldPanelOne.add(secondRow);
fieldPanelOne.add(thirdRow);
fieldPanelOne.add(fourthRow);
fieldPanelTwo.add(fifthRow);
fieldPanelTwo.add(sixthRow);
fieldPanelTwo.add(seventhRow);
fieldPanelTwo.add(eighthRow);
fieldPanelTwo.add(ninthRow);

//adds button to panel
buttonPanelOne.add(calcButton);
buttonPanelOne.add(submitButton);
buttonPanelOne.add(clearButton);

//Adds panels to frame
c.add(fieldPanelOne, BorderLayout.NORTH);
c.add(fieldPanelTwo, BorderLayout.CENTER);
c.add(buttonPanelOne, BorderLayout.SOUTH);

//adds funtion to buttons
calcButton.addActionListener(this);
submitButton.addActionListener(this);
clearButton.addActionListener(this);

//adds function to dropdown lists
cityBox.addActionListener(this);
cityBoxTwo.addActionListener(this);
vehicleBox.addActionListener(this);
fuelBox.addActionListener(this);

}

public void actionPerformed(ActionEvent e)
{
//Calculates total cost with or without an oil change
fuel = Double.parseDouble (costPerGallon.getText());
mpg = Integer.parseInt (milesPerGallon.getText());
total = Double.parseDouble (miles.getText());
numOfGallons = total/mpg;
totalCost = numOfGallons*fuel;
costPerMile = totalCost/total;
  
  

DecimalFormat twoDigit = new DecimalFormat("$#,###.##");

if (total < 3000)
{
JOptionPane.showMessageDialog(null,"Your approximate cost of the trip is " + twoDigit.format(totalCost) + ".","Approximate Cost",JOptionPane.INFORMATION_MESSAGE);
}

else
{
totalCost = numOfGallons * fuel + oilChange;
costPerMile = totalCost/total;

JOptionPane.showMessageDialog(null,"Your total cost of the trip is " + twoDigit.format(totalCost) + ".","Cost With Oil Change", JOptionPane.INFORMATION_MESSAGE);
}
  
try
{
BufferedWriter br = new BufferedWriter(new PrintWriter(new File("results.txt")));
br.write("Source City DEst City Miles Vehicle Fuel in Gallons Cost Cos/Mile");
br.newLine();
br.write(cityBox.getSelectedItem()+" "+cityBoxTwo.getSelectedItem()+" "+total+" "+vehicleBox.getSelectedItem()+" "+numOfGallons+" "+twoDigit.format(totalCost)+" "+twoDigit.format(costPerMile));
  
br.close();
}
catch(Exception ex)
{
System.out.println(e);
  
}
  
}

public void itemStateChanged(ItemEvent ie)
{
}
}

The program now writes the trip to file

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote