how to create a custom constructor in Java?

0

In a class called Coins, the following members are present: to. Two private string variables called country and currency, the value of the variables it must be typed from the keyboard. The code to request this value must write it in the class of the Main method. b. Create a custom constructor with two parameters of type String that accepts the parameters corresponding to the variables of the class and showing a message. By example "The legal currency of El Salvador is the dollar", in this message you must concatenate the variables that you will receive in the parameter. Implement the class.

import java.util.*;

class monedaPais{
   private String moneda, pais;

   Scanner sc = new Scanner(System.in);

   public static void main(String args[])
   {
   System.out.println("ingrese el pais");
   pais = sc.nextLine();
   System.out.println("Ingrese la moneda");
   moneda = sc.nextLine();
   }

    public monedaPais (string moneda, string pais){

         this.moneda = moneda; 
          this.pais = pais;

        }


}
    
asked by Ruslan López 05.11.2017 в 01:05
source

2 answers

0

My solution would be:

import java.util.Scanner;

public class MonedaPais {

    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);

        System.out.println("Escribe el país: ");
        String p = kbd.nextLine();

        System.out.println("Escribe la moneda: ");
        String m = kbd.nextLine();

        Moneda moneda = new Moneda(p, m);

    }

}

class Moneda {
    private String pais, moneda;

    public Moneda(String pais, String moneda) {
        this.pais = pais;
        this.moneda = moneda;

        System.out.println("La moneda de curso legal de " + pais + " es el " + moneda);
    }
}

A tip: The names of the classes always start with a capital letter. Eclipse for example, warns you when you try to create it.

    
answered by 05.11.2017 / 01:28
source
0

I understand that it is an exercise. But keep in mind that this is generally not done, for example the constructor is not usually used to print things, usually the classes have a toString method or get methods to obtain values from within them.

Also, the main method would not generally go in this class.

I have renamed the class by calling it MonedaPais , since the naming convention It is important for a better understanding and organization of the code.

Let's see then a code with what you would lack:

Main class

import java.util.*;
class MonedaPais { //Generalmente la clase principal se llama Main

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);

        System.out.println("ingrese el pais");
        String pais = sc.nextLine();
        System.out.println("Ingrese la moneda");
        String moneda = sc.nextLine();

        /*Cerrar scanner*/
        sc.close();
        /*Implementar Clase
         *aquí creamos una instancia de la clase Monea
         *pasándole dos parámetros al constructor
         *que son los valores escritos en el scanner
        */
        Moneda m = new Moneda(moneda, pais);
    }

}

Currency Class

public class Moneda {

    private String moneda;
    private String pais;

    /*
     *Constructor con dos parámetros
     *Cuando se cree una instancia de la clase con estos dos parámetros
     *se construirá una instancia de ella, haciendo lo que haya aquí
     *IMPORTANTE: Los constructores no suelen usarse para imprimir cosas
     *Las clases deben estar dotadas de métodos para ello
     *aquí se hace así por requerimiento de la pregunta
    */
    public Moneda(String moneda, String pais) {

        this.moneda = moneda;
        this.pais = pais;

        /*Imprimir valores*/
        System.out.println("La moneda de curso legal de: " + pais + " es: " + moneda);

    }
}

Out

ingrese el pais
Japón
Ingrese la moneda
Yen
La moneda de curso legal de: Japón es: Yen
    
answered by 05.11.2017 в 01:25