Friday 22 March 2024

SUNY Buffalo : MS Tracks and Specializations (Graphic Representation)

Graduate degree options provided by Departement of CSE at SUNY Buffalo can be graphically represented as : 

 


(Click on the images for enlarged view 🔍)

The graphic representation of the semester wise breakdown of programm can be depicted as : 




Please comment below if you find anything wrong in the representations. Or, if you want to add more in this.


Thursday 4 January 2024

LineRandomizer in Python : Utility Function

While Memorizing vocab for GRE, I decided to jumble up my word list in random order. This small python utility program helped me achieve that. Hope it helps you as well. 


'''
Author: Kunal Krishna
Date: 30/05/2023
'''

'''
Programme Purpose : Given a text file containing N lines,
    return a new file which has reorder those lines in random order.
    This has many utilities e.g. in memorizing a list of vocabulary.
Input: File1.text :
    >a
    >b
    >c
Output: File2.text : Random ordering
    >c
    >a
    >b
'''

import os
import re
import random
import numpy as np

# 1. Open file.txt : https://docs.python.org/3/library/functions.html#open
inpFile= open("D:\[win.kunal]\Desktop\Diff_Words.txt","r")
linesList = inpFile.readlines() #list of lines
# for line in linesList :
#     line = line.strip("\n")

# 2. Create an output file
resPath= "D:\my_codes\py_code\LineRandomizer"
resFileName = 'Difficult_Words.txt'
file_result = open(os.path.join(resPath,resFileName), "w") # w : creates/overrides existing
# alternatively
# file_result = open("Difficult_Words.txt", "w")

count = 0
# 3.Process the lines : randomize
while (len(linesList) > 0) :
    #chose a random word from the list
    random_word = np.random.choice(linesList)

    validWordExpression = "[~\*\#\w\s]"
    if( len(random_word.strip()) >0
            and re.match(validWordExpression, random_word) ) :
        # write the random word in the new file
        print(str(count+1)+". inserting..." + random_word.upper())
        file_result.write(random_word.upper())
        count +=1

    #also remove the word from linesList to avoid repetation
    linesList.remove(random_word)

# 4. Close the file
inpFile.close()
file_result.close()
if count>0 :  
    print(str(count)+" words inserted successfully!!!")
else :  
    print("No word(s) inserted.")

Tuesday 28 November 2023

Creating Immutable Custom Class in Java

 A frequent Java interview question is : 

How to create a Class which is immutable? 

Like any other question, the answer begins with the understandig what do we really mean by the underlying concept - in this case : immutablity !!! 

Making a class immutable entails ensuring two solemn contracts : 

  1. Class is closed for extension i.e it cannot be inherited. (why is that necessary? ans at bottom)
  2. Once the object is created, the state of fields cannot chage till its existence in JVM.
In other words, A variable/class of an immutable type may only be changed by re-assigning to that variable. When we wish to only modify some portion of an immutable, we are compelled to reassign the whole object.

Let's take Student class as an example. 




This class can be made Immutable in following way (observe the Student class carefully) : 



KEY TAKEAWAYS : 
  1. finalize class 
  2. set class mebers as "private" and "final
  3. Assign new object to instance variable by copying all values 
  4. remove all setters() 
  5. return duplicate(new object) in getters() instead of actual instance variable

Finally answer to the question : Why do we need to declare an Immutable class final? 

Well the question is quite debatable. 

There are several reasons why we need to declare a class as final to make it immutable.
  1. To prevent subclasses from overriding methods and changing the state of the object.
    • If a class is not final, a subclass could override a method and change the state of the object. This would violate the principle of immutability, which states that the state of an object cannot be changed once it is created.
  2. To ensure that the object is thread-safe.
    • Immutable objects are thread-safe because they cannot be changed by multiple threads at the same time. This is important for objects that are shared between multiple threads, such as objects in a cache.
  3. To improve performance.
    • The JVM can make some optimizations for immutable objects, such as caching the hash code of the object. This can improve the performance of applications that use immutable objects.
  4. To make the code more readable and easier to understand.
    • Immutable objects are easier to reason about because their state cannot change. This makes the code more readable and easier to understand.

~ Happy Coding ~

Saturday 28 October 2023

Pictorial Guide to understand Access Modifier in Java

 

Access Modifiers in Java has been explained beautifully in blogs like this

But this blog aims to leave a pictorial memory in reader's mind. 

Usually, it is represented in tabular form like this ... 


But I wanted to present even more practical schematic diagram. 



Private

Default (aka package-private)


Protected


Public