Monday, 16 October 2017

Types of Variables in java

In this post, we will talk about java variables. Like a different type of variable how to use it?. how to access it etc?. So java is the class base language the class contains five elements.


Java Class Elements are


1.variable
2.methods
3.constructor
4.instance block
5.static block



Variables


So C/C++ language contains variable int a=10 b=20 etc. in the same way java also contains some variable part. so to work with employe project we need employee id, name, salary etc.So every project required variable concepts. So before going to the variable concept.First of all, we must know what is the purpose of the variable in java. So the variable is used to store the values by using these value we achieve the project requirements.For example, an employee project, we need employee salary, name, id etc.

So in a single line variable are used to store value and by using these value we achieve the project requirements. generally, variables are 3 types.


Types of Variable


1. local variables
2. instance variables
3. static variables


1. local variables


1. The variable which is declared inside method or constructor or blocks also. Those variables called local variables

Example:-

class 
{
public static void main(String[] args) 
{
//local variables
int a=50;
int b=20
System.out.println(a);
System.out.println(a);
}
}

2. The scope of the local variables only within the method only or within the constructor or within the blocks only. So simply can say that local variable cannot be accessed the outside the method, block, constructors. If we trying to access the local variable outside it generates the error message.

Example:-

class demo
{
public static void main(String[] args) 
{
void method()
{
int a=30;
System.out.println(a); //possible

}
void method2()
{
System.out.println(a);// not possible to print inside the method

}
}
}

3. local variable Memory Allocated when method start and the whenever the method is completed memory is released.Local variable stored in stack memory.


2. Instance variables


So before the brief of instance variables, we must know areas of java language.java contains 2 types of areas, instance area, and static area.so once we understand these 2 areas then java variables concept is very easy. So the body of instance method called instance area, and the body of the static method called the static method.

Example:-
   
class demo
{

void method()// instance method
{

//instance area

}
static void method2() //static method
{
//static area

public static void main(String[] args) 
{

}
}


1. The variable that is declared inside the class but outside the method, Those variables called instance variables.

2. The scope of the instance variable is inside the class all methods, constructor and blocks are able to access.

3. For the instance variable memory allocated when the object is created and memory is destroyed when the object is destroyed.

4. Instance variable stored in heap memory

Note:- When the same area we can access the instance variable directly. But in a different area, first of all, we must allocate memory of instance variable So how memory is allocated, linked with the object, means create an object of that class in a single line, first of all, create an object of that class. And access that class instance variable and method by using that object.

Instance var, method==========>direct access========>instance area

Instance var,method==============>throw object========>static area

Example:-

class demo
 {
//instance var
int x=20;
int y=30;
  void m()//instance method
{
 //instance area
 System.out.println(x+y);
     }

 public static void main(String[] args)//main method (static method) 
{
//static area
 demo d new demo();
 System.out.println(d.x);
 System.out.println(d.y);
 d.m();
}
}



3. Static variable


1. The variable that is declared inside the class but outside the method with a static modifier, Those variables called static variables.

Example:-

class 
{ //static  variables
static int a; 
static float b;
static char c;
}

2. Static variable memory allocated when the .class file is loading.

3. The scope of static variable is inside the class 

4. Static variable stored in non-heap memory.

Note:- access the static variable and method by using the class name. Reason static variable memory allocated when the .class file is loading. means before the main method load, static variable memory allocated.

Example:-

class demo
 {
//static var
static int x=20;
static int y=30;
  void m()//instance method
{
 //instance area
  System.out.println(demo.x);
 System.out.println(demo.y);
     }

