From 8600a6813b76a50274cfe1e6366dc6755a94ca51 Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Thu, 12 May 2016 18:25:45 +0545 Subject: [PATCH 1/3] #3 mutliplier assignment done --- .../phpjava/assignmenttwo/MutiplierChecker.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/MutiplierChecker.java diff --git a/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/MutiplierChecker.java b/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/MutiplierChecker.java new file mode 100644 index 0000000..e1e17ef --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/MutiplierChecker.java @@ -0,0 +1,16 @@ +package com.lftechnology.phpjava.assignmenttwo; + +/** + * Created by naresh on 5/12/16. + */ +public class MutiplierChecker { + public static void main(String[] args) { + int sum = 0; + for (int i = 0; i < 1000; i++) { + if (i % 3 == 0 || i % 5 == 0) { + sum += i; + } + } + System.out.print(sum); + } +} From dd18773e0cbc534939fef469a11f85d788bddbb7 Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Mon, 16 May 2016 17:38:20 +0545 Subject: [PATCH 2/3] #2 #3 #4 #5 fibaonacci and menu option added --- .../assignmenttwo/MutiplierChecker.java | 50 +++++++++++++++++-- .../assignmenttwo/SalutationSplitter.java | 48 +++++++++++++----- 2 files changed, 81 insertions(+), 17 deletions(-) diff --git a/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/MutiplierChecker.java b/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/MutiplierChecker.java index e1e17ef..d45ca5a 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/MutiplierChecker.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/MutiplierChecker.java @@ -1,16 +1,56 @@ package com.lftechnology.phpjava.assignmenttwo; +import com.lftechnology.phpjava.menu.MenuReader; +import com.lftechnology.phpjava.menu.ProgramMenu; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + /** * Created by naresh on 5/12/16. */ -public class MutiplierChecker { - public static void main(String[] args) { +public class MutiplierChecker implements MenuReader { + + /** + * read the user input for the action + */ + @Override + public void scanner() { + Scanner scan = new Scanner(System.in); + System.out.println("Please enter the max value."); + String maxValue = scan.nextLine(); + + System.out.println("Please enter first number "); + String firstNumber = scan.nextLine(); + System.out.println("Please enter second number "); + String secondNumber = scan.nextLine(); + + int result = this.multiplier(Integer.valueOf(maxValue), Integer.valueOf(firstNumber), Integer.valueOf(secondNumber)); + System.out.println("Sum of numbers below " + maxValue + " which are multiple of " + firstNumber + " and " + secondNumber + " is = " + result); + + System.out.println("\n\n\nPlease press any key to return the previous menu or enter 0 to exit.\n"); + + String input = scan.nextLine(); + if (input.equals("0")) { + ProgramMenu.run(0); + } else { + ProgramMenu.showMenu(); + } + } + + /** + * @param maxValue + * @param firstNumber + * @param secondNumber + */ + public int multiplier(int maxValue, int firstNumber, int secondNumber) { int sum = 0; - for (int i = 0; i < 1000; i++) { - if (i % 3 == 0 || i % 5 == 0) { + for (int i = 0; i < maxValue; i++) { + if (i % firstNumber == 0 || i % secondNumber == 0) { sum += i; } } - System.out.print(sum); + return sum; } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/SalutationSplitter.java b/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/SalutationSplitter.java index 951261d..990ccee 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/SalutationSplitter.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/assignmenttwo/SalutationSplitter.java @@ -1,21 +1,45 @@ package com.lftechnology.phpjava.assignmenttwo; +import com.lftechnology.phpjava.menu.MenuReader; +import com.lftechnology.phpjava.menu.ProgramMenu; + +import java.util.Scanner; + /** * @author Naresh Maharjan */ -public class SalutationSplitter { - public static String name = ""; - public static String salutaion = ""; - - public static void main(String[] args) { - for (int i = 0; i < args.length; i++) { - if (i == 0) { - name = args[0]; - } else { - salutaion = salutaion + " " + args[i]; +public class SalutationSplitter implements MenuReader { + public void salutationSplitter(String input) { + String name = ""; + String[] nameAndSalutation = input.split(" "); + String salutaion = nameAndSalutation[0]; + String space = ""; + for (int i = 0; i < nameAndSalutation.length; i++) { + if (i > 0) { + name += space + nameAndSalutation[i]; + space = " "; } } - System.out.println(name); - System.out.println(salutaion); + System.out.println("\n\nSalutation : " + salutaion); + System.out.println("Name : " + name); + } + + + /** + * read the user input for the action + */ + @Override + public void scanner() { + Scanner scan = new Scanner(System.in); + System.out.println("Please enter the name and salutation."); + String nameAndSalutation = scan.nextLine(); + salutationSplitter(nameAndSalutation); + System.out.println("\n\n\nPlease press any key to return the previous menu or enter 0 to exit. \n"); + String input = scan.nextLine(); + if (input.equals("0")) { + ProgramMenu.run(0); + } else { + ProgramMenu.showMenu(); + } } } From fad0dc6cb44ff4b21bef5f92e48582b70213a3c3 Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Mon, 16 May 2016 17:39:30 +0545 Subject: [PATCH 3/3] #2 #3 #4 #5 fibaonacci and menu option added --- .../assignmentthree/EvenFibonacci.java | 50 ++++++++++++ .../assignmentthree/LargestPalindrome.java | 78 +++++++++++++++++++ .../lftechnology/phpjava/menu/MenuReader.java | 12 +++ .../phpjava/menu/ProgramMenu.java | 71 +++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/assignmentthree/EvenFibonacci.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/assignmentthree/LargestPalindrome.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/menu/MenuReader.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/menu/ProgramMenu.java diff --git a/naresh/src/main/java/com/lftechnology/phpjava/assignmentthree/EvenFibonacci.java b/naresh/src/main/java/com/lftechnology/phpjava/assignmentthree/EvenFibonacci.java new file mode 100644 index 0000000..096bd16 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/assignmentthree/EvenFibonacci.java @@ -0,0 +1,50 @@ +package com.lftechnology.phpjava.assignmentthree; + +import com.lftechnology.phpjava.menu.MenuReader; +import com.lftechnology.phpjava.menu.ProgramMenu; + +import java.util.Scanner; + +/** + * Created by naresh on 5/16/16. + */ +public class EvenFibonacci implements MenuReader { + /** + * read the user input for the action + */ + @Override + public void scanner() { + + Scanner scan = new Scanner(System.in); + System.out.println("Please enter the max value."); + String maxValue = scan.nextLine(); + int max = Integer.valueOf(maxValue); + int counter = 0; + int sum = 0; + int a, b, c; + a = 1; + b = 2; + + while (counter < max) { + c = a + b; + a = b; + b = c; + counter = b; + if (counter > max) { + break; + } + if (counter % 2 == 0) { + sum = sum + counter; + } + } + System.out.println("Sum of fibonacci numbers is :" + sum); + System.out.println("\n\n\nPlease press any key to return the previous menu or enter 0 to exit.\n"); + + String input = scan.nextLine(); + if (input.equals("0")) { + ProgramMenu.run(0); + } else { + ProgramMenu.showMenu(); + } + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/assignmentthree/LargestPalindrome.java b/naresh/src/main/java/com/lftechnology/phpjava/assignmentthree/LargestPalindrome.java new file mode 100644 index 0000000..55a65ac --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/assignmentthree/LargestPalindrome.java @@ -0,0 +1,78 @@ +package com.lftechnology.phpjava.assignmentthree; + + +import com.lftechnology.phpjava.menu.MenuReader; +import com.lftechnology.phpjava.menu.ProgramMenu; + +import java.util.Scanner; + +/** + * @author Naresh Maharjan + */ +public class LargestPalindrome implements MenuReader { + + /** + * find the largest palindrome number + * + * @param startIndex + * @param endIndex + * @return + * @author Naresh Maharjan + */ + public long findLargestPalindrome(int startIndex, int endIndex) { + long palindrome = 0; + int max = Math.max(startIndex, endIndex); + int min = Math.min(startIndex, endIndex); + for (int i = max; i >= min; i--) { + for (int j = max; j >= min; j--) { + long product = i * j; + if (palindrome < product && isPalindrome(product)) { + palindrome = product; + } + } + } + return palindrome; + } + + /** + * check if the number is palindrome or not + * + * @param nr + * @return + * @author Naresh Maharjan + */ + public boolean isPalindrome(long nr) { + long rev = 0; // the reversed number + long x = nr; // store the default value (it will be changed) + while (x > 0) { + rev = 10 * rev + x % 10; + x /= 10; + } + return nr == rev; // returns true if the number is palindrome + } + + /** + * read the user input for the action + */ + @Override + public void scanner() { + System.out.println("Please enter two numbers to find out the largest palindrome possible from the its product."); + Scanner scan = new Scanner(System.in); + System.out.println("Please enter first number "); + String firstNumber = scan.nextLine(); + System.out.println("Please enter second number "); + String secondNumber = scan.nextLine(); + + long palindrome = this.findLargestPalindrome(Integer.valueOf(firstNumber), Integer.valueOf(secondNumber)); + System.out.println("Largest possible palindrome from the product of " + firstNumber + " and " + secondNumber + " is = " + palindrome); + + System.out.println("\n\n\nPlease press any key to return the previous menu or enter 0 to exit.\n"); + + String input = scan.nextLine(); + if (input.equals("0")) { + ProgramMenu.run(0); + } else { + ProgramMenu.showMenu(); + } + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/menu/MenuReader.java b/naresh/src/main/java/com/lftechnology/phpjava/menu/MenuReader.java new file mode 100644 index 0000000..1b5b8f0 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/menu/MenuReader.java @@ -0,0 +1,12 @@ +package com.lftechnology.phpjava.menu; + +/** + * Created by naresh on 5/16/16. + */ +public interface MenuReader { + + /** + * read the user input for the action + */ + void scanner(); +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/menu/ProgramMenu.java b/naresh/src/main/java/com/lftechnology/phpjava/menu/ProgramMenu.java new file mode 100644 index 0000000..fa363c3 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/menu/ProgramMenu.java @@ -0,0 +1,71 @@ +package com.lftechnology.phpjava.menu; + +import com.lftechnology.phpjava.assignmentthree.EvenFibonacci; +import com.lftechnology.phpjava.assignmentthree.LargestPalindrome; +import com.lftechnology.phpjava.assignmenttwo.MutiplierChecker; +import com.lftechnology.phpjava.assignmenttwo.SalutationSplitter; + +import java.util.Scanner; + +/** + * Created by naresh on 5/16/16. + */ +public class ProgramMenu { + public static String[] menu = { + "1. Split Salutation", + "2. Check Multiple", + "3. Find Largest Palindrome", + "4. Even Fibonacci", + "0. To Exit", + }; + + public static void main(String[] args) { + showMenu(); + } + + public static void showMenu() { + System.out.println("Please enter the option number"); + Scanner scan = new Scanner(System.in); + for (String option : menu) { + System.out.println(option); + } + int index = scan.nextInt(); + run(index); + } + + public static void run(int index) { + switch (index) { + case 1: + SalutationSplitter salutationSplitter = new SalutationSplitter(); + salutationSplitter.scanner(); + break; + case 2: + MutiplierChecker mutiplierChecker = new MutiplierChecker(); + mutiplierChecker.scanner(); + break; + case 3: + LargestPalindrome largestPalindrome = new LargestPalindrome(); + largestPalindrome.scanner(); + break; + case 4: + EvenFibonacci evenFibonacci = new EvenFibonacci(); + evenFibonacci.scanner(); + break; + case 0: + System.out.println("Tata Horn OK please. :)"); + System.exit(0); + default: + System.out.println("Invalid option. Please enter the number mentioned in the menu."); + Scanner scan = new Scanner(System.in); + System.out.println("Please press any key to return the previous menu or enter 0 to exit"); + String input = scan.nextLine(); + if (input.equals("0")) { + System.exit(0); + } else { + showMenu(); + } + break; + + } + } +}