0% found this document useful (0 votes)
6 views

Java and OOP Basics - Academy

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java and OOP Basics - Academy

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Java and OOP

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

class Person { class Car {


String first_name; String brand;
String last_name; String model;
int id; int seats_number;
} }
Methods
Algorithms to define what an object can do, implying issues like object
instantiation, setting properties, getting properties, etc.

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;
}

public void printNumber( int number){


System. out.println( "Number " + number);
}

public boolean isEnoughPeople ( int numberOfPeople){


return numberOfPeople >= 10;
}
Access Modifier
Attributes:
protected int age = 12;
public String name = “Andres”;
private boolean isAlive = true;
String lastName = “Castillo”;

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

public class Account { What if I need to create multiple accounts?


private String name;
private String address;
private double balance;

public void display (String name, String address,


double balance) {
System.out.print(name);
System.out.print(" (");
System.out.print(address);
System.out.print(") has $");
System.out.print(balance);
}

public String getName(){


return this.name;
}
}
Putting all together: Objects

An instance of a specified class, corresponding to a real world object. It contains


certain properties and behavior.

Account account = new Account();

account.display(“Barry”,”Dorado 45 St.”, 200);


Exercise
● What is the difference between a class and an object?
● Define in Java a class for a square, a rectangle and a triangle, including the
needed attributes.
● Add access modifiers to the classes defined before.
Methods (Again!)
Accessor methods: Provide access to the Account class’s attributes.

Getters:
public String getName(){
return this.name;
}

Bad practice Usage: person.getFirstName();

account.name = "Barry Burd";

In fact, it should not be allowed since attributes Setters:


public void setName(String name){
should be private on every class this.name = 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(){
}

Usage: Account myAccount = new Account();


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(){
this.name = “John Snow”;
this.address = “Dorado 45 st.”;
this.balance = 200;
}

Usage: Account myAccount = new Account();


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(String name, String address, double balance){


this.name = name;
this.address = address;
this.balance = balance;
}

Usage: Account myAccount = new Account(“John Snow”,”Dorado 45 st.”, 200 );


Methods Exercise
● Include constructors to the previously defined classes
Main Class
Class where the defined classes and objects are going to be tested

public class Test {

public static void main(String args[]){


//main code goes here, example below
Account account = new Account();
account.display(“Barry”,”Dorado 45 St.”, 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.

I should create a program to agilize this operations.


Homework
Develop a Java program to manage a bank account. The account contains the
owner name and the money amount, which should be $0 by default.The user may
choose between add and retrieve money from the account. Every transaction
should print a message to the user showing the money amount on the account
after the transaction. The user should not be able to retrieve a money amount
higher than the available funds. The user should be able to finish the execution
any time.
Q&A

You might also like