Day 0-9 of 30 Days of code @ HackerRank

You are fully capable of deciding your destiny. The only question is what will you choose to be.

— Spock’s father

So , I started HackerRank’s 30 Days of Code because I had to start learning Java and it was damn interesting and I am happy that I learned soo much through these contents and I think a lot of the credit would go to the amazing video lessons from Kathryn Hodge (@blondibytes) and even though I was able to complete 10 day’s work in just 4 days but I feel guilty of not doing anything in the past 2 days , which is absolutely unprecedented as I code for at least 6 days . Here’s the Github repository to the code of the 10 days –

https://github.com/aalpanigrahi/pset/tree/master/hackerrank/30DaysOfCode

So , I have covered the following concepts in these 10 lessons –

  1. Class , fields and methods . Syntax of class –

class <class_name> {

<dataType1> <fieldName1>;

<dataType2> <fieldName2>;

…..

<dataTypeN> <fieldNameN>;

<modifier1> <modifierName1>(<dataType1> <pName1>,…,<dataType1> <pName1>) { <method_block> }

…..

<modifierN> <methodNameN>(<dataType1> <pName1>,…,<dataType1> <pName1>) { <method_block> }

2. Naming convention for classes : Should begin with Capital letters and for conjunction of multiple phrases it should follow CamelCase (e.g. birthOfDate , buildDate ) or Lower snake case (e.g. Student_Class).

3. Concept of variable and it’s naming convention : Should begin with small letter and should follow CamelCase in case of conjunction of multiple words. (e.g. dateOfBirth).It shouldn’t have spaces or any special character other than _ and it shouldn’t begin with a number. variable of a class is called a field.

4. Java as a statically typed language and declaration & initialization of data type for a variable :-

<DataTypeName> variableName = <value>;

Initialization is possible because of assignment “=” operator , which assigns value on RHS to LHS (which is the variable).

5. Functions as a sequence of instructions to perform a given task. Syntax of function :-

<modifier> <dataType/void> functionName(<dType1> var1 , …… ,<dTypeN> varN)

{ <function_block> }

6. Methods are just functions of a class & objects are instances of a class.

7. Scanner class for accepting input . Different scanner class methods –

next()

next<name>() where <name>=BigDecimal , BigInteger , Boolean , Byte , Double , Float , Int , Line , Long , Short

Initialization of scanner class –

Scanner <objectName> = new Scanner ( System.in );

8. Difference between next() and nextLine().

9. Class for the purpose of abstraction.

10. Primitive data type (int , double , float , boolean , char).

11. Statement terminator and it’s use .

12. Boolean data type and the values it can store.

13. Public scope of a function & void modifier.

14. Creating instance of any given class :-

ClassName objectName = new ClassName(); //Default

ClassName objectName = new ClassName(value1 , value2 , … , valueN); //Parammetrized

Calling non-static method of a given class’s object –

objectName.methodName();

objectName.methodName(value1 , value2 , …. , valueN );

15. Difference between print() , printf() and println() .

16. Class as a reference data type . other example of referenced data type is Arrays , Linked lists , Stacks , Queues , … etc

17. Reference data type vs Primary data type.

18. What is a data type ? It defines , stores and restricts what given value can be stored in a given variable.

19. Primitive data types : byte , short , int , long , float , double , boolean and char .

20. String class and it’s objects .

21. use of scannerObjectName.close()

22. Assigning default values to fields of a class through assignment after declaration.

<modifier> ClassName

{ <dataType> fieldName = <value>; }

23. Default constructors are created by the compiler which assigns default values to the fields (if the fields are present in the class) , but they can also be specified by the programmer . Here’s how to do it –

public ClassName() {

this.fieldName1 = pSpecifiedValue1;

this.fieldName2 = pSpecifiedValue2;

…….

this.fieldNameN = pSpecifiedValueN;

}

24. A parameter for a function is basically the input a function receives from the user. Syntax for a parametrized constructor

public ClassName (<dataType1> <value1> , ….. ,<dataTypeN> <valueN>) {

this.fieldName1 = <value1> ;

…….

this.fieldNameN = <valueN> ; }

25. A return statement , returns back the value for a given method or a function.

NOTE – It is compulsory to either assign void or data type for the data type.

26. Re-usability of classes.

27. Types of operators based on no of operands : Unary , binary and ternary .

28 Basic arithmetic operators : + – * / %

Additional operators : + (as String concatenation) , ++ (post/preincrementation) , — (post/predecrementation) , ! (negation) , == (equality comparison) , != (non-equality comparison) , <= >= < > (comparison operators) , && || (logical AND OR) , ? : (if-then-else) .

29. Difference between classes and objects .

30. if-else statements allow to control the flow of execution based on certain conditions. Syntax of if-else statements –

if (<testCondition>)

{ <ifBlock> }

else

{ <elseBlock> }

In this case <ifBlock> would be executed when <testCondition> gives true as boolean output otherwise <elseBlock> would be executed.

31. Importing and object declaration of Random class , which is used to obtain random numbers –

Importing : import java.util.Random;

Declration : Random randomObjectName = new Random();

Generating random integer : randomObjectName.nextInt();

32. Accessing absolute value :

Math.abs(<value>); /*Value should be a number , no import required , output is +ve*/

33. Logical operators : || (Logical disjunction) , && (Logical conjunction) & ! (negation)

34. Ternary expressions :-

<expression> ? <output1> : <output2>

if <expression> is true <output1> is considered otherwise <output2> is considered. This is similar to if-else.

35. Switch-case condition : Quite useful when it comes to multiple-decision branching. Syntax of the switch-case statements –

switch (<testValue>) {

case val0:

behaviour0;

break;

………….

case valN:

behaviourN;

break;

default:

defaultBehaviour;

break;

}

In this case a testValue has to be passes to switch and behavior has to be specified for case and default case is to be provided in the condition when no other cases have been specified.

36. Static methods and fields of a class can be called from main function without declaring object for the provided class , this is because main() itself is a static method. Calling a non-static method from main() isn’t possible.

37. this is a reference variable which points to the currently used object.

e.g. for a class className , which has a method methodName , so we can call the method for the given class by this.methodName() or for a field fieldName , we could call the provided field by using this.fieldName.

38. Getter method is used to return the value of a field for a given object. Syntax of getter method :-

public <dataType> getFieldName()

{ return this.fieldName; }

39. Setter method is used to set the value for a given field of a class’s object. Syntax of setter method :-

public void setFieldName(<dataType> newFieldName)

{ this.fieldName=newFieldName; }

40. Boolean operators are operators which deals with boolean values (true/false).

A && B := true only when both expressions A and B are true

A || B := true only when either of expressions A or B are true

! C := true only when expression C is false

A != B := true only when value A is not true to value B

41. Class method is a method of a class which is static , so it can called without declaring object for a given function.

42. Method for accepting a character as input –

scannerObject.next().charAt(0);

43. While loop is a block of code which is executed repeatedly under some condition. Syntax of do-while loops :-

while (<condition>)

{ <while_block> }

44. Do-while loop is a loop similar to while loop , but here a block of statement is executed first and then the while block’s condition is tested –

do { <do_block> }

while (<condition>);

45. Scope of a local variable is limited to a given block.

46. For loop allows looping through initialization (which involves declaration of local variables), termination condition and incrementation condition :-

for(<dataType> variableName = <initialCondition> ; variableName><operator><terminationCondition>; <variableIncrementer> )

{ <forBlock> }

47. Break statement allows to break the loop even when the loop’s test-condition is satisfied. They should be called under certain condition –

if ( <testCondition> )

{ break; }

48. Nested for loops : For loops within for loops

49. Arrays are collections of homogeneous data type. They are a container object which could store a fixed number of values for any given data type.

50. Iteration : Passing through each element of java.

51. A container object is a class or a data structure whose instances are a collection of other objects.

52. Declaration of an array and allocation of it’s size :-

<dataType>[] <arrayName> = new <dataType>[<size>]

In case of an array , the size is fixed , it cannot change.

53. Every class in java is a child of object class , either directly or indirectly. toString() is a method which is used to represent the element of Array class. Method to print out values of a given array –

System.out.println( Arrays.toString( <arrayName> ) );

54. Statements for allocation , declaration and initialization of an array –

<dataType>[] <arrayName>; //Declaration

<dataType>[] <arrayName> = new <dataType>[<size>]; //Declaration & Allocation

//Declaration , Initialization & Allocation

<dataType>[] <arrayName> ={<val1>, ….. , <valN>};

55. Default value of int , float/double and string is 0 , 0.0 and none respectively.

56. Array as a reference data structure.

57. arrayName[<position>] can be used to extract individual elements of an array. If the <position> > the length of the array then a null pointer would be returned and there would be an indexError .

58. Syntax for sorting an array :-

Arrays.sort(arrayName);

Using the above sort() of Arrays would change the original order of the array forever.

59. Syntax of for-each loop :-

for( <dataType> <varName> : <arrayName> )

{ System.out.println(<varName>) }

this is similar to python’s for e in arrayName statement.

Array class can be imported by :- import java.util.Array;

60. Dictionary is a container data type of variable length which stores data by using key and value pairs , where an key-value can be inserted or deleted. This is quite contrary to the array data type which has fixed size. Dictionary is an abstract data type and it doesn’t exist directly similar to Arrays , rather it is created by mapping .

Key —– > Value (Key to value mapping)

61. Statement to create mapping –

Map < WDataType1 , WDataType2 > nameOfDictionary = new HashMap <WDataType1 , WDataType2 >();

Where the data type of key as well as value should be of wrapper class.

To use the above statements we can use –

import java.util.Map;

import java.util.HashMap;

62. Statement to add values to dictionary –

nameOfDictionary.put( keyN , valueN );

63. Statement to extract values based on keys –

nameOfDictionary.get( key1 );

64. Statement to extract keyset –

nameOfDictionary.keySet( );

65. Statement to extract value set –

nameOfDictionary.values( );

66. Statement to extract size of the dictionary –

nameOfDictionary.size( );

67. Wrapper classes are classes whose object wraps and is used to store primitive object. When the objects for this class is initialized then it’s method stores the value of primitive data type. Data type declaration for Wrapper class data types –

char — Character byte — Byte short — Short

long — Long float — Float double — Double

boolean — Boolean

68. Syntax to print the entire dictionary –

System.out.println( nameOfDictionary.toString() );

This is possible because the Map class is part of object class and so it’s object can be printed by toString()

69. Syntax to clear the entire dictionary –

nameOfDictionary.clear() ;

70. To check whether the entire dictionary is empty or not –

System.out.println( nameOfDictionary.isEmpty() );

71. To remove an element from the dictionary based on key value –

nameOfDictionary.remove(<key>,value) ;

72. Recursion is a process of repeating the an algorithm again and again by calling the function again and again until the base condition isn’t satisfied.

73. Base condition is the condition at which repetition of an algorithm would stop and Recursive condition is the condition in which recursion would continue.

74. Class variables are special attributes of classes . In java they can be declared by using static keyword. Class variable and class method is synonymous with static variable and method. The value of a class variable isn’t dependent on the instance.

75. Instance variables are variables whose value is dependent on the instance or the object of the class.

76. Scope of static variables is global and so it cannot be declared within any method.

77. Constructor overloading : A way to overload a constructor.

78. Argument : Value passed as an argument.

79. Strings use ” ” & characters use ‘ ‘.

80. Explicit typecasting of variable to another can be done by –

(dataType) variableName ;

81. Statement to convert a string to a character array –

char[] charArrayName = stringName.toCharArray();

82. Data structures are a way of organizing data into efficient storage and retrieval.

83. arrayName.length is used extract the value of array’s length or dimension.

Okay , this turned out to be the longest log I have ever written. May be I will try to break down the logs of 5 lessons from next time.