Showing posts with label abstraction. Show all posts
Showing posts with label abstraction. Show all posts

Friday, July 11, 2025

Leetcode 3110 - Score of a string problem analysis

 Hi Everyone, This was the first attempt answer for the Leetcode problem on score of a string. 

https://leetcode.com/problems/score-of-a-string

class Solution {
    public int scoreOfString(String s) {
        sum = 0;
        char[] ssize = s.toCharArray();
        for (int i=0;i<ssize.length-1; i++){
            sum = sum + abs(ssize(i).asciicode-ssize(i+1).asciicode)
        }
            return sum
        }

Issues with the code - 

Issue Description
sum not declared   - sum is used without type declaration.
Incorrect syntax: abs() - Java requires Math.abs() for absolute value.
Incorrect character access: ssize(i) - Use ssize[i], not parentheses.
Non-existent property: .asciicode Characters in Java already act as ASCII values when cast to int.
Missing semicolons and braces Several semicolons and } missing.
Overall formatting and structure Needs improvement for clarity and compilation.


Learning from the code: 

1. Declare the variables. 
2. For each Math method, use Math.<method>
3. Array access using [].
4. asciiCode. each character has a ascii value, which can be used while converting to int.
5. colons are missing


Corrected code for this 


class Solution {
    public int scoreOfString(String s) {
        int sum = 0;
        char[] ssize = s.toCharArray();
        for (int i = 0; i < ssize.length - 1; i++) {
            sum += Math.abs(ssize[i] - ssize[i + 1]);
        }
        return sum;
    }
}

Thursday, July 11, 2024

Object Oriented Programming Concepts in a MindMap

 

Understanding Object-Oriented Programming (OOP) is fundamental for modern software development. OOP concepts, such as classes and objects, encapsulation, inheritance, and polymorphism, allow developers to create modular, reusable, and maintainable code. 

MindMap Representation of OOP's Concept


Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs.

Understanding Class

Class in Java is a  blueprint of attributes and behavior (actions) of an object.

Below are the members of a class:

  • Class Variables (Static Fields): These are common to all instances of a class and are declared with the `static` modifier. 
  • Instance Variables (Non-static Fields): These are unique to each instance of a class. Following the `Bicycle` example, `cadence`, `gear`, and `speed` would be instance variables, with each `Bicycle` object having its own copy.
  • Methods: These are functions that define the behavior of the objects created from the class. They can be static (class methods) or non-static (instance methods).
  • Constructors: Special methods used to initialize new objects. They have the same name as the class and may take parameters to set initial values for the instance variables.
  • Nested Classes: Classes can contain other classes, known as nested classes.

Principle of OOPs concept:

  •  Encapsulation: Bundling the data with the code that manipulates it
    • Bundling data (variables) and methods (functions) that operate on the data into a class. 
    • Access to the data is restricted and controlled through public methods known as getters and setters. 
    • Allows for data hiding, where internal workings of a class are hidden from outside  interference and misuse. 
    • Promotes modularity and maintainability of code 
    • Enhances data security, as only approved methods can modify the internal state of objects.

    • Data Types 

      • Public: accessible from any other class, not just the class in which it is declared.
      • Protected: accessible within its own class and by derived classes.
      • Private: accessible within the class in which it is declared, and not from any other class.

  • Abstraction: Hiding complex realities while exposing only the necessary parts.
    • Achieved through abstract classes and interfaces, allowing for the creation of classes that cannot be instantiated on their own but can be sub classed.
    • Abstract classes may contain abstract methods without a body; these methods must be implemented by subclasses.
    • An interface is a blueprint for a class
      •  Include abstract methods and constants.
      • Used to achieve abstraction and multiple inheritance, as a class can implement multiple interfaces.
      • Class implementing an interface must override all of its methods.
      • Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

  • Inheritance: Allowing new objects to take on the properties of existing one.
    • Inheritance in Java allows one class to inherit the features (fields and methods) of another class.
    • Syntax for inheritance uses the `extends` keyword
    • Java supports different types of inheritance: single, multilevel, and hierarchical.
    • Multiple inheritance is not supported in Java through classes, but it can be achieved using interfaces.
  • Polymorphism: Allows objects to take on multiple forms.
    • Manifests in two primary forms: compile-time (static) and runtime (dynamic) polymorphism.
    • Compile-time polymorphism is achieved through method overloading, where multiple methods have the same name but different parameter lists.
    • Runtime polymorphism is realized through method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass.