Java and OOP Basics - Academy
Java and OOP Basics - Academy
Basics
Quality Engineering Studio
Bogotá - Colombia
Academy
Agenda
● Java Basics
● Java Setup
● Data Types
● OOP
● Classes
● Objects
● Attributes
● Access Modifiers
● Methods
Java Basics
Java is a programming language created on 1996, whose philosophy is based on
5 principles.
1. It is object oriented
2. It allows to run the same program on multiple platforms
3. It does include by default support for working on networks
4. It allows secure remote execution
5. It is easy to use
Java Setup
1. Install JDK 8 and JRE 8
2. Add JDK/bin on system PATH
3. Add JAVA_HOME environment variable with JDK route.
4. Add JAVA_HOME to PATH environment variable
5. On CMD run java -version and javac -version
6. Install the IDE
Data Types
● Integer numbers:
○ byte (8 bits) (until 127)
○ short (16 bits) (until 32767)
○ int (32 bits) (until 2000 millions)
○ long (64 bits) (until 9 million billions)
● Real numbers:
○ float (32 bits)
○ double (64 bits)
● Character:
○ char (16 bits) (unicode value)
● Boolean:
○ boolean (true / false)
OOP (Object Oriented Programming)
Programming paradigm in which objects control data inputs to generate correct
data outputs. Programming is done close to an imperative way, but keeping
modularity and maintainability.
Class
Defines the
structure of
Attributes Methods
Objects
Store their features Communicate
and state on between them
with
Access Modifiers
Indicators to determine who is going to be able to access to the object properties,
methods, or even to its own class.
● Default
● Private
● Protected
● Public
Access Modifier
Classes
A class defines the properties and behavior of a certain object type
<Access Modifier> class <name> {
<Class body>
}
class Person { class Car {
} }
Note:
A class name should always start with a capital letter and should use CamelCase
Attributes
Features or properties of the class
Form:
<Access Modifier> <Return data type> <name> ( <parameters>){
<Method body>
Note:
A method name should always start with a lowercase letter and should use CamelCase
Methods
public int sumNumbers( int num1, int num2){
return num1 + num2;
}
Methods:
protected int getAge(){}
public String setFullName(String name, String last){}
private boolean isAlive(Person p){}
Classes:
public class Person{}
class Animal{}
Reading and Printing on Java
Printing Reading
System.out.println( "Hello"); Scanner scan = new Scanner(System. in);
int number = scan.nextInt();
String name = scan.nextLine();
Putting all together: Objects
Getters:
public String getName(){
return this.name;
}
Usage: person.setFirstName(“Jhon”);
Good news! You can generate getters and setters with the IDE
Methods (Again!)
Constructor: used to initialize the object’s state. Each time an object is created using new() keyword at least
one constructor (it could be default constructor) is invoked to assign initial values to the data members of the
class. Constructor(s) of a class must has same name as the class name in which it resides.
public Account(){
}
public Account(){
this.name = “John Snow”;
this.address = “Dorado 45 st.”;
this.balance = 200;
}
}
Exercise
● Read the needed values to create each figure defined, create instances of
each figure, and print the areas and perimeters of each figure.
Exercises
● For my last birthday, my mom gave me a calculator to help me to approve my
physics test. My new calculator is able to perform the following operations:
○ Add, subtract, divide and multiply.