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 2 of 2

Thread: Need Help with Accessor and Mutator Methods

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help with Accessor and Mutator Methods

    I am working on a vehicle program which enables vehicle to drive and fill up the vehicle's tank from fuel station.
    Here is the code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
     
     
    package carapplication;
     
    import java.util.Scanner;
     
     
    class FuelStation{
        static double fuel;
     
         public void setfuel(double fuel){
            FuelStation.fuel=fuel;
     
        }
        public static Vehicle fillup(Vehicle c){
             c.setfuel(c.getfuel()+10);
            return c;
        }
     
     
    }
     
     
    class FuelOverFlowException extends Exception {
     
    }
     
    class OutOfFuelException extends Exception {
     
    }
     
     
     
    class Vehicle {
        double fuel=10;
     
        public double getfuel(){
            return fuel;
        }
     
     
        public void drive(double km) {
     
               if (fuel < 0)
                 {
                try {
                    throw new OutOfFuelException();
                } catch (OutOfFuelException ex) {
     
                }
                 }
     
                 else if (fuel > 60)
                 {
                try {
                    throw new FuelOverFlowException();
                } catch (FuelOverFlowException ex) {
     
                }
                 }
    }
     
        public void setfuel(double fuel){
            this.fuel=fuel;
     
     
        }
     
    }
    class Car extends Vehicle{
     
     
    }
     
     
    /**
     *
     * @author Ankur
     */
    public class Main {
        static double fuel;
     
        /**
         * @param args the command line arguments
         */
        public static void main(String [] args){
     
            Scanner input = new Scanner (System.in);
            // TODO code application logic here
            FuelStation audi = new FuelStation();
     
            for (;;){
            System.out.println("M for Display this Menu."+"\n"+"F for displaying current litres of fuel"+"\n"+"X for Filling Up the Vehicle With X litres"+"\n"+"D for Driving the Car");
            String ch = input.next();
     
            if (ch.equalsIgnoreCase("X"))
            {
                System.out.println("Enter Desired amount of fuel");
                fuel = input.nextDouble();
                audi.setfuel(fuel);
     
            } else if (ch.equalsIgnoreCase("F")){
                System.out.println("You have "+fuel+"litres left");
            }
     
        }
     
    }
    }

    I have some problem with mutator method, It does changes the value of fuel but its does not add to the previous value. Any suggestion how can I do this?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need Help with Accessor and Mutator Methods

    I have some problem with mutator method, It does changes the value of fuel but its does not add to the previous value.
    What do you mean by 'it' and add to the previous value? More importantly, when you run the program with a certain input, how do you expect it to behave and how does it actually behave?

Similar Threads

  1. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 08:33 AM
  2. Accesor and Mutator help
    By clevel211 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 5th, 2010, 05:06 PM
  3. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM
  4. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM
  5. What is the matching mutator method?
    By ssmith in forum Object Oriented Programming
    Replies: 1
    Last Post: November 3rd, 2009, 10:03 PM