Question

I have this class determines how much petrol is being purchased and the cost. The gas...

I have this class determines how much petrol is being purchased and the cost. The gas is divided between regular, premium, and super unleaded. The program reads (R, P, or S) that designates which kind of gasoline was purchased.

class Petrol

{

private static final double Premium = 0;

private static final double Super = 0;

char gas;

String type;

double amount, cost;

private String free;

private double regular;

Petrol(char ch, double a)

{

gas = ch;

amount = a;

}

void calculateCost()

{

switch(gas)

{

case 'r':

case 'R':

      type = "Regular";

      cost = amount * regular;

      if (amount > 10)

            free = REGULAR_WASH1;

break;

case 'p':

case 'P':

      type = "Premium";

      cost = amount * Premium;

      if (amount > 10)

            free = REGULAR_WASH2;

break;

case 's':

case 'S':

      type = "Super";

      cost = amount * Super;

      if (amount > 10)

            free = REGULAR_WASH3;

break;

default:

            type = "Unkown";

            break;

}

}

public String toString()

{

      if (type.equals("Unknown"))

            return "Petrol is " + type + " cannot be served";

      else

            return "The type of petrol that will be served is " + type

                  +"\nYour bill is: $" + cost + "\n " + free;

}

I also have the test class below:

import javax.swing.JOptionPane;

class TestPetrol

{

public static void main(String[] arg)

{

String str = JOptionPane.showInputDialog("COP2210 Petrol Station"

+ "\nEnter the type of petrol"

+ "\nr/R - for regular gas"

+ "\np/P - for premium gas"

+ "\ns/S - for super gas");

char ch = str.charAt(0);

str = JOptionPane.showInputDialog("Enter the amount of gas to be purchased");

float amount = Float.parseFloat (str);

Petrol p = new Petrol(ch, amount);

p.calculateCost();

JOptionPane.showMessageDialog(null, p.toString());

}

}

The problem I’m having is the program needs to offer a reward after buying 10 gallons of gas. That reward being a car wash. I have to add the declarations below but I don’t know where to place them without getting an error or the amount coming out to zero.

static final float REGULAR_GAS                 = 2.99f;

static final float PREMIUM_GAS               = 3.15;

static final float SUPER_GAS                       = 3.25;

               

static final String REGULAR_WASH = "You get free wash excluding under carriage and wheels";

static final String REGULAR_WASH = "You get free wash excluding under carriage";

static final String REGULAR_WASH = "You get free super wash";

When I run the program, it should determine whether or not the customer gets a free wash

and it calculate the cost for petrol purchased. If I can get help on where the place these lines to get the program to run properly I’d appreciate it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution: The variable should be placed above the Petrol Class Constructor as you can see in the below code.

Program Code to Copy:

Petrol.java

class Petrol {

    static final float REGULAR_GAS = 2.99f;

    static final float PREMIUM_GAS = (float) 3.15;

    static final float SUPER_GAS = (float) 3.25;

    final String REGULAR_WASH1 = "You get free wash excluding under carriage and wheels";

    final String REGULAR_WASH2 = "You get free wash excluding under carriage";

    final String REGULAR_WASH3 = "You get free super wash";


    char gas;

    String type;

    double amount, cost;

    private String free = "";

    Petrol(char ch, double a) {

        gas = ch;

        amount = a;

    }

    void calculateCost() {

        switch (gas) {

            case 'r':

            case 'R':

                type = "Regular";

                cost = amount * REGULAR_GAS;

                if (amount > 10)

                    this.free = REGULAR_WASH1;

                break;

            case 'p':

            case 'P':

                type = "Premium";

                cost = amount * PREMIUM_GAS;

                if (amount > 10)

                    this.free = REGULAR_WASH2;

                break;

            case 's':

            case 'S':

                type = "Super";

                cost = amount * SUPER_GAS;

                if (amount > 10)

                    this.free = REGULAR_WASH3;

                break;

            default:

                type = "Unknown";

                break;

        }

    }

    public String toString() {

        if (type.equals("Unknown"))

            return "Petrol is " + type + " cannot be served";

        else

            return "The type of petrol that will be served is " + type

                    + "\nYour bill is: $" + cost + "\n " + this.free;

    }
}
TestPetrol.java

import javax.swing.JOptionPane;

class TestPetrol {

    public static void main(String[] arg) {

        String str = JOptionPane.showInputDialog("COP2210 Petrol Station"

                + "\nEnter the type of petrol"

                + "\nr/R - for regular gas"

                + "\np/P - for premium gas"

                + "\ns/S - for super gas");

        char ch = str.charAt(0);

        str = JOptionPane.showInputDialog("Enter the amount of gas to be purchased");

        float amount = Float.parseFloat(str);

        Petrol p = new Petrol(ch, amount);

        p.calculateCost();

        JOptionPane.showMessageDialog(null, p.toString());


    }

}

Program Output Screenshots:

Add a comment
Know the answer?
Add Answer to:
I have this class determines how much petrol is being purchased and the cost. The gas...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

  • Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for...

    Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for their inventory management system. The program needs the following classes: A main class (SpaceCarDealer.java) that includes the main method and GUI. Two instantiable classes: CarDealer.java o Variables to store the dealer name, and the carsOnLot array in Car type. o A constructor to initialize the name variable with the given dealer name. o An accessor method to return the name of the dealer. o...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • I Have a problem with my JAVA class "Directivo". In the " public double sueldo() "...

    I Have a problem with my JAVA class "Directivo". In the " public double sueldo() " method, the instruction say "Calculate the salary of a Directivo the following way: Invoke method salary of the father and add him the extra bonus" My question is : How can I do this ? how can i implement that instruction in the public double sueldo() method public class Directivo extends Planta implements Administrativo{    private double bonoExtra;    public Directivo( String nom, String...

  • please do the program in simple programming it is for my first c++ computer class i...

    please do the program in simple programming it is for my first c++ computer class i posted the example on pic 2,3 which is how this should be done Write a program that calculates and prints the bill for a cellular telephone company. The company offers two types of services: regular and premium. Its rates vary depending on the type of service. The rates are computed as follows: Regular service: $10.00 plus the first 50 minutes are free. Charges for...

  • c++ only Design a class representing an Employee. The employee would have at least these private...

    c++ only Design a class representing an Employee. The employee would have at least these private fields (Variables): name: string (char array) ID: string (char array) salary: float Type: string (char array) All the variables except for the salary should have their respective setters and getters, and the class should have two constructors, one is the default constructor and the other constructor initializes the name,ID and type of employee with valid user input. The class should have the following additional...

  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • Hello can someone help me in my code I left it as comment  task #0, task#1, task#2a...

    Hello can someone help me in my code I left it as comment  task #0, task#1, task#2a and task#2b the things that I am missing I'm using java domain class public class Pizza { private String pizzaCustomerName; private int pizzaSize; // 10, 12, 14, or 16 inches in diameter private char handThinDeep; // 'H' or 'T' or 'D' for hand tossed, thin crust, or deep dish, respecitively private boolean cheeseTopping; private boolean pepperoniTopping; private boolean sausageTopping; private boolean onionTopping; private boolean...

  • ​I have to create two classes one class that accepts an object, stores the object in...

    ​I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList {    private ShoppingItem [] list;    private int amtItems = 0;       public ShoppingList()    {       list=new ShoppingItem[8];    }       public void add(ShoppingItem item)    {       if(amtItems<8)       {          list[amtItems] = ShoppingItem(item);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT