Question
2. This part asks you to modify the Java program you just created. The gas station only has a finite amount of each type of
2. This part asks you to modify the Java program you just created. The gas station only has a finite amount of each type of gas. Add instance variables that track how many gallons of each type of gas the station has to sell. You can fix the initial value to be 1000 gallons each. Add a method to print current supply Modify the sellregular/super methods to subtract from supply after each sale. 2b. Further modify the sellregular/super methods to call the internal gouge() function when the total supply of gass drops below 200 gallons. Now you have to becareful that this should only be called once: For example: A->currentSupply(); // say current supply is 210 gallons (regular+super) A->sellregular(20); // supply now 190 gallons, CALL GOUGE to double prices A->sellregular(10); // supply now 180 gallons, but DON'T call gouge again! You may need to declare additional variables, maybe even functions, to implement this algorithm correctly.
public class lab1 { public static void main(String[] argc) { gasStation A = new gasStation(2.69, 3.09); gasStation B = new gasStation(2.49, 2.99); A.sellregular(10); A.sellsuper(8); B.sellsuper(11); B.sellregular(12); if(A.moreprofits(B)) System.out.println("Station A is more prifotable. "); else System.out.println("Station B is more profitable. "); gasStation[] G = new gasStation[10]; for(int i=0;i<10;i++) G[i]=new gasStation(2.59,3.19); for(int i=0;i<10;i++) { G[i].sellregular((int)(Math.random()*26+77)); G[i].sellsuper((int)(Math.random()*26+77)); } gasStation highest = G[0]; for(int i=1;i<10;i++) { System.out.println(G[i].getsales()); if(G[i].moreprofits(highest)) highest = G[i]; } System.out.println("highest total sales is: "+highest.getsales()); } }
class gasStation{ protected double regularprice; protected double superprice; protected double sales;
public gasStation(double r, double s) { regularprice = r; superprice = s; sales = 0; } public void sellregular(double gallons) { sales += regularprice * gallons; } public void sellsuper(double gallons) { sales += superprice * gallons; } public double getsales() { return sales; } public boolean moreprofits(gasStation other) { return sales > other.sales; } private void gouge() { superprice *= 2; regularprice *= 2; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started