Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
UNIDAD 0. Object-Oriented
Programming
Variables and Identifiers
 A variable is a computer’s memory zone with a value stored to
be used later in a program.
 A variable is identified by: a name (identifier), data type, range
of values.
 Identifier: is the name given to a variable, class, or method.
 Rules:
– Can start with a Unicode letter, underscore (_), or dollar sign ($).
– Are case-sensitive and have no maximum length.
– The null value, or the Boolean values “true” or “false”, can´t be
used.
– Identifiers must be descriptive. Avoid use abbreviation
Variables and Identifiers
 Declaration and Initialization: We have to create variables before
being used it in a program, indicating its name and type, that is, we
have to declare the variable.
int numAlumnos = 15;
double radio = 3.14, importe = 102.95;
Class and Objects
 Classes are the base of programming oriented to objects.
 A class is a set of data (attributes) and actions (methods) that use that data.
The definition of a class is:
 The word public is optional: If you use public, all classes can see, but if you
can’t, only the classes in the same package can see it.
 All variables and methods must be defined inside the class {…}. An object
(instance), is a software bundle of related state and behaviour. Software
objects are often used to model the real-world objects that you find in everyday
life.
[public] class Classname {
// definition of attributes and methods
... }
Persona primerObjeto;
/(class Persona)
Persona segundoObjeto;
//(class Persona)
Class and Objects
 Important features of a class:
– All attributes and methods of Java must belong to a class.
– There aren´t global variables and global functions.
– If a class extends another one, it inherits all its variables and methods.
– Java has a standard class hierarchy (Java´s Api), so it doesn’t start from
zero. Users can use that classes.
– multiple heritage doesn´t exist . A class can only inherit from one class.
– Every class in Java derives from Object class. It is the base of all Java
class hierarchy.
– In the same file, you can create several classes, but only one of them can
be public.
– The Java file (*.java) and the public class must be the same name. If the
class within the file isn´t public, it is not necessary that the file is called as
the class.
– We refer to the methods or variables of our class through reference “this”.
– The classes are grouped into packages, indicating in the first line:
(package packageName;).
Class. Structure of a class
/**Structure of a Java class*/
public classNombreDeLaClase {
// Class attributes
// Class methods
// main method, that indicates where the ejecution starts
public static voidmain(String[] args) {
// Method variables
// Method ejecution
}
}
Class. Structure of a class
Elements of a class:
 Class attributes: Variables you use in the class level.
 Methods declaration: Fragments of code that allows us to
create actions in our class.
 Principal method main(). In this method, we write the order
you want to execute the program, because this is de first
method in the execution.
 Variables declaration. It is used to create objects of classes
that have been created.
 “Sentencias”.It is the part that runs in the program.
Objets. Creation
 Variables declaration.
 We use the new operator to create an object (allocate memory)
 Object without instance: A reference to an object might not be
assigned to any instance, that is, it might not have been done memory
allocation. There is a special value called null, which indicates when
an object instance is not assigned. Then, when you declare a new
cocheC of Coche class:
Coche cocheA;
Coche cocheB;
cocheA= new Coche();
cocheB= new Coche();
Coche cocheC; // vale "null" por defecto
Objets. Attributes
 Attributes allow you to save the information and features of an object.
Example:
We need to know some information about a car.
- Model
- Car registration
- Color
We have to print the car model.
Objets. Attributes
Solution:
class Coche {
private String modelo;
private String matricula;
private String color;
public void setModelo( String modelo){
this.modelo = modelo;
}
public String getModelo(){
return this.modelo;
}
public static void main(String[] args) {
Coche coche = new Coche();
coche.setModelo ("Audi 100"); //Se podría hacer así (Correcto)
System.out.println("El modelo es: " + coche.getModelo() );
//o así ( Intentar evitarlo) System.out.println("El modelo es: " + coche.modelo);
}
}
In this case, we have declared that Coche class has three attributes: model, car registration
and colour. As you can see, you can give an initial value to the attributes in the declaration or
not.
Objets. Attributes
Remember:
The attributes of a class must always be declared as private and you can access
them through access methods.
This is the way how we can control access to our variables.
private String Nombre ;
public void setNombre( String Nombre ){
this.Nombre = Nombre ;
}
public String getNombre(){
return Nombre
}
Objets. Methods
 The methods are used to define the object's behaviour in their interactions with