 public static void main(String[] args)//main method (static method) 
{
//static area
 demo d = new demo();
 System.out.println(demo.x);
 System.out.println(demo.y);
 d.m();
}
Read More »

Wednesday, 4 October 2017

Hacker Rank solution For Day 19: Interfaces

Problem:- Write a Hackerrank Solution For Day 19: Interfaces or Hacker Rank Solution Program In C++ For " Day 19: Interfaces " or Hackerrank 30 days of code Java Solution:Day 19: Interfaces solution or Hackerrank solution for 30 Days of Code Challenges or Hackerrank 30 days of code Java Solution, Day 19: Interfaces solution, or C/C++ Logic & Problem Solving: Day 19: Interfaces or Hacker Rank Solution For Day 19: Interfaces.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

Explanation:- This is a very simple problem(Interfaces) we have to just calculate divisor sum and print the sum of the divisor. we can do this by run the loop(started with 1 and end with N) and put the condition if number N is divided by I(where I = 1 to N) than the sum of the divisor is added each time when if N%I==0. Let's take an example and try to understand the problem step by step.

Example:- Let's assume Number N is 24 and we have to calculate the Divisor sum so we divide the number by I=1 to I=24 with the help of "For Loop" Take a look at step by step.

Step 1: If(N % I ==0) then Sum or total is Sum = Sum + I Or Total = Total + I.

Step 2: Print the total outside the "For Loop". Here Divisor of 24 = 1 + 2 + 3 + 4 + 6 + 8 + 12 + 24 = 60.

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Extreme Recommended:- If this post is beneficial for you and you want Updates for New post then please like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.


Tip:- Copy the colored code or full code(According to Requirement) and paste it into hacker rank editor.All solution provided here are in C++ (CPP) if any reader wants these solutions in C, and Java comments below or Email me with your query like " day n solution in C / C++ / Java. Check the end of the post solutions with the full explanation

Submit Your Solution Here:- Click Here

Solution:-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
class AdvancedArithmetic
{
    public:
        virtual int divisorSum(int n)=0;
};

struct Calculator : public AdvancedArithmetic
{
    int divisorSum(int n)
    {
    int total=0,i;
        for(i=1; i<=n; i++)
        { 
            if(n%i == 0)
            { 
            total+=i; 
            }
        }
        return total;
    }
};

int main()
{
    int n;
    cin >> n;
    AdvancedArithmetic *myCalculator = new Calculator(); 
    int sum = myCalculator->divisorSum(n);
    cout << "I implemented: AdvancedArithmetic\n" << sum;
    return 0;
}

Output:-


Hacker Rank solution For Day 19: Interfaces

You May Also Like:-

1. Hackerrank Solution For Day 12: Inheritance

2. Hackerrank Solution For Day 13: Abstract Classes

3. Hackerrank Solution For Day 14: Scope

4. Hackerrank Solution For Day 15: Linked List

5. Hackerrank Solution For Day 20: Sorting

6. Hackerrank Solution For Day 21: Generics

7. Hackerrank Solution For Day 22: Binary Search Trees

8. Hackerrank Solution For Day 23: BST Level-Order Traversal

9. Hackerrank Solution For Day 24: More Linked Lists

10. Hackerrank Solution For Day 25: Running Time and Complexity

Read More »

Hacker Rank solution For Day 20: Sorting

Problem:- Write a Hackerrank Solution For Day 20: Sorting or Hacker Rank Solution Program In C++ For " Day 20: Sorting " or Hackerrank 30 days of code Java Solution:Day 20: Sorting solution or Hackerrank solution for 30 Days of Code Challenges or Hackerrank 30 days of code Java Solution, Day 20: Sorting solution, or C/C++ Logic & Problem Solving: Day 20: Sorting or Hacker Rank Solution For Day 20: Sorting.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

Explanation:- This is a Simple case of sorting we have to sort the array and also count the total number of swap and minimum and maximum elements of an array this is an Advance Bubble Sort Example we also put the one special condition If array is already sorted than no need to run an array or for saving a time complexity if an array is already sorted than running time complexity is O(n). If an array is not sorted then we have to swap array elements and also count the swap how many swaps needed to sort an array. I strongly recommend checking some Sorting problem Like Bubble Sort, Selection Sort, Insertion Sort for full clear doubts.

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Extreme Recommended:- If this post is beneficial for you and you want Updates for New post then please like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.


Tip:- Copy the colored code or full code(According to Requirement) and paste it into hacker rank editor.All solution provided here are in C++ (CPP) if any reader wants these solutions in C, and Java comments below or Email me with your query like " day n solution in C / C++ / Java. Check the end of the post solutions with the full explanation

Submit Your Solution Here:- Click Here

Solution:-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() 
{
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int i, j, n, temp, count=0, Swaps;
    cin>>n;
    int array[n];
    
    for (i = 0; i <n; i++) 
    {
        cin>>array[i];
    } 
    for (i = n-1; i >0; i--) 
    {
        Swaps = 0;
    for (j = 0; j <i; j++) 
    {           
        if(array[j]>array[j+1])
        {
            temp=array[j];
            array[j]=array[j+1];
            array[j+1]=temp;
            Swaps++;
            count++;
        }
    }
    if (Swaps == 0) 
    {
        break;
    }
    }   
    cout<<"Array is sorted in "<<count<<" swaps.\n";
    cout<<"First Element: "<<array[0]<<endl;
    cout<<"Last Element: "<<array[n-1]<<endl;
    return 0;
}

Output:-


Hacker Rank solution For Day 20: Sorting

You May Also Like:-

1. Hackerrank Solution For Day 13: Abstract Classes

2. Hackerrank Solution For Day 14: Scope

3. Hackerrank Solution For Day 15: Linked List

4. Hackerrank Solution For Day 19: Interfaces

5. Hackerrank Solution For Day 21: Generics

6. Hackerrank Solution For Day 22: Binary Search Trees

7. Hackerrank Solution For Day 23: BST Level-Order Traversal

8. Hackerrank Solution For Day 24: More Linked Lists

9. Hackerrank Solution For Day 25: Running Time and Complexity

10. Hackerrank Solution For Day 26: Nested Logic

Read More »

Hacker Rank solution For Day 26: Nested Logic

Problem:- Write a Hackerrank Solution For Day 26: Nested Logic or Hacker Rank Solution Program In C++ For " Day 26: Nested Logic " or Hackerrank 30 days of code Java Solution:Day 26: Nested Logic solution or Hackerrank solution for 30 Days of Code Challenges or Hackerrank 30 days of code Java Solution, Day 26: Nested Logic solution, or C/C++ Logic & Problem Solving: Day 26: Nested Logic or Hacker Rank Solution For Day 26: Nested Logic.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

Explanation:- We have to solve the problem according to given task, the task is given below. There are 4 cases given in the task our solution should pass the all 4 cases according to condition and print the Hackos or fine. Let's take an example and try to find out the solution of this problem.

Task:- Nested Logic Full filled the given task. 
1. If the book is returned on or before the expected return date, no fine will be charged (i.e.: fine = 0).
2. If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, fine = 15 Hackos * (The number of days late).
3. If the book is returned after the expected return month but still within the same calendar year as the expected return date, the fine = 500 Hackos * (The number of days late).
4. If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000 Hackos

Example:- Suppose we have given below input. Input formate is first DD: MM: YYYY

Actual  =====>15 12 2015
Expected ===>15 6 2015

Now according for our logic in the program.

diff =    15 - 15 = 0.
mdiff = 12 - 6 = 6. 
ydiff =  2015 - 2015 = 0.

Now first condition is failed actual year - expected year so according to ternary operator second part will be executed, so according to second part (Month-Month1>0&&ydiff==0) this part is passed cause 12 - 6 = 6 and year difference(actual year - expected ) is 0 so Month difference ((actual year - expected) * 500). Month difference is 6 * 500 = 3000. So this is a Hackos we have to pay.

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Extreme Recommended:- If this post is beneficial for you and you want Updates for New post then please like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.


Tip:- Copy the colored code or full code(According to Requirement) and paste it into hacker rank editor.All solution provided here are in C++ (CPP) if any reader wants these solutions in C, and Java comments below or Email me with your query like " day n solution in C / C++ / Java. Check the end of the post solutions with the full explanation

Submit Your Solution Here:- Click Here

Solution:-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() 
{
    int Day, Month, Year, Day1, Month1, Year1, mdiff, ddiff, ydiff, diff;
    
    cin>>Day>>Month>>Year>>Day1>>Month1>>Year1;

    mdiff = Month - Month1;
    ddiff = Day - Day1;
    ydiff = Year - Year1;
    diff=(Year-Year1>0)?10000:(Month-Month1>0&&ydiff==0)?mdiff*500:(Day-Day1>0 && ydiff==0)?ddiff*15:0;    
    cout<<diff<<endl;

    return 0;
}

Output:-


Hacker Rank solution For Day 26: Nested Logic

You May Also Like:-


1. Hackerrank Solution For Day 20: Sorting

2. Hackerrank Solution For Day 21: Generics

3. Hackerrank Solution For Day 22: Binary Search Trees

4. Hackerrank Solution For Day 23: BST Level-Order Traversal

5. Hackerrank Solution For Day 24: More Linked Lists

6. Hackerrank Solution For Day 25: Running Time and Complexity

7. Hackerrank Solution For Day 26: Nested Logic

8. Hackerrank Solution For Day 27: Testing

9. Hackerrank Solution For Day 28: RegEx, Patterns, and Intro to Databases

10. Hackerrank Solution For Day 29: Bitwise AND


Read More »

Tuesday, 3 October 2017

Hacker Rank Solution For Day 18: Queues and Stacks

Problem:- Write a Hackerrank Solution For Day 18: Queues and Stacks or Hacker Rank Solution Program In C++ For " Day 18: Queues and Stacks " or Hackerrank 30 days of code Java Solution:Day 18: Queues and Stacks solution or Hackerrank solution for 30 Days of Code Challenges or Hackerrank 30 days of code Java Solution, Day 18: Queues and Stacks solution, or C/C++ Logic & Problem Solving: Day 18: Queues and Stacks or Hacker Rank Solution For Day 18: Queues and Stacks.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.


Hacker Rank Solution For Day 18: Queues and Stacks

Explanation:- Before going to solve first we have to know how Stack and Queue works and what is the difference between Stack and Queue so let's take an into of both Stack and Queue. here we have to check string is Palindrome or Not. First, see this C++ Program To Check String Is Palindrome Or Not and also See this C++ Program To Find Whether A Number Is Palindrome Or Not.


Stack:- Stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element of the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out).


Hacker Rank Solution For Day 18: Queues and Stacks

source:- Wikipedia

Queue:- A Queue  is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure


Hacker Rank Solution For Day 18: Queues and Stacks

source:- Wikipedia

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

We can create a whole Stack and Queue program by using an array but here with the help of #include <stack> and #include <queue> header file or we can use one Master header file #include<bits/stdc++.h> instead of using multiple header files.


Extreme Recommended:- If this post is beneficial for you and you want Updates for New post then please like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.

Tip:- Copy the colored code or full code(According to Requirement) and paste it into hacker rank editor.All solution provided here are in C++ (CPP) if any reader wants these solutions in C, and Java comments below or Email me with your query like " day n solution in C / C++ / Java. Check the end of the post solutions with the full explanation

Submit Your Solution Here:- Click Here

Solution:-

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

class Solution 
{
    //Write your code here
    stack<char> stk;
    queue<char> que;
    public:
    
char c;
    
    void pushCharacter(char ch) 
{
        stk.push(ch);
    }
        char popCharacter() 
{
        c = stk.top();
        stk.pop();
        return c;
    }
    void enqueueCharacter(char ch) 
{
        que.push(ch);
    }
    char dequeueCharacter() 
{
        c = que.front();
        que.pop();
        return c;
    }
};

int main() 
{
    // read the string s.
    string s;
    getline(cin, s);
    
  // create the Solution class object p.
    Solution obj;
    
    // push/enqueue all the characters of string s to stack.
    for (int i = 0; i < s.length(); i++) 
{
        obj.pushCharacter(s[i]);
        obj.enqueueCharacter(s[i]);
    }
    
    bool isPalindrome = true;
    
    // pop the top character from stack.
    // dequeue the first character from queue.
    // compare both the characters.
    for (int i = 0; i < s.length() / 2; i++) 
{
        if (obj.popCharacter() != obj.dequeueCharacter()) 
{
            isPalindrome = false;
            
            break;
        }
    }
    
    // finally print whether string s is palindrome or not.
    if (isPalindrome) 
{
        cout << "The word, " << s << ", is a palindrome.";
    } else 
{
        cout << "The word, " << s << ", is not a palindrome.";
    }
    
    return 0;
}

Output:-


Hacker Rank Solution For Day 18: Queues and Stacks


You May Also Like:-


1. Hackerrank Solution For Day 12: Inheritance

2. Hackerrank Solution For Day 13: Abstract Classes

3. Hackerrank Solution For Day 14: Scope

4. Hackerrank Solution For Day 15: Linked List

5. Hackerrank Solution For Day 19: Interfaces

6. Hackerrank Solution For Day 20: Sorting

7. Hackerrank Solution For Day 21: Generics

8. Hackerrank Solution For Day 22: Binary Search Trees

9. Hackerrank Solution For Day 23: BST Level-Order Traversal


10. Hackerrank Solution For Day 24: More Linked Lists


Read More »

Hackerrank Solution For Day 17: More Exceptions

Problem:- Write a Hackerrank Solution For Day 17: More Exceptions or Hacker Rank Solution Program In C++ For " Day 17: More Exceptions " or Hackerrank 30 days of code Java Solution:Day 17: More Exceptions solution or Hackerrank solution for 30 Days of Code Challenges or Hackerrank 30 days of code Java Solution, Day 17: More Exceptions solution, or C/C++ Logic & Problem Solving: Day 17: More Exceptions or Hacker Rank Solution For Day 17: More Exceptions.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.



Hackerrank Solution For Day 17: More Exceptions

Logic:- As we solve day 16 problem this is an extension of that problem. So in this problem we have to do More Exception, Basically in this problem two parameters will be given N and P and we have to calculate N^P as we know that we can not find the root and square root of negative number or in this problem while N and P are positive programs will work fine and if any N and P were Negative than Run Time error occur for handling this type of error(Run Time) we use Handler called Exception. Consider a below Input and output and check the answer in Explanation section.

Explanation:- As the given input first line is a total number of input then First input, a second input, third input and last forth input. First input is 3 (N) and 5 (P) so N^P or 3^5 is equal 243 same for second input 2^4 is equal to 16 But in third input (-1)^(-2) this can't be for avoid this type of error(Run Time Error) we use Try-Catch(Exception Handler). Now if the user enters third input than our program finds a runtime error and throw a control to catch block(print the message in the catch block).

Input

4
3 5
2 4
-1 -2
-1 3

Output

243
16
n and p should be non-negative.
n and p should be non-negative.

Syntax of Try-Catch

try
{
statement......1
statement......2
statement......3
.
.
.
statement......n
}
catch(exception )
{
statements //message
}

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Basically, Try- catch is used in a runtime error in a program. Read full about Try-Catch click here

Extreme Recommended:- If this post is beneficial for you and you want Updates for New post then please like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.

Tip:- Copy the colored code or full code and paste it into hacker rank editor.All solution provided here are in C++ (CPP) if any reader wants these solutions in C, and Java comments below or email me with your query like " day n solution in C / C++ / Java. Check the end of the post solutions with the full explanation

Submit Your Solution Here:- Click Here

Solution:-

#include <cmath>
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;

//Write your code here
class Calculator 
{
    public:
    int power(int first, int second) 
    {
        if(first < 0 || second < 0) 
        {
           throw runtime_error("n and p should be non-negative");
        }
        return pow(first, second);
    }
};

int main()
{
    Calculator myCalculator=Calculator();

    int T,n,p;
    cin>>T;

    while(T-->0)
{
      if(scanf("%d %d",&n,&p)==2)
{
          try
{
               int ans=myCalculator.power(n,p);
               cout<<ans<<endl; 
          }
          catch(exception& e)
{
             cout<<e.what()<<endl;
          }
      }
    }
    
}

Output:-


Hackerrank Solution For Day 17: More Exceptions


You May Also Like:-


1. Hackerrank Solution For Day 12: Inheritance

2. Hackerrank Solution For Day 13: Abstract Classes

3. Hackerrank Solution For Day 14: Scope

4. Hackerrank Solution For Day 15: Linked List

5. Hackerrank Solution For Day 18: Queues and Stacks

6. Hackerrank Solution For Day 19: Interfaces

7. Hackerrank Solution For Day 20: Sorting

8. Hackerrank Solution For Day 21: Generics

9. Hackerrank Solution For Day 22: Binary Search Trees


10. Hackerrank Solution For Day 23: BST Level-Order Traversal


Read More »

Hacker Rank Solution For Day 16: Exceptions - String to Integer

Problem:- Write a Hackerrank Solution For Day 16: Exceptions - String to Integer or Hacker Rank Solution Program In C++ For " Day 16: Exceptions - String to Integer " or Hackerrank 30 days of code Java Solution:Day 16: Exceptions - String to Integer solution or Hackerrank solution for 30 Days of Code Challenges or Hackerrank 30 days of code Java Solution, Day 16: Exceptions - String to Integer solution, or C/C++ Logic & Problem Solving: Day 16: Exceptions - String to Integer or Hacker Rank Solution For Day 16: Exceptions - String to Integer

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.



Hacker Rank Solution For Day 16: Exceptions - String to Integer

Logic:- There is no logic for this problem we have to use try-catch block(same as Java) as we know if the condition in the try block is true then the program is working fine else catch part will be executed. In this problem, we are taking a string input and printing its string value or string can't be converted then it should print Bad String that is why we use Try Catch block if program convert string into integer then it is Ok. if not than Catch block will be executed and we put a message on catch block.

stoi - The stoi() function takes a string as an argument and returns its value.

Syntax of Try-Catch

try
{
statement......1
statement......2
statement......3
.
.
.
statement......n
}
catch(exception )
{
statements //message
}

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Basically, Try- catch is used in a runtime error in a program. Read full about Try-Catch click here

Extreme Recommended:- If this post is beneficial for you and you want Updates for New post then please like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.


Tip:- Copy the colored code or full code and paste it into hacker rank editor.All solution provided here are in C++ (CPP) if any reader wants these solutions in C, and Java comments below or email me with your query like " day n solution in C / C++ / Java. Check the end of the post solutions with the full explanation

Submit Your Solution Here:- Click Here

Solution:-

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main()
{
    string S;   
    cin >> S;
    
    int n;

    try
    {
        n=stoi(S);
        cout<<n<<endl;
    }
    catch(exception e)
    {
        cout<<"Bad String";
    }
    return 0;
}

Output:-


Hacker Rank Solution For Day 16: Exceptions - String to Integer


You May Also Like:-

1. Hackerrank Solution For Day 12: Inheritance

2. Hackerrank Solution For Day 13: Abstract Classes

3. Hackerrank Solution For Day 14: Scope

4. Hackerrank Solution For Day 15: Linked List

5. Hackerrank Solution For Day 17: More Exceptions

6. Hackerrank Solution For Day 18: Queues and Stacks

7. Hackerrank Solution For Day 19: Interfaces

8. Hackerrank Solution For Day 20: Sorting

9. Hackerrank Solution For Day 21: Generics


10. Hackerrank Solution For Day 22: Binary Search Trees


Read More »

Sunday, 3 September 2017

Introduction of Java

Basic Knowledge of Java programming language-

You can also find the GeeksforGeeks Solution For School Domain .Below You Can Find The Solution Of Basic,Easy ,Medium ,Hard ,Misc.You Can Also Direct Submit Your Solution to GeeksforgeeksSame Problem .You Need to login then you can submit you answers .Here Mostly Solutions Available in C++ .You also can Find HackerRank Solution Click Here 


Author: James Gosling

Vendor: Sun Micro System

Project name: Green Project

Type: open source & free software

Initial Name: OAK language

Present Name: java

Extensions : .java & .class & .jar

Initial version : jdk 1.0 (java development kit)

Present version: java 8 March 2015

Operating System : multi Operating System

Symbol: coffee cup with saucer

Objective: To develop web applications

SUN: Stanford Universally Network

Slogan/Motto: WORA(write once run anywhere)




Read More »

Importance and Introduction of Core Java

Basic Knowledge of Java programming language


Author: James Gosling

Vendor: Sun Micro System

Project name: Green Project

Type: open source & free software

Initial Name: OAK language

Present Name: java

Extensions: .java & .class & .jar

Initial version: jdk 1.0 (java development kit)

Present version: Java 8 March 2015

Operating System: multi Operating System

Symbol: coffee cup with saucer

Objective: To develop web applications

SUN: Stanford Universally Network

Slogan/Motto: WORA(write once run anywhere)

Importance of Core Java


According to the SUN, 3 billion devices run on the Java language only.

1) Java is used to develop Desktop Applications such as MediaPlayer, Antivirus etc.

