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.

9. Write a program to add 8 to the number 2345 and then divide it by 3. Now, the modulus of the quotient is taken with 5 and then multiply the resultant value by 5. Display the final result.

 public class Main {

      public static void main(String[] args) {

          int a = 2345;

          int b = a + 8;

          int c = b / 3;

          int d = c % 5;

          int e = d * 5;

          System.out.println(e);

     }

}


NOTE:

  • The output of this program will come 20.
  • Used int as there is no decimal in the value.
  • Didn't use Scanner as there was no need to take input from the user.

8. Write a program to calculate the perimeter of a triangle having sides of length 2,3 and 5 units.

 public class Main {

      public static void main(String[] args) {

          int a = 2;

          int b 3;

          int c = 5;

          int perimeterOfATriangle = a + b + c;

          System.out.println("The perimeter of the triangle is : " + perimeterOfATriangle);

     }

}


NOTE:

  • Didn't need Scanner as it was not needed to take input from the user.
  • Used int as there was no decimal in the value.
  • We all know that perimeter of a triangle is calculated by adding all three sides which                                 I supposed a , b and c in above program.

7. Length and breadth of a rectangle are 5 and 7 respectively. Write a program to calculate the area and perimeter of the rectangle.

 public class Main {

      public static void main(String[] args) {

          int length = 5;

          int breadth = 7;

          int area = length * breadth;

          int perimeter = 2 * (length + breadth);

          System.out.println("The area is : " + area);

          System.out.println("The perimeter is : " + perimeter);

     }

}


NOTE:

  • Didn't need Scanner because there was no need to take input from the user.
  • Used int because there was no decimal in input.
  • To calculate area and perimeter we need length and breadth which is provided to us as input. 
  • Used int to calculate as we didn't need decimal point in our output for now.

Tuesday, July 13, 2021

6. Write a program to assign a value of 100.235 to a double variable and then convert it to int.

 public class Main {

      public static void main(String[] args) {

          double a = 100.235;

          System.out.println((int)a);

     }

}


NOTE:

  • Didn't use Scanner as there is no input to ask from the user.
  • Used double as we needed to change double.
  • Used System.out.println((int)a); to change double to int.
  • Double and int are different because Double can contain decimal value but int can not.                       For example : double is 100.235 then int will be 100 as it cant contain decimal values.

5. Print the ASCII value of the character 'h'.

 public class Main {

      public static void main(String[] args) {

          System.out.println((int)'h');

     }

}


NOTE:

  • ASCII  vlaue  is a character encoding scheme used for electronics communication.
  • Used System.out.println((int)'h'); to print the ASCII vlaue of h which is 104.
  • Didn't need Screen as  there was no input to ask from the user.

4. Write a program to print the product of the numbers 8.2 and 6.

 public class Main {

      public static void main(String[] args) {

          float product = 8.2f * 6.0f;

          System.out.println("The product of 8.2 and 6 is : " + product);

     }

}


NOTE:

  • Didn't need Scanner because the input from the user was already given.
  • Used float because of decimal number in the input.
  • Necessary to use f in last of the number in float.
  • In System.out.println("The product of 8.2 and 6 is : " + product); used + product to                      print value of the product.

3. Write a program to print the area of a rectangle of sides 2 and 3 units respectively.

 public class Main {

      public static void main(String[] args) {

          int area = 2 * 3;

          System.out.println("The area is : " + area);

     }

}


NOTE:

  • Used multiplication(*) operator to multiply.
  • Didn't need Scanner class as input was already given.
  • Used int because there was no decimal in the input.

2. Write a Java program to print an int, a double and a char on screen.

public class Main {
      public static void main(String[] args) {
          System.out.println("Int");
          System.out.println("Double");
          System.out.println("Char");
     }
}

NOTE:
  • Every input is given in the question so we do not need to use Scanner class.
  • Used System.out.println(""); to print on the screen .
  • In java semi-colon (;) is very important.
  • Semi-colon makes the compiler know where a line of code is ending.

Monday, July 12, 2021

1 . Write a program to calculate percentage of a given student in CBSE board exam . His marks from 5 subjects must be taken as input from the keyboard. (Marks out of 100)

 import java.util.Scanner;


