![]() |
| Nitin P Wadakar |
Completed M.Tech in IT from VIT, Vellore and he is a fresher from Bengaluru. Helped in contributing these programs.
----------------------------------------------------------------------------------------------------------------
CommonUtils class is an utility class and any class can reuse methods from it.
1. Read input from keyboard and used as a common class.
package com.thebeautifuljava.utils;
import java.util.Scanner;
public class CommonUtils {
public static int readFromKeyBoard() {
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
}
----------------------------------------------------------------------------------------------------------------Two different ways of writing logic to find biggest of 2 numbers.
package com.thebeautifuljava.simpleprograms;
public class BiggestOfTwoNumbers {
public static void main(String[] args) {
System.out.println("BIGGEST OF 2 NUMBERS IS:" + findBiggest(100, 65));
System.out.println("BIGGEST OF 2 NUMBERS IS (Ternary):"
+ findBiggestTernary(100, 65));
}
// method 1
private static int findBiggest(int first, int second) {
if (first > second)
return first;
return second;
}
// method 2
private static int findBiggestTernary(int first, int second) {
return (first > second) ? first : second;
}
}
----------------------------------------------------------------------------------------------------------------Best way to re-use CommonUtils class.
3. Biggest of 2 numbers by inputting through keyboard.
package com.thebeautifuljava.simpleprograms; import com.thebeautifuljava.utils.CommonUtils;
public class BiggestOfTwoNumbersRead {
public static void main(String[] args) {
System.out.println("ENTER FIRST NUMBER :");
int first = CommonUtils.readFromKeyBoard();
System.out.println("ENTER SECOND NUMBER :");
int second = CommonUtils.readFromKeyBoard();
System.out.println("BIGGEST OF 2 NUMBERS IS:" + findBiggest(first, second));
}
private static int findBiggest(int first, int second) {
if (first > second)
return first;
return second;
}
}
----------------------------------------------------------------------------------------------------------------Best way to re-use CommonUtils class.
4. Calculate factorial of a given number.
package com.thebeautifuljava.simpleprograms;
import com.thebeautifuljava.utils.CommonUtils;
public class Factorial {
public static void main(String[] args) {
System.out.println("ENTER NUMBER :");
int number = CommonUtils.readFromKeyBoard();
System.out.println("Factorial :" + getFactorial(number));
}
private static long getFactorial(int number) {
// TODO Auto-generated method stub
int factorial = 1;
for (int i = 1; i <= number; i++) {
factorial = factorial * i;
}
return factorial;
}
}
----------------------------------------------------------------------------------------------------------------
5. Print fibonacci series.
package com.thebeautifuljava.simpleprograms;
import com.thebeautifuljava.utils.CommonUtils;
public class FibonacciSeries {
public static void main(String args[]) {
System.out.println("ENTER LIMIT :");
int limit = CommonUtils.readFromKeyBoard();
printNumbers(limit);
}
private static void printNumbers(int limit) {
int first = 0, second = 1;
// prints 0 and 1
System.out.println("FIBONACCI SERIES:");
System.out.print(first + " " + second);
for (int i = 2; i < limit; i++) {
int next = first + second;
System.out.print(" " + next);
first = second;
second = next;
}
}
}
----------------------------------------------------------------------------------------------------------------
6. Check whether number is prime.
package com.thebeautifuljava.simpleprograms;
import com.thebeautifuljava.utils.CommonUtils;
public class PrimeNumberCheck {
public static void main(String args[]) {
System.out.println("PLEASE ENTER A NUMBER :");
int number = CommonUtils.readFromKeyBoard();
// validation check for negative numbers and zero
if (number <= 0) {
throw new IllegalArgumentException("PLEASE ENTER A POSITIVE NUMBER");
}
if (number == 1) {
throw new IllegalArgumentException("NUMBER IS NOT PRIME");
}
System.out.println("IS PRIME NUMBER :" + isPrime(number));
}
private static boolean isPrime(int number) {
// If number is 50, then sqrt(50) will be 7.071...
int sqrt = (int) Math.sqrt(number) + 1;
for (int i = 2; i < sqrt; i++) {
// check for divisibility of a number
if (number % i == 0) {
return false;
}
}
return true;
}
}
----------------------------------------------------------------------------------------------------------------


No comments:
Post a Comment