UNIT 2 IMPORTANT NOTES:

2.1 Learning Objective - Relationship between Class and Object:

  • In object-oriented programming, a class is a blueprint or template for creating objects.
  • An object is an instance of a class.
  • The class defines the structure and behavior of the object.
  • Objects created from the same class share the same characteristics but can have different data.

2.2 Learning Objective - Calling Constructors:

  • Constructors are special methods used to create objects.
  • They have the same name as the class and can be called to create objects.
  • Constructors can be called without parameters or with parameters, allowing you to initialize object data during creation.
  • When calling a constructor, you should specify the correct types of parameters based on the constructor’s signature.
  • For example, new MyClass() calls a parameterless constructor, and new MyClass(param1, param2) calls a constructor with parameters param1 and param2.

2.3 Learning Objective - Calling Non-Static Void Methods (Without Parameters):

  • In Java, methods are functions defined within a class that perform specific tasks.
  • Non-static void methods do not return a value and are called using an object.
  • You can call such methods without passing any parameters.
  • For example, if you have an object myObject of class MyClass, you can call myObject.myMethod() to execute a non-static void method.

2.4 Learning Objective - Calling Non-Static Void Methods (With Parameters):

  • Non-static void methods can also accept parameters.
  • When calling these methods, you need to pass the required parameters as specified in the method’s signature.
  • For example, if you have a method myMethod that takes parameters param1 and param2, you can call it using myObject.myMethod(param1, param2).

2.5 Learning Objective - Calling Non-Static Non-Void Methods:

  • Non-static non-void methods return a value when executed.
  • These methods may or may not accept parameters.
  • When calling such methods, you can capture the return value.
  • The return type of the method should match the type of variable used to store the result.
  • For example, int result = myObject.calculateSum(x, y) captures the result of a non-static non-void method.

2.6 Learning Objective - String Class Methods:

  • The String class in Java is used to work with text.
  • You can create String objects and perform various operations using String methods.
  • String methods allow you to manipulate and analyze text data, such as finding the length of a string, concatenating strings, and more.

2.7 Learning Objective - String Class (More API and Packages Centric):

  • Java’s java.lang package contains essential classes, including the String class.
  • When working with the String class, you can handle exceptions like IndexOutOfBoundsException when accessing characters beyond the string’s length.
  • The java.lang package provides core functionality and is automatically imported into Java programs.

2.8 Learning Objective - Wrapper Classes (Integer and Double):

  • Wrapper classes are used to work with primitive data types as objects.
  • Integer and Double are examples of wrapper classes for int and double data types.
  • You can create Integer and Double objects, and call their respective methods to perform operations like conversion and manipulation.

2.9 Learning Objective - Calling Static Methods and Math Class:

  • Static methods belong to a class rather than a specific object.
  • You can call static methods using the class name.
  • The Math class in Java provides a set of mathematical functions and constants.
  • You can call methods from the Math class to perform mathematical operations.
public class PersonName {
    private String fullName;

    public PersonName(String fullName) {
        this.fullName = fullName;
    }

    public String getFirstName() {
        StringBuilder firstName = new StringBuilder();
        // for loop, i++ is jus starting at index 0 and increasing the index until it reaches a space
        for (int i = 0; i < fullName.length(); i++) {
            char c = fullName.charAt(i);
            if (c == ' ') {
                // this makes it so that the for loop iteration stops when it reaches a space, which would logically be the space between first and last name
                break; 
            }
            firstName.append(c);
        }
        return firstName.toString();
    }
     
    // this is just a cut paste of the first name code, does the exact same thing
    public String getLastName() {
        StringBuilder lastName = new StringBuilder();
        boolean foundSpace = false;
        for (int i = 0; i < fullName.length(); i++) {
            char c = fullName.charAt(i);
            if (foundSpace) {
                lastName.append(c);
            }
            if (c == ' ') {
                foundSpace = true;
            }
        }
        return lastName.toString();
    }

