patternjavaModerate
Camp challenges in one program
Viewed 0 times
onecampchallengesprogram
Problem
My counselor in a camp I went to over the summer gave me a code challenge. To take 6 challenges (listed below) and have all of them in one program, and have the user choose which one to use. Please review all of my code. If there is anything wrong, inefficient, or if I use bad practices, let me know; I'm still new at this. PS: If there are any methods I made that are already built into Java, ignore them, I had to reinvent the wheel
-
Given a list of numbers, add up all the even numbers from that list and return the sum.
-
Given a list of numbers, return whether that list is in order or not
-
Given two words, return a list of letters that occur in both words
-
Given a long string representing a sentence, and a shorter string representing a word, find if the word is in the sentence.
-
Given a string return which letter is most frequent.
-
Given a string, make it a palindrome. You may only add characters to the end of the string.
```
package Challenges;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class ChallengeOneToFive {
@SuppressWarnings("resource")
public static void main(String[] args) {
System.out.println("Which Challenge would you like to see?\n1: Even Number Adder \n2: Number Sorter \n3: Word Letter Occurance \n4: Sentence Words \n5: Frequent Letters \n6: Palindromes");
Scanner scan = new Scanner(System.in);
String choice = scan.nextLine();
if(choice.equals("1")) {
Challenge1();
} else if(choice.equals("2")) {
Challenge2();
} else if(choice.equals("3")) {
Challenge3();
} else if(choice.equals("4")) {
Challenge4();
} else if(choice.equals("5")) {
Challenge5();
} else if(choice.equals("6")) {
Challenge6();
}
}
@SuppressWarnings("resource")
//Challenge 1 is Completed
public static int Challenge1() {
System.out.println("How many numbers will you be entering?");
Scanner scan = new Scanner(Syst
-
Given a list of numbers, add up all the even numbers from that list and return the sum.
-
Given a list of numbers, return whether that list is in order or not
-
Given two words, return a list of letters that occur in both words
-
Given a long string representing a sentence, and a shorter string representing a word, find if the word is in the sentence.
-
Given a string return which letter is most frequent.
-
Given a string, make it a palindrome. You may only add characters to the end of the string.
```
package Challenges;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class ChallengeOneToFive {
@SuppressWarnings("resource")
public static void main(String[] args) {
System.out.println("Which Challenge would you like to see?\n1: Even Number Adder \n2: Number Sorter \n3: Word Letter Occurance \n4: Sentence Words \n5: Frequent Letters \n6: Palindromes");
Scanner scan = new Scanner(System.in);
String choice = scan.nextLine();
if(choice.equals("1")) {
Challenge1();
} else if(choice.equals("2")) {
Challenge2();
} else if(choice.equals("3")) {
Challenge3();
} else if(choice.equals("4")) {
Challenge4();
} else if(choice.equals("5")) {
Challenge5();
} else if(choice.equals("6")) {
Challenge6();
}
}
@SuppressWarnings("resource")
//Challenge 1 is Completed
public static int Challenge1() {
System.out.println("How many numbers will you be entering?");
Scanner scan = new Scanner(Syst
Solution
Your menu could be replaced by a switch instead. Switches are more readable, faster, easier to maintain/expand and the idiomatic way of doing such things:
You may want a construction that keeps asking the user for valid input unless he enters a given exit sequence.
Challenge 1 and 2 can handle different amounts of input. While it's not required for challenge 3, it would clean-up your code considerably. The moment you start using variable names like
int choice = Integer.valueOf(scan.nextLine());
switch (choice) {
case 1: Challenge1();
break;
case 2: Challenge2();
break;
case 3: Challenge3();
break;
case 4: Challenge4();
break;
case 5: Challenge5();
break;
case 6: Challenge6();
break;
default: System.out.println("Invalid challenge selected");
break;You may want a construction that keeps asking the user for valid input unless he enters a given exit sequence.
Challenge 1 and 2 can handle different amounts of input. While it's not required for challenge 3, it would clean-up your code considerably. The moment you start using variable names like
word2 and s2 you should ask yourself whether those are descriptive. Descriptive variable naming helps a lot when adding features later on or trying to understand what you've written a long time ago when looking back on it.Code Snippets
int choice = Integer.valueOf(scan.nextLine());
switch (choice) {
case 1: Challenge1();
break;
case 2: Challenge2();
break;
case 3: Challenge3();
break;
case 4: Challenge4();
break;
case 5: Challenge5();
break;
case 6: Challenge6();
break;
default: System.out.println("Invalid challenge selected");
break;Context
StackExchange Code Review Q#110301, answer score: 11
Revisions (0)
No revisions yet.