Posts

Showing posts from March, 2020

Java Tutorials Series - Factorial program using Recursion

Recursion is basically an Algorithm in Java. If we call a method from inside the same method, we get a StackOverflow error in stack trace. It is always recommended not to use recursion but in some conditions it can be very helpful. Let's now look at Factorial program below. Factorial Program using Recursion -  Factorial logic as you might already know - 4! = 4*3*2*1 = 24 5! = 5*4*3*2*1 = 120 and the series go on... public class Maths { public static void main(String[] Args) { System.out,println(factorial(5)) ; private static int factorial(int value) { if(value==1) { return 1; } return(factorial(value - 1) * value);  } } //output of the above program is 125 That's it! the above code demonstrates the factorial program using Recursion. Another great example of this algorithm is implemented in The Tower of Hanoi . This post is being written at the time when there is pandemic all over the world and we are locked down in o...

Java Tutorials Series - Enum types

We will be learning about Enum in Java, how they can be implemented and what is the usage of Enum in Java in this tutorial. Consider there is a variable and it can have limited values in it, in that case we can make good use of Enum. Enum Types in Java Syntax of creating Enum in Java is similar to that of creating classes or interfaces in Java. by now, we already know that Enum contains the set of fixed values that can be used by a variable. It may or may not be in any order. Before learning Enum, let's look at one of the code below - Existing code in Java without the usage of enum - public class App { public static final int CAT = 0; public static final int DOG = 1; public static final MOUSE =2; public static void main(String[] Args) { int animal = CAT; switch(animal) { case DOG :     System.out.println("This is a DOG");     break; case CAT :    System.out.println("This is CAT");    break; } } } Can you see th...

Java Tutorials Series - Creating and Writing Text Files

We will be covering how to create and write fe lines in a text file using java code. We will make use of BufferedWriter and FileWriter classes in doing so. Creating and Writing Text Files - public class App { public static void main(String[] Args) {        File file = new File("test.txt");        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {        bw.write("Line one");        bw.newLine();        bw.write("Line two");        bw.newLine();        bw.write("Last line");    } catch(IOException e) { System.out.println("unable to write on File"+ file.toString()); } } } That's it! Stay tuned for more tutorials on Java. Happy learning!

Java Tutorials Series - Abstract Classes

What is an Abstract class and why do we need them? Let's uncover few fundamentals from Abstract Classes. Abstract Classes in Java - Few major points regarding Abstract classes are - We cannot create objects for Abstract classes. In other words, abstract classes cannot be instantiated. Abstract classes can have abstract methods as well as non-abstract methods. All the classes which are extending abstract classes must implement abstract methods. A class can extend only one abstract class but can implement multiple interfaces. An abstract class can have or nor have method implementation with or without body. This is all there is which needs to be taken care for Abstract Classes. Stay tuned for more awesome concepts. Happy Learning!

IELTS Task 1 Complain Letter

Image
In this Article we will cover a Band 8 letter. IELTS Task 1 Complain Letter -  Collocation used is highlighted below - Nevertheless at 9 am but your check-in system and it was not until 9:10 am that you I have a business meeting with international partners next day and now I can't be there at the right tine which can cause me a big loss of 5 million dollars. alternative flight before tonight so that I can  Few words which can be noted - unavoidably missed expectantly We will be covering more such letter. Stay tuned for more awesome content.

Java Tutorial Series - Handling Exceptions

Exceptions in Java is a topic which can help you deal with the real time problems faced in your project. What if you are getting an exception in stack trace and you simply want your scripts to move forward, in short you can handle expected exceptions in your scrips. In this article we will be learning about exceptions in java. Handling Exceptions in Java There are 2 different ways to handle exception in Java - Adding Try catch block Add throws declaration 1. Add Throws Declaration - public class App {       public static void main(String[] Args) throws FileNotFoundException {           File file = new File("text.txt");           FileReader fr = new FileReader(file); } } //prints FileNotFoundException in stack trace If you will run above code, you will get FileNotFoundException in Stack trace. This will hinder your execution. To overcome this limitation, let's look at Implementation of ...

Java Tutorial Series - How to read a text file

