Last active
October 29, 2019 14:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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