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

I\'m getting compile errors, please help. I\'m suppose to create 2 gas pumps obj

ID: 3653370 • Letter: I

Question

I'm getting compile errors, please help. I'm suppose to create 2 gas pumps objects and print out the data about them using joptionpane. public class GasPump { private double pricePerGallon; private double gallons; private int fuelType; private int paymentType; private float washPrice; public static int Regular = 1; public static int MidGrade = 2; public static int SuperUnleaded = 3; public static int Diesel = 4; public static int PropaneGas = 5; public static int Hydrogen = 6; public static int Ethanol = 7; public GasPump() {pricePerGallon = 5.00;} public GasPump (double price) { pricePerGallon = price;} public void setCost(double cost) { double pricePerGallon = cost;} double getCost() { return pricePerGallon; } public setGrade(int fuelType) // i'm having errors (invalid declaration) { String fuelName; switch (fuelType) { case 1: fuelName = "Regular Unleaded"; break; case 2: fuelName = "Mid grade Unleaded"; break; case 3: fuelName = "Super Unleaded"; break; case 4: fuelName = "Diesel"; break; case 5: fuelName = "Propane Gas"; break; case 6: fuelName = "Hydrogen Gas"; break; case 7: fuelName = "Ethanol E-85"; break; default: System.out.println("Unknown fuel type" + fuelType); fuelName = "Unknown"; } } public String getGrade() { return fuelName; } double calcBill (double gallons) { return (pricePerGallon * gallons); } public void paymentMethod (int paymentType) { switch (paymentType) { case 1: payInsideMethod(); break; case 2: payOutsideMethod(); break; default: System.out.println("Error: Unknown payment type: See cashier"); } } public void payInsideMethod ( ) { System.out.println("Pay cashier inside the station"); } public void payOutsideMethod ( ) { System.out.println("Pay at the pump with credit card"); } public float washPrice (int washType) { washPrice = 0.00; switch (washType) { case 1: washPrice = 8.00; break; case 2: washPrice = 9.00; break; case 3: washPrice = 10.00; break; default: System.out.println("Error: Unknown wash type: See cashier"); } } }

Explanation / Answer


public class GasPump {
        private double pricePerGallon;
        private double gallons;
        private int fuelType;
        private int paymentType;
        private float washPrice;
        public static int Regular = 1;
        public static int MidGrade = 2;
        public static int SuperUnleaded = 3;
        public static int Diesel = 4;
        public static int PropaneGas = 5;
        public static int Hydrogen = 6;
        public static int Ethanol = 7;
        public String fuelName;
        public GasPump() {
                pricePerGallon = 5.00;
        }
        public GasPump (double price) {
                pricePerGallon = price;
        }
        public void setCost(double cost) {
                double pricePerGallon = cost;
        }
        double getCost() {
                return pricePerGallon;
        }
        // you missed the return type
        public void setGrade(int fuelType) // i'm having errors (invalid declaration)
        // declare this fuelName to be instance variable.
        {
                //String fuelName;
                switch (fuelType) {
                        case 1: this.fuelName = "Regular Unleaded"; break;
                        case 2: this.fuelName = "Mid grade Unleaded"; break;
                        case 3: this.fuelName = "Super Unleaded"; break;
                        case 4: this.fuelName = "Diesel"; break;
                        case 5: this.fuelName = "Propane Gas"; break;
                        case 6: this.fuelName = "Hydrogen Gas"; break;
                        case 7: this.fuelName = "Ethanol E-85"; break;
                        default: System.out.println("Unknown fuel type" + fuelType);
                                this.fuelName = "Unknown";
                }
        }
        public String getGrade() { return this.fuelName; }
        double calcBill (double gallons) { return (pricePerGallon * gallons); }
        public void paymentMethod (int paymentType) {
                switch (paymentType) {
                        case 1: payInsideMethod(); break;
                        case 2: payOutsideMethod(); break;
                        default: System.out.println("Error: Unknown payment type: See cashier");
                }
        }
        public void payInsideMethod ( ) { System.out.println("Pay cashier inside the station"); }
        public void payOutsideMethod ( ) { System.out.println("Pay at the pump with credit card"); }
        // method name was same as your variable inside this method
        // you have declared washPrice to be float , in java float requires a f after aconstant double value .
        public float GetwashPrice (int washType) {
                washPrice = 0.00f;
                switch (washType) {
                        case 1: washPrice = 8.00f; break;
                        case 2: washPrice = 9.00f; break;
                        case 3: washPrice = 10.00f; break;
                        default: System.out.println("Error: Unknown wash type: See cashier");
                }
                return washPrice;
        }
        public static void main(String [] args){
                GasPump g = new GasPump();
                GasPump g1 = new GasPump(20.0);
                g.setCost(15.0);
                g1.setCost(45.0);
                System.out.println("cost of g is " + g.getCost() + " and cost of g1 is " + g1.getCost());
                g.setGrade(Hydrogen);
                g1.setGrade(Diesel);
                System.out.println("grade of g is " + g.getGrade() + " and grade of g1 is " + g1.getGrade());
                double g1bill = g1.calcBill(20);
                double gbill = g.calcBill(18);
                System.out.println("bill of g for 18 gallons is " + gbill + " bill of g1 for 20 gallon is " + g1bill);
        }
}

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