Download Extra Credit Part 1 SOLUTION

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
// Extra Credit Part 1 SOLUTION
//////////////////////////////////////////////////////////
import java.util.*;
public class Part1
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("What is your name?");
String name = keyboard.nextLine();
System.out.println("Hello, " + name + "!");
if(name.equals("Alphonso") || name.toUpperCase().equals("ALPHONSO"))
{
System.out.print("Alphonso is an interesting name! Did you know that it is Italian in origin, " +
"and roughly translated means" + " \"I'm Ready to Rumble\" ?");
}
}
}
2
// Extra Credit Part 2 SOLUTION
//////////////////////////////////////////////////////////
import java.util.*;
public class Part2
{
public static void main(String[] args)
{
String userText = "Rex Angus Cornelius Winkus";
System.out.println("The user's name is: " + userText + "\n");
System.out.println("In UPPERCASE: " + userText.toUpperCase() + "\n");
System.out.println("In lowercase: " + userText.toLowerCase() + "\n");
System.out.println("The length of their name is: " + userText.length() + " characters" + "\n");
System.out.println("Their last name is: " + userText.substring(20, 26) + "\n");
System.out.println("Their first name is: " + userText.substring(0, 3) + "\n");
System.out.println("Their middle names are: \"" + userText.substring(4, 9) + "\" and \"" + userText.substring(10,19) + "\"\n");
System.out.println("If you replace all the \"s\"'s with \"z\"'s, it looks like: "
+ userText.replace("s", "z") + "\n");
System.out.println("If you replace \"Rex\" with \"Tex\", it looks like: " + userText.replace("Rex", "Tex") + "\n");
System.out.println("If you omit the middle names, their name would be: " + userText.replace("Angus Cornelius ", "") + "\n");
System.out.println("If you only omit one of the middle names, their name would either be: \"" +
userText.replace("Angus ", "") + "\" or \"" + userText.replace("Cornelius ", "" ) + "\"");
}
}
3
// Extra Credit Part 3 SOLUTION
//////////////////////////////////////////////////////////
public class Part3
{
public static void main(String[] args)
{
String[] names = new String[] {"Mal", "Inara", "Derrial", "River", "Kaylee"};
//Prints out all of the elements in the "names" array
for(int i = 0; i < names.length; i++)
{
System.out.println(names[i]);
}
}
}
4
// Extra Credit Part 4 SOLUTION
//////////////////////////////////////////////////////////
import java.util.*;
public class Part4
{
//Checks if the name the user entered matches one of the names in the "names" array
public static boolean checkName(String name, String[] listOfNames)
{
for(int i = 0; i < listOfNames.length; i++)
{
if(name.equals(listOfNames[i]))
return true;
}
return false;
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String[] names = new String[] {"Mal", "Inara", "Derrial", "River", "Kaylee"};
System.out.println("Please enter your name:");
String userName = keyboard.nextLine();
if(checkName(userName, names))
System.out.println("We have a match!");
else
System.out.println("No match...");
}
}
5
// Extra Credit Part 5 SOLUTION
//////////////////////////////////////////////////////////
import java.util.*;
public class Part5
{
Scanner keyboard = new Scanner(System.in);
//Prompt user for their user name until they pick a correct one
public String promptUserName(String[] listOfUsers)
{
System.out.println("Please enter your username:");
String userName = keyboard.nextLine();
return userName;
}
//Prompt user for their password until they enter the one matching their user name
public boolean promptPassword(String userName, String[] listOfUsers, String[] passwordDatabase)
{
System.out.println("Please enter your password:");
String userPassword = keyboard.nextLine();
if(checkPassword(userName, userPassword, passwordDatabase, listOfUsers))
{
System.out.println("Welcome " + userName + "!");
return true;
}else
{
System.out.println("User name unknown or password doesn't match\n");
return false;
}
}
//Checks if the password entered by the user matches their user name
public static boolean checkPassword(String user, String password, String[] passwordDatabase, String[] listOfUsers)
{
for(int i = 0; i < passwordDatabase.length; i++)
{
if(password.equals(passwordDatabase[i]) && user.equals(listOfUsers[i]))
return true;
}
return false;
}
6
//main method
public static void main(String[] args)
{
Part5 p5 = new Part5();
String[] users = new String[] {"Mal", "Inara", "Derrial", "River", "Kaylee"};
String[] passwords = new String[] {"serenity", "companion", "shepherd", "psychic", "mechanic"};
boolean logIn = false;
while(!logIn)
{
String user = p5.promptUserName(users);
if(p5.promptPassword(user, users, passwords))
logIn = true;
}
}
}
7
// Extra Credit Part 6 SOLUTION
//////////////////////////////////////////////////////////
//The user will be able to continue loging in until they either
//fill up all the slots in the arrays, or they decide to not add a new user
import java.util.*;
public class Part6
{
Scanner keyboard = new Scanner(System.in);
//Asks the user if they would like to add another user
public boolean userChoice()
{
System.out.println("Would you like to add a new user?(y / n)");
String choice = keyboard.next();
if(choice.equals("y"))
return true;
else
return false;
}
//Method used for adding a new user to the "users" array
public String[] newUser(String[] listOfUsers, int index)
{
System.out.println("New user name:");
listOfUsers[index] = keyboard.next();
return listOfUsers;
}
//Method used for adding a new password to the "passwords" array
public String[] newPassword(String[] passwordDatabase, int index)
{
System.out.println("New user's password:");
passwordDatabase[index] = keyboard.next();
keyboard.nextLine();
return passwordDatabase;
}
8
//Prompt user for their user name until they pick a correct one
public String promptUserName(String[] listOfUsers)
{
System.out.println("Please enter your username:");
String userName = keyboard.nextLine();
return userName;
}
//Prompt user for their password until they enter the one matching their user name
public boolean promptPassword(String userName, String[] listOfUsers, String[] passwordDatabase)
{
System.out.println("Please enter your password:");
String userPassword = keyboard.nextLine();
if(checkPassword(userName, userPassword, passwordDatabase, listOfUsers))
{
System.out.println("Welcome " + userName + "!");
return true;
}else
{
System.out.println("User name unknown or password doesn't match\n");
return false;
}
}
//Checks if the password entered by the user matches their user name
public static boolean checkPassword(String user, String password, String[] passwordDatabase, String[] listOfUsers)
{
for(int i = 0; i < passwordDatabase.length; i++)
{
if(password.equals(passwordDatabase[i]) && user.equals(listOfUsers[i]))
return true;
}
return false;
}
9
//Returns an array with the first five of the ten slots filled with user names
public static String[] createUserArray()
{
String[] users = new String[10];
users[0] = "Mal";
users[1] = "Inara";
users[2] = "Derrial";
users[3] = "River";
users[4] = "Kaylee";
return users;
}
//Returns an array with the first five of the ten slots filled with passwords
public static String[] createPasswordArray()
{
String[] passwords = new String[10];
passwords[0] = "serenity";
passwords[1] = "companion";
passwords[2] = "shepherd";
passwords[3] = "psychic";
passwords[4] = "mechanic";
return passwords;
}
10
//Initiates the login procedure
public void logIn (String[] userNames, String[] userPasswords, boolean successful)
{
int subscript = 5;
while(!successful)
{
String user = this.promptUserName(userNames);
if(this.promptPassword(user, userNames, userPasswords) && (subscript < 10))
{
if(this.userChoice())
{
userNames = this.newUser(userNames, subscript);
userPasswords = this.newPassword(userPasswords, subscript);
subscript++;
}else
{
successful = true;
}
}else
{
successful = true;
}
}
}
//main method
public static void main(String[] args)
{
Part6 p6 = new Part6();
String[] users = createUserArray();
String[] passwords = createPasswordArray();
p6.logIn(users, passwords, false);
// I ADDED THIS SNIPPET FOR TEST PURPOSES :-)
for(int i = 0; i < users.length; i++)
{
System.out.println(users[i]);
}
}
}
Related documents