answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
AlekseyPX
3 months ago
13

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)
Computers and Technology
1 answer:
Rzqust [1K]3 months ago
8 0

Response:

Refer to the explanation

Clarification:

import java.util.*;

class UserName{

ArrayList<String> potentialNames;

UserName(String fName, String lName){

if(this.isValidName(fName) && this.isValidName(lName)){

potentialNames = new ArrayList<String>();

for(int j=1;j<fName.length()+1;j++){

potentialNames.add(lName+fName.substring(0,j));

}

}else{

System.out.println("firstName and lastName should only consist of letters.");

}

}

public boolean isTaken(String name, String[] array){

for(int j=0;j<array.length;j++){

if(name.equals(array[j]))

return true;

}

return false;

}

public void removeUnavailableUserNames(String[] takenNames){

String[] namesArray = new String[this.potentialNames.size()];

namesArray = this.potentialNames.toArray(namesArray);

for(int j=0;j<takenNames.length;j++){

if(isTaken(takenNames[j],namesArray)){

int idx = this.potentialNames.indexOf(takenNames[j]);

this.potentialNames.remove(idx);

namesArray = new String[this.potentialNames.size()];

namesArray = this.potentialNames.toArray(namesArray);

}

}

}

public boolean isValidName(String str){

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

for(int j=0;j<str.length();j++){

if(str.charAt(j)<'a'||str.charAt(j)>'z' && (str.charAt(j)<'A' || str.charAt(j)>'Z'))

return false;

}

return true;

}

public static void main(String[] args) {

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

System.out.println(user1.potentialNames);

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

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

System.out.println("potentialNames prior to removal: "+user2.potentialNames);

user2.removeUnavailableUserNames(existing);

System.out.println("potentialNames following removal: "+user2.potentialNames);

}

}

You might be interested in
A have a string, called "joshs_diary", that is huge (there was a lot of drama in middle school). But I don't want every one to k
maria [1035]

Answer:

C and G

Explanation:

In the C programming language, the symbols ' * ' and ' & ' are utilized to create pointers and access references to those pointers, respectively. The ' * ' is used in conjunction with specific identifiers to define a pointer to a variable's location in memory, whereas the ' & ' is always positioned in front of a variable as an r_value for the specified pointer.

5 0
2 months ago
1.Which of the following class definitions defines a legal abstract class?a. class A { abstract void unfinished() { } }b. class
zubka84 [1067]
The correct option is c) abstract class A { abstract void unfinished(); }. In order for a class to be considered a legal abstract class, it must include the keyword 'abstract' designated before the class name, and additionally, the abstract functions must also incorporate the 'abstract' keyword alongside a void return type.
8 0
1 month ago
Create a conditional expression that evaluates to string "negative" if user_val is less than 0, and "non-negative" otherwise.
maria [1035]

Answer:

The revised code is as follows:

user_val = int(input())

cond_str = 'non-negative'  

if user_val < 0:

   cond_str = 'negative'  

print(user_val, 'is', cond_str)

Explanation:

This retrieves input for user_val

user_val = int(input())

This sets cond_str to 'non-negative'

cond_str = 'non-negative'

In cases where user_val is below 0

if user_val < 0:

Here cond_str changes to 'negative'

   cond_str = 'negative'  

This displays the intended output

print(user_val, 'is', cond_str)

4 0
3 months ago
A pedometer treats walking 2,000 steps as walking 1 mile. Write a program whose input is the number of steps, and whose output i
ivann1987 [1066]

Response:

steps = int(input("Specify the number of steps: "))

miles = steps / 2000

print('{:.2f}'.format(miles))

Explanation:

Prompt the user to input the number of steps and convert the input to an integer.

Compute the corresponding miles by dividing the number of steps by 2000.

Output the miles formatted to two decimal places.

5 0
3 months ago
Use Excel to develop a regression model for the Consumer Food Database (using the "Excel Databases.xls" file on Blackboard) to p
oksian1 [950]
Step 1: Use the provided formula to create an Indicator Variable for cities with metro areas. Step 2: Apply a filter to isolate data specific to metro cities, selecting only those marked with Metro Indicator 1. Step 3: Transfer the filtered data to a new worksheet. Step 4: Navigate to Data - Data Analysis - Regression. Step 5: Input the specified Y-variable and X-variable ranges as indicated. Choose the output range and ensure residuals are checked, which will produce the Output Summary and the Predicted Values alongside Residuals. Please see the accompanying attachment.
5 0
1 month ago
Other questions:
  •  How does critically analyzing technology add value to interactions with people in personal and professional contexts?
    9·2 answers
  • Write the definition of a function power_to, which receives two parameters. The first is a float and the second is an integer. T
    5·1 answer
  • Which of the following is NOT true about data?
    8·2 answers
  • Team A found 342 errors during the software engineering process prior to release. Team B found 184 errors. What additional measu
    12·1 answer
  • Write lines of verse that rhyme to remember the following information:
    16·2 answers
  • Write a function in the cell below that iterates through the words in file_contents, removes punctuation, and counts the frequen
    6·1 answer
  • What is the name of the item that supplies the exact or near exact voltage at the required wattage to all of the circuitry insid
    14·1 answer
  • In today’s fast-paced, often "agile" software development, how can the secure design be implemented?
    11·1 answer
  • Pendant Publishing edits multi-volume manuscripts for many authors. For each volume, they want a label that contains the author'
    14·1 answer
  • int decode2(int x, int y, int z); is compiled into 32bit x86 assembly code. The body of the code is as follows: NOTE: x at %ebp+
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!