Monday, April 28, 2025

Java OO Tutorial - Interfaces

An interface is a collection of abstract methods (methods without an implementation) and constants. An interface can be used to define a set of methods that a class must implement, without specifying how those methods are implemented.

To declare an interface in Java, you use the interface keyword followed by the name of the interface. 

interface Car {
	public void operationA();
	public void operationB();
}

interface Bike {
	public void lights();
}

class ModelA implements Car, Bike {
	
	public void operationA() {
		System.out.println("Operation A");
	}
	
	public void operationB() {
		System.out.println("Operation B");
	}
	
	public void lights() {
		System.out.println("Generic Lights Starts");
	}
}

public class Main {	
	
	public static void main(String[] args) {
		
		ModelA firstObj = new ModelA();
		firstObj.operationA();
		firstObj.operationB();
		firstObj.lights();
	}		
}

This code defines an interface Car with two abstract methods operationA() and operationB(). It also defines another interface Bike with one abstract method lights()

interface Car {
	public void operationA();
	public void operationB();
}

interface Bike {
	public void lights();
}

Next, a class named ModelA is defined that implements both the Car and Bike interfaces. This class provides the implementation of all the abstract methods declared in the interfaces it implements. 

class ModelA implements Car, Bike {
	public void operationA() {
		System.out.println("Operation A");
	}
	
	public void operationB() {
		System.out.println("Operation B");
	}
	
	public void lights() {
		System.out.println("Generic Lights Starts");
	}
}

The operationA() method of ModelA class simply prints "Operation A" to the console. The operationB() method of ModelA class prints "Operation B" to the console. The lights() method of ModelA class prints "Generic Lights Starts" to the console.

Finally, in the main() method of the Main class, an instance of the ModelA class is created and the methods operationA(), operationB(), and lights() are called on it. 

public class Main {	
	
	public static void main(String[] args) {
		
		ModelA firstObj = new ModelA();
		firstObj.operationA();
		firstObj.operationB();
		firstObj.lights();
	}		
}

This code demonstrates how a class can implement multiple interfaces and provide implementation for all the methods declared in them. It also demonstrates how objects of a class can be created and methods can be called on them.

Java OO Tutorial - Getters and Setters - Encapsulation

Getters and setters are methods that are used to access and modify the values of private instance variables of a class, respectively.

A getter method is used to retrieve the value of a private instance variable, and it typically has a name that starts with "get" followed by the name of the variable. For example, if a class has a private instance variable named "name", then the getter method for that variable could be named "getName()". A typical implementation of a getter method simply returns the value of the instance variable.

A setter method, on the other hand, is used to set the value of a private instance variable, and it typically has a name that starts with "set" followed by the name of the variable. For example, if a class has a private instance variable named "age", then the setter method for that variable could be named "setAge(int age)". A typical implementation of a setter method takes a parameter of the same type as the instance variable and sets its value to the instance variable.

By using getters and setters, you can control the access to the instance variables of a class, and you can enforce constraints on the values that are being set. For example, you could check if the value being set is within a certain range or if it meets certain conditions before actually setting it. This way, you can ensure that the state of your object remains consistent and valid.

//Base.java

public class Base {
	
	private String name;
	
	public void setName(String a) {
		this.name = a;
	}
	
	public String getName() {
		return name;
	}	
}

Our code defines a class named Base that has a private instance variable name of type String. It also provides two methods to access and modify the value of this variable: a setter method setName() and a getter method getName().

The setName() method takes a parameter of type String and sets the value of the name variable to the value of the parameter. The getName() method returns the current value of the name variable.

The use of the private access modifier for the name variable ensures that it can only be accessed and modified within the Base class. By providing public getter and setter methods, other classes can access and modify the value of the name variable in a controlled way, without exposing the variable itself to direct modification from outside the class.

Note that the use of the this keyword in the setName() method is used to refer to the instance variable name, which is otherwise shadowed by the method parameter a of the same name.

//Main.java

public class Main {	
	
	public static void main(String[] args) {
		
		Base someObj = new Base();
		someObj.setName("Some Value");
		
		System.out.println(someObj.getName());		
	}		
}

In this case our code defines a class named Main that has a main() method. The main() method creates a new object of type Base named someObj using the new operator. It then calls the setName() method on the someObj object, passing it a string value "Some Value" as a parameter. This sets the value of the name instance variable of the someObj object to "Some Value".

