Doubt about Builders and use of getters and Setters

6

I am practicing with a program to read data from a Pseudo Speed and Pressure sensor using getters and setter. As the sensor does not exist, the values that the user enters via the reader from the main method have been assigned.

The exercise explicitly asks that the class must have two constructors and I do not know how to get there. At the moment I have this code that works quite well. But it does not fulfill the request to have two constructors, or at least I can not see it.

package sensores;

public class Auto {
     private double angularSpeed;
     private double pres;
     private String name;

        public Auto()
        {

        }


        public double getSpeed()
        {
            return angularSpeed;
        }

        public void setSpeed(double s)


{
        angularSpeed = s;
    }

    public double getPres()
    {
        return pres;
    }

    public void setPres(double p)
    {
        pres = p;
    }

    public String getName(){
        return name;
    }
     public void setName(String n){
         name = n;
     }

}

I understand that I could create a second constructor like that.

public Auto(double speed, double pres){
   this.pres = pres;
   this.speed = speed;
}

This would serve to assign the setters, but the getters are left out ... can this be done? and if so, how do I implement the getters?

    
asked by Cromewar Gk 14.07.2017 в 08:12
source

2 answers

5

Like the methods, builders can also overload ( see Java language specification, section 8.8.8: Constructor Overloading ), that is, that a class can have several constructors .

Having several builders (because we can have more than two if necessary), it is not a bad practice if its use is justified. This is useful if we want to have a class with constructors that do different tasks.

Thus, we can create instances of the class:

  • calling the default constructor: new Clase(); in which case it will acquire the values that have been indicated by default ... or null values in case of not having default values.
  • calling another constructor with its parameters new Clase(param 1, param 2, param ...); , in which case it will be created with the specified parameters.

Here is an example. The essentials are explained in comments of the code:

Código: Ver Demo

class Rextester
{  
    public static void main(String args[])
    {

       /*Creando el objeto de esta forma
        * llamará al constructor por defecto
        * y si hay valores por defecto los adquiere
        * si no hay valores establece todo a null, 0.0, etc.
        */

      Auto myAuto1 = new Auto();
       System.out.println("Valor de AngularSpeed: "+myAuto1.getAngularSpeed());
       System.out.println("Valor de Pre: "+myAuto1.getPres());
       System.out.println("Valor de Name: "+myAuto1.getName());

       /*Creando el objeto de esta forma
        * llamará al constructor con parámetros*/
       Auto myAuto2 = new Auto(10.2, 2.2, "Nombre Auto 2");
       System.out.println("Valor de AngularSpeed: "+myAuto2.getAngularSpeed());
       System.out.println("Valor de Pre: "+myAuto2.getPres());
       System.out.println("Valor de Name: "+myAuto2.getName());

    }
}



class Auto
{

     private double dblAngularSpeed;
     private double dblPres;
     private String strName;

   Auto()
   {
       //Constructor por defecto... los valores no son obligatorios

       dblAngularSpeed = 0.0;
       dblPres = 0.0;
       strName = "Nuevo";

   }
   Auto(double dblAngularSpeed1, double dblPres1, String strName1)
   {
       //Constructor con parámetros
       dblAngularSpeed = dblAngularSpeed1;
       dblPres = dblPres1;
       strName = strName1;
   }
   //Getters y Setters
   public double getAngularSpeed() {
       return dblAngularSpeed;
   }
   public void setAngularSpeed(double dblAngularSpeed) {
       this.dblAngularSpeed = dblAngularSpeed;
   }
   public double getPres() {
       return dblPres;
   }
   public void setPres(double dblPres) {
       this.dblPres = dblPres;
   }
   public String getName() {
       return strName;
   }
   public void setName(String strName) {
       this.strName = strName;
   }
}

Resultado :

Valor de AngularSpeed: 0.0
Valor de Pre: 0.0
Valor de Name: Nuevo

Valor de AngularSpeed: 10.2
Valor de Pre: 2.2
Valor de Name: Nombre Auto 2
    
answered by 14.07.2017 в 14:04
4

You can create a constructor that receives parameters and another that does not receive parameters and it makes a call to the constructor with parameters to assign a default value

public Auto(double angularSpeed,double press,String name){
     this.angularSpeed = angularSpeed;
     this.press = press;
     this.name = name;
}

public Auto() {
     Auto(2.5,3.4,"por defecto");
} 

For example, when someone calls the constructor without parameters, values are assigned by defects to the attributes

    
answered by 14.07.2017 в 12:21