enter data Scanner to constructor

0

Someone who explains to me please, how can I invoke the other classes using the constructor with the data that I enter by keyboard using Scanner, thanks.

CODE:

import java.util.*;
class Principal{

    public static void main(String[] args) {

        Principal pr=new Principal();


    }

               String titulo, autor, editorial;

               int nPaginas,anio;

               public Principal(String titulo,String autor,int nPagina){
                   this.titulo=titulo;
                   this.autor=autor;
                   this.nPaginas=nPaginas;

                              Scanner e=new Scanner(System.in);

                              System.out.println("Digite titulo del libro: ");

                              titulo=e.nextLine();

                              System.out.println("Digite autor del libro: ");

                              autor=e.nextLine();

                              System.out.println("Digite numero de paginas del libro: ");

                              nPaginas=e.nextInt();

               }

       public Principal(String editorial, int anio)

       {

           this.editorial=editorial;

           this.anio=anio;

       }

               public Principal (String msg)

               {

                              System.out.println(msg);

               }

               private void Datos1()

               {

                              System.out.println("Titulo del libro: "+ titulo);

                              System.out.println("Autor: "+ autor);

                              System.out.println("Numero de paginas: "+ nPaginas);

               }

               public void getDAtos1()

               {

                              Datos1();

               }

               public void Datos2()

               {

                              System.out.println("Editorial: " + editorial);

                              System.out.println("Año de publicacion: " + anio);

               }

}
class Docente{

   public String nombre="jorge";

   public String apellido="Peñate";

   public String materia="ciencias";

   public Docente(String msg){

       System.out.println("--------------------------------------");

       System.out.println(msg);

       System.out.println("--------------------------------------");

   }

private void nCompleto(){

    System.out.println(nombre+" "+apellido);

}   

private void getMateria(){

    System.out.println(materia);

}

public void getData(){

    nCompleto();

    getMateria();

}

}



class Alumno{

   public String carrera;

   public String materia;

   String nombre="Marcos";

   String carnet="22222";

   int edad=25;

   public Alumno(){

       System.out.println("Datos del Alumno");

       System.out.println("--------------------------------------");

   }

   public Alumno(String carrera, String materia){

       this.carrera=carrera;

       this.materia=materia;

   }

   public void getDatos(){

       System.out.println(this.nombre);

       System.out.println(this.carnet);

       System.out.println(this.edad);

   }

}
    
asked by david 15.05.2018 в 02:13
source

1 answer

0

