Sunday, 19 November 2017

Practice Programs on String in java

Given below are all the possible programs that will help you get a good grasp on String concept in java. (Update in Progress)

Note: Follow all the business rules where applicable.

  1. Write a method that takes a String and calculates the total number of characters in the String. Business Rules :- 
    Rule 1 : If any special characters is present in the string , print -1. Rule 2 : String should contain only alphanumeric values and white spaces.
  2. Write a method which takes a string as input and returns true if its a valid IP address and false if not a valid IP.
    Business Rules :-                                                                                                                          Rule 1 : IP address should be like 10.10.23.56
    Rule 2 : All the digit must be between 0 to 255 except for the first digit which is 1 to 255.
    Rule 3 : Total 4 numbers would be there separated by only dots.
    Rule 4 : All the fields must be either dot or digits(no white space or symbols or alphabets allowed).
  3. Write a method that accepts a string and returns the total number of vowels in that string.
  4. Write a method that takes a string array and returns the count of palindromes in the string array.
  5. Write a method that will accept a string and will return the string after removing all characters other than alphabets.
  6. Write a method which accepts a string(ex. This is a pen and is used to write) and returns a list<int> where the index of all occurrences of "is" is stored. 
  7. Write a method that will take two strings and will return an integer which is the frequency of first string in second string .
    Input1:- is
    Input2:-He is a good boy,but his brother is not.
    Output:- 2
  8. Write a method that will take a string and will remove duplicate characters keeping only the first occurrence.
    Input:- Kolkataaaaa   Output:- Kolat
  9. Write a method that will take a string and will return an int array that contains the length of the largest and smallest words.
  10. Write a method that will take a string which will be the password for a very secured account.
    Business Rules:-The string should be maximum 10 characters long,minimum 8 characters long, should have a minimum 1 special character, 1 uppercase character , 1 lowercase character and 1 digit. The string should not start with digit or special symbols. No space is allowed. If all conditions satisfy return true and otherwise false.

Thursday, 2 November 2017

Can we keep more than one class in a single java source file?

We can have more than one class in a single java source file but with certain restriction.

Among multiple classes that you create in a single source file, only one can be public. Find an example given below,


package basic;

public class MultiplePublic {

      public static void main(String[] args) {
            //By java radar
            System.out.println("Executing main method");
            AnotherMultiplePublic amp = new AnotherMultiplePublic();
            amp.sampleMethod();
      }

}

class AnotherMultiplePublic {

      public void sampleMethod(){
            System.out.println("Print sample method");
      }

}

OUTPUT:
Executing main method

Print sample method


As shown above, write both classes in same source file with MultiplePublic class being public. As stated above if only one class is declared public then the code will run fine. So in this case the code will be executed without any glitches and produce output as stated under output header.

 Second Scenario:

Now, we will try to assign public access modifier to more than one class. Say two classes will have public modifier.

In this scenario, look at the below given screen-shot, to find out the result:

java radar examples
Multiple public class in same source file


See the red under line below AnotherMultiplePublic class in the screenshot above. Place the mouse cursor above it and you will see the error message:

                'The public type AnotherMultiplePublic must be defined in its own file'


Conclusion: That means in the same source file one cannot have more than one public class. 
Though there is no problem if you want to keep more than one class in same source file and you restrict the application of public modifier to only one class as we did in our first scenario.


Note for Readers: Please share your valuable thoughts on this post's question to help others.


You may also like to read:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.