Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

String maxValue = scan.nextLine();
int max = Integer.valueOf(maxValue);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Use "Integer.parseInt" for this string-to-int conversion. rule

int counter = 0;
int sum = 0;
int a, b, c;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Declare "b" on a separate line. rule
MINOR Declare "c" on a separate line. rule

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

System.out.println("\n\n\nPlease press any key to return the previous menu or enter 0 to exit.\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule


String input = scan.nextLine();
if (input.equals("0")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Move the "0" string literal on the left side of this string comparison. rule

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL Cast one of the operands of this multiplication operation to a "long". rule

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.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

Scanner scan = new Scanner(System.in);
System.out.println("Please enter first number ");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

String firstNumber = scan.nextLine();
System.out.println("Please enter second number ");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule


System.out.println("\n\n\nPlease press any key to return the previous menu or enter 0 to exit.\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule


String input = scan.nextLine();
if (input.equals("0")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Move the "0" string literal on the left side of this string comparison. rule

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove this unused import 'java.util.ArrayList'. rule

import java.util.List;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove this unused import 'java.util.List'. rule

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.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

String maxValue = scan.nextLine();

System.out.println("Please enter first number ");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

String firstNumber = scan.nextLine();
System.out.println("Please enter second number ");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule


System.out.println("\n\n\nPlease press any key to return the previous menu or enter 0 to exit.\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule


String input = scan.nextLine();
if (input.equals("0")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Move the "0" string literal on the left side of this string comparison. rule

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

System.out.println("Name : " + name);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

}


/**
* 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.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

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");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

String input = scan.nextLine();
if (input.equals("0")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Move the "0" string literal on the left side of this string comparison. rule

ProgramMenu.run(0);
} else {
ProgramMenu.showMenu();
}
}
}
12 changes: 12 additions & 0 deletions naresh/src/main/java/com/lftechnology/phpjava/menu/MenuReader.java
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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Annotate the "MenuReader" interface with the @FunctionalInterface annotation rule


/**
* 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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Add a private constructor to hide the implicit public one. rule

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");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

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:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR End this switch case with an unconditional break, return or throw statement. rule

System.out.println("Tata Horn OK please. :)");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

System.exit(0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Remove this call to "exit" or ensure it is really required. rule

default:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Reduce this switch case number of lines from 11 to at most 5, for example by extracting code into methods. rule

System.out.println("Invalid option. Please enter the number mentioned in the menu.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this usage of System.out or System.err by a logger. rule

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")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Move the "0" string literal on the left side of this string comparison. rule

System.exit(0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Remove this call to "exit" or ensure it is really required. rule

} else {
showMenu();
}
break;

}
}
}