Finally, the main() method calls the getName() method on the someObj object and passes its return value to the println() method of the System.out object, which prints the value of the name instance variable to the console.

This code demonstrates the use of getter and setter methods to access and modify private instance variables of an object in a controlled way. It also demonstrates the use of the new operator to create new objects of a class, and the use of the System.out.println() method to print output to the console.

Java OO Tutorial - Abstract Classes and Methods

An abstract class is a class that is declared with the abstract keyword, which means that it cannot be instantiated directly. An abstract class is designed to be extended by other classes, and it provides a base set of methods or functionality that derived classes can implement or override. An abstract class can have both abstract and non-abstract methods.

An abstract method is a method that is declared with the abstract keyword, but does not have an implementation. It only has a method signature, which includes the method name, return type, and parameters, but does not have any method body. Abstract methods are declared in abstract classes, and they are meant to be implemented by non-abstract subclasses. 

abstract class Base {
	
	public abstract void firstAbsMethod();
	public abstract void secondAbsMethod();	
	
}

class Details extends Base {
	
	public void firstAbsMethod() {
		System.out.println("I am from firstAbsMethod");
	}
	
	public void secondAbsMethod() {
		System.out.println("I am from secondAbsMethod");
	}
}

public class Main {	
	
	public static void main(String[] args) {
	
		Details someObj = new Details();
		someObj.firstAbsMethod();
		someObj.secondAbsMethod();
	}		
}

Here is a block-by-block explanation of the code: 

abstract class Base {
    public abstract void firstAbsMethod();
    public abstract void secondAbsMethod();
}

This defines an abstract class Base that contains two abstract methods named firstAbsMethod and secondAbsMethod. These methods have no implementation and are intended to be overridden by subclasses of Base

class Details extends Base {
    public void firstAbsMethod() {
        System.out.println("I am from firstAbsMethod");
    }
    public void secondAbsMethod() {
        System.out.println("I am from secondAbsMethod");
    }
}

The Details class extends the Base class and provides implementations for the two abstract methods declared in Base. In other words, Details is a concrete class that can be instantiated and used. It defines its own behavior for the two abstract methods, providing the necessary implementations. 

public class Main {
    public static void main(String[] args) {
        Details someObj = new Details();
        someObj.firstAbsMethod();
        someObj.secondAbsMethod();
    }
}

Finally, the Main class contains a main method that creates an instance of Details and calls its firstAbsMethod and secondAbsMethod methods. The output of running this program will be: 

I am from firstAbsMethod
I am from secondAbsMethod

This demonstrates the concept of abstract classes and methods in Java, where a subclass must provide implementations for the abstract methods declared in its superclass, or else be declared abstract itself.

Java OO Tutorial - Inner Class - Nesting

An inner class is a class that is defined within the body of another class.  

class OutsiderClass {
	
	public void outsiderMethod() {
		System.out.println("I am from outsiderMethod");
	}
	
	class InsiderClass {
		
		public void insiderMethod() {
			System.out.println("I am from insiderMethod");
		}
	}
}

public class Main {	
	
	public static void main(String[] args) {
		
		OutsiderClass outObj = new OutsiderClass();
		outObj.outsiderMethod();
		
		OutsiderClass.InsiderClass inObj = outObj.new InsiderClass();
		inObj.insiderMethod();		
	}		
}

Here's a detailed explanation of the code, block by block: 

class OutsiderClass {
	
	public void outsiderMethod() {
		System.out.println("I am from outsiderMethod");
	}
	
	class InsiderClass {
		
		public void insiderMethod() {
			System.out.println("I am from insiderMethod");
		}
	}
}

The above code defines two classes: OutsiderClass and InsiderClass. InsiderClass is an inner class defined inside OutsiderClass.

OutsiderClass has one method outsiderMethod() that simply prints out a message to the console using System.out.println().

InsiderClass has one method insiderMethod() that also prints out a message to the console. 

public class Main {	
	
	public static void main(String[] args) {
		
		OutsiderClass outObj = new OutsiderClass();
		outObj.outsiderMethod();
		
		OutsiderClass.InsiderClass inObj = outObj.new InsiderClass();
		inObj.insiderMethod();		
	}		
}

