Java and OOP Basics Session 2 - Academy
Java and OOP Basics Session 2 - Academy
Basics
Quality Engineering Studio
Bogotá - Colombia
Agenda
● Last session
● Package
● Class structure
● Static modifier
● Conditionals
● Loops
● Collections
Last Session
● What data types did you learn about?
● What is a Class?
● What is an Attribute?
● What is an Object?
● What happens with String?
● How many Access Modifiers are?
Package
It’s a mechanism to encapsulate a group of classes, sub packages or other
element.s They are used for:
● org.geeksforgeeks.practice
● org.globant.java.basic
Class structure
package com.globant.java.basic;
import java.util.Date;
Getters:
public String getName(){
return this.name;
}
Usage: person.setFirstName(“Jhon”);
Good news! You can generate getters and setters with the IDE
Static
When the static keyword is used it implies there are class attributes or methods. In
that case the element is unique for all instances (objects) of the class (it occupies
a single place in memory).
On attributes:
<Access Modifier> static <Data type> <name>;
On methods:
<Access Modifier> static <Return Data type> <name> (<parameters..>){
<Method body>
};
Example 1
public class Calculator{
import Calculator.*;
}
Example 2
public class Person{
import Person.*;
Note: The logic operator “AND” is represented as “&&”; and the operator “OR” as “||”
Loops in Java
for (int i=0; i<10; i++){
int i = 0;
while (i<10) {
System. out.println( "El contador va en: " + i);
i = i+1;
}
Collections
Data structures to organize in different ways data groups of any type or class.
Example: String
0 1 2 3 4 5 6 7 8 9 10
Collections
Data structures to organize in different ways data groups of any type or class.
Some collection types on java are:
myList.set(1,5);
names.get(0);
int size = names.size();
names.remove(1);
int size2 = names.size();
int index= names.indexOf( "María");
names.get(index) ;
}
Exercise
● In order to become my own boss, I gonna start a supermarket business. I
need a program to help me:
○ Add the new products I get on my inventory to sell in the supermarket.
○ List the selling prices of every product
○ Sell the available products
○ Remove from my inventory the sold products
Homework
I have a restaurant, with its respective name and menú. Each option on the menú
contains its name and price. Make a program to: