1answer.
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
Katena32 [7]
3 years 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:
Evgen [1.6K]3 years ago
8 0

Answer:

See explaination

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++){

if(str.charAt(i)<'a'||str.charAt(i)>'z' && (str.charAt(i)<'A' || 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);

}

}

You might be interested in
Consider the system of simultaneous equations:
FromTheMoon [43]

Answer:

System of linear equations is solved below and explained in detail.

Explanation:

Part a:

M = [1  3  5  2; 2  -4  7  -3; 0  -4  -7  3; 5  -3  2  1];

c =  [7;  -3;  -1;  0];

Part b:

The statement used for the solution of system of linear equation will be:

X = linsolve(M,c)

where X will give the values of x1, x2, x3, x4 respectively.

Part c:

The system is solved in matlab using above equation and the results are attached in a file.

The values for X are:

x1 = -2/7

x2 = 3/7

x3 = 4/7

x4 = 11/7

4 0
3 years ago
Q) CITY column of a table contains information such as Bangalore , Bangalore-64 , Bangalore-56001 , Mumbai - 400002 etc in order
saw5 [17]
Data splitting:::::::::::)
7 0
3 years ago
A _____ cloud allows an organization to take advantage of the scalability and cost-effectiveness that a public cloud computing e
balu736 [363]

Answer:

Hybrid

Explanation:

Hybrid cloud is a solution that combines a private cloud with one or more public cloud services, with proprietary software enabling communication between each distinct service.

5 0
3 years ago
Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less th
Burka [1]

Answer:

// This program is written in C++

// Comments are used for explanatory purpose

// Program starts here

#include<iostream.h>

#include<stdlib.h>

int main()

{

// Declare variables

int num, selectno;

string status;

randomize();

//Generate random number;

num=rand()%10000;

// Prompt to guess a number

cout<<"You have only 10 tries\nTake a guess: ";

int tries = 0;

while (tries != 10)

{

cin>>selectno;

if(selectno == num){

cout<<"You passed at the "<<count+1<<" attempt";

tries = 10;

}

else

{

cout<<"You failed. Take another guess\n You have "<<10 - count + 1 <<" attempts";

}

tries++;

if(tries >= 10)

{

break;

}

}

return 0;

}

8 0
3 years ago
Help ASAP!!Choose all the basic elements of algorithms. A.Selection B.Loops C.Flow Charts D.Sequencing E.Combinations F.Iteratio
serg [7]

Answer:

b c d

Explanation:

8 0
3 years ago
Read 2 more answers
Other questions:
  • Dr. Laos gets a referral for a 6 month old who has become listless and stopped eating lately. Her pediatrician wanted her to be
    15·1 answer
  • If your network subnet mask is /16, what is the maximum number of host ids available for this network?
    15·1 answer
  • 12. Kelly would like to know the average bonus multiplier for the employees. In cell C11, create a formula using the AVERAGE fun
    7·1 answer
  • Read the excerpt from Act 111, scene iii of Romeo and Juliet. Which is the best paraphrase of this dialogue
    11·1 answer
  • Join zoom meet <br>id=547 458 9345<br>pw=sencHURI​
    8·2 answers
  • Elsa wants to save her work at the office to be continued at home either on a pen drive or CD. Outline three reasons why she wil
    6·1 answer
  • g 'write a function that takes as input a list and outs a new list containing all elements from the input
    5·1 answer
  • Which tool can be used to increase the space between a bullet point or a number and text?
    11·2 answers
  • PLEASE HELPPPPP ASAP, 50 POINT'S + BRAINLIEST
    14·1 answer
  • Discuss the ways you can perform to prevent your computer/device and its data/contents from being stolen. Define two-facto authe
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!