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
1 month 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 [894]1 month 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
When adopting and implementing a Software as a Service (SaaS) platform such as Salesforce for your business, which responsibilit
Amiraneli [921]

Answer:A SaaS platform entrusts the client company with the duty of providing applications online, as well as creating, hosting, and maintaining the product.

Explanation:

An example of Software as a Service (SaaS) is Salesforce, which provides business applications over the internet.

Software as a Service (SaaS) utilizes cloud technology to host web-based applications, allowing access to users via the internet without necessitating installation or maintenance of the application software. Users simply need an internet connection and a web browser to utilize the service.

8 0
18 days ago
Define a new object in variable sculptor that has properties "name" storing "Michelangelo"; "artworks" storing "David", "Moses"
zubka84 [945]

Answer:

String [] artworks = {"David","Moses","Bacchus"};

Sculptor sculptor = new Sculptor("Michaelangelo",artworks,

"March 6, 1475","February 18, 1564");

Explanation:

  • In order to successfully tackle this task;
  • You should create a class (Java is the chosen language) featuring fields for the name, an array of artworks (strings), the date of birth and the date of death.
  • Utilize a constructor to initialize these fields
  • In a separate class SculptorTest, within the main method, generate a String array artworks assigned to {"David","Moses","Bacchus"};
  • Then create a new object of the class using the code provided in the answer section.
  • Refer to the provided code for the complete class definition below:

public class Sculptor {

private String name;

private String [] artworks;

private String bornDate;

private String diedDate;

public Sculptor(String name, String[] artworks, String bornDate, String diedDate) {

this.name = name;

this.artworks = artworks;

this.bornDate = bornDate;

this.diedDate = diedDate;

}

}

//Test Class with a main method

class SculptorTest{

public static void main(String[] args) {

String [] artworks = {"David","Moses","Bacchus"};

Sculptor sculptor = new Sculptor("Michaelangelo",artworks,

"March 6, 1475","February 18, 1564");

}

}

6 0
14 days ago
Define a method calcPyramidVolume with double data type parameters baseLength, baseWidth, and pyramidHeight, that returns as a d
Rzqust [894]

Answer:

The C++ method is defined as follows:

double calcPyramidVolume(double baseLength, double baseWidth, double pyramidHeight){

   double baseArea = calcBaseArea(baseLength, baseWidth);

   double volume = baseArea * pyramidHeight;

   return volume;    

}

Explanation:

This establishes the calcPyramidVolume method

double calcPyramidVolume(double baseLength, double baseWidth, double pyramidHeight){

This invokes the calcBaseArea method to derive the base area of the pyramid

   double baseArea = calcBaseArea(baseLength, baseWidth);

This calculates the volume based on the base area

   double volume = baseArea * pyramidHeight;

This yields the calculated volume

   return volume;  

}

Refer to the attached document for the complete program that includes all required methods.

5 0
24 days ago
A video conferencing application isn't working due to a Domain Name System (DNS) port error. Which record requires modification
Rzqust [894]

Answer:

Service record (SRV)

Explanation:

Service records, known as SRV records, contain information defining aspects of the DNS like port numbers, server details, hostnames, priority, weight, and the IP addresses of designated service servers.

The SRV record serves as a valuable reference for locating specific services, as applications needing those services will search for the corresponding SRV record.

When configured, the SRV provides the necessary ports and personal settings for a new email client; without this, the parameters within the email client will be incorrect.

8 0
1 month ago
Which of the following describes ETL? a) A process that transforms information using a common set of enterprise definitions b) A
zubka84 [945]

Answer:

The right choice is d) All of these options are accurate.

Explanation:

ETL refers to Extract, Transform, and Load. An ETL framework retrieves data from various sources, upholds standards for data quality and consistency, standardizes data so disparate sources can be integrated, and ultimately presents the data in formats suitable for application development and decision-making by end-users.

4 0
1 month ago
Other questions:
  • Sites like Zillow get input about house prices from a database and provide nice summaries for readers. Write a program with two
    12·1 answer
  • Light travels at 3 × 108 meters per second. A light-year is the distance a light beam travels in one year.Write a PYTHON program
    14·1 answer
  • Two middle-order batsmen are compared based on their performance in their previous cricket match.
    7·1 answer
  • 3. Megan and her brother Marco have a side business where they shop at flea markets, garage sales, and estate
    9·1 answer
  • Your revenue is $22,000. Your Cost of Goods is 16,250. Your gross profit is _____________, fill in the blank,.
    14·1 answer
  • To finish creating a design for the webpage, use one shape to cut away part of the other. Create a 700 pixel by 700 pixel square
    10·1 answer
  • Sara is writing a program to input her monthly phone bills and output the month name and amount for the month with maximum amoun
    7·1 answer
  • Factoring of integers. Write a python program that asks the user for an integer and then prints out all its factors. For example
    13·1 answer
  • Assign to avg_owls the average owls per zoo. Print avg_owls as an integer. Sample output for inputs: 1 2 4 3
    7·1 answer
  • In a survey of 7200 T.V. viewers, 40% said they watch network news programs. Find the margin of error for this survey if we want
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!