The above code is the Main class, which contains the main() method. Inside the main() method:

  1. An instance of OutsiderClass is created using the new keyword: OutsiderClass outObj = new OutsiderClass();
  2. The outsiderMethod() is called on the instance of OutsiderClass using the dot notation: outObj.outsiderMethod();
  3. An instance of InsiderClass is created using the new keyword and the constructor for InsiderClass, which is accessed using the instance of OutsiderClass created earlier: OutsiderClass.InsiderClass inObj = outObj.new InsiderClass();
  4. The insiderMethod() is called on the instance of InsiderClass using the dot notation: inObj.insiderMethod();

When this code is run, it will output the following to the console: 

I am from outsiderMethod
I am from insiderMethod

Java OO Tutorial - super Keyword in Java

In Java, super is a keyword that refers to the immediate parent class of a subclass. It is used to call a constructor, method, or variable of the parent class from within the subclass. 

class Primary {
	
	public void sameName() {
		System.out.println("I am from Primary Class");
	}
}

class Secondary extends Primary {
	
	public void sameName() {
		super.sameName();
		System.out.println("I am from Secondary Class");
	}	
}

public class Main {	
	
	public static void main(String[] args) {
		
		//Primary primObj = new Primary();
		//primObj.sameName();
		
		Secondary secObj = new Secondary();
		secObj.sameName();		
	}	
}

This Java program defines three classes: Primary, Secondary, and Main.

The Primary class has one method called sameName(), which simply prints the message "I am from Primary Class" to the console. 

class Primary {
    public void sameName() {
        System.out.println("I am from Primary Class");
    }
}

The Secondary class extends the Primary class and overrides the sameName() method. Inside the sameName() method of the Secondary class, the super keyword is used to call the sameName() method of the parent class, and then it prints the message "I am from Secondary Class" to the console. 

class Secondary extends Primary {
    public void sameName() {
        super.sameName();
        System.out.println("I am from Secondary Class");
    }
}

The Main class contains the main() method, which creates an object of the Secondary class using the new keyword and assigns it to a reference variable named secObj. Then, it calls the sameName() method on the secObj reference variable. 

public class Main {
    public static void main(String[] args) {
        Secondary secObj = new Secondary();
        secObj.sameName();
    }
}

When the sameName() method of the secObj object is called, the following output will be printed to the console: 

I am from Primary Class
I am from Secondary Class

This shows that when a subclass overrides a method of its parent class, it can call the overridden method of the parent class using the super keyword, and then it can add its own functionality to the method.

In this example, the Secondary class added the message "I am from Secondary Class" to the output, but it first called the sameName() method of the parent Primary class to include its message "I am from Primary Class".

Java OO Tutorial - this Keyword in Java

In Java, this is a keyword that refers to the current instance of a class. It can be used inside the methods or constructors of a class to refer to the current object on which the method is being called or the constructor is being invoked.

class InternalOperations {
	int x;
	int y;
	
	public InternalOperations(int a, int b) {
		this.x = a;
		this.y = b;		
	} 	
}

public class Main {	
	
	public static void main(String[] args) {
		
		InternalOperations someObj = new InternalOperations(10, 20); 
						
		System.out.println("X Value: " + someObj.x);
		System.out.println("Y Value: " + someObj.y);
		
		InternalOperations otherObj = new InternalOperations(5, 55); 
		
		System.out.println("X Value: " + otherObj.x);
		System.out.println("Y Value: " + otherObj.y);		
	}	
}

This Java program defines two classes: InternalOperations and Main.

The InternalOperations class has two instance variables x and y of type int. It also has a constructor that takes two integer parameters a and b, and initializes the instance variables x and y with the values of the parameters. 

class InternalOperations {
    int x;
    int y;

    public InternalOperations(int a, int b) {
        this.x = a;
        this.y = b;
    }
}

The Main class contains the main() method, which is the entry point of the program.

Inside the main() method, two objects of the InternalOperations class are created using the new keyword and the constructor that takes two integer arguments.

The first object is assigned to a reference variable named someObj, and the second object is assigned to a reference variable named otherObj

public class Main {
    public static void main(String[] args) {
        InternalOperations someObj = new InternalOperations(10, 20);
        InternalOperations otherObj = new InternalOperations(5, 55);
        // ...
    }
}

After the objects are created, the values of the instance variables x and y of both objects are printed using the System.out.println() method. 

System.out.println("X Value: " + someObj.x);
System.out.println("Y Value: " + someObj.y);

System.out.println("X Value: " + otherObj.x);
System.out.println("Y Value: " + otherObj.y);

The output of the program will be: 

