Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: update static field and call method within the class?

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    New York
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: update static field and call method within the class?

    What exactly is your question?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Location
    New York
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,416
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default 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

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Location
    New York
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: update static field and call method within the class?

    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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,416
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: update static field and call method within the class?

    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.

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Location
    New York
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,416
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: update static field and call method within the class?

    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.

Similar Threads

  1. Replies: 9
    Last Post: November 5th, 2011, 10:22 AM
  2. Call method(s) within the same class
    By mwr76 in forum Object Oriented Programming
    Replies: 8
    Last Post: September 26th, 2011, 12:58 AM
  3. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 06:51 PM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 04:24 PM