public class Main {

      public static void main(String[] args) {

Scanner S = new Scanner(System.in);

System.out.println("Enter your marks in English out of 100 : ");

float a = S.nextFloat();

System.out.println("Enter your marks in Science out of 100 : ");

float b = S.nextFloat();

System.out.println("Enter your marks in History out of 100 : ");

float c = S.nextFloat();

System.out.println("Enter your marks in Computer out of 100 : ");

float d = S.nextFloat();

System.out.println("Enter your marks in Hindi out of 100 : ");

float e = S.nextFloat();

float sum = a + b + c + d + e;

float avg = sum / 5;

System.out.println("The percentage is : " + avg);

     }

}


NOTE :

  • Needed Scanner class to take input from the user.
  • Used float because user can input marks with decimal number.
  • Took sum to calculate average.
  • Calculated average because (a + b + c + d + e)/500 * 100 since 500 and 100 can be                             divided the average marks is the percentage of the user. 


5 easy java exersizes with solutions.

 11. Write a Java program to print the area and perimeter of a circle.

Test Data:
Radius = 7.5

-->

public class Main {

      public static void main(String[] args) {

          float radius = 7.5f;

          int pie = 22/7;

          float area = pie * radius * radius;

          float perimeter = 2 * pie * radius;

          System.out.println("The area of the circle is = " + area);

          System.out.println("The perimeter of the circle is = " + perimeter);

     }

}


12. Write a Java program that takes three numbers as input to calculate and print the average of the numbers.

-->

import java.util.*;


public class Main {

      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 average = (firstNumber + secondNumber + thirdNumber) / 3;

          System.out.println("The average is : " + average);

     }

}


13. Write a Java program to print the area and perimeter of a rectangle.

Test Data:
Width = 5.5 Height = 8.5

-->

import java.util.*;


public class Main {

      public static void main(String[] args) {

          Scanner sc = new Scanner(System.in);

          System.out.println("Enter the length of the rectangle");

          float length = sc.nextFloat();

          System.out.println("Enter the breadth of the rectangle");

          float breadth = sc.nextFloat();

          float area = length * breadth;

          float perimeter = 2 * ( length + breadth);

          System.out.println("The area of the rectrangle is : " + area);

          System.out.println("The perimeter of the rectrangle is : " + perimeter);

     }

}


14. Write a Java program to print an American flag on the screen. 

-->

public class Main {

      public static void main(String[] args) {

          System.out.println("* * * * * * ==================================");

          System.out.println(" * * * * *  ==================================");

          System.out.println("* * * * * * ==================================");

          System.out.println(" * * * * *  ==================================");

          System.out.println("* * * * * * ==================================");

          System.out.println(" * * * * *  ==================================");

          System.out.println("* * * * * * ==================================");

          System.out.println(" * * * * *  ==================================");

          System.out.println("* * * * * * ==================================");

          System.out.println("=========================================");

          System.out.println("=========================================");

          System.out.println("=========================================");

          System.out.println("=========================================");

          System.out.println("=========================================");

          System.out.println("=========================================");

     }

}


15. Write a Java program to swap two variables. 

-->

public class Main {

      public static void main(String[] args) {

          int a , b , c;

          a = 12;

          b = 21;

          System.out.println("variables before swapping are a = "+ a + " and b = " + b);

          c = a;

          a = b;

          b = c;

          System.out.println("variables after swapping are a = "+ a + " and b = " + b);

     }

}


NOTE: 

  • I use float because the user can input decimal value instead of integer value.
  • This is a beginner friendly so i didn't use loops.
  • We can calculate area , perimeter , etc using java.

ANNOUNCEMENT:I'll be posting more five questions and solutions tomorrow so stay tuned. If you have any suggestions tell me it'll help me improve.

How to make a rock , paper , scissors game in java. (Beginners edition).

 import java.util.*;

public class Main {