X Value: 10
Y Value: 20
X Value: 5
Y Value: 55

This shows that each object of the InternalOperations class has its own instance variables x and y, which can be initialized and accessed independently of each other.

The program demonstrates the use of constructors to create objects of a class and the use of instance variables to store object-specific data. 

Java OO Tutorial - Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit the properties (fields) and behaviors (methods) of another class. In Java, we use the "extends" keyword to create a subclass that inherits from a parent (superclass) class.

A subclass can inherit fields and methods from a superclass and also add its own fields and methods, and override the behavior of inherited methods. The benefit of inheritance is that it allows us to reuse code that already exists in a superclass, reducing the amount of code we need to write. 

//Operations.java 

class BasicOperations {
	
	public void addition(int x, int y) {
		System.out.println(x + y);
	}
}

class MoreOperations extends BasicOperations {
	public void multiplication(int x, int y) {
		System.out.println(x * y);
	}
}

public class Operations extends MoreOperations {
	
	public void substraction(int x, int y) {
		System.out.println(x - y);
	}

	public static void main(String[] args) {
		
		Operations myObj = new Operations();
		
		myObj.addition(10, 5);
		myObj.substraction(10, 5);
		myObj.multiplication(10, 5);	

	}
}

Here's an explanation of the code block by block: 

//Operations.java 

class BasicOperations {
	
	public void addition(int x, int y) {
		System.out.println(x + y);
	}
}

This is the BasicOperations class with a single method addition that takes two integer arguments and prints their sum. 

class MoreOperations extends BasicOperations {
	public void multiplication(int x, int y) {
		System.out.println(x * y);
	}
}

This is the MoreOperations class that extends the BasicOperations class and adds a method multiplication that takes two integer arguments and prints their product. 

public class Operations extends MoreOperations {
	
	public void subtraction(int x, int y) {
		System.out.println(x - y);
	}

	public static void main(String[] args) {
		
		Operations myObj = new Operations();
		
		myObj.addition(10, 5);
		myObj.subtraction(10, 5);
		myObj.multiplication(10, 5);	

	}
}

This is the Operations class that extends the MoreOperations class and adds a method subtraction that takes two integer arguments and prints their difference. It also contains a main method that creates an instance of the Operations class and calls its addition, subtraction, and multiplication methods.

When we run the main method, it creates an instance of the Operations class and calls its addition, subtraction, and multiplication methods.

Since the Operations class extends the MoreOperations class, which in turn extends the BasicOperations class, it has access to all the methods in these classes.

Therefore, it can call the addition, subtraction, and multiplication methods without having to define them in the Operations class itself.

Java OO Tutorial - Constructors

A constructor is a special method that is used to create objects of a class. It is called automatically when an object is created using the "new" keyword. The constructor has the same name as the class and does not have a return type, not even void. 

public class Main {
	
	int x; 
	
	//This is Constructor - same name as Class name
	public Main() {
		x = 10;
	}	
			
	public static void main(String[] args) {
		
		Main newObj = new Main();
		
		System.out.println(newObj.x);
		
		newObj.x = 555;
		
		System.out.println(newObj.x);					
	}	
}

Here's a block-by-block breakdown of the program: 

public class Main {
    int x;

    public Main() {
        x = 10;
    }

In this block, we define a class named "Main" with a single member variable "x". We also define a constructor for this class that sets the value of "x" to 10. 

public static void main(String[] args) {
        Main newObj = new Main();

        System.out.println(newObj.x);

        newObj.x = 555;

        System.out.println(newObj.x);
    }

In this block, we define the main method which is the entry point for the program. Inside this method, we create an instance of the Main class named "newObj" using the constructor. We then print out the value of "x" using "System.out.println(newObj.x);", which will output "10" since that is the value set by the constructor. We then set the value of "x" to 555 and print it out again, which will output "555".

When we run this program, it will output the following to the console: 

10
555

This program demonstrates how a constructor can be used to set initial values for member variables when an object is created using the "new" keyword. In this case, the constructor sets the initial value of "x" to 10. 

public class Main {
	
	int x;
	String someString;
	
	//This is Constructor - same name as Class name
	public Main(int a, String b) {
		x = a;
		someString = b;
	}	
	
	public static void main(String[] args) {

		Main newObj = new Main(777, "hack The Planet");
		
		System.out.println("X value: " + newObj.x + " String Value: " + newObj.someString);	
	}	
}

Here's a block-by-block breakdown of the program: 

public class Main {
    int x;
    String someString;