2) Java is Used to Develop Web Applications such as durgajobs.com, irctc.co.in etc.

3) Java is Used to Develop Enterprise Application such as Banking applications.

4) Java is Used to Develop Mobile Applications.

5) Java is Used to Develop Embedded System.

6) Java is Used to Develop SmartCards.

7) Java is Used to Develop Robotics.

8) Java is used to Develop Games etc.


Technologies Depends on Core Java






JAVA VERSIONS

------------------------------------------------------------------------------------------
Java Alpha & beta: 1995

JDK 1.0: 1996

JDK1.1: 1997

J2SE 1.2: 1998

J2SE 1.3: 2000

J2SE 1.4: 2002

J2SE 1.5: 2004

JAVA SE 6: 2006

JAVA SE 7: 2011

JAVA SE8: 2015


Read More »

Thursday, 11 May 2017

Java Program for Find The Salary of an Employee With Employee Grade

Problem:- Java Program For Find The Salary Of An Employee With Employee Grade (Performance). or Java Program For Find The Salary Of An Employee With Employee Grade or simple employee program in java or employee details program in java or employee salary calculation program in java or employee details program in java using inheritance or java program for employee details using array or java program for employee payroll or employee salary calculation program in java,

Note:- Please See This is A Requested Problem From Pakistan. See You Can Also Request Any Problem Regarding Programming. So Don't Forgot To Share Thanks, Learning Point (Chowk Azam, Pakistan) Writing For Us.