We can read a text file using few Java libraries, in this tutorial we will learn how to do it. How to Read a text file using Java - Firstly, copy the path of the text file. public class App {    public static void main(String[] Args) throws FileNotFoundException{        String filename = "C:\users\some_text_file.txt"; // Windows machine either accepts / or \\  File textFile = new File(filename); Scanner in = new Scanner(textFile)  While(in.hasNextLine()) { String line = in.nextLine(); System.out.println(line); } in.close(); } } That's it!!! and that's the easiest way to read a text file using scanner class. You can modify the code as per your needs. How to Read a file using FileReader - public class App {     public static void main(String[] Args) {     File file = new File("test.txt");     BufferedReader br = Null;   try {    FileReader fr = n...

Java Tutorial Series - Casting Numerical Values

In this article, we will learn about Casting numerical values. Casting is used to convert one data type to another. Casting Numerical values -  Before we begin, let's cover the size of data types or memories utilised by the data types - int - 32 bit short - 16 bit byte - -128 to 127 ~256 Let's see an example below - byte byteValue = 20; short shortValue = 55; int intValue = 888; long longValue = 23355; float floatValue = 8834.8f; double doubleValue = 32.4; System.out.println(Byte.MAX_VALUE); // prints 127 Converting data type long into int - intValue =(int)longValue; System.out.println(intValue);    //prints 23355 Converting double into int - doubleValue = intValue; System.out.println(doubleValue);    //prints 23355.0 Note - no need to cast it since we are not cutting off the value, only appending the value. Converting float into int - intValue = (int)floatValue; System.out.println(intValue); // prints 8834 Note - It won't...

Java Tutorial Series - OOPS - Encapsulation

We will learn more about OOPS concepts, We have already covered Inheritance, Polymorphism in recent articles. Let us learn about Encapsulation in this article. Have you ever used predefined function while coding? Do you ever wonder what is the internal logic behind that code? No? This is where encapsulation is implemented, the motive behind using that specific function is achieved without getting into detail. We already understand the access specifiers now which is covered in the recent article, to implement encapsulation, we must be having good understanding of access modifiers in Java. Adding to it, one must use getters and setters as well while implementing encapsulation. OOPS - Encapsulation -  Encapsulation is a design pattern which gives flexibility to your framework. Always remember 3 rules if you want to implement Encapsulation - Always make instance variable private Always make public accessor methods and force calling code to use these methods instead of directly call...

Java Tutorial Series - OOPS - Polymorphism

Let's see how polymorphism is implemented and how it works in this article. We will also look at upcasting and down casting in Java. Polymorphism in Java - Polymorphism means achieving a single task in multiple ways, or in many forms. We have broadly 2 categories of Polymorphism Compile time/ early binding/ static binding/ overloading Run time/ late binding/ dynamic binding/ overriding //Parent class pubic class Plant { public void grow() { System.out.println("Plant growing"); } } public class Tree extends Plant { public void grow() { System.out.println("Tree growing"); } } public static void main(String[] Args) { Plant plant = new Tree(); // this is polymorphism plant.grow(); } //Output - Tree growing object of child class is pointing to reference variable of parent class. In simple words, remember this - Parent pc = new child(); Upcasting and down casting in Java -  Let's have a look at how upcasting and down castin...

Java Tutorial Series - Access Specifiers

In this Java tutorial, we will cover Access Specifiers - Public, Private and Protected, their visibility and the scope. Access Modifiers in Java - There are 4 different access modifiers in Java - Public Private Protected Default (undefined) private - Private access modifiers can only be accessed within the same class. Even if the class is extended by different sub-class, yet it won't be able to access the access modifier having keyword 'Private'.   protected - Can be accessed by child classes and the parent class where the variable is declared. It works in same package although. We don't need to extend the class, if an object is created of the parent class, it will still work. public - can be accessed from anywhere. Default - works in same package. It works like protected access modifier. All in all, we don't need to keep small details, high level summary will also work - private - only within same class public - from anywhere pr...

Java Tutorial Series - Interface

This tutorial will cover basics of Interface, why do we use them and what are the certain rules we need to keep in mind while working on Interface. Interface in Java -  Let's say there are multiple classes which have common function, so one class can extend only one parent class, in this case we will proceed with creating an interface and defining the method without a body. Classes which are implementing the interface must use all the methods defined in the interface. Consider below example - public interface CentralBank {        public void withdraw();        public void deposit(); } //Class which is implementing CentralBank interface public class HDFC implements CentralBank{ private String repo = 7; private int withdraw_rate = 15; private int deposit_rate = 18; public HDFC(String repo) {  this.repo = repo; } public void FDRates() {   System.out.println("FD Rates"); } @Override public void withdraw(...

Java Tutorial Series - OOPS - Inheritance

Now we have gone through the basics of Core Java which will set up a base for future concepts to be learned. In this series, we will be covering OOPS concepts, we have understood basic concepts, Collections in Java, now it is high time to dive deep into OOPS. Inheritance in Java -  Using the concept of Inheritance, sub class can inherit the properties of base class, in other words sub class can use the methods of parents class and can also override them. We can see it in below example - public class Machine {          public void start() {               System.out.println("Machine Started");               }       public void stop() {          System.out.println("Machine Stopped");          } } public Car extends Machine { @Override public void start() {               S...

Java Tutorial Series - Playing with Strings

Strings are the most important topic when learning Java. We will be dealing with different operations on Strings in this article, will get to know what StringBuilder class does, StringBuffer class, the difference between the two and string formatting actions. A String is immutable, which means once it is created, it cannot be changed. StringBuilder Class in Java-  Let's see an example on how to use append method for StringBuilder - StringBuilder sb = new StringBuilder() ; sb.append("This is testing blog"); sb.append(" "); sb.append("We are learning about Strings here"); System.out.println(sb.toString()); Difference between StringBuilder and StringBuffer -  Ever wondered why Java provide 2 different classes with similar functionality. StringBuffer is thread safer version of StringBuilder. It is used in multi threaded version. StringBuilder is light in weight because it is not thread safe. String Formatting -  \n - u...