    public Main(int a, String b) {
        x = a;
        someString = b;
    }

In this block, we define a class named "Main" with two member variables "x" and "someString". We also define a constructor for this class that takes two parameters, "a" and "b", which are used to set the values of "x" and "someString", respectively. 

public static void main(String[] args) {
        Main newObj = new Main(777, "hack The Planet");

        System.out.println("X value: " + newObj.x + " String Value: " + newObj.someString);
    }

In this block, we define the main method which is the entry point for the program. Inside this method, we create an instance of the Main class named "newObj" using the constructor and passing the arguments 777 and "hack The Planet". We then print out the value of "x" and "someString" using "System.out.println()", which will output "X value: 777 String Value: hack The Planet".

When we run this program, it will output the following to the console: 

X value: 777 String Value: hack The Planet

This program demonstrates how a constructor with parameters can be used to set initial values for member variables when an object is created using the "new" keyword with specific arguments. In this case, the constructor takes two arguments, an integer and a string, and sets the initial values of "x" and "someString" to the values passed as arguments.

Java OO Tutorial - Static vs Non Static Methods

Methods can be classified as either static or non-static. Here are the differences between these two types of methods:

  1. Static methods are associated with the class itself, not with any particular instance of the class. Non-static methods, on the other hand, are associated with an instance of the class.

  2. Static methods can be called without creating an instance of the class. Non-static methods, on the other hand, can only be called on an instance of the class.

  3. Static methods can only access static variables and other static methods. Non-static methods can access both static and non-static variables and methods.

  4. Static methods are often used for utility functions that do not depend on the state of any particular instance of a class. Non-static methods are often used for methods that operate on the state of an instance of a class. 

public class Main {
	
	static void directUsage() {
		System.out.println("Something from directUsage Method. ");
	}
	
	public void mustUseObjects() {
		System.out.println("I can be used only from Objects ");
	}
		
	public static void main(String[] args) {
		
		directUsage();
		
		Main newObj = new Main();
		
		newObj.mustUseObjects();
	}	
}

Here's a block-by-block breakdown of the program: 

public class Main {
    static void directUsage() {
        System.out.println("Something from directUsage Method. ");
    }

In this block, we define a static method named "directUsage" that simply prints out a message to the console. 

public void mustUseObjects() {
        System.out.println("I can be used only from Objects ");
    }

In this block, we define a non-static method named "mustUseObjects" that also prints out a message to the console. 

public static void main(String[] args) {
        directUsage();

        Main newObj = new Main();

        newObj.mustUseObjects();
    }

In this block, we define the main method which is the entry point for the program. Inside this method, we call the static method "directUsage" directly on the class. Then we create an instance of the Main class named "newObj" using the "new" keyword. Finally, we call the non-static method "mustUseObjects" on the "newObj" instance.

When we run this program, it will output the following to the console: 

Something from directUsage Method.
I can be used only from Objects

This program demonstrates that static methods can be called directly on the class without the need to create an instance of the class. Non-static methods, on the other hand, can only be called on an instance of the class.

Java OO Tutorial - Classes and Objects

Object-oriented programming (OOP) is a programming paradigm that is used in Java and other programming languages. It is based on the concept of objects, which can contain data and code to manipulate that data.

In Java, OOP is implemented using classes and objects.

A class is a blueprint or template that defines the characteristics and behavior of a particular type of object.

An object is an instance of a class, created using the "new" keyword, which has its own state and behavior.

In OOP, objects interact with each other by sending messages or calling methods. A method is a block of code that performs a specific task and can be called by an object. Methods can have parameters and return values, which allow them to take input and produce output. 

public class Main {	
	int x = 10;
	String someString = "Some Value";	
	
