112k views
5 votes
This question involves the creation of user names for an online system. A user name is created based on a user’s first and last names. A new user name cannot be a duplicate of a user name already assigned. You will write the constructor and one method of the UserName class. A partial declaration of the UserName class is shown below.

public class UserName

{

// The list of possible user names, based on a user’s first and last names and initialized by the constructor.

private ArrayList possibleNames;



/** Constructs a UserName object as described in part (a).

* Precondition: firstName and lastName have length greater than 0

* and contain only uppercase and lowercase letters.

*/

public UserName(String firstName, String lastName)

{ /* to be implemented in part (a) */ }



/** Returns true if arr contains name, and false otherwise. */

public boolean isUsed(String name, String[] arr)

{ /* implementation not shown */ }



/** Removes strings from possibleNames that are found in usedNames as described in part (b).

*/

public void setAvailableUserNames(String[] usedNames)

{ /* to be implemented in part (b) */ }

}

(a) Write the constructor for the UserName class. The constructor initializes and fills possibleNames with possible user names based on the firstName and lastName parameters. The possible user names are obtained by concatenating lastName with different substrings of firstName. The substrings begin with the first character of firstName and the lengths of the substrings take on all values from 1 to the length of firstName.

The following example shows the contents of possibleNames after a UserName object has been instantiated.

Example

UserName person = new UserName("john", "smith");

After the code segment has been executed, the possibleNames instance variable of person will contain the following String objects in some order.

"smithj", "smithjo", "smithjoh", "smithjohn"

Write the UserName constructor below.

/** Constructs a UserName object as described in part (a).

* Precondition: firstName and lastName have length greater than 0

* and contain only uppercase and lowercase letters.

*/

public UserName(String firstName, String lastName)

Write the UserName method setAvailableUserNames. The method removes from possibleNames all names that are found in usedNames. These represent user names that have already been assigned in the online system and are therefore unavailable.

A helper method, isUsed, has been provided. The isUsed method searches for name in arr. The method returns true if an exact match is found and returns false otherwise.

Assume that the constructor works as specified, regardless of what you wrote in part (a). You must use isUsed appropriately to receive full credit.

Complete the setAvailableUserNames method below.

/** Removes strings from possibleNames that are found in usedNames as described in part (b).

*/

public void setAvailableUserNames(String[] usedNames)

User Tacocat
by
5.8k points

1 Answer

3 votes

Answer:

See explaination

Step-by-step explanation:

import java.util.*;

class UserName{

ArrayList<String> possibleNames;

UserName(String firstName, String lastName){

if(this.isValidName(firstName) && this.isValidName(lastName)){

possibleNames = new ArrayList<String>();

for(int i=1;i<firstName.length()+1;i++){

possibleNames.add(lastName+firstName.substring(0,i));

}

}else{

System.out.println("firstName and lastName must contain letters only.");

}

}

public boolean isUsed(String name, String[] arr){

for(int i=0;i<arr.length;i++){

if(name.equals(arr[i]))

return true;

}

return false;

}

public void setAvailableUserNames(String[] usedNames){

String[] names = new String[this.possibleNames.size()];

names = this.possibleNames.toArray(names);

for(int i=0;i<usedNames.length;i++){

if(isUsed(usedNames[i],names)){

int index = this.possibleNames.indexOf(usedNames[i]);

this.possibleNames.remove(index);

names = new String[this.possibleNames.size()];

names = this.possibleNames.toArray(names);

}

}

}

public boolean isValidName(String str){

if(str.length()==0) return false;

for(int i=0;i<str.length();i++) str.charAt(i)>'Z'))

return false;

return true;

}

public static void main(String[] args) {

UserName person1 = new UserName("john","smith");

System.out.println(person1.possibleNames);

String[] used = {"harta","hartm","harty"};

UserName person2 = new UserName("mary","hart");

System.out.println("possibleNames before removing: "+person2.possibleNames);

person2.setAvailableUserNames(used);

System.out.println("possibleNames after removing: "+person2.possibleNames);

}

}

User Shivon
by
4.3k points