Sunday, April 27, 2025

Java Tutorial - Detailed Explanations

This is a simple Java program with a main method that prints out two strings using the System.out.println method.

The first string is "Some String" and the second string is "Something More".

There is also a comment in the code, which is denoted by the double forward slashes (//). Comments are ignored by the compiler and are used to add notes or explanations to the code.

public class Main {

	public static void main(String[] args) {
		System.out.println("Some String");
		System.out.println("Something More");
		//Comment
	}	
}

 The main function is a special method that serves as the entry point for a Java application. When you run a Java program, the Java Virtual Machine (JVM) looks for a main method in the specified class and starts executing the code inside the main method.

A class is a blueprint or a template that defines the characteristics and behavior of objects. A class provides a way to define the structure, state, and behavior of objects that belong to it.

The state of an object refers to the values of its attributes or instance variables, which are defined by the class. The behavior of an object refers to the operations or methods that can be performed on it.

An object is an instance of a class. It is a concrete entity that can hold data and perform operations defined by its class. When you create an object, you are creating an instance of a class and allocating memory for its state.

The main method has a specific signature that must be followed for the JVM to be able to execute it.

public static void main(String[] args) is the signature of the main method, which serves as the entry point for a Java application.

Here's what each part of the signature means:

  • public: This is an access modifier that specifies that the main method is accessible from anywhere in the program. This means that the method can be called by other classes or methods, regardless of which package they are in.
  • static: This is a keyword that indicates that the main method belongs to the class rather than to an instance of the class. This means that the main method can be called without first creating an object of the class.
  • void: This is the return type of the main method, which indicates that the method does not return any value. In other words, the main method does not produce a result that can be used by other parts of the program.
  • main: This is the name of the method, which is a reserved name in Java. This name is recognized by the Java Virtual Machine (JVM) as the entry point for the program.
  • String[] args: This is the parameter of the main method, which is an array of strings. The args parameter allows you to pass command-line arguments to the program when it is executed. These arguments can be used by the program to modify its behavior.

System.out.println() is a method used to print a string to the console. It is commonly used for debugging and logging purposes, as well as for general output of information to the user.

Here's what each part of System.out.println() means:

  • System: This is a class in the Java standard library that provides access to the standard input, output, and error streams of the program.
  • out: This is a static field in the System class that represents the standard output stream, which is used to print data to the console.
  • println(): This is a method of the PrintStream class, which is the type of the out field. The println() method prints a string to the console and appends a newline character at the end, so that subsequent output will appear on a new line.

Here's an example of using System.out.println() to print a string to the console:

System.out.println("Hello, world!");

In this example, the string "Hello, world!" is printed to the console, followed by a newline character.

Semicolons are used to mark the end of a statement. A statement is a unit of code that performs a specific action. For example, assigning a value to a variable, calling a method, or declaring a class are all statements in Java.

Semicolons are necessary in Java because they help the compiler to understand the structure of the code and identify where one statement ends and the next begins. If you forget to include a semicolon at the end of a statement, the compiler will generate an error.

When you run this program, it will print out the two strings in the console.

In Java, indentation is not required by the compiler, but it is considered to be an important aspect of code readability and maintainability.

Indentation is the practice of formatting your code by using spaces or tabs to align related lines of code. By indenting your code, you make it easier for yourself and other developers to read and understand the structure and flow of your code.

While the Java compiler doesn't care about indentation, it's a best practice to use consistent and meaningful indentation in your code. This can help to prevent errors and make it easier to identify issues with your code. 

Java Tutorial - Eclipse Installation and First Run

public class Main {

	public static void main(String[] args) {
		System.out.println("Some String");
		System.out.println("Something More");
		//Comment
	}	
}

Saturday, April 26, 2025

C# OO - Read From and Write to a Text File

Program.cs

using System;
using System.IO;

namespace LearnCS
{
    class Program
    {        
        static void Main(string[] args)
        {
            string toFile = "I will be sent to external file";
            File.WriteAllText("external.txt", toFile);

            string fromFile = File.ReadAllText("external.txt");
            Console.WriteLine(fromFile);

            Console.ReadLine();
        }
    }
}

C# OO - Enum and Switch Statement

Program.cs

using System;

namespace LearnCS
{ 
    class Program
    {
        enum Options
        {
            Audio,
            Video,
            Controls
        }
        static void Main(string[] args)
        {
            Options c1 = Options.Controls;
            //Console.WriteLine(c1);

            switch(c1)
            {
                case Options.Audio:
                    Console.WriteLine("Audio Settings");
                    break;
                case Options.Video:
                    Console.WriteLine("Video Settings");
                    break;
                case Options.Controls:
                    Console.WriteLine("Controls");
                    break;
            }

            Console.ReadLine();
        }
    }
}

C# OO - Multiple Interfaces

Program.cs

using System;

namespace LearnCS
{
    interface IBackupA
    {
        void BackupA();
    }
    interface IBackupB
    {
        void BackupB();
    }

    class Sec : IBackupA, IBackupB
    {
        public void BackupA()
        {
            Console.WriteLine("I Will backup Server A");
        }
        public void BackupB()
        {
            Console.WriteLine("I Will backup Server B");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Sec p1 = new Sec();
            p1.BackupA();
            p1.BackupB();

            Console.ReadLine();
        }
    }
}

C# OO - Interface

Program.cs

using System;

namespace LearnCS
{
    interface IPrim
    {
        void IPrimPrint();        
    }

    class Sec : IPrim
    {
        public void IPrimPrint()
        {
            Console.WriteLine("Body of Method must be in Sec Class");
        }        
    }

    class Program
    {
        static void Main(string[] args)
        {
            Sec p1 = new Sec();
            p1.IPrimPrint();            

            Console.ReadLine();
        }
    }
}

C# OO - Abstract Classes & Methods

using System;

namespace LearnCS
{
    abstract class Prim
    {
        public abstract void PrimPrinter();        
    }

    class Sec : Prim
    {
        public override void PrimPrinter()
        {
            Console.WriteLine("Now I can be used");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            Sec p1 = new Sec();
            p1.PrimPrinter();

            Console.ReadLine();
        }
    }
}

C# OO - Polymorphism - Virtual and Override

using System;

namespace LearnCS
{
    class Prim
    {
        public virtual void sameMethod()
        {
            Console.WriteLine("sameMethod, Prim Class, Simple Job");
        }
    }

    class Sec : Prim
    {
        public override void sameMethod()
        {
            Console.WriteLine("sameMethod, Sec Class, Complicated Job");
        }

    }

    class Tert : Prim
    {
        public override void sameMethod()
        {
            Console.WriteLine("sameMethod, Tert Class, Defficult Job");
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Prim p1 = new Prim(); //Something Basic/Simple
            Prim p2 = new Sec(); //Complicated Job
            Prim p3 = new Tert(); //Difficult Job

            p1.sameMethod();
            p2.sameMethod();
            p3.sameMethod();

            Console.ReadLine();
        }
    }
}

C# OO - Inheritance

Program.cs

using System;

namespace LearnCS
{
    class Prim
    {
        public string primString = "Value From Prim Class";

        public void primPrinter()
        {
            Console.WriteLine("I am result of primPrinter Method");
        }
    }

    class Sec : Prim
    {
        public string secString = "Value from Sec Class";
    }
    class Program
    {
        static void Main(string[] args)
        {
            Sec p1 = new Sec();
            Console.WriteLine(p1.primString);
            p1.primPrinter();

            Console.WriteLine(p1.secString);

            Console.ReadLine();
        }
    }
}

C# OO - Automatic Properties - { get; set; }

using System;

namespace LearnCS
{
    class Storage {
        public string someString
        { get; set; }
       
        public int someNumber 
        { get; set; }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            Storage p1 = new Storage();

            p1.someString = "New Value";
            Console.WriteLine(p1.someString);

            Storage p2 = new Storage();
            p2.someNumber = 654546;
            Console.WriteLine(p2.someNumber);

            Console.ReadLine();
        }
    }
}