    public static void main(String[] args) {
        // sample bf name to show working capabilities
        PersonName person = new PersonName("Theo Huntalas");

        String firstName = person.getFirstName();
        String lastName = person.getLastName();

        System.out.println("First Name: " + firstName);
        System.out.println("Last Name: " + lastName);
    }
}

PersonName.main(null);

First Name: Theo
Last Name: Huntalas

UNIT 3 IMPORTANT NOTES:

3.1 Learning Objective - Evaluate Boolean Expressions with Relational Operators:

  • In programming, we often use Boolean expressions to make decisions.
  • These expressions involve the use of relational operators to compare values.
  • Types of Relational Operators include:
    • Equal to: ==
    • Not Equal to: !=
    • Greater than: >
    • Less than: <
    • Greater than or equal to: >=
    • Less than or equal to: <=
  • These operators return either true or false based on the comparison result.

3.2, 3.3, and 3.4 Learning Objectives - Conditional Statements:

  • Conditional statements are used to represent branching logical processes in code.
  • Nested if-else statements allow you to create more complex decision trees.
  • Compound boolean expressions combine multiple conditions using logical operators:
    • && (logical AND) returns true if both conditions are true.
    • || (logical OR) returns true if at least one condition is true.
  • Short-circuited evaluation means that if a logical expression can be determined with only the evaluation of the first part, the second part may not be evaluated. This can be used for optimization.

3.6 Learning Objective - De Morgan’s Law:

  • De Morgan’s Law is a set of rules that help simplify and understand complex boolean expressions.
  • It allows us to rewrite expressions involving logical NOT, AND, and OR operators.
  • The rules state that:
    • NOT (A AND B) is equivalent to (NOT A) OR (NOT B).
    • NOT (A OR B) is equivalent to (NOT A) AND (NOT B).

3.7 Learning Objective - Comparing Object References:

  • In programming, you often need to compare object references to determine if they refer to the same object.
  • This is done using boolean expressions.
  • The == operator is used for object reference comparison. It checks if two references point to the same object in memory.
  • It’s important to note that == compares the memory address, not the content of the objects.
  • For comparing the content of objects, especially with Strings, you should use .equals() or .compareTo() methods instead of the == operator.



import java.util.Scanner;

public class PasswordChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter your password: ");
        String password = scanner.nextLine();

        System.out.println("You entered this password: " + password);

        if (isPasswordValid(password)) {
            System.out.println("Password is valid.");
        } else {
            System.out.println("Password is invalid. It should meet the following criteria:");
            if (password.length() < 8) {
                System.out.println("- At least 8 characters long.");
            }
            if (!containsUppercaseLetter(password)) {
                System.out.println("- At least one uppercase letter.");
            }
            if (!containsLowercaseLetter(password)) {
                System.out.println("- At least one lowercase letter.");
            }
            if (!containsDigit(password)) {
                System.out.println("- At least one digit (0-9).");
            }
            if (!containsSpecialCharacter(password)) {
                System.out.println("- At least one special character (!, @, #, $, %, ^, &, *).");
            }
        }

        scanner.close();
    }

    public static boolean isPasswordValid(String password) {
        return password.length() >= 8 &&
               containsUppercaseLetter(password) &&
               containsLowercaseLetter(password) &&
               containsDigit(password) &&
               containsSpecialCharacter(password);
    }

    public static boolean containsUppercaseLetter(String password) {
        return password.chars().anyMatch(Character::isUpperCase);
    }

    public static boolean containsLowercaseLetter(String password) {
        return password.chars().anyMatch(Character::isLowerCase);
    }

    public static boolean containsDigit(String password) {
        return password.chars().anyMatch(Character::isDigit);
    }

    public static boolean containsSpecialCharacter(String password) {
        String specialCharacters = "!@#$%^&*";
        return password.chars().anyMatch(c -> specialCharacters.indexOf(c) >= 0);
    }
}


PasswordChecker.main(null);
Enter your password: You entered this password: Check123!
Password is valid.

UNIT 4 IMPORTANT NOTES:

