Skip to content

Introduction to Programming πŸ‘¨β€πŸ’» in Java with Data structures + Algorithms taught at @WhatAfterCollege πŸŽ“.

License

Notifications You must be signed in to change notification settings

anishLearnsToCode/java-wac-batch-32

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

66 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Introduction To Java β˜• + Programming Fundamentals

made-with-java hackerrank-java hackerrank-ds hackerrank-algo hackerrank-interview-prep

Workshop Timings Workshop Dates: 16th - 29th July 2020 (weekdays)
Workshop Timings: 12:00 AM - 3:30 PM (12 - 15.30)
Break Timings: 2:00 - 2:20 PM (14 - 14.20)
Exceptional: 27th July workshop to be conducted @ 5:30 PM - 9:00 PM (17.30 - 21)

Class Recordings πŸ“½ Course Flow 🌊

Index

Introduction

Solutions to all sample problems on HackerRank under the Java domain can be looked up here.

Programming is a very hands process and is both an art as well as a science. We are engineers and are required to create efficient solutions but at the same time our programs should be highly readable and flexible and all the other snappy terms which makes it an art as well.

To become proficient in this art, there are many resources, and books and tutorials. Each has it's merit and making the first step in any direction is commendable, but the cardinal factor at the end of the day will be you sitting down (or standing) and writing code. No book or resource can substitute that.

So, what are you waiting for πŸ˜€πŸ˜‰ - try as many questions (below or otherwise) as you can....

Stalk your Instructor on GitHub & linkedIn.

Environment Setup

We need to install and configure a few things before we can write and run any Java code. To write Java code we need the Java runtime environment (JRE) and the Java Development Kit.

1. Install the Java Development Kit (JDK)

JDK Version >= 1.8 is recommended i.e. Java version 8 or above is recommended for this course and personal development projects. Install Java for your OS from the Oracle Website.

To check that java has been correctly installed on your machine run the following command on your terminal:

java -version

It should have an output akin to:

java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

Once that Java has been successfully installed, we need to install a code editor or IDE so that we can write programs and run them. I suggest using VS Code if you prefer a Code editor over an IDE (or if you don't know the difference between Code editor and IDE πŸ˜‰). using a code editor will aso be less intensive on computing resources.

I personally prefer the JetBrains IntelliJ IDEA, but warning ⚠ it is a heavy software and might not run properly on all machines (especially laptops that are constrained for resources).

2. Installing VS Code (or go to step 3 - installing IntelliJ IDEA)

  1. Download the setup from here.
  2. Run the setup which is pretty straight forward. Just click next like 10 times and voila!

3. Installing JetBrains IntelliJ IDEA

  1. You can either install the community edition (free) from here.
  2. Or you can create an account on JetBrains if you have a university email address and then install the JetBrains Toolbox.
  3. You can easily manage JetBrains IDE's and projects using the ToolBox app. From the ToolBox app you can now install either IntelliJ IDEA Community Edition or Ultimate Edition.

4. Installing git (Optional - Only required for Windows users)

This is an optional step of your getting started guide and can be skipped. Although installing git and using it in your projects is highly recommended. For a learning path to learn git have a look at the Future Path + Scope section.

Install git from the git-scm website. Run the setup and click next like 10 times and use the recommended settings for installation.

There will be a section when it will ask the standard text editor and gie you an option between Vim and nano. If you are not familiar with Vim then opt for nano.

IMPORTANT Opt for nano if not familiar with Vim.

To check your installation of git check that git bash has been intalled and run the following command on your terminal.

git --version

The output should be akin to

git version 2.24.1.windows.2

5. Writing your first Java Program

Open your text editor/IDE and create a new file HelloWorld.java and in that file copy paste the following code snippet.

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

To run the code navigate to file in terminal and run the following commands.

javac HelloWorld.java
java HelloWorld
Hello World # output

Day 1

Topics Covered

Sample Problems To Try

Problem Source Solution Link
Welcome to Java HackerRank Solution
Java StdIn and StdOut I HackerRank Solution
Java If Else HackerRank Solution
Data Types Problem HackerRank Solution
Java Int To String HackerRank Solution
Java Currency Formatter Problem HackerRank Solution
Java String Reverse HackerRank Solution

Day 2

Topics Covered

Sample Problems To Try

Problem Source Solution Link
Java Output Formatting HackerRank Solution
Java Strings Introduction HackerRank Solution
Java Substring HackerRank Solution
Java String Compare HackerRank Solution
Java Date and Time HackerRank Solution

Loop based & Pattern based questions given here

Problem No. Solution
Question 1 Solution
Question 2 Solution
Question 3 Solution
Question 4 Solution
Question 5 Solution
Question 6 Solution
Question 7 Solution
Question 8 Solution
Question 9 Solution
Question 10 Solution
Question 11 Solution
Question 12 Solution
Question 13 Solution
Question 14 Solution
Question 15 Solution
Question 16 Solution
Question 17 Solution
Question 19 Solution

Day 3

Topics Covered

Sample Problems to Try

Problem Solution Link
Question 18. a Solution
Question 18. b Solution
Question 18. c Solution
Question 18. d Solution
Question 18. e Solution
Question 18. f Solution
Question 1 WAP (Write a program) to a print the following pattern. User enters rows [See Solution]
********
***  ***
**    **
*      *    
* * ** ** *** *** ********
Question 2 WAP to print the following pattern. User will enter number of rows (rows >= 2). If user enters rows < 2 then no pattern is printed. You can also add a second variable called columns that takes how many stars * to print in the first and last line. [See Solution]
********
*      *
*      *
*      *
*      *
********
Question 3 WAP to print the following pattern, User enters rows [See Solution]
*****
*  *
* *
*
Question 4 WAP to print the following pattern. User enters rows. [See Solution]
* * * * *
 * * * *
  * * *
   * *
    *
Question 5 WAP to print the following pattern. User enters rows.
    *
   * *
  * * *
 * * * *
* * * * *
Question 6 WAP to print the following pattern. User enters rows. [See Solution]
    *
   * *
  *   *
 *     *
* * * * *
Question 7 WAP to print the following pattern. User enters rows. [See Solution]
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Question 8 WAP to print the following pattern. User enters rows. [See Solution]
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Question 9 WAP to print the following pattern. User enters rows. [See Solution]
1
1 2
1 x 3
1 x x 4
1 x x x 5
1 2 3 4 5 6
Question 10 WAP to print the following pattern. User enters rows. [See Solution]
A
A B A
A B C B A
A B C D C B A
Question 11 WAP to print the following pattern. User enters rows. [See Solution]
********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
****5*5*5*5*5****
***6*6*6*6*6*6***
**7*7*7*7*7*7*7**
*8*8*8*8*8*8*8*8*
Question 12 WAP to print the following pattern. User enters rows. [See Solution]
   *
  * *
 * * *
  * *
   *
Question 13 WAP to print the following pattern. User enters rows. [See Solution]
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
Question 14 WAP to print the following pattern. User enters rows. [See Solution]
*
**
***
****
***
**
*

In case rows = 4

*
**
**
*
Question 15 WAP to print the following pattern. User enters rows. [See Solution] For rows = 3
3
44
555
6666
555
44
3

For rows = 2

2
33
444
33
2

For rows = 5

5
66
777
8888
99999
101010101010
99999
8888
777
66
5
Question 16 WAP to print the following pattern. User enters rows. [See Solution] For rows = 4
1
2 3
4 5 6
7 8 9 10

For rows = 2

1
2 3
Question 17 WAP to print the following pattern. User enters rows. [See Solution] For rows = 4
1
2*3
4*5*6
4*5*6
2*3
1

If rows = 4

1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
Question 18 WAP to print the following pattern (Pascal's Triangle). User enters rows. [See Solution] For rows = 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

The pattern is derived in the pascal's triangle using values in the previous row.

1
1 1
1 (1 + 1) 1
1 (1 + 2) (2 + 1) 1
1 (1 + 3) (3 + 3) (3 + 1) 1
Question 19 WAP to print the following pattern (Hollow Rhombus). User enters rows. [See Solution] For rows = 3
******
**  **
*    *
*    *
**  **
******

For rows = 5

**********
****  ****
***    ***
**      **
*        *
*        *
**      **
***    ***
**** *****
**********
Question 20

WAP to print the following pattern (Butterfly pattern) [See Solution]

For rows = 3

*    *
**  **
******
******
**  **
*    *

For rows = 5

*        *
**      **
***    ***
****  ****
**********
**********
****  ****
***    ***
**      **
*        *

Day 4

Topics Covered

Sample Problems

  1. Time Complexity Problems [InterviewBit]

  2. WAP to create a function add() that takes 2 integer values and returns the sum. [See Solution]

public static int add(int a, int b) {
}
  1. WAP to create a function difference() that takes 2 integer values and returns the difference. [See Solution]
public static int difference(int a, int b) {
}
  1. WAP to create a function product() that takes 2 integer values and returns the product. [See Solution]
public static int product(int a, int b) {
}
  1. WAP to create a function divide() that takes 2 integer values and returns the quotient. [See Solution]
public static int divide(int a, int b) {
}
  1. WAP that creates a function which calculates the factorial of a number n. Where: [See Solution]
0! = 1
1! = 1
3! = 1 * 2 * 3 = 6
5! = 1 * 2 * 3 * 4 * 5 = 120
  1. WAP to create a function that calculates the Binomial Coefficient nCr. Where: [See Solution]
nCr = n! / (r! * (n-r)!)
4C0 = 4! / 0! 4! = 1
3C2 = 3! / 2!*1! = 6 / 2 = 3
  1. WAP to create a function that calculates the Permutation value nPr. Where [See Solution]
nPr = n! / (n-r)!
5P0 = 5! / 5! = 1
6P3 = 6! / 3! = 6*5*4 = 120
  1. WAP to create a function to check whether a given number is prime or not. [See Solution]
private static boolean isPrime(int number) {
} 
  1. WAP to create a function that returns the length of a hypotenuse of a right angle triangle given the lengths of the other 2 sides [See Solution]
private static double hypotenuseLength(double a, double b) {
}
  1. WAP to create a function to return the day of the week (name) from the day number. Example [See Solution]
private static String dayFrom(int value) {
}
dayFrom(0) --> "Sunday"
dayFrom(1) --> "Monday"
...
...
dayFrom(6) --> "Saturday"

Day 5

Topics Covered

Sample Problems

  • WAP to return the value of the sum of the following series recursively given n. [See Solution]

    1^1 + 2^2 + 3^3 + 4^4 + ... + n^n 
    
  • WAP a program that returns that sum of the first N natural numbers and finds this sum recursively. [See Solution]

    f(N) = 1 + 2 + 3 + ... + N   
    

    so, for N = 3 f(3) = 1 + 2 + 3 = 6 and for N = 5 f(5) = 15.

  • WAP a program that calculates the sum of the series

    f(N) = 1 + 2^2 + 3^2 + ... + N^2
    

    using a recursive function. [See Solution]

Day 6

Topics Covered

Sample Problems

  1. WAP to input an integer number length from the user and then instantiate an integer array of size length and then display that array on the terminal with all entered values.
    [See Solution]

  2. WAP that inputs an integer array from the user and returns the sum of all its elements. Create a dedicated method for calculating array sum with the following signature. [See Solution]

    private static int sum(int[] array);
  3. WAP that inputs an integer array from the user and prints the number of even, odd and zero elements. [See Solution]

  4. WAP to implement linear searching in an array. The user enters an array of size n and then an integer element. search for this element in the array and return the first index at which it is found otherwise return -1 if not found in the array. [See Solution]

  5. WAP that takes an array from the user and returns a reverse array with the order of all elements reversed and then prints this reversed array. [See Solution]

    If the input array is array = [1, 2, 3, 4, 5] then the resultant array should be = [5, 4, 3, 2, 1]
    

    The method should have the following signature.

     private static int[] reverse(int[] array);
  6. WAP that takes an integer array from the user and returns the product of all it's elements. [See Solution] The method signature should be

    private static long product(int[] array);
  7. WAP to tell whether an array is an anagram or not. The user will enter an integer array. [See Solution] For example

    array = [1, 2, 1] this is an anagram as the reverse of this array is also [1, 2, 1]
    array = [1, 1, 1, 1] This is also an anagram as the reverse is the same [1, 1, 1, 1]
    array = [] The empty array is also an anagram as the reverse is [] the same.
    array  = [-1, 0] is not an aanagram as the reverse = [0, -1] isn't the same.
    

    Crete a method with the following signature that returns boolean; true or false i it is an anagram or not.

     private static boolean isAnagram(int[] array);
  8. WAP to Merge 2 sorted arrays and return the new array which is also sorted. [See Solution] For example

    Let there be 2 arrays first = [1, 10, 20] and second = [1, 2, 4, 10, 67] Define a function with 
    a sigature as:
    
     private static int[] merge(int[] first, int[] second);
     which should return an array = [1, 1, 2, 4, 10, 10, 20, 67]
    

Day 7

Topics Discussed

Sample Problems

Problem Platform Solution Link
Java Inheritance I HackerRank Solution
Java Inheritance II HackerRank Solution
Java Abstract Class HackerRank Solution
Java Interface HackerRank Solution
Java Method Overriding HackerRank Solution
Java Overriding II (Super Keyword) HackerRank Solution

Day 8

Topics Covered

Sample Problems

Try the problems given below and have a look at the solution only after you have solved it to see the different implementations and ideas we might've used or to use as a reference if unable to solve yourself.

Problem Platform Solution Link
Java List HackerRank Solution
Java ArrayList HackerRank Solution
Java Comparator HackerRank Solution
Covariant Return Types HackerRank Solution

Day 9

Topics Discussed

Sample Problems

Try the problems given below and have a look at the solution only after you have solved it to see the different implementations and ideas we might've used or to use as a reference if unable to solve yourself.

Problem Platform Solution Link
Java Stack HackerRank Solution
Java Priority Queue HackerRank Solution
Maximum Element HackerRank Solution
Equal Stacks HackerRank Solution
Balanced Brackets HackerRank Solution

Day 10

Topics Discussed

Sample Problems

Problem Platform Solution Link
Java Anagrams HackerRank Solution
Java Hashset HackerRank Solution
Java Map HackerRank Solution

Capstone Project

To showcase what we have learnt in this 10 day training workshop I have built a small hangman cli game that covers the different language constructs we have seen and also shows an example project that can be built using Java. See here :octocat:.

Further Reading

The following online resources/videos, practice Questions and Books πŸ“š are a good place to further enhance your Java Knowledge.

Hint: :octocat: points to solution repo links...

Future Path??

Java is a very popular programming language and by learning the basics and stepping out into the Object Oriented Programing using Java could never be a bad step ;)

You can now advance in many different directions and learn many varied skill-sets.

Core Java

Something that you think about doing is further learning more Java basics and improving your fundamental understanding of the different concepts in Java such as different recipes to use the Object Oriented Programming to create different design patterns etc.

Also learning about threads and multi-threaded programs and understanding the core library and interfaces and concepts such as Reflection in a better manner.

This can be done by going through the questions on the Java Domain on hackerRank whose solutions can be found here.

You could also read up about the language comprehensively in Herbert Schildt's Excellent Reference volume here πŸ“˜

Data Structures and Algorithms πŸ‘©β€πŸ’»

Or like me you can take a deep dive in solving problems because that's just too much fun πŸŽ‰. I highly recommend HackerRank's Data Structures & Algorithms domain whose solutions can be found here & here. I also recommend trying out programming challenges on leet code for those who may not like HackerRank's UI. And if you do try that, do help me in building up my leet code solutions repository by contributing (making pull requests).

Learning Git git-scm

This is not very correlated to Java, but Git is a technology being used by all organizations big and small that wish to maintain their code over teams of varied sizes and manage projects. Even this repository which you are currently reading in is being maintained by git & has been deployed on github.

Being proficeint with git and version control will help you manage all your projects, be in any language Java, Python, C++ and even non-programming projects very efficiently and you will be able to easily maintain project state over all your devices.

There is an excellent Version Control with Git course on Coursera by Atlassian or you can even try this Git Introductory 30min Video on YouTube to learn the basics of git.

So, what are you waiting for git started 😁 (bad pun!)

Web Development

Java being such a versatile and popular language has many web frameworks that have been built on it. The most popular ones being:

What is the Difference between Spring and Spring Boot?

Spring Boot is basically an extension of the Spring framework which eliminated the boilerplate configurations required for setting up a Spring application. It takes an opinionated view of the Spring platform which paved the way for a faster and more efficient development eco-system

Web development consists of 2 major parts. One is designing the client side that mainly covers designing the interface and front end controls that the client will be able to see and interact with. The core front-end technologies are HTML, CSS and JavaScript and all popular frameworks such as Angular, Vue & React have been built using these core technologies only!

So, if you are interested in front-end design you can learn the basics of HTML5 + CSS3 + js @ w3 schools and then learn any popular front-end framework. The other part of web development is the server side part which is hidden from the user and this is where all the heavy lifting and computation takes place. This is also where all the logic such as validation, authentication and parsing of data takes place.

Both Spring ad Spring Boot are server side frameworks built on Java and after this course you can easily get started with either as you are now familiar with all Language constructs.

About

Introduction to Programming πŸ‘¨β€πŸ’» in Java with Data structures + Algorithms taught at @WhatAfterCollege πŸŽ“.

Topics

Resources

License

Stars

Watchers

Forks

Languages