"write a code in java that input salary and grade of an employee and apply below conditions:

i) in case of grade 15 or above than bonus is 15%
ii) in case of grade 16 or above than bonus is 20%
iii) in case of grade 18 or above than bonus is 25%
iv) after calculating total salary deduct 13% GST in case that salary is 15000 or above. deduct 15% GST and in case that salary is 22000 or above.
v) Add 6% bonus at the end

Calculate net salary according to above condition and display it."




Logic:- So according to first three conditions in the problem I use if-else Statement For a grade of employee see below.

if(grade==15)
{
 bonus=(basic_salary*15)/100;
} 
else if(grade==16 || grade==17 )
{
 bonus=(basic_salary*20)/100;
}
else if(grade>=18)
{
 bonus=(basic_salary*25)/100;
}

After That, We Get a Basic Salary + Bonus. Now According to GST ( Goods and Service Tax ) Conditions Calculate a remaining Salary After All Calculation 6% Bonus.

if(basic_salary>=15000 && basic_salary<22000 basic_salary-="gst;" basic_salary="" bonus1="" else="" gst="(basic_salary*13)/100;" if="">=22000)
{
 gst=(basic_salary*15)/100;
 
 basic_salary-=gst;
 
 bonus1=(basic_salary*6)/100;

 basic_salary+=bonus1;

}
else
{
 bonus1=(basic_salary*6)/100;

 basic_salary+=bonus1;
}
At the end print the Salary.

