How to send data received with Scanner (input from keyboard) to constructor?

0

The task I have consists of:

  • Create 3 classes, the first will be superclass and will have 5 attributes that you choose, these will enter from the keyboard using a builder and will show them.
  • In the second class you will have 4 private attributes, 3 methods will be private and a public will invoke the private members and     show them. Also this class will have access to the constructor of the     superclass.
  • The third class will have a method and a constructor that you will overload as many times as you wish those methods will be public or     private.
  • The fourth class will have the main method where it will invoke the previous ones.
  • When doing the code, I do not know why the part in which the object to invoke the constructor invokes in the main class and that it prints the data entered by the keyboard.

    this is what I have been able to do with the code:

    Class "mainframe":

    import java.util.Scanner;
    class claseprincipal{
    
        //tendrá el método principal la cual tendrá su nombre
        public static void main(String[] args) {
            //Aqui estoy invocando el primer constructor con la clase scanner para introducir datos mediante teclado
            clase1 clase1=new clase1();
    //ACA ME MARCA ERROR estoy invocando al constructor de la clase 2 (subclase) y no al de la clase1 (superclase) como lo hice en la ocacion anterior
            clase2 clase2=new clase2(int Nummanos,int Numpies,int Numojos,int Numpiernas,int Numdedos);
    
    
    
        }
    }
    class clase1{
        //5 atributos atributos,desde teclado
            int Nummanos,Numpies,Numojos,Numpiernas,Numdedos;
    
        //utilizando un constructor y los mostrara.
                public clase1() {
                Scanner entrada=new Scanner(System.in);
                System.out.println("Digite el numero de manos: ");
                int Nummanos=entrada.nextInt();
                System.out.println("Digite el numero de pies: ");
                int Numpies=entrada.nextInt();
                System.out.println("Digite el numero de ojos: ");
                int Numojos=entrada.nextInt();
                System.out.println("Digite el numero de piernas: ");
                int Numpiernas=entrada.nextInt();
                System.out.println("Digite el numero de dedos: ");
                int Numdedos=entrada.nextInt();
                }
    
                public clase1(int Nummanos,int Numpies,int Numojos,int Numpiernas,int Numdedos){
                    this.Nummanos=Nummanos;
                    this.Numpies=Numpies;
                    this.Numojos=Numojos;
                    this.Numpiernas=Numpiernas;
                    this.Numdedos=Numdedos;
                }
    }
    

    Class "class2":

    class clase2 extends clase1{
        // 4 atributos privados
        private String ocupacion="estudiante";
        private String carrera="Ingenieria en sistemas y computacion";
        private double estatura=1.70;
        private int pesokg=70;
        //3 métodos dos serán privados y un publico que invoque a los miembros privados y los muestre
        private void datospersonales(){
            System.out.println("La ocupacion es " + ocupacion + " y la carrera es " + carrera);
        }
        private void datosfisicos(){
            System.out.println("La estatura es " + estatura + " y el peso en kg es " + pesokg);
        }
        public void getdatos(){
            datospersonales();
            datosfisicos();
        }
        //Ademas  esta clase tendrá acceso al  constructor de la superclase
        public clase2(int Nummanos,int Numpies,int Numojos,int Numpiernas,int Numdedos){
            super(Nummanos,Numpies,Numojos,Numpiernas,Numdedos);
        }
    }
    

    Class "class3":

    class clase3{
        // tendrá un método y un constructor que usted va a sobrecargar las veces que desee esos métodos serán públicos o privados
        //constructor sobrecargado
        public clase3(){
        }
        String mensaje;
        public clase3(String mensaje){
        }
        //metodo
        public void metodo() {
            System.out.println("--------------------------------------------------------------------------------");
        }
    }
    
        
    asked by david 05.06.2018 в 20:10
    source

    1 answer

    0

    The problem you have with the line:

    clase2 clase2=new clase2(int Nummanos,int Numpies,int Numojos,int Numpiernas,int Numdedos);
    

    is because you are not passing values as arguments. The constructor of clase2 is waiting for integer values.

    Example:

    // Correcto
    clase2 clase2 = new clase2(1, 1, 1, 1, 1);
    

    If you want to instantiate class2 without passing anything, you must have an empty constructor in clase2 , example, in clase2 :

    // Constructor vacío para permitir la instancia de esta clase
        public clase2() {
        }
    

    Then, in claseprincipal :

    clase2 clase2 = new clase2();
    

    You decide which one to choose. Now, you should look for the way to print the data that has clase1 . I suggest using Getters , if you allow it.

    Note: in this clase1 constructor you are not printing anything. The only thing it does is to receive values and then assign them to the variables of the class.

    // Esto no imprime nada
    public clase1(int Nummanos,int Numpies,int Numojos,int Numpiernas,int Numdedos){
                    this.Nummanos=Nummanos;
                    this.Numpies=Numpies;
                    this.Numojos=Numojos;
                    this.Numpiernas=Numpiernas;
                    this.Numdedos=Numdedos;
                }
    
        
    answered by 05.06.2018 в 20:47