Automation QA Testing Course Content

Method Excercises

1)Insert the missing part to call myMethod from main

static void myMethod(){
System.out.println("I just got executed!");
}
public static void main(String[] args){

}


2)Insert the missing part to call myMethod from main two times.
static void myMethod(){
System.out.println("I just got executed!");
}
public static void main(String[] args){


}

3)Add a fname parameter of type String to myMethod, and output "John Doe". & call myMethod from main()?

public static void myMethod(-- --){
System.out.println(--+" Doe ");

public static void main(String[] args){

}

4)Insert the missing part to print the number 8 in main, by using a specific keyword inside myMethod:

static int myMethod(int x){

 ------5+x;
}
public static void main(String[] args){

}

------------------------------------------------------------------------------------------------------------------------5)Write a program with a method named getTotal that accepts two integers as an argument and return its sum. Call this method from main( ) and print the results.

import java.util.Scanner; public class SumTwoNumber { public static void main(String[] args) { Scanner console = new Scanner(System.in); int num1, num2; System.out.print("Enter first number: "); num1 = console.nextInt(); System.out.print("Enter second number: "); num2 = console.nextInt(); int sum = getTotal(num1, num2); System.out.println("Sum: " + sum); } public static int getTotal(int number1, int number2) { return number1 + number2; } }
-------------------------------------------------------------------------------------
6)Write a method named isEven that accepts an int argument. The method should return true if the argument is even, or false otherwise. Also write a program to test your method

Solution:
import java.util.Scanner; public class TestEven { public static void main(String[] args) { Scanner console = new Scanner(System.in); int num; System.out.print("Enter an integer: "); num = console.nextInt(); if(isEven(num)) { System.out.println("Number is even"); } else { System.out.println("Number is odd"); } } public static boolean isEven(int number) { if(number % 2 == 0) { return true; } else { return false; } } }
-----------------------------------------------------------------------------------
7)
Write a value-returning method, isVowel that returns the value true if a given character is a vowel, and otherwise returns false. In main() method accept a string from user and count number of vowels in that string.

Solution:
import java.util.Scanner; public class TestEven { public static void main(String[] args) { Scanner console = new Scanner(System.in); String sentence; System.out.print("Enter a string: "); sentence = console.nextLine(); int count = 0; for(int i = 0; i < sentence.length(); i++) { if(isVowel(sentence.charAt(i))) { count++; } } System.out.println("Number of vowels: " + count); } public static boolean isVowel(char letter) { switch(letter) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': return true; default: return false; } } }
======================================================================================
8)A prime number is a number that is evenly divisible only by itself and 1. For example, the number 5 is prime because it can be evenly divided only by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 4, and 6.
Write a method named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Also write main method that displays prime numbers between 1 to 500.

Solution:
public class PrimeNumbers { public static void main(String[] args) { for(int i = 1; i <= 500; i++) { if(isPrime(i)) { System.out.print(i + " "); } } } public static boolean isPrime(int number) { for(int i = 2; i < number; i++) { if(number % i == 0) { return false; } } return true; } }
===================================================================================

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.