Search code examples
javamutators

Mutator methods not working, NetBeans


I try to change the values of coupe but the output doesn't change. In NetBeans, I saved these two programs under the same project and package. I did not include it here because it may have made the code too long, but I also wrote accessor methods which work fine, so I am confused as to why the mutator methods don't work.

Class code:

package auto;

public class Auto
{
    private String model;
    private int milesDriven;
    private double gallonsOfGas;

    public Auto(String startModel,
                int startMilesDriven,
                double startGallonsOfGas)
    {
    model = startModel;
    setMilesDriven(startMilesDriven);
    setGallonsOfGas(startGallonsOfGas);
    }

    public void setModel(String newModel)
    {
        model = newModel;
    }

    public void setMilesDriven(int newMilesDriven)
    {
        if (newMilesDriven >= 0)
            milesDriven = newMilesDriven;
        else
        {
            System.err.println("Miles driven cannot be negative.");
            System.err.println("Value not changed.");
        }
    }

    public void setGallonsOfGas(double newGallonsOfGas)
    {
        if (newGallonsOfGas >= 0.0)
            gallonsOfGas = newGallonsOfGas;
        else
        {
            System.err.println("Gallons of gas cannot be negative.");
            System.err.println("Value not changed.");
        }
    }
}

Client class code:

package auto;

import java.text.DecimalFormat;

public class AutoClient
{
    public static void main(String [] args)
    {
        DecimalFormat milesPattern = new DecimalFormat("#,###");

        Auto coupe = new Auto("Corvette", 300000, 0.0);

        String coupeModel = coupe.getModel();
        int coupeMiles = coupe.getMilesDriven();
        double coupeGallons = coupe.getGallonsOfGas();

        System.out.println("coupe:"
                            + "\nmodel: " + coupeModel
                            + "\nmiles: " + milesPattern.format(coupeMiles)
                            + "\ngallons: " + coupeGallons);    

        coupe.setModel("Viper");
        coupe.setMilesDriven(10000);
        coupe.setGallonsOfGas(50.0);

        System.out.println("coupe:"
                            + "\nmodel: " + coupeModel
                            + "\nmiles: " + milesPattern.format(coupeMiles)
                            + "\ngallons: " + coupeGallons); 
    }
}

Solution

  • Given your current code, after you changed the values

    coupe.setModel("Viper");
    coupe.setMilesDriven(10000);
    coupe.setGallonsOfGas(50.0);
    

    You need to get them again

    coupeModel = coupe.getModel();
    coupeMiles = coupe.getMilesDriven();
    coupeGallons = coupe.getGallonsOfGas();
    

    Before you can call

    System.out.println("coupe:"
                            + "\nmodel: " + coupeModel
                            + "\nmiles: " + milesPattern.format(coupeMiles)
                            + "\ngallons: " + coupeGallons); 
    

    I suggest you update Auto and add a toString()

    @Override
    public String toString() {
      return "coupe:"
                 + "\nmodel: " + coupeModel
                 + "\nmiles: " + milesPattern.format(coupeMiles)
                 + "\ngallons: " + coupeGallons;
    }
    

    Then you can replace (in both places)

    System.out.println("coupe:"
                 + "\nmodel: " + coupeModel
                 + "\nmiles: " + milesPattern.format(coupeMiles)
                 + "\ngallons: " + coupeGallons); 
    

    With

    System.out.println(coupe); // <-- Java will call toString() for you