Seeing your code for the first time has been torture for me. How is it possible for your teacher to allow you to program in that way? all together in the same class? ... I have taken the time to re-do your exercise and comment step by step so that you can understand it perfectly and start programming as professionals do.

  • You must have each object in its own class (a separate java), that is, Student in one class, Teacher in another and Book in another. In the same way, another class with the main method.
  • It is always advisable to use Encapsulation in Java. Encapsulation is one of the fundamental principles in the object-oriented programming (OOP), so implement getter and setter is one of the ways to apply encapsulation in the program code. With the getters you will get the value of the variables that the class has and with the setters you will modify its value. All this also helps you have a code cleaner, reduced and less likely to make mistakes.
  • After you see my code and understand it, it only remains to use Scanner to request data and save them in the variables of the classes. I'm sure you can do this without my help.
  • Here I leave all the code necessary for your exercise (and fixed).

    Student Class:

    public class Alumno {
    
        // Variables
        private String carrera;
    
        private String materia;
    
        private String nombre;
    
        private String carnet;
    
        private int edad = 25;
    
        // Constructor vacío para permitir que esta clase sea instanciada
        public Alumno() {
        }
    
        // Getters
        public String getCarrera() {
            return carrera;
        }
    
        public String getMateria() {
            return materia;
        }
    
        public String getNombre() {
            return nombre;
        }
    
        public String getCarnet() {
            return carnet;
        }
    
        public int getEdad() {
            return edad;
        }
    
        // Setters
        public void setCarrera(String carrera) {
            this.carrera = carrera;
        }
    
        public void setMateria(String materia) {
            this.materia = materia;
        }
    
        public void setNombre(String nombre) {
            this.nombre = nombre;
        }
    
        public void setCarnet(String carnet) {
            this.carnet = carnet;
        }
    
        public void setEdad(int edad) {
            this.edad = edad;
        }
    
        @Override
        public String toString() {
            return "Alumno [carrera=" + carrera + ", materia=" + materia + ", nombre=" + nombre + ", carnet=" + carnet
                    + ", edad=" + edad + "]";
        }
    }
    

    Teaching class:

    public class Docente {
    
        // Variables
        private String nombre;
        private String apellido;
        private String materia;
    
        // Constructor vacío para permitir que esta clase sea instanciada
        public Docente() {
        }
    
        // Getters
        public String getNombre() {
            return nombre;
        }
    
        public String getApellido() {
            return apellido;
        }
    
        public String getMateria() {
            return materia;
        }
    
        // Setters
        public void setNombre(String nombre) {
            this.nombre = nombre;
        }
    
        public void setApellido(String apellido) {
            this.apellido = apellido;
        }
    
        public void setMateria(String materia) {
            this.materia = materia;
        }
    
        @Override
        public String toString() {
            return "Docente [nombre=" + nombre + ", apellido=" + apellido + ", materia=" + materia + "]";
        }
    }
    

    Book Class:

    public class Libro {
    
        // Variables
        private String titulo;
        private String autor;
        private String editorial;
        private int nPaginas;
        private int anio;
    
        // Constructor vacío para permitir que esta clase sea instanciada
        public Libro() {
        }
    
        // Getters
        public String getTitulo() {
            return titulo;
        }
    
        public String getAutor() {
            return autor;
        }
    
        public String getEditorial() {
            return editorial;
        }
    
        public int getnPaginas() {
            return nPaginas;
        }
    
        public int getAnio() {
            return anio;
        }
    
        // Setters
        public void setTitulo(String titulo) {
            this.titulo = titulo;
        }
    
        public void setAutor(String autor) {
            this.autor = autor;
        }
    
        public void setEditorial(String editorial) {
            this.editorial = editorial;
        }
    
        public void setnPaginas(int nPaginas) {
            this.nPaginas = nPaginas;
        }
    
        public void setAnio(int anio) {
            this.anio = anio;
        }
    
        @Override
        public String toString() {
            return "Libro [titulo=" + titulo + ", autor=" + autor + ", editorial=" + editorial + ", nPaginas=" + nPaginas
                    + ", anio=" + anio + "]";
        }
    }
    

    Exercise Class (with the main method):

    public class Ejercicio {
    
        public static void main(String[] args) {
    
            /**
             * Instancias de cada clase.
             * Modificaremos el valor de sus variables con los SETTERS.
             */
            Libro libro = new Libro();
            libro.setTitulo("Los cuentos de pepito");
            libro.setAutor("Ramon Orlando");
            libro.setEditorial("Disney");
            libro.setAnio(2009);
    
            Docente docente = new Docente();
            docente.setNombre("Jose");
            docente.setApellido("Perez");
            docente.setMateria("Informática");
    
            Alumno alumno = new Alumno();
            alumno.setNombre("Manuel Castillo");
            alumno.setMateria("Informática");
            alumno.setCarrera("Ing. Sistemas");
            alumno.setCarnet("Tipo A");
            alumno.setEdad(26);
    
            /**
             * Imprimir en consola cada objeto.
             * Haremos uso del método toString() que tiene cada clase.
             * Puedes modificar este método en cada clase para que tenga la apariencia que desees.
             */
            System.out.println(libro.toString()); 
            System.out.println(docente.toString()); 
            System.out.println(alumno.toString()); 
    
            /**
             * Obtener un dato en específico de cada clase e imprimirlo
             */
            System.out.print("\n"); // Salto de línea 
            System.out.println("Título del libro: " + libro.getTitulo()); 
            System.out.println("Nombre del docente: " + docente.getNombre() + " " + docente.getApellido()); 
            System.out.println("Nombre del alumno: " + alumno.getNombre()); 
        }
    }
    

    If you still have doubts about how and where to request data with Scanner and how to save them (I hope you have solved it on your own, even mentally), here is an example (the following code goes inside the class Exercise, in the main method):

    // Pedir dato (en este caso un nuevo nombre para el alumno)
    Scanner sc = new Scanner(System.in);
    System.out.print("Ingrese el nuevo nombre del alumno: ");
    String nuevoNombre = sc.nextLine();
    sc.close(); // Cerrar el Scanner
    
    // Guardar el dato en la clase Alumno utilizando el SETTER
    alumno.setNombre(nuevoNombre);
    

    I hope I have helped you, regards!

        
    answered by 15.05.2018 / 06:08
    source