4.1 Learning Objective - Represent Iterative Processes Using a While Loop:

  • In programming, iterative processes involve repeating a set of instructions or statements.
  • A while loop is a control structure that allows you to execute a block of code repeatedly as long as a specified condition is true.
  • The while loop checks the condition before each iteration, and if the condition becomes false, the loop terminates.
  • While loops are useful for tasks that need to be performed a certain number of times or until a specific condition is met.

4.3 Learning Objective - String Manipulation Methods in Java:

  • When working with String objects in Java, various methods are available to manipulate and work with strings.
  • String.substring is used to retrieve a particular portion of a string, allowing you to extract a substring based on its starting and ending positions.
  • String.equals is used to compare the content of two strings, checking if they are equal in content.
  • String.length is used to determine the length (number of characters) in a string.

4.4 Learning Objective - Nested Iteration Processes:

  • Nested iteration involves using one or more loops inside another loop.
  • This technique allows you to perform more complex repetitive tasks and iterate through multi-dimensional data structures.
  • Common nested loops include using nested for loops to traverse 2D arrays and matrices.
  • Proper control structures and loop conditions are essential to avoid infinite loops.

4.5 Learning Objective - Informal Code Analysis:

  • Informal code analysis is the process of understanding, reviewing, and evaluating code without formally applying formal methods.
  • One aspect of code analysis involves counting statement executions, which indicates how many times a statement is executed during program execution.
  • Understanding statement execution counts helps in analyzing code behavior and identifying performance bottlenecks.
  • It’s an essential skill for optimizing code and ensuring that it behaves as intended.



for (int k = 0; k < 40; k++){
    if (k % 4 == 0){
        System.out.println(k); 
    }
}
0
4
8
12
16
20
24
28
32
36

UNIT 6 IMPORTANT NOTES:

6.1 Array Creation and Access:

  • An array is a data structure used to implement a collection of object reference data.
  • Elements are the individual values stored within an array.
  • The index of an element is the position of that element within the array.
  • In Java, the first element of an array is at index 0, and the last element is at index (length - 1).
  • The length of an array is the number of elements it contains, representing the size of the array.

6.2 Traversing Array (1D):

  • In Java, arrays are indexed from 0 to the number of elements minus one.
  • Standard for loops are a common way to iterate through arrays.
  • The for loop typically has three components:
    • init: The initialization expression initializes a variable and is executed only once.
    • condition: The condition statement is evaluated for every iteration, and the loop continues as long as the condition is true.
    • incr/decr: The increment or decrement statement is applied to the variable, updating the initial expression, and is executed after each iteration.

6.3 Enhanced For Loop for Arrays:

  • The enhanced for loop, also known as the “for-each” loop, provides a simplified way to loop through elements in an array, collection, or other iterable data structures.
  • It eliminates the need for explicit index management and simplifies code for iterating through elements.
  • Enhanced for loops are particularly useful when you need to iterate through all elements in a collection sequentially.

6.4 Developing Algorithms Using Arrays:

  • Developing algorithms involving arrays is a fundamental aspect of programming.
  • Algorithms define a step-by-step process to solve a specific problem.
  • Arrays are commonly used in various algorithms to store, manipulate, and process data efficiently.
  • When working with arrays in algorithm development, it’s essential to consider factors like array size, access patterns, and element manipulation.



public class PrimeNumberFinder {
    public static void main(String[] args) {
        int[] numbers = {2, 7, 15, 11, 4, 31, 20, 17};
        
        System.out.println("Prime numbers using a for loop:");
        for (int number : numbers) {
            if (isPrime(number)) {
                System.out.println(number);
            }
        }

        System.out.println("\nPrime numbers using a while loop:");
        int index = 0;
        while (index < numbers.length) {
            if (isPrime(numbers[index])) {
                System.out.println(numbers[index]);
            }
            index++;
        }
    }

    // Helper method to check if a number is prime
    public static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        if (num <= 3) {
            return true;
        }
        if (num % 2 == 0 || num % 3 == 0) {
            return false;
        }
        for (int i = 5; i * i <= num; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }
}

PrimeNumberFinder.main(null);
Prime numbers using a for loop:
2
7
11
31
17