	public static void main(String[] args) {
		Main firstObj = new Main();
		Main secondObj = new Main();
		Main thirdObj = new Main();
		
		System.out.println(firstObj.x);
		
		System.out.println(secondObj.x);
		
		firstObj.x = 555;
		
		System.out.println(firstObj.x);
		
		System.out.println(thirdObj.someString);					
	}	
}

This is a small Java program that defines a class named "Main" and creates three objects of that class. Let's break it down block by block: 

public class Main {
    int x = 10;
    String someString = "Some Value";

In the first block, we define the Main class and declare two instance variables: "x" and "someString". "x" is an integer with a default value of 10, and "someString" is a string with a default value of "Some Value". 

public static void main(String[] args) {
    Main firstObj = new Main();
    Main secondObj = new Main();
    Main thirdObj = new Main();

In the second block, we define the main method which is the entry point for the program. Inside this method, we create three objects of the Main class: firstObj, secondObj, and thirdObj using the "new" keyword. 

System.out.println(firstObj.x);

System.out.println(secondObj.x);

In the third block, we print out the value of "x" for the firstObj and secondObj. Since "x" is an instance variable, each object has its own value of "x". The output of this block would be: 

10
10
firstObj.x = 555;

System.out.println(firstObj.x);

In the fourth block, we set the value of "x" for firstObj to 555 and print out its new value. The output of this block would be: 

555
System.out.println(thirdObj.someString);

In the fifth block, we print out the value of "someString" for thirdObj. Since "someString" is an instance variable, each object has its own value of "someString". The output of this block would be: 

Some Value

Overall, this program demonstrates the concept of object-oriented programming by creating multiple objects of a class and manipulating their instance variables.

Java Tutorial - Method Overloading

Method overloading in Java is a feature that allows a class to have multiple methods with the same name, but with different parameters. This means that you can define two or more methods with the same name in a class, as long as they have different parameter lists.

In method overloading, the compiler determines which version of the method to use based on the number, types, and order of the arguments passed to it. When you call an overloaded method, the compiler chooses the appropriate method to execute based on the arguments you pass to it. 

public class Main {
	
	public static int sameName(int x, int y) {
		return x + y;
	}
	
	public static String sameName(String x, String y) {
		return x + " " + y;
	}
	
	public static double sameName(double x, double y) {
		return x + y;
	}
	
	public static void main(String[] args) {
		
		System.out.println(sameName(5, 10));
		System.out.println(sameName("asdas", "asdasda"));
		
		System.out.println(sameName(5.0, 10.0));		
	}	
}

Here's an explanation of the code block by block:

  1. We begin with defining a Java class called Main
public class Main {
  1. Inside the Main class, we define three static methods with the same name sameName
public static int sameName(int x, int y) {
	return x + y;
}

public static String sameName(String x, String y) {
	return x + " " + y;
}

public static double sameName(double x, double y) {
	return x + y;
}
  1. Each of the sameName methods has a different signature, which means they take different types of parameters. The first method takes two int parameters, the second method takes two String parameters, and the third method takes two double parameters.

  2. Inside each method, we simply perform a simple arithmetic operation (+ operator for int and double parameters) or concatenate the String parameters with a space.

  3. In the main method, we call the sameName methods with different arguments and print the returned results: 

System.out.println(sameName(5, 10));
System.out.println(sameName("asdas", "asdasda"));
System.out.println(sameName(5.0, 10.0));		
  1. The first call to sameName passes two int arguments (5 and 10) and returns an int value (15). This is printed to the console.

  2. The second call to sameName passes two String arguments ("asdas" and "asdasda") and returns a String value ("asdas asdasda"). This is also printed to the console.

  3. The third call to sameName passes two double arguments (5.0 and 10.0) and returns a double value (15.0). This is also printed to the console.

In summary, this code demonstrates how method overloading works in Java by defining multiple methods with the same name but different parameter types, and then calling them with different arguments to perform different tasks.

Java Tutorial - Returning a Value from Method

The "return" keyword is used in methods to specify the value that the method should produce as output when it is called. The return type of a method determines the type of value that the method can return.

When a method is called, it executes its code and may produce a result. This result can be returned to the calling code using the "return" keyword. The value that is returned can be assigned to a variable or used in an expression in the calling code.

public class Main {	
	
	public static int simpleCalc(int x, int y) {
		int result = x + y;
		return result;
	}
	
	public static void doubleIt(int x, int y) {
		int dResult = (simpleCalc(x, y) * 2);
		System.out.println(dResult);
	}

	public static void main(String[] args) {								
				
		System.out.println(simpleCalc(5, 5));	
		
		doubleIt(5, 5);		
		
	}	
}

Here is a block by block explanation of the code: 

	public static int simpleCalc(int x, int y) {
		int result = x + y;
		return result;
	}

This block defines a static method called "simpleCalc" that takes two integer arguments "x" and "y", adds them together, and returns the result as an integer. 

	public static void doubleIt(int x, int y) {
		int dResult = (simpleCalc(x, y) * 2);
		System.out.println(dResult);
	}

This block defines another static method called "doubleIt" that takes two integer arguments "x" and "y". It calls the "simpleCalc" method with the arguments "x" and "y" to calculate their sum, doubles the result, and prints it to the console. 

	public static void main(String[] args) {								
				
		System.out.println(simpleCalc(5, 5));	
		
		doubleIt(5, 5);		
		
	}	
}

This block defines the "main" method which is the entry point for the program. It contains two lines of code. The first line calls the "simpleCalc" method with arguments 5 and 5 and prints the result to the console. The second line calls the "doubleIt" method with the same arguments and prints the doubled result to the console.

Java Tutorial - Method Parameters

Method parameters are variables that are defined in the method's signature and are used to pass data into the method when it is called. In Java, method parameters are enclosed in parentheses after the method name.

public class Main {	
	
	public static void printSomething(String x) {
		System.out.println(x);
	}
	
	public static void simpleCalc(int x, int y) {
		int result = x + y;
		System.out.println("Add: " + result);
	}
	
	public static void printChar(char x, char y) {
		System.out.println(x + " " + y);
	}

	public static void main(String[] args) {		
								
		//printSomething("John Snow");
		//printSomething("Samantha");
		
		//simpleCalc(5, 5);
		
		printChar('Z', 'B');		
	}	
}

Here is a block by block explanation of the code: 

public class Main {	

This line declares a new public class called "Main". 

	public static void printSomething(String x) {
		System.out.println(x);
	}

This block defines a static method called "printSomething" that takes a String argument "x" and prints it to the console using the "println" method from the "System.out" object. 

	public static void simpleCalc(int x, int y) {
		int result = x + y;
		System.out.println("Add: " + result);
	}

This block defines another static method called "simpleCalc" that takes two integer arguments "x" and "y", adds them together, and prints the result to the console. 

	public static void printChar(char x, char y) {
		System.out.println(x + " " + y);
	}

This block defines a third static method called "printChar" that takes two character arguments "x" and "y" and prints them to the console separated by a space. 

	public static void main(String[] args) {		
		//printSomething("John Snow");
		//printSomething("Samantha");
		//simpleCalc(5, 5);
		printChar('Z', 'B');		
	}	
}

This block defines the "main" method which is the entry point for the program. It contains four lines of code, but only one of them is not commented out. The uncommented line calls the "printChar" method with the characters 'Z' and 'B' as arguments.

Java Tutorial - Methods

A method is a block of code that performs a specific task and can be called by other parts of a program to perform that task. Methods are used to encapsulate logic and promote code reuse, which makes programs easier to read, maintain, and modify.

A method is defined by specifying its name, return type, and parameter list. The return type specifies the data type of the value returned by the method, or void if the method does not return a value. The parameter list specifies the input values that the method expects, if any. 

Parentheses are used when defining functions (or methods in Java) to declare the parameters that the function expects to receive. The parameters are placed inside the parentheses and separated by commas.

public class Main {
	
	public static void simplePrinter() {
		System.out.println("I am from simplePrinter method");
	}
	
	public static void multiCall() {
		System.out.println("Results using multiCall method\n");
		backupPrinter();
		backupPrinter();
		backupPrinter();
	}

	public static void main(String[] args) {
		
		//simplePrinter();		
		//backupPrinter();
		
		multiCall();							
		 
	}
	
	public static void backupPrinter() {
		System.out.println("I am from backupPrinter method");
	}
}

Here's a block-by-block explanation of the program: 

public class Main {

This line defines the class named Main

    public static void simplePrinter() {
        System.out.println("I am from simplePrinter method");
    }

This line defines a static method named simplePrinter(), which simply prints a message to the console. 

    public static void multiCall() {
        System.out.println("Results using multiCall method\n");
        backupPrinter();
        backupPrinter();
        backupPrinter();
    }

This line defines another static method named multiCall(), which prints a message to the console and then calls the backupPrinter() method three times.

    public static void main(String[] args) {
        multiCall();                        
    }

This line defines the main() method, which is the entry point for the program. It simply calls the multiCall() method. 

    public static void backupPrinter() {
        System.out.println("I am from backupPrinter method");
    }
}

This line defines the backupPrinter() method, which prints a message to the console.

When the program runs, the main() method is called, which in turn calls the multiCall() method. The multiCall() method prints a message and then calls the backupPrinter() method three times, each time printing a message to the console.

What "public static void" means

In Java, public static void are access modifiers and a return type that can be used to define methods. Here's what each of these keywords means:

  • public: This is an access modifier that means the method can be accessed from anywhere within the program. Other access modifiers include private, protected, and the default (no modifier).
  • static: This keyword indicates that the method belongs to the class itself, rather than an instance of the class. This means that the method can be called without creating an instance of the class.
  • void: This is the return type of the method, which means the method does not return any value. If the method were to return a value, the return type would be replaced with the type of the value being returned.

So in the context of a method signature like public static void methodName(), it means that the method is accessible from anywhere in the program, belongs to the class itself, and does not return any value.

Java Tutorial - Array Operations

An array is a data structure that stores a collection of elements of the same data type. Each element in an array is identified by an index, which is a number that represents its position in the array.

Some of the most common operations that can be performed on arrays include:

  1. Initializing an array: This involves declaring an array variable and assigning it a set of values.

  2. Accessing array elements: This involves retrieving the value of an element in the array using its index.

  3. Modifying array elements: This involves changing the value of an element in the array using its index.

  4. Finding the length of an array: This involves determining the number of elements in the array.

  5. Looping through array elements: This involves iterating over the elements in an array and performing an operation on each element.

  6. Sorting array elements: This involves arranging the elements in an array in ascending or descending order.

  7. Searching for a specific element in an array: This involves finding the index of an element in the array that matches a specified value.

Arrays are a fundamental data structure in programming, and many programming languages provide built-in support for arrays. Some examples of programming languages that support arrays include Java, C++, Python, and JavaScript. 

public class Main {

	public static void main(String[] args) {
								
		 String[] progLang = {"Java", "C++", "Python", "Perl", "Lisp"};
		 int[] numbers = {1, 3, 5, 25, 5000};
		 
		 System.out.println("Language at Pos 0: " + progLang[0]);
		 System.out.println("Number at Pos 0: " + numbers[0]);
		 
		 System.out.println("Language at Pos 4: " + progLang[4]);
		 
		 //Change elements
		 
		 progLang[0] = "Prolog";
		 System.out.println("Language at Pos 0: " + progLang[0]);
		 
		 numbers[0] = 10000;
		 System.out.println("Number at Pos 0: " + numbers[0]);
		 
		 //Length of Array - Number of elements:
		 
		 System.out.println("Num of ProgLangs: " + progLang.length);
		 System.out.println("Num of Numbers: " + numbers.length);
		 
		 for (String x: progLang) {
			 System.out.println(x);
		 }		
	}	
}

Here's a detailed explanation of each block of code in our Java program: 

public class Main {

This line defines a public class called "Main". This is the main class that contains the main method. 

public static void main(String[] args) {

This line defines the main method. This method is executed when the program is run. 

// create arrays
String[] progLang = {"Java", "C++", "Python", "Perl", "Lisp"};
int[] numbers = {1, 3, 5, 25, 5000};

These lines create two arrays: "progLang" and "numbers". The "progLang" array contains a list of programming language names, and the "numbers" array contains a list of numbers. 

// access elements of arrays
System.out.println("Language at Pos 0: " + progLang[0]);
System.out.println("Number at Pos 0: " + numbers[0]);
System.out.println("Language at Pos 4: " + progLang[4]);

These lines access elements of the "progLang" and "numbers" arrays using their indices. The first line prints the first element of the "progLang" array, the second line prints the first element of the "numbers" array, and the third line prints the fifth element of the "progLang" array. 

// modify elements of arrays
progLang[0] = "Prolog";
System.out.println("Language at Pos 0: " + progLang[0]);
numbers[0] = 10000;
System.out.println("Number at Pos 0: " + numbers[0]);

These lines modify elements of the "progLang" and "numbers" arrays using their indices. The first line changes the first element of the "progLang" array to "Prolog", and the second line changes the first element of the "numbers" array to 10000. The following two lines print out the updated elements. 

// get length of arrays
System.out.println("Num of ProgLangs: " + progLang.length);
System.out.println("Num of Numbers: " + numbers.length);

These lines use the "length" property of the arrays to print out the number of elements in each array. 

// loop through array elements
for (String x: progLang) {
    System.out.println(x);
}

This is a for-each loop that iterates over the elements of the "progLang" array and prints each element to the console. The loop iterates over each element in the array and assigns each element to the variable "x". The code inside the loop then executes for each element.

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...