C# OO - Getters and Setters

Program.cs

using System;

namespace LearnCS
{
    class Storage {
        private string someString;   //fields
        public string MitmString    //property
        {
            get { return someString; }  //getter
            set { someString = value; }   //setter
        }

        private int Number;
        public int MitmNumber
        {
            get { return Number; }
            set { Number = value; }
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            //Storage p1 = new Storage();
            //p1.MitmString = "New Value";

            //Console.WriteLine(p1.MitmString);

            Storage p2 = new Storage();
            p2.MitmNumber = 5000;

            Console.WriteLine(p2.MitmNumber);

            Console.ReadLine();
        }
    }
}

C# OO - Private - Access Modifiers

Program.cs

using System;

namespace LearnCS
{
    class Storage {
        private string someString = "Something Simple";
        private int privID = 5;

        public int MultiBy2() {
            return (privID * 2);
        }

        public string DoubleIt() {
            return (someString + " " + someString);
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {

            Storage p1 = new Storage();
            //Console.WriteLine(p1.someString);

            Console.WriteLine(p1.MultiBy2());
            Console.WriteLine(p1.DoubleIt());

            Console.ReadLine();
        }
    }
}

C# OO - Constructor Parameters

Program.cs

using System;

namespace LearnCS
{
    class Product
    {
        string prodStatus; //activ or obsolete
        int prodID; //individual product number
        char prodWarehouse; //storage, M for Moscow, B for Berlin

        Product(string status, int ID, char warehouse) {
            prodStatus = status;
            prodID = ID;
            prodWarehouse = warehouse;
        }        

        static void Main(string[] args)
        {
            Product p1 = new Product("activ", 123456, 'M');

            Console.WriteLine(p1.prodStatus);
            Console.WriteLine(p1.prodID);
            Console.WriteLine(p1.prodWarehouse);

            Console.ReadLine();
        }
    }
}

C# OO - Constructors

Program.cs

using System;

namespace LearnCS
{
    class Person
    {
        string status;

        Person() {
            status = "activ";
        }

        static void Main(string[] args)
        {

            Person p1 = new Person();
            Console.WriteLine(p1.status);

            Person p2 = new Person();
            Console.WriteLine(p2.status);

            Console.ReadLine();
        }
    }
}

C# OO - Object Modification

Program.cs


using System;

namespace LearnCS
{
    class Person
    {
        string name;
        string lastname;
        int SSN;

        static void Main(string[] args)
        {

            Person p1 = new Person();
            p1.name = "John";
            p1.lastname = "Snow";
            p1.SSN = 456465456;

            Console.WriteLine("P1 Name: " + p1.name);
            Console.WriteLine("P1 LastName: " + p1.lastname);
            Console.WriteLine("P1 SSN: " + p1.SSN);

            p1.SSN = 1111111111;

            Console.WriteLine("P1 Name: " + p1.name);
            Console.WriteLine("P1 LastName: " + p1.lastname);
            Console.WriteLine("P1 SSN: " + p1.SSN);

            Console.ReadLine();
        }
    }
}

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 .  ...