Introduction to Java focused on Object-Oriented Programming (OOP).
Week Two of the course.
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
A method is a block of code that runs when called.
Methods can take parameters and return results.
Essential for code reuse.
Example Java code for method creation (incomplete code).
A method performs certain functions and actions.
Methods allow passing parameters and returning results.
Reusability: code can be defined once and utilized multiple times.
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 of a method that adds two numbers:
int addNumbers(int numberOne, int numberTwo) {
int result = numberOne + numberTwo;
return result;
}
int answer = addNumbers(20, 30);
modifier static returnType nameOfMethod(parameter1 datatype parameter01, ...) { }
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.
Create a method that passes two numbers and returns the largest.
Implement a basic calculator using Java methods:
Take two numbers as input
Take an operator input (+, -, *, /)
Display the result
Recursion: when a function calls itself.
Simplifies complex problems into manageable ones.
Sum(n) = 0 + 1 + 2 + ... + n
For n=10, calculate Sum(10) using recursion.
Sum(n) = n + sum(n-1)
Base Case: Sum(0) = 0
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;
}
}
}
Write a program to find the factorial of a number using recursion.
Definition: factorial of n (n!) = 1 * 2 * ... * n.
OOP: modular, reusable software systems modeled as objects with attributes and behaviors.
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.
OOP models real-world concepts, improving developer-stakeholder communication.
Supports team collaboration; encourages well-planned software architecture.
Languages like Java, C++, and Python support OOP, establishing it as a common programming approach.
Class: Blueprint for creating objects, defining properties and behaviors.
Object: Instance of a class representing specific entities with data and operations.
class {ClassName} { }
public class Person {
String name;
int age;
void walk() {
System.out.println("walking...");
}
}
{ClassName} {ObjectName} = new {ClassName}();
Person personOne = new Person();
Use dot operator:
E.g., personOne.name = "John";
Declaration: Person person;
Instantiation: person = new Person();
Initialization: Classes provide constructors for object initialization.
Create a Java class representing a book with attributes: title, author, isbn, publishedYear.
Include methods barrow() and return().
Define a class named Car with attributes for make, model, year, color, vehicle number.
Include methods start() and stop().
Define a class named Student with appropriate attributes and methods.
A distributed, open-source version control system for tracking code changes, merging, and reverting.
Industry standard for developers and data scientists.
Stores files and history in a local repository; creates commits.
Commits are snapshots linked in a history graph.
Branches allow parallel works without interference; can be merged back to the main version.
Files can be in modified, staged, or committed states.
Install Git on Windows through download link.
Create a GitHub account and repository.
Configure user name and email in command line.
Clone repository via HTTPS link; initialize a new repository with 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.