Question

In: Computer Science

Programming Task 1: Please use the following framework to implement a class for partially filled arrays....

Programming Task 1:

Please use the following framework to implement a class for partially filled arrays.

class PartiallyFilledArray

{

     double[] content;          // content of the array

     int size;                  // actual size of the array

     // creating an array whose physical size is 100

     PartiallyFilledArray()

     {

           // your implementation

     }

     // creating an array whose physical size is given by maxSize

     PartiallyFilledArray(final int maxSize)

     {

           // your implementation

     }

     // insertion

     boolean insert(_____________________)

     {

           // your implementation

     }

     // deletion

     boolean delete(_____________________)

     {

           // your implementation

     }

     // displaying all elements

     void display()

     {

           // your implementation

     }

}

Programming Task 2:

Please use the above class to create an empty partially filled array whose maximum size is 2000. (1) Consecutively add 20 different random integer values ranging from -100 to 100 to the array and display the content. (2) Delete the first 3 elements and display the content. Notice that you should not directly use the internal data field of PartiallyFilledArray in your test code.

Solutions

Expert Solution

All the explanations is given in the comments of the code itself.

Code--

import java.util.*;
class PartiallyFilledArray
{
     double[] content;          // content of the array
     int size;                  // actual size of the array
     // creating an array whose physical size is 100
     PartiallyFilledArray()
     {
           content=new double[100];
           size=-1;
     }

     // creating an array whose physical size is given by maxSize
     PartiallyFilledArray(final int maxSize)
     {
           content=new double[maxSize];
           size=-1;
     }
     // insertion
     boolean insert(double data)
     {
       //if stack is full return false
         if(content.length==(size+1))
               return false;
         size++;
         content[size]=data;
         return true;
     }
     // deletion an element at the given index
     boolean delete(int index)
     {
       //if it is an invalid index return false
           if(size<index)
               return false;
           for(int i=index;i<size;i++)
           {
               content[i]=content[i+1];
           }
           size--;
           return true;
     }
     // displaying all elements
     void display()
     {
           System.out.println("Displaying the elements:");
           for(int i=0;i<=size;i++)
           {
               //for convinence I will display only 2 digits after decimal
               System.out.printf("%.2f ",content[i]);
           }
           System.out.println();
     }
}

//programming task 2
public class ArrayTest
{
   public static void main(String[] args)
   {
       //create an array of size 2000
       PartiallyFilledArray pfa=new PartiallyFilledArray(2000);
      
       //consecutively add 20 random elements
       Random r=new Random();
       for(int i=0;i<20;i++)
       {
           pfa.insert((r.nextDouble()*200)-100);
       }
       //display
       pfa.display();
       //delete first 3 elements
       pfa.delete(0);
       pfa.delete(0);
       pfa.delete(0);
       //display
       pfa.display();
   }
}


Code(Programming Task-2) Screenshot--


Output (Copied)--

Displaying the elements:
-86.87 32.17 -94.46 28.54 -48.46 26.82 -33.65 34.16 43.66 76.54 -71.30 -1.78 20.33 -21.10 -79.09 -12.55 -14.76 -86.24 -49.63 -46.72
Displaying the elements:
28.54 -48.46 26.82 -33.65 34.16 43.66 76.54 -71.30 -1.78 20.33 -21.10 -79.09 -12.55 -14.76 -86.24 -49.63 -46.72


Note--

Please upvote if you like the effort.


Related Solutions

Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Objectives In this lab you will review passing arrays to methods and partially filled arrays. Requirements...
Objectives In this lab you will review passing arrays to methods and partially filled arrays. Requirements 1. Fill an array with data from an input file sampledata-io.txt (Attached) a. Assume no more than 100 data items, terminated by -1 for sentinel b. Note that the sample data file has other information after the sentinel; it should cause no problem c. Read and store data d. Print the number of data stored in the array e. Add a method to reverse...
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
implement please... ........................................... public class TernarySearch {     /** Task: Searches for a value in an array....
implement please... ........................................... public class TernarySearch {     /** Task: Searches for a value in an array.      * @param a an array of Comparable objects      * @param desiredItem an item to search for         * @param n an integer > 0      */     public static <T extends Comparable<? super T>>     boolean ternarySearch(T[] a, T desiredItem, int n)     {         // it is assumed that the data is already sorted         return ternarySearch(a, 0, n-1, desiredItem);     } // end ternarySearch     /** Task: recursive ternarySearch search through...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the main module that creates and manipulates CVehicle objects cars.dat -- a text file that contains name data for the main module 4th file is cvehicle.cpp and it needs to be created from scratch, and cvehicle.h needs to be filled in This was the test drive: carOne = Hyundai Sonata carTwo = Hyundai Sonata carThree = Enter the make and model of a vehicle: toyota...
Cryptography - please use Python to complete the following task. You don't have to implement AES...
Cryptography - please use Python to complete the following task. You don't have to implement AES just import the necessary libraries. Can you fix my code? Thanks! Read in a 128 bit key from the user (16 characters). If the user enters a key of any other size it's fine to terminate the program with an error. Read in a string of arbitary length from the user. (ie, the string could be 1 character, or 50,000, or anywhere in between)....
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
How are PHP arrays different than arrays in other programming languages? Please discuss in depth.
How are PHP arrays different than arrays in other programming languages? Please discuss in depth.
Use the framework of the QTM as we developed it in class to do the following:...
Use the framework of the QTM as we developed it in class to do the following: Explain why Friedman argued that the FED should follow a monetary policy rule and what that rule is, why Friedman argued such a rule might have lessened the impact of the Great Depression and the inflation of the 1970’s. What are the critical assumptions that Friedman must use to make the QTM work? Based on Friedman’s stated criteria, is it possible to provide a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT