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

Java and OOP Basics Session 2 - Academy

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

Java and OOP Basics Session 2 - Academy

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

Java and OOP

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:

● Preventing naming conflicts.


● Providing controlled access.

Common naming convention:

● org.geeksforgeeks.practice
● org.globant.java.basic
Class structure
package com.globant.java.basic;

import java.util.Date;

public class Person {


private String name = "Andres";
private int age = 25;
private Date birthDate = new Date("11/14/1993");

public void updateInformation(String newName, int newAge, Date newBirthDate) {


this.name = newName;
this.age = newAge;
this.birthDate = newBirthDate;
}

public String printInformation() {


String fullInformation = ( "Name: " + this.name + "\nAge: " + this.age + "\nBirth date: " + this.birthDate);
return fullInformation;
}
}
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
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{

public int sum(int a, int b) {


return a+b; Non Static
}
}

import Calculator.*;

public static void main(String[] args) {

Calculator calculator = new Calculator ();


int result = Calculator.sum( 20,5);
}
Example 1
public class Calculator{

public static int sum(int a, int b) {


return a+b; Static
}
}

import static Calculator.*;

public static void main(String[] args) {

int result = sum(20,5);

}
Example 2
public class Person{

private String name ="Pedro";


Non Static
public String getName() {
return name;
}
}

import Person.*;

public static void main(String[] args) {

Person person1 = new Person();


String name = person1.getName();
}
Example 2
public class Person{

private static String name ="Pedro";


Static
public static String getName() {
return name;
}
}

import static Person.*;

public static void main(String[] args) {

String name = getName();


}
Conditionals in Java
if (person.getAge() <= 17){
System. out.println( "La persona es menor de edad");
} else {
System. out.println( "La persona es mayor de edad");
}

if ((a > b && a > c)|| a > 100 ){


System. out.println( "A es mayor que b y c, o a es mayor que 100");
}

Note: The logic operator “AND” is represented as “&&”; and the operator “OR” as “||”
Loops in Java
for (int i=0; i<10; i++){

System. out.println( "El contador va en: " + 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:

● List (Data list accessible on any point)


● Set (Unordered group with non repeated data)
● Map (Data group with structure value - key)
● Queue (First In First Out)
● Dequeue (Double Ended Queue)
Collections: Operation
● Add a new element
● Get a specific position
● Get size
● Search elements
● Remove an element
List
public static void main(String[] args){

List<int> myList= new ArrayList< int>();


myList.add(3);
myList.add(4);
myList.add(6);

myList.set(1,5);

for (int i=0; i<myList.size(); i++){


System. out.println( myList.get(i));
}
}
Example
public static void main(String[] args){

ArrayList <String> names = new ArrayList<>();


names.add("Felipe");
names.add("Carlos");
names.add("María");

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:

1. Add 5 recipes to the menu


2. Replace the third option for a vegan recipe
3. Print the amount of recipes on the menu
4. Print the whole menu specifying name and price.

You might also like