Prime numbers using a while loop:
2
7
11
31
17

UNIT 7 IMPORTANT NOTES:

7.1 ArrayList Introduction:

  • ArrayLists are dynamic data structures, allowing their size to grow or shrink as needed.
  • In contrast, arrays are static in size, meaning you need to specify their size at the time of creation.
  • When dealing with arrays, if you want to change their size, you’d have to create a new array of a different size and copy data from the initial array to the new one.
  • ArrayLists provide a convenient way to manage collections of related object reference data without the need for manual resizing.

7.2 ArrayList Methods:

  • ArrayLists are used to represent collections of related object reference data.
  • ArrayList objects are part of Java’s standard library and provide numerous methods for managing and manipulating data in lists.
  • These methods include adding and removing elements, accessing elements by index, and more.

7.3 Traversing ArrayLists:

  • With an ArrayList, you can traverse objects using a for or while loop.
  • Traversing objects in an ArrayList is similar to iterating through objects in an array.
  • You can access and manipulate the elements of an ArrayList using loops.

7.4 Developing Algorithms Using ArrayLists:

  • When working with ArrayLists, you can develop algorithms that involve iterating through the elements using for or while loops.
  • You can also use enhanced for loops to simplify traversal.
  • In the realm of algorithms, within the context of ArrayLists, students can: a. Recognize established algorithms. b. Customize existing algorithms. c. Create new algorithms that involve ArrayList traversal.

7.5 Searching:

  • Sequential/linear search algorithms are used to search for specific information in arrays or ArrayList objects.
  • These algorithms involve checking each element in the array or ArrayList one by one until the desired information is found.
  • Searching is an essential operation when working with data collections.

7.6 Sorting:

  • Sorting algorithms like selection sort and insertion sort are used to arrange the elements in ascending or descending order in arrays or ArrayList objects.
  • These algorithms involve comparing and rearranging elements to achieve the desired order.
  • Sorting is crucial for organizing data and making it more accessible for various applications.

7.7 Ethical Issues around Data Collection:

  • This learning objective focuses on understanding the ethical issues related to data collection and storage.
  • It involves explaining the risks of privacy associated with collecting and storing personal data on computer systems.
  • Ethical considerations are essential when dealing with data, particularly sensitive or personal information.



If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
  # Relaunch as an elevated process:
  Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
  exit
}

netsh advfirewall export "C:\Main\modules\firewallexport.wfw"

netsh advfirewall import "C:\Main\modules\firewall.wfw"

pause 

Write-Host Scan through firewall and firewall rules and remove anything suspicious

firewall.cpl

pause 

Write-Host "Profiles and Profiles Settings"
#Turning on Firewall for all Profiles
Netsh advfirewall set allprofiles state on

#Block Inbound and Allow Outbound for all Profiles, Block all inbound on public profile
Netsh advfirewall set allprofiles firewallpolicy blockinbound,allowoutbound
netsh advfirewall set publicprofile firewallpolicy blockinboundalways,allowoutbound

#Log Dropped and Allowed Connections
netsh advfirewall set allprofiles logging droppedconnections enable
netsh advfirewall set allprofiles logging allowedconnections enable

#Log Firewall
Set-NetFirewallProfile -all -LogMaxSizeKilobytes 10240 -LogAllowed true -LogBlocked true -notifyonlisten true

#Notifications
netsh advfirewall set allprofiles settings inboundusernotification enable

#Unicast Response
netsh advfirewall set allprofiles settings unicastresponsetomulticast disable

# Disable Merge of Gpedit Rules with Local Firewall Rules for Public Profile
reg add HKLM\SOFTWARE\Policies\Microsoft\WindowsFirewall\PublicProfile /v AllowLocalPolicyMerge /t REG_DWORD /d 0 /f

# $registryPath = "HKLM\SOFTWARE\Policies\Microsoft\WindowsFirewall\PublicProfile"
# $Name = "AllowLocalPolicyMerge"
# $value = "0"
# IF(!(Test-Path $registryPath))
#   {
#     New-Item -Path $registryPath -Force | Out-Null
#     New-ItemProperty -Path $registryPath -Name $name -Value $value `
#     -PropertyType DWORD -Force | Out-Null}
# ELSE {
#     New-ItemProperty -Path $registryPath -Name $name -Value $value `
#     -PropertyType DWORD -Force | Out-Null}

# Disable Merge of Local Connection Rules with Gpedit Settings
reg add HKLM\SOFTWARE\Policies\Microsoft\WindowsFirewall\PublicProfile /v AllowLocalIPsecPolicyMerge /t REG_DWORD /d 0 /f

# $registryPath = "HKLM\SOFTWARE\Policies\Microsoft\WindowsFirewall\PublicProfile"
# $Name = "AllowLocalIPsecPolicyMerge"
# $value = "0"
# IF(!(Test-Path $registryPath))
#   {
#     New-Item -Path $registryPath -Force | Out-Null
#     New-ItemProperty -Path $registryPath -Name $name -Value $value `
#     -PropertyType DWORD -Force | Out-Null}
# ELSE {
#     New-ItemProperty -Path $registryPath -Name $name -Value $value `
#     -PropertyType DWORD -Force | Out-Null}

# Disable IPV6 Redirects 
netsh interface ipv6 set global icmpredirects=disabled

# Configuring Manually the Default Current Hop Limit
netsh interface ipv6 set global defaultcurhoplimit=64

# Disable ISATAP
netsh interface ipv6 isatap set state disabled

# Disable Teredo
netsh interface ipv6 set teredo type=disabled

# Disable 6to4
netsh interface ipv6 6to4 set state disabled

# Set the MTU, Disable Router Discovery and Minimize DAD Transmits Per Interface
# netsh  interface  ipv6  set  interface  "1"  mtu=1280  dadtransmits=0 routerdiscovery=disabled

pause

UNIT 8 IMPORTANT NOTES:

8.1: What is a 2D Array?

  • A 2D array, also known as a two-dimensional array, is a data structure that stores elements in rows and columns.
  • It can be thought of as a table or grid of elements, with each element identified by its row and column index.
  • 2D arrays are used to represent structured data, such as matrices, tables, or images.
  • They provide a way to organize and access data in a grid-like fashion, making them useful for various applications.

8.2: Traversing 2D Arrays:

  • Traversing a 2D array involves iterating through its elements to access and process data.
  • Common methods for traversing 2D arrays include nested loops, where one loop iterates over rows, and another loop iterates over columns.
  • Proper control structures are essential to ensure that all elements are visited, and the desired order of traversal is maintained.
  • Traversing 2D arrays is crucial for performing operations on structured data efficiently.

8.3: Image Representation with 2D Arrays:

  • 2D arrays are commonly used to represent images in computer graphics and image processing.
  • In this context, each element of the 2D array corresponds to a pixel in the image.
  • The color information for each pixel is stored in the array, allowing for the manipulation and processing of images.
  • 2D arrays are used to create, modify, and analyze images in various applications, including graphics software, digital photography, and computer vision.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ImageConverter {

    public static void main(String[] args) {
        try {
            // load the PNG image
            BufferedImage pngImage = ImageIO.read(new File("/home/vardaan/vscode/vsstudent/images/image0.png"));

            // create a new image in RGB format (JPEG)
            BufferedImage jpegImage = new BufferedImage(
                pngImage.getWidth(),
                pngImage.getHeight(),
                BufferedImage.TYPE_INT_RGB
            );

            jpegImage.createGraphics().drawImage(pngImage, 0, 0, null);

            String outputFilePath = "/home/vardaan/vscode/vsstudent/images/image0converted.jpg";
            File output = new File(outputFilePath);

            // save as a JPEG file
            ImageIO.write(jpegImage, "jpg", output);

            System.out.println("PNG image has been transformed into a JPEG image.");

            System.out.println("You can download the converted image from this link: " + outputFilePath);
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}


ImageConverter.main(null);
PNG image has been transformed into a JPEG image.
You can download the converted image from this link: /home/vardaan/vscode/vsstudent/images/image0converted.jpg