Nelson Azevedo
Nelson Azevedo

Reputation: 21

How to change values in the SET Method

I am starting to develop my skills in JAVA, however I have a doubt. I'm creating an object in JAVA, created the constructor and so on, then, it asks "Change the AGE_RECENT value from 1 to 3", I initially declared this as final because I never thought it would change, so no SET or GET were created. I am wondering how can I change the value from 1 to 3 in the SET Method. I have this variable

private static int AGE_RECENT=1;

I did this.

   public void setAgeRecent() {
    Vehicle.AGE_RECENT = 3; 
}

It works if you run the program, it changes the variable's value, however nothing was declared in that method as every SET method. Just wondering how can I do this. If this is correct, good, if not, thanks for helping!

As someone asked, the code.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tp1;

/**
 *
 * @author Nelson
 */
public class Vehicle {

/** Variáveis da classe, têm como função **/
 private String registration;

private int registrationYear;

private double consumption;

private double autonomy;

private int cilinderCapacity;

/**
* Final variables. They are final because they do not suffer any kind of modification during the project.
* YEAR_OMISSION is 2016 because the currect year is 2016. 
* ENVIRONMENTAL_CHARGE_OMISSION is 0.10(10 cents), gave this value because there is nothing to mention the
especific value, hence why I gave 0.10.
* RATING_RECENT = Is a string, just has the text "RECENT" inside.
* RATING_COMTEMPORY - Another string, just with the "Comtempory" text inside.
* RATING_CLASSIC - Yet again another string, with the "Classic" text.
* AGE_RECENT - It is to help to compare if a vehicle is recent or not, it has the value 3.
* AGE_CLASSIC - It is to again help to compare, value is 20.
*/

private static final int YEAR_OMISSION = 2016;
private static final double ENVIRONMENTAL_CHARGE_OMISSION=0.10;
private static final String RATING_RECENT="Recent";
private static final String RATING_CONTEMPORY="Contempory";
private static final String RATING_CLASSIC="Classic";
private static int AGE_RECENT=1;
private static final int AGE_CLASSIC=20;




/**
* Constructor of the object, it has the Registration
     * @param registration
     * @param registrationYear - The year the vehicle was first registered.
     * @param consumption - How many liters the vehicle consumes.
     * @param autonomy - How many KMs a vehicle can go without refuelling.
     * @param cilinderCapacity - How many Cubic Inches the engine has.
*/
 public Vehicle(String registration,int registrationYear, double consumption, double autonomy, int cilinderCapacity) {
 this.registration = registration;
 this.registrationYear = registrationYear;
 this.consumption = consumption;
 this.autonomy = autonomy;
 this.cilinderCapacity = cilinderCapacity;
 }

/**
* Null Constructor, it has no values, they will be attributed in the MAIN Class.
*/

 public Vehicle() {
 this.registration = "";
 this.registrationYear = 0;
 this.consumption = 0;
 this.autonomy = 0;
 this.cilinderCapacity =0;
 this.registrationYear = YEAR_OMISSION;
}
 /**
* Copy Constructor.

 */
public Vehicle(Vehicle vehicle) {
 this.registration = vehicle.getRegistration();
 this.registrationYear = vehicle.getRegistrationYear();
 this.consumption = vehicle.getConsumption();
 this.autonomy = vehicle.getAutonomy();
 this.cilinderCapacity = vehicle.getCilinderCapacity();
 }

    public String getRegistration() {
        return registration;
    }

    public int getRegistrationYear() {
        return registrationYear;
    }

    public double getConsumption() {
        return consumption;
    }

    public double getAutonomy() {
        return autonomy;
    }

    public int getCilinderCapacity() {
        return cilinderCapacity;
    }


    public double getYearRecent() {
        return AGE_RECENT;
    }

    public double getAgeRecent(){
        return AGE_RECENT;
    }



    public void setRegistration(String registration) {
        this.registration = registration;
    }

    public void setRegistrationYear(int registrationYear) {
        this.registrationYear = registrationYear;
    }

    public void setConsumption(double consumption) {
        this.consumption = consumption;
    }

    public void setAutonomy(double autonomy) {
        this.autonomy = autonomy;
    }

    public void setCilinderCapacity(int cilinderCapacity) {
        this.cilinderCapacity = cilinderCapacity;
    }

   public void setAgeRecent() {
    Vehicle.AGE_RECENT = 3; 
}



/**
 * Calculate the age of the vehicle to compare in the vehicleRating method
 * @return The year, which is 2016 minus the year the vehicle was first registered.
 */
private int calculateAge(){
    return YEAR_OMISSION-this.registrationYear;

} 

/**
 * Calculate the Circulation Tax.
 * @return Returns the value of the Environmental Charge multiplied by the Cilinder Capacity of the vehicle.
 */
    public double calculateCirculationTax(){

  return ENVIRONMENTAL_CHARGE_OMISSION*cilinderCapacity;

        }





   /**
    * Classify the vehicle based on the age.
    * If the result given by the calculateAge method is minor than the AGE_RECENT variable(3), then it will
    return "Recent"
    * If the result is between Age_RECENT and AGE_CLASSIC(20), then it will say "Contemporary"
    * If none of the IFs apply, it will return "Classic".
   **/
public static String vehicleRating(Vehicle vehicle) {
if(vehicle.calculateAge() < Vehicle.AGE_RECENT) { 
    return  Vehicle.RATING_RECENT; }
else if ((vehicle.calculateAge()>=Vehicle.AGE_RECENT)&&(vehicle.calculateAge()<=Vehicle.AGE_CLASSIC)){
    return Vehicle.RATING_CONTEMPORY;}
else 
 return Vehicle.RATING_CLASSIC;

}

    @Override
    public String toString() {
        return "Vehicle{" + "registration=" + registration + ", registrationYear=" + registrationYear + ", consumption=" + consumption + ", autonomy=" + autonomy + ", cilinderCapacity=" + cilinderCapacity + '}';
    }


}

Upvotes: 1

Views: 858

Answers (2)

A static varible, or class variable, may be used without the need to create an instance of that class. But its value may be changed freely at runtime.

A final variable is not a variable in a true sense, because it's value can't be changed at runtime.

Thus, you may have a set method for a static variable, but never to a final variable.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726649

A setter that takes no arguments is simply a method, not a setter. In order to work as a setter a method must take a parameter that matches the type of the value being set - in your case, that would be int:

public static void setAgeRecent(int age) {
    AGE_RECENT = age; 
}

Note a few things here:

  • Since AGE_RECENT is static, setAgeRecent should be static
  • Since AGE_RECENT and setAgeRecent are static members of the same class Vehicle, you do not need to qualify AGE_RECENT with Vehicle

Now users of your class would be able to call your static setter as follows:

Vehicle.setAgeRecent(3);

Upvotes: 1

Related Questions