update static field and call method within the class?
Im new to java so I've already done this wrong probably but i need to write
"A private method pump(double pump) that "dispenses" the specified amount of gas; appropriately updates the totalSales field, and returns the cost of the gas pumped."
i was supposed to make a static field that represents the total amount of sales for the pump in dollars
an accessor method that returns the total amount of sales in dollars
a constructor that tales one double parameter, gas price per gallon
a private method that returns the cost of a given amount of gas
This is my code so far
public class GasPump
{ static int TotalPumpSales;
public static int getTotalPumpSales
{ return TotalPumpSales
}
double GasPricePerGallon;
private double getCost(double Gallons)
{ double Cost = GasPricePerGallon*Gallons
return Cost
}
I know I've messed up a lot probably, i have no idea what I'm doing right now.
if i can learn how to call the getCost method ill know how to call the one I'm asking you to help me make and i won't need help with the rest of the problem that i haven't posted.
Thanks for your time people
Re: update static field and call method within the class?
What exactly is your question?
Re: update static field and call method within the class?
" A class Car has a method getTankCapacity, which returns the capacity of the car's fuel tank. It also has a method getGasAmt, which returns the current volume of gas in the tank and a method addGass(double gallons), which adds a given amount of gas to the tank. Write a class GasPump with the following features:
-A static field the that represents the total amount of sales for the pump in dollars
-an accessor method that returns the total amount of sales in dollars
-a constructor that tales one double parameter, gas price per gallon
-a private method getCost(double gallons) that returns the cost of a given amount of gas
-A private method pump(double gallons) that "dispenses" the specified amount of gas, appropriately updates the totalSales field, and returns the cost of gas pumped
-Two overloaded fill methods: fill(Car car) that fills the car's tank, and fill(Car car, double dollarLimit) that pumps the maximum amount of gas that does not overflow the car's tank and does not exceed dollarLimit; both fill methods return the cost of the pumped gas."
" avoid unnecessary duplication of code. For example pump should call getCost and both fill methods should call pump. "
There it is entirely. I just want help with the code i have down so far and pump(double gallons) , specifically how to call getCost.
Re: update static field and call method within the class?
Can you post your current code. Be sure to wrap it in code tags:
[code=java]<YOUR CODE HERE>[/code] to get highlighting
Re: update static field and call method within the class?
Code java:
public class GasPump
{ static int TotalPumpSales;
public static int getTotalPumpSales
{ return TotalPumpSales
}
double GasPricePerGallon;
private double getCost(double Gallons)
{ double Cost = GasPricePerGallon*Gallons
return Cost
}
Theres probably a lot of mistakes.
Re: update static field and call method within the class?
Quote:
Theres probably a lot of mistakes.
Did you try compiling it? The compiler will tell you if you have coded it wrong.
Coding conventions recommend that you do NOT put any code on the same line following a {
leave the rest of the line blank.
Indent each level of nesting within a pair of {}s at least 3-4 spaces.
Re: update static field and call method within the class?
Its supposed to be written only and i don't wana try compiling it. Im worried that i didn't write the static part of the question correctly, because i don't know what an accessor method is. And i understand indenting after { , i was just lazy sorry.
Re: update static field and call method within the class?
Quote:
i don't wana try compiling it.
If you can't compile it, then I don't know how you will know if the code is correct.
Asking some one else to check your code is exactly the same as compiling it.