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;
}
}
Question 2
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.
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;
}
}
}
Question 3
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.
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;
}
}
}
Question 4
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.
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;
}
}
Question 5
A nonnegative integer is called a palindrome if it reads forward and backward in the same way. For example, the numbers 5, 121, 3443, and 123454321 are palindromes. Write a method that takes as input a nonnegative integer and returns true if the number is a palindrome; otherwise, it returns false. Also write a program to test your method.
import java.util.Scanner;
public class TestPalindrom
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int num;
System.out.print("Enter a number: ");
num = console.nextInt();
if(isPalindrom(num))
{
System.out.println("Number is palindrom");
}
else
{
System.out.println("Number is not palindrom");
}
}
public static boolean isPalindrom(int number)
{
int temp = number;
int remainder;
int reverse = 0;
while(temp > 0)
{
remainder = temp % 10;
temp = temp / 10;
reverse = reverse * 10 + remainder;
}
return reverse == number;
}
}