Week 2 -Object-Oriented Programming & GitHub

Welcome to Java!

  • Introduction to Java focused on Object-Oriented Programming (OOP).

  • Week Two of the course.

Week Two Objectives

  • L02-01: Using Java Methods

  • L02-02: Using Java Recursive Methods

  • L02-03: Learning Object-Oriented Programming Principles

  • L02-04: Java Object & Class

  • L02-05: GitHub Setting Up

Using Java Methods

Overview

  • A method is a block of code that runs when called.

  • Methods can take parameters and return results.

  • Essential for code reuse.

Example Code

  • Example Java code for method creation (incomplete code).

What is a Method?

  • A method performs certain functions and actions.

  • Methods allow passing parameters and returning results.

  • Reusability: code can be defined once and utilized multiple times.

Declaring a Java Method

Syntax

  • returnType methodName(datatype parameter1, datatype parameter2, ...) { }

  • Components:

    • returnType: Defines what value is returned (or 'void' if none).

    • methodName: Identifier for the method.

    • method body: Enclosed in {} containing the statements.

Example Code

  • Example of a method that adds two numbers:

int addNumbers(int numberOne, int numberTwo) {
int result = numberOne + numberTwo;
return result;
}

Using a Method

Example Call

  • int answer = addNumbers(20, 30);

Complete Syntax for Declaring a Method

  • modifier static returnType nameOfMethod(parameter1 datatype parameter01, ...) { }

Example Code

  • Example method for adding two doubles:

public static double add(double numberOne, double numberTwo) {
return numberOne + numberTwo;
}
  • modifier: Defines access level (e.g., public, private).

  • parameters: Values passed into the method.

Exercises

Exercise 02

  • Create a method that passes two numbers and returns the largest.

Exercise 03

  • Implement a basic calculator using Java methods:

    • Take two numbers as input

    • Take an operator input (+, -, *, /)

    • Display the result

Using Java Methods Recursively

Overview

  • Recursion: when a function calls itself.

  • Simplifies complex problems into manageable ones.

Recursion Example

Adding Numbers

  • Sum(n) = 0 + 1 + 2 + ... + n

  • For n=10, calculate Sum(10) using recursion.

Recursive Relationship

  • Sum(n) = n + sum(n-1)

  • Base Case: Sum(0) = 0

Example Code

public class Example {
public static void main(String[] args) {
int result = sum(10);
System.out.println(result);
}
public static int sum(int n) {
if (n > 0) {
return n + sum(n - 1);
} else {
return 0;
}
}
}

Exercises on Recursion

Exercise 04

  • Write a program to find the factorial of a number using recursion.

  • Definition: factorial of n (n!) = 1 * 2 * ... * n.

Object-Oriented Programming Principles

Introduction

  • OOP: modular, reusable software systems modeled as objects with attributes and behaviors.

OOP Advantages

Key Concepts
  • Modularity: Creating self-contained objects to promote reusable designs.

  • Code Organization: Makes code easier to understand and maintain.

  • Encapsulation: Hides internal workings of an object for improved data security.

  • Inheritance: Allows subclasses to inherit properties from superclasses, promoting code reuse.

  • Abstraction: Focuses on high-level operations while hiding complex implementation.

  • Polymorphism: Enables objects to respond to method calls in multiple ways.

Modeling in OOP

Real-World Entities

  • OOP models real-world concepts, improving developer-stakeholder communication.

Collaboration & Software Design

  • Supports team collaboration; encourages well-planned software architecture.

Industry Standard

  • Languages like Java, C++, and Python support OOP, establishing it as a common programming approach.

Java Object & Class

Classes vs. Objects

  • Class: Blueprint for creating objects, defining properties and behaviors.

  • Object: Instance of a class representing specific entities with data and operations.

Class Syntax

  • class {ClassName} { }

Example Code

public class Person {
String name;
int age;
void walk() {
System.out.println("walking...");
}
}

Creating Objects in Java

Syntax

  • {ClassName} {ObjectName} = new {ClassName}();

Example

  • Person personOne = new Person();

Accessing Attributes

  • Use dot operator:

    • E.g., personOne.name = "John";

Declaring, Instantiating, and Initializing an Object

Steps

  1. Declaration: Person person;

  2. Instantiation: person = new Person();

  3. Initialization: Classes provide constructors for object initialization.

Exercises

Exercise 05 - Modeling a Book

  • Create a Java class representing a book with attributes: title, author, isbn, publishedYear.

  • Include methods barrow() and return().

Exercise 06 - Modeling a Car

  • Define a class named Car with attributes for make, model, year, color, vehicle number.

  • Include methods start() and stop().

Exercise 07 - Modeling a Student

  • Define a class named Student with appropriate attributes and methods.

GitHub and Version Control

What is Git?

  • A distributed, open-source version control system for tracking code changes, merging, and reverting.

  • Industry standard for developers and data scientists.

How Git Works

  • Stores files and history in a local repository; creates commits.

  • Commits are snapshots linked in a history graph.

Branching

  • Branches allow parallel works without interference; can be merged back to the main version.

Commits

  • Files can be in modified, staged, or committed states.

Setting Up Git and GitHub

  • Install Git on Windows through download link.

  • Create a GitHub account and repository.

Configuring GitHub

  • Configure user name and email in command line.

Clone, Initialize, and Commit

  • Clone repository via HTTPS link; initialize a new repository with Git commands.

Basic Git Commands

  • git init: Creates a local repo.

  • git clone <url>: Copies a remote repo.

  • git add <file>: Stages files for commit.

  • git commit -m "Message": Creates a snapshot of changes.

  • git push: Sends commits to the remote repo.

  • Further commands for managing branches, viewing remotes, and merging.

robot