Sunday, September 26, 2021

Week one of practice questions

 Question 1 : Write a Java method to count all vowels in a string.

import java.util.Scanner;

public class Practice_Day_1 {

    static int countVowels(String str){
        int count = 0;
        for (int i = 0i < str.length(); i++) {
            if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                    || str.charAt(i) == 'o' || str.charAt(i) == 'u' ||
                     str.charAt(i) == 'A' ||
                     str.charAt(i) == 'E' || str.charAt(i) == 'I'
                    || str.charAt(i) == 'O' || str.charAt(i) == 'U') {
                count = count + 1;
            }
        }
        return count;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a String");
        String str = sc.nextLine();
        System.out.println(countVowels(str));
    }
}

Saturday, September 25, 2021

Day 1 practice questions.{Methods(static method) Questions With Solutions}

Question 1 : Write a Java method to find the smallest number among three numbers.

import java.util.Scanner;

public class Practice_Day_1 {
    static float smallestNumber(float a , float b , float c){
        float d;
        if (a<b || a<c) {
            d = a;
        }
        else if (b<a || b<c) {
           d = b;
        }
        else{
            d = c;
        }
        return d;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first number");
        float firstNumber = sc.nextFloat();
        System.out.println("Enter the second number");
        float secondNumber = sc.nextFloat();
        System.out.println("Enter the third number");
        float thirdNumber = sc.nextFloat();
        float smallNumber = smallestNumber(firstNumber , secondNumber , thirdNumber);
        System.out.println(smallNumber);
    }
}

Output:
Enter the first number 25 Enter the second number 37 Enter the third number 29 25.0

Question 2 : Write a Java method to compute the average of three numbers.
import java.util.Scanner;

public class Practice_Day_1 {

    static float average(float a , float b , float c){
        float d = (a + b + c) / 3;
        return d;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first number");
        float firstNumber = sc.nextFloat();
        System.out.println("Enter the second number");
        float secondNumber = sc.nextFloat();
        System.out.println("Enter the third number");
        float thirdNumber = sc.nextFloat();
        float avg = average(firstNumber , secondNumber , thirdNumber);
        System.out.println(avg);
    }
}

Output:
Enter the first number 25 Enter the second number 45 Enter the third number 65 45.0

Question 3 : Write a Java method to display the middle character of a string. 
Note: a) If the length of the string is odd there will be two middle characters.
b) If the length of the string is even there will be one middle character.

import java.util.Scanner;

public class Practice_Day_1 {
   static void middleCharacter(String a){
        int position;
        int length;
        int b = a.length();
        if (b % 2 == 0) {
            position = b / 2 - 1;
            length = 2;
            System.out.println(a.substring(position , position + length));
        }
        else{
            position = b / 2;
            length = 1;
            System.out.println(a.substring(position , position + length));
        }
    }
    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter a String : ");
            String a = sc.nextLine();
            middleCharacter(a);
    }
}

Output 1:(Odd number of characters in the string)
Enter a String : Like The Post h

Output 2:(Even number of characters in the string)
Enter a String : void oi

Friday, July 30, 2021

14 . Write a program to print numbers from 1 to 10.

 for (int i = 1; i <= 10; i++) {

                System.out.println(i);
        }

Thursday, July 15, 2021

13. Write a java program to replace spaces with underscores.

public class Main {

      public static void main(String[] args) {

          String name = "Berry is my brother and he is a nice brother";

          System.out.println(name.replace(" ", "_"));

     }

}


NOTE:

The output of this program is Berry_is_my_brother_and_he_is_a_nice_brother. By using .replace(); all the spaces in the String will be changed to underscores in the output. 

12. Write a java program to convert String "BerRY" to lowercase.

 public class Main {

      public static void main(String[] args) {

          String name = "BerRY";

          System.out.println(name.toLowerCase());

     }

}


NOTE:

The  output of the above program is berry . Didn't import Scanner as no input was to be taken from the user. String method toLowerCase(); is used to change all upper case letters to lower case letters.

11. Write a program to print a string entered by user.

 import java.util.Scanner;


public class Main {

      public static void main(String[] args) {

          Scanner sc = new Scanner(System.in);

          System.out.println("Enter a String. ");

          String st = sc.nextLine();

          System.out.println(st);

     }

}


NOTE:

Imported Scanner as input was to be taken from the user. Then asked the user the String . The user can input any kind of String he/she likes. Then at last the String given by the user will be Shown in the Screen.

Wednesday, July 14, 2021

10. Write a program to check if the two numbers 23 and 45 are equal.

 public class Main {

      public static void main(String[] args) {

          System.out.println(23 == 45);

     }

}


NOTE:

  • The output of this program will come false as we used boolean operator(==).
  • Didn't use Scanner as no input from the user was needed to be taken.
  • We can also do this program by storing 23 and 45 in int , float , etc.