-
Notifications
You must be signed in to change notification settings - Fork 0
Naresh 3 #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Naresh 3 #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| int counter = 0; | ||
| int sum = 0; | ||
| int a, b, c; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| System.out.println("\n\n\nPlease press any key to return the previous menu or enter 0 to exit.\n"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| String input = scan.nextLine(); | ||
| if (input.equals("0")) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ProgramMenu.run(0); | ||
| } else { | ||
| ProgramMenu.showMenu(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <nareshmaharjan@lftechnology.com> | ||
| */ | ||
| public class LargestPalindrome implements MenuReader { | ||
|
|
||
| /** | ||
| * find the largest palindrome number | ||
| * | ||
| * @param startIndex | ||
| * @param endIndex | ||
| * @return | ||
| * @author Naresh Maharjan <nareshmaharjan@lftechnology.com> | ||
| */ | ||
| 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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if (palindrome < product && isPalindrome(product)) { | ||
| palindrome = product; | ||
| } | ||
| } | ||
| } | ||
| return palindrome; | ||
| } | ||
|
|
||
| /** | ||
| * check if the number is palindrome or not | ||
| * | ||
| * @param nr | ||
| * @return | ||
| * @author Naresh Maharjan <nareshmaharjan@lftechnology.com> | ||
| */ | ||
| 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."); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| Scanner scan = new Scanner(System.in); | ||
| System.out.println("Please enter first number "); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| String firstNumber = scan.nextLine(); | ||
| System.out.println("Please enter second number "); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| System.out.println("\n\n\nPlease press any key to return the previous menu or enter 0 to exit.\n"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| String input = scan.nextLine(); | ||
| if (input.equals("0")) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ProgramMenu.run(0); | ||
| } else { | ||
| ProgramMenu.showMenu(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package com.lftechnology.phpjava.assignmenttwo; | ||
|
|
||
| import com.lftechnology.phpjava.menu.MenuReader; | ||
| import com.lftechnology.phpjava.menu.ProgramMenu; | ||
|
|
||
| import java.util.ArrayList; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| import java.util.List; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| import java.util.Scanner; | ||
|
|
||
| /** | ||
| * Created by naresh on 5/12/16. | ||
| */ | ||
| 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."); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| String maxValue = scan.nextLine(); | ||
|
|
||
| System.out.println("Please enter first number "); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| String firstNumber = scan.nextLine(); | ||
| System.out.println("Please enter second number "); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| System.out.println("\n\n\nPlease press any key to return the previous menu or enter 0 to exit.\n"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| String input = scan.nextLine(); | ||
| if (input.equals("0")) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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 < maxValue; i++) { | ||
| if (i % firstNumber == 0 || i % secondNumber == 0) { | ||
| sum += i; | ||
| } | ||
| } | ||
| return sum; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <nareshmaharjan@lftechnology.com> | ||
| */ | ||
| 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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| System.out.println("Name : " + name); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
|
|
||
| /** | ||
| * 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."); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| String input = scan.nextLine(); | ||
| if (input.equals("0")) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ProgramMenu.run(0); | ||
| } else { | ||
| ProgramMenu.showMenu(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.lftechnology.phpjava.menu; | ||
|
|
||
| /** | ||
| * Created by naresh on 5/16/16. | ||
| */ | ||
| public interface MenuReader { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| /** | ||
| * read the user input for the action | ||
| */ | ||
| void scanner(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| System.out.println("Tata Horn OK please. :)"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| System.exit(0); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| default: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| System.out.println("Invalid option. Please enter the number mentioned in the menu."); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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")) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| System.exit(0); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } else { | ||
| showMenu(); | ||
| } | ||
| break; | ||
|
|
||
| } | ||
| } | ||
| } | ||




There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.