other objects, that is, the actions of the object. According to the example of the
Coche class objects, it can obtain his model, assign registration, and so on.
 We have two methods in Coche class: one called dameModelo() to ask the
class object for the model , and another, ponModelo() for assign a new model
to a Coche object.
class Coche{
private String modelo;
private String matricula;
private String color;
public String dameModelo { ... }
public void ponModelo(String nuevoModelo) { ... }
}
Objets. Constructor Methods
 To create an object, we use a new instruction followed by the class name and
“()”:
 Every class has a default constructor which is provided automatically and has
the same name as the class without parameters.
 In most common cases, when a class is created, if someone wants to instantiate
it, it will have to be created under certain conditions. This is what allows us
constructors. It would be interpreted as "if you want to use this class (car),
information about model, colour and registration must be given."
 You can create multiple constructors. To define a constructor, access type, class
name , parameters that i accepts and the code of de method have to be typed :
[access type] nombreClase (parameters){
body ;
}
Coche profesor = new Coche();
Objets. Constructor Methods
 Constructors declared replace the ones provided by default.
 Now, to build any car, what you need to provide the appropriate values according to the
constructor.
Example:
For instancing the class Coche, we have to know:
- The model of the car, its color and the registration.
- Solution:
class Coche {
Coche ( String modelo, String color, String matricula) {
this.modelo = modelo;
this.color = color;
this.matricula = matricula;
}
Coche (String modelo, String color ){
this.modelo= modelo;
this.color = color;
}
}
Coche cocheA = new Coche(“Audi100", "Granate", “Z-0000-AA”);
Coche cocheB = new Coche (“Audi100", "Granate");

More Related Content

Unidad o informatica en ingles

  • 2. Variables and Identifiers  A variable is a computer’s memory zone with a value stored to be used later in a program.  A variable is identified by: a name (identifier), data type, range of values.  Identifier: is the name given to a variable, class, or method.  Rules: – Can start with a Unicode letter, underscore (_), or dollar sign ($). – Are case-sensitive and have no maximum length. – The null value, or the Boolean values “true” or “false”, can´t be used. – Identifiers must be descriptive. Avoid use abbreviation
  • 3. Variables and Identifiers  Declaration and Initialization: We have to create variables before being used it in a program, indicating its name and type, that is, we have to declare the variable. int numAlumnos = 15; double radio = 3.14, importe = 102.95;
  • 4. Class and Objects  Classes are the base of programming oriented to objects.  A class is a set of data (attributes) and actions (methods) that use that data. The definition of a class is:  The word public is optional: If you use public, all classes can see, but if you can’t, only the classes in the same package can see it.  All variables and methods must be defined inside the class {…}. An object (instance), is a software bundle of related state and behaviour. Software objects are often used to model the real-world objects that you find in everyday life. [public] class Classname { // definition of attributes and methods ... } Persona primerObjeto; /(class Persona) Persona segundoObjeto; //(class Persona)
  • 5. Class and Objects  Important features of a class: – All attributes and methods of Java must belong to a class. – There aren´t global variables and global functions. – If a class extends another one, it inherits all its variables and methods. – Java has a standard class hierarchy (Java´s Api), so it doesn’t start from zero. Users can use that classes. – multiple heritage doesn´t exist . A class can only inherit from one class. – Every class in Java derives from Object class. It is the base of all Java class hierarchy. – In the same file, you can create several classes, but only one of them can be public. – The Java file (*.java) and the public class must be the same name. If the class within the file isn´t public, it is not necessary that the file is called as the class. – We refer to the methods or variables of our class through reference “this”. – The classes are grouped into packages, indicating in the first line: (package packageName;).
  • 6. Class. Structure of a class /**Structure of a Java class*/ public classNombreDeLaClase { // Class attributes // Class methods // main method, that indicates where the ejecution starts public static voidmain(String[] args) { // Method variables // Method ejecution } }
  • 7. Class. Structure of a class Elements of a class:  Class attributes: Variables you use in the class level.  Methods declaration: Fragments of code that allows us to create actions in our class.  Principal method main(). In this method, we write the order you want to execute the program, because this is de first method in the execution.  Variables declaration. It is used to create objects of classes that have been created.  “Sentencias”.It is the part that runs in the program.
  • 8. Objets. Creation  Variables declaration.  We use the new operator to create an object (allocate memory)  Object without instance: A reference to an object might not be assigned to any instance, that is, it might not have been done memory allocation. There is a special value called null, which indicates when an object instance is not assigned. Then, when you declare a new cocheC of Coche class: Coche cocheA; Coche cocheB; cocheA= new Coche(); cocheB= new Coche(); Coche cocheC; // vale "null" por defecto
  • 9. Objets. Attributes  Attributes allow you to save the information and features of an object. Example: We need to know some information about a car. - Model - Car registration - Color We have to print the car model.
  • 10. Objets. Attributes Solution: class Coche { private String modelo; private String matricula; private String color; public void setModelo( String modelo){ this.modelo = modelo; } public String getModelo(){ return this.modelo; } public static void main(String[] args) { Coche coche = new Coche(); coche.setModelo ("Audi 100"); //Se podría hacer así (Correcto) System.out.println("El modelo es: " + coche.getModelo() ); //o así ( Intentar evitarlo) System.out.println("El modelo es: " + coche.modelo); } } In this case, we have declared that Coche class has three attributes: model, car registration and colour. As you can see, you can give an initial value to the attributes in the declaration or not.
  • 11. Objets. Attributes Remember: The attributes of a class must always be declared as private and you can access them through access methods. This is the way how we can control access to our variables. private String Nombre ; public void setNombre( String Nombre ){ this.Nombre = Nombre ; } public String getNombre(){ return Nombre }
  • 12. Objets. Methods  The methods are used to define the object's behaviour in their interactions with other objects, that is, the actions of the object. According to the example of the Coche class objects, it can obtain his model, assign registration, and so on.  We have two methods in Coche class: one called dameModelo() to ask the class object for the model , and another, ponModelo() for assign a new model to a Coche object. class Coche{ private String modelo; private String matricula; private String color; public String dameModelo { ... } public void ponModelo(String nuevoModelo) { ... } }
  • 13. Objets. Constructor Methods  To create an object, we use a new instruction followed by the class name and “()”:  Every class has a default constructor which is provided automatically and has the same name as the class without parameters.  In most common cases, when a class is created, if someone wants to instantiate it, it will have to be created under certain conditions. This is what allows us constructors. It would be interpreted as "if you want to use this class (car), information about model, colour and registration must be given."  You can create multiple constructors. To define a constructor, access type, class name , parameters that i accepts and the code of de method have to be typed : [access type] nombreClase (parameters){ body ; } Coche profesor = new Coche();
  • 14. Objets. Constructor Methods  Constructors declared replace the ones provided by default.  Now, to build any car, what you need to provide the appropriate values according to the constructor. Example: For instancing the class Coche, we have to know: - The model of the car, its color and the registration. - Solution: class Coche { Coche ( String modelo, String color, String matricula) { this.modelo = modelo; this.color = color; this.matricula = matricula; } Coche (String modelo, String color ){ this.modelo= modelo; this.color = color; } } Coche cocheA = new Coche(“Audi100", "Granate", “Z-0000-AA”); Coche cocheB = new Coche (“Audi100", "Granate");