Example:- Lets Assume Employee Salary is 20,000 And Grade is 15 Then Calculate an Employee Salary.

20,000 Salary and 15 Grade Bonus is 15% then Bonus = 3000.

Now Total Salary is 20,000+3000=23,000

Now Salary is Greater than 22,000 Then 15% GST, So GST = 3450.

Now Total Salary is 23,000 - 3450 = 19550.

At the End 6% Bonus Then Bonus = 1173

So Gross Salary Of Employee is =19500+1173=20723.

Gross Salary = 20723 .

Try This Java Program For Find The Gross Salary Of An Employee


Solution:- Java Program for Find The Salary of an Employee With Employee Grade


import java.util.Scanner;
import java.lang.*;

/* Program By Ghanendra Yadav
   Visit http://www.programmingwithbasics.com
*/

class employeesalary
{
public static void main(String args[]) 
{
 float basic_salary,bonus1=0,bonus=0,gst;
 int grade;
 
 Scanner scan=new Scanner(System.in);

 System.out.println("Enter Basic Salary Of Employee : ");
 basic_salary=scan.nextFloat();

 System.out.println("\nEnter The Grade Of Employee : ");    
 grade=scan.nextInt();
 
if(grade==15)
{
 bonus=(basic_salary*15)/100;
} 
else if(grade==16 || grade==17 )
{
 bonus=(basic_salary*20)/100;
}
else if(grade>=18)
{
 bonus=(basic_salary*25)/100;
}
 
 basic_salary+=bonus;
 
if(basic_salary>=15000 && basic_salary<22000 basic_salary-="gst;" basic_salary="" bonus1="" else="" gst="(basic_salary*13)/100;" if="">=22000)
{
 gst=(basic_salary*15)/100;
 
 basic_salary-=gst;

 bonus1=(basic_salary*6)/100;

 basic_salary+=bonus1;
}
else
{
 bonus1=(basic_salary*6)/100;

 basic_salary+=bonus1;
} 
    System.out.println("\nGross Salary Of Employee : "+basic_salary);
}
} 

Output:-
Read More »