      public static void main(String[] args) {

          System.out.println("0 = rock");

          System.out.println("1 = paper");

          System.out.println("2 = scissors");

          Scanner sc = new Scanner(System.in);

          System.out.println("Enter a number from 0 to 2");

           int num = sc.nextInt();

   Random ra = new Random();

   int Number=ra.nextInt(3);

       for(int counter 1;  counter <= 1; counter++){

     }

if(Number <= 0 && num <= 0){

  System.out.println("draw");

}

else if(Number <= 0 && num <= 1){

  System.out.println("win");

}

else if(Number <= 0 && num <= 2){

  System.out.println("loose");

}

else if(Number <= 1 && num <= 0){

  System.out.println("loose");

}

        else if(Number <= 1 && num <= 1){

  System.out.println("draw");

}

else if(Number <= 1 && num <= 2){

  System.out.println("win");

}

else if(Number <= 2 && num <= 0){

  System.out.println("win");

}

else if(Number <= 2 && num <= 1){

  System.out.println("loose");

}

else{

  System.out.println("draw");

}

     }

}


NOTE: 

  • Else- if takes boolean operators.i.e.<= , >= , etc.
  • Else- if prints the output if the statement is true.
  • Imported random using import java.util.random;
  • Random picks a random number (any number from 0 to infinite) but we made                              it so that it picks number from 0 to 2 by keeping the limit value in its operator.
  • for eg: int Number=ra.nextInt(3);
  • I used int because i didn't want decimal point in any numbers .


ANNOUNCEMENT: I'll be posting more five questions and solutions tomorrow so stay tuned. If you have any suggestions tell me it'll help me improve.

5 easy java exercises with solution.

 6. Write a Java program to print the sum (addition), multiply, subtract, divide and remainder of two numbers.

-->

import java.util.Scanner;


public class Main {

      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();

          float sum = firstNumber + secondNumber;

          float multiply = firstNumber * secondNumber;

          float substract = firstNumber - secondNumber;

          float divide = firstNumber / secondNumber;

          float remainder = firstNumber % secondNumber;

      `   System.out.println(sum);

          System.out.println(multiply);

          System.out.println(substract);

          System.out.println(divide);

          System.out.println(remainder);

     }

}


7. Write a Java program that takes a number as input and prints its multiplication table upto 10.

-->

import java.util.Scanner;


public class Main {

      public static void main(String[] args) {

          Scanner sc = new Scanner(System.in);

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

          int number = sc.nextInt();

          System.out.print(number + " *  1 = ");

          System.out.println(number * 1);

          System.out.print(number + " *  2 = ");

          System.out.println(number * 2);

          System.out.print(number + " *  3 = ");

          System.out.println(number * 3);

          System.out.print(number + " *  4 = ");

          System.out.println(number * 4);

          System.out.print(number + " *  5 = ");

          System.out.println(number * 5);

          System.out.print(number + " *  6 = ");

          System.out.println(number * 6);

          System.out.print(number + " *  7 = ");

          System.out.println(number * 7);

          System.out.print(number + " *  8 = ");

          System.out.println(number * 8);

          System.out.print(number + " *  9 = ");

          System.out.println(number * 9);

          System.out.print(number + " * 10 = ");

          System.out.println(number * 10);

     }

}


8. Write a Java program to display the following pattern.

Sample Pattern :

   J    a   v     v  a                                                  
   J   a a   v   v  a a                                                 
J  J  aaaaa   V V  aaaaa                                                
 JJ  a     a   V  a     a
-->
public class Main {
  public static void main(String[] args) {
  System.out.println("   J    a   v     v  a    ");
  System.out.println("   J   a a   v   v  a a   ");
  System.out.println("J  J  aaaaa   V V  aaaaa  ");
  System.out.println(" JJ  a     a   V  a     a ");
 }
}

9. Write a Java program to compute the specified expressions and print the output.
Test Data:
((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5))
-->
public class Main {
      public static void main(String[] args) {
          System.out.println(((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5)));
     }
}

10. Write a Java program to compute a specified formula.
4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))
-->
public class Main {
      public static void main(String[] args) {
          System.out.println(4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11)));
     }
}

NOTE:
  1. Java is an OOP(object oriented program). 
  2. System.out.println(); is used to print a word , number or a text

                     in the screen.

  3.We can calculate mathematical operations in System.out.println();

                  by not using " ".

  

ANNOUNCEMENT:  I'll be posting more five questions and solutions tomorrow so stay tuned. If you have any suggestions tell me it'll help me improve.