Skip to content

Instantly share code, notes, and snippets.

@dadiletta
Last active October 29, 2019 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadiletta/f81511e25f59b06f5757127e8d6a201d to your computer and use it in GitHub Desktop.
Save dadiletta/f81511e25f59b06f5757127e8d6a201d to your computer and use it in GitHub Desktop.
Practice for my AP Computer Science students interested in making more efficient code
/**
* Help! I used too many lines of code. Help me make this more efficient.
* This main method must create an array with the same words shown below. Then I
* have to test each word with this validatePassword method. If the password is valid,
* I have to print a message. Can you make this more efficient?
*/
public static void main(String[] args) {
int count = 0;
String[] words;
words = new String[4];
words[count] = "Imagine";
count++;
words[count] = "yourself";
count++;
words[count] = "on";
count++;
words[count] = "Mars";
for(int x = 0; x < words.length; x++){
boolean answer = validatePassword(words[x]);
if (answer == true){
System.out.println("Password accepted");
}
else{
//No action is required if the password fails.
}
}
}
public static boolean validatePassword(String w){
boolean answer = w.equals("Mars");
if(answer == true){
return true;
}
else if(answer != true){
return false;
}else return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment