Tip:
Highlight text to annotate it
X
CRISTIAN>> Hi, I'm Cristian. Welcome to the laboratory number nine of the module "Development
of Computer System Functions".
In this lab you will learn the basic principles of object-oriented programming in Java.
Open Eclipse by double clicking on its icon. To do this lab you will need to create two
classes. But as you know, the first step is to create a new project. To do this, choose
Archivo -> Nuevo -> Proyecto Java. In the wizard that it will be appeared put "Lab09"
in the "Nombre de proyecto" textbox and click on "Finalizar>" button.
The next step is to create the first class of the project, which is a class that it will
store the information about students. Deploy the project by double clicking on its name
and click the right mouse button on the src icon. Select Nueva -> Clase. In the wizard
that appears, put "Alumno" in the "Nombre" textbox. Then click on "Finalizar".
Within the Alumno class you have to write its attributes, that is:
int attribute called dni String attribute called nombre
GregorianCalendar attribute called fechaNacimiento
Create the constructor of the class, which receives as parameters the values of the attributes
of the class. That is:
Create setter and getter methods of all those attributes:
getDni() returns an int with the value of the dni attribute
getNombre() returns a String with the value of the nombre attribute
getFechaNacimiento() returns a GregorianCalendar with the value of the fechaNacimiento attribute
setDni(int) receives an int parameter and assigns it to dni attribute
setNombre(String) receives a String parameter and assigns it to nombre attribute
setFechaNacimiento(GregorianCalendar) receives a GregorianCalendar parameter and assigns
it to fechaNacimiento attribute
Finally, create toString() method that returns a String with the values of the attributes.
Now create another class, that you will use as a test of the Alumno class. Deploy the
project by double clicking on its name and click the right mouse button on the src icon.
Select Nueva -> Clase. In the wizard it will be appeared, put "TestAlumno" in the "Nombreā
textbox and check "public static void main (String [] args)" checkbox. Then, click on
"Finalizar".
Import GregorianCalendar class, which is located in the java.util package:
import java.util.GregorianCalendar;
We need it to work with birthdates. Then you have to create a GregorianCalendar class object
called fechaNacimientoJuan and you have to call a constructor of that class that accepts
year, month and birthday as parameters. That is:
Create an object of the Alumno class called alumnoJuan and call the constructor of the
class with the information of the student:
Finally, call toString() method of the Alumno class and display its content, namely:
Run the code
and see the result.
And this is all in this laboratory number 9. Thank you very much for your attention.
See you in the lab number 10.