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.
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:



I have this class determines how much petrol is being purchased and the cost. The gas...
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...
Hi, I need to change the conversion where stack is into a class stack and add a template <class type> PROMPT: write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. if the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. #include #include #include #include #include using namespace std; double conversion(string); int...
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...
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 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...
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 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);...
I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main { public static void main(String[]...
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){...
software testing without making any changes to the java classes provided below, come up with some junit test cases to test the code. To get you started, here are four test cases you must implement: • Use the setString() function in MyCustomStringInterface to set the value to “H3y, l3t'5 put s0me d161ts in this 5tr1n6!11!!”. Then test to see if the CountNumbers() function is equal to the number of sequential digits in the original string (in this case, 9). •...