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
Juli2301 [7.4K]
2 years ago
8

Recall that a Set is an abstract data type somewhat similar to a Bag, they can store a finite collection of objects without any

particular order. However, unlike a Bag, a Set cannot contain duplicates. The add() method for a Set is therefore very similar to the add() method for a Bag, with the additional requirement that it must first confirm that the item being added is unique. Write the member method addLikeASet that implements the following logic: When invoked the method takes the T parameter 'anEntry' and determines if 'anEntry' already exists in the bag. If the bag does not already contain 'anEntry' then the method attempts to add it to the bag, returning true when successful. If the bag already contained 'anEntry' the method does not attempt to add 'anEntry' and returns false. Your implementation code for this problem may NOT access/invoke any of the Bag API methods but, since it is a member method, your code may access the fields numberOfEntries and contents. Your solution code may also include helper methods.
Computers and Technology
1 answer:
lina2011 [118]2 years ago
3 0

Answer:

Here is the addLikeASet() method:

public boolean addLikeASet(T anEntry){  // method takes the T parameter anEntry

       boolean res = true;  //sets the result to true

       if(Arrays.asList(bag).contains(anEntry);  {  //checks if the bag already contains anEntry

           res=false;         }  //returns false

       else         {   if the bag does not already contain anEntry

           bag[numberOfEntries]=anEntry;  //enters the anEntry in bag array

           numberOfEntries++;         }  //increments the  numberOfEntries by 1

       return res;     } //returns true

Explanation:

You can also implement this method like this:

public boolean isFilled(){ // the function to check if the bag is full

       return numberOfEntries == bag.length;  } // number of entries equals length of the bag which shows that the bag is full

public boolean addLikeASet(T anEntry){

       boolean res=true;

       if (!isFilled() || (Arrays.asList(bag).contains(anEntry));  //checks if the bag is full or contains anEntry already

           res=false;         //returns false

       else         { if bag does not already contain anEntry

           bag[numberOfEntries]=anEntry; //add anEntry to the bag

           numberOfEntries++;         } add 1 to the count of numberOfEntries

       return res;     } //returns true

You can also use a loop to check if anEntry is already present in bag:

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

    if(anEntry.equals(bag[i])) {

      return false;      }    }  

  return true;

You might be interested in
A bicycle sharing company is developing a multi-tier architecture to track the location of its bicycles during peak operating ho
fgiga [73]

Answer:

d use Amazon API Gateway with Amazon kinesis...

5 0
1 year ago
Choose the correct function to convert the user’s response to the number 3.5. &gt;&gt;&gt; answer = input("How much does the sam
FromTheMoon [43]

Answer:

The answer to the given question is given bellow in the explanation section:

Explanation:

<p>This is python code</p>

<p>In python when you enter input into the input function as given:</p>

<code> answer = input("How much does the sample weigh in grams? ") </code>

I will be taken as string.

So I have to convert this string to number format here the given data is float value i-e 3.5

so let convert it. using float function.

<code> answer = float (input("How much does the sample weigh in grams? ") )  </code>

6 0
1 year ago
Read 2 more answers
g Fill the validateForm function to check that the phone number is 10 characters long and that the user name is less than 11 cha
Rom4ik [11]

Answer:

function validateForm(event)

{

event.preventDefault();

var phoneNumber = form.phoneNumber.value;

var userName = form.userName.value;

if(phoneNumber.length!=10)

console.log("Phone Number is Invalid");

if(userName.length<11)

console.log("User Name is Invalid"); }

8 0
1 year ago
Axel is finally documenting his work. What could he be writing?
allochka39001 [22]

Answer: maybe his wrtting ezam

Explanation:

7 0
2 years ago
Although the optical discs are all 5 ¼”, a CD drive can not read a DVD disc, and a DVD drive can not read a Blu-ray disc. Why no
otez555 [7]

Answer:

For storing the data into a CD, we need to burn it. And the surface of the CD is comprised of the polycarbonate layer that is molded as the spiral tracks over the surface of the CD. And the Data is being stored as a sequence of minute grooves that are called the pits which are encoded on the spiral tracks. And the region in between the pits is known as lands.

The DVD or the digital video dos is based on the optical data storage which is almost the same as the CD. The analog data is transformed into the digital data, and this is encoded on the disc starting from the inside edge, and towards out. This digital information is being encoded into the surface as pits, and the surface acts as the recording layer. A DVD can hold data up to 4.77 to 8 GB.

Since DVD size is bigger than CD, A DVD drive can read CD, but the CD drive cannot read the DVD,

And unlike the above two, which make use of the red laser for reading and writing, the blu ray makes use of the blue laser. Also, it has the smaller pits, tiny beam as well as small track that together make it possible to store around 25 GB of data in one single blue ray disc. And this is five times more than the size of a DVD.  

The Blu ray Discs are good for playing the CD as well as DVD. However, the opposite is not possible due to the size.

Explanation:

Please check the answer section.

4 0
2 years ago
Other questions:
  • When you reboot your system, the computer follows start-up instructions stored in this type of memory. multiple choice dram sdra
    15·2 answers
  • In a spreadsheet, the instructions for carrying out calculations are called __________. recalculations formulas templates macros
    13·1 answer
  • When long labels are required, which of these commands can you use to improve readability of a worksheet?
    14·2 answers
  • Given: an int variable k, an int array current Members that has been declared and initialized, an int variable memberID that has
    11·1 answer
  • Identify the normalized form of the mantissa in 111.01.
    14·1 answer
  • C# Write, compile, and test a program named PersonalInfo that displays a person’s name, birthdate, work phone number, and cell p
    8·1 answer
  • There is a file "IT4983" in my home (personal) directory. I don’t know my current working directory. How can I find out the file
    11·1 answer
  • Which of these tools can best be used as a self assessment for career planning purposes? a personality test an asset analysis a
    5·1 answer
  • When drivers have no control over their driving environment and are stuck in traffic, the lack of control over the traffic event
    13·1 answer
  • In a particular field, there are trees in a l single row from left to right. Each tree has a value V You cut trees from left to
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!