Question

In: Computer Science

Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

  1. Copy the following java codes and compile

//// HighArray.java

//// HighArrayApp.java

Study carefully the design and implementation HighArray class and note the attributes and its methods.

  

  1. Create findAll method which uses linear search algorithm to return all number of occurrences of specified element.

/**
* find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit.
*
* @param foundElement   Element to be found
*/
int findAll(int foundElement)

  1. Modify the HighArray class to create a deleteAll method, which deletes a specified element from the array (if it exists, else return 0). Moving subsequent elements down one place in the array to fill the space vacated by the deleted element. The method should delete all the element occurrences.

    /**
    * Delete all element occurrences from array if found otherwise do nothing.
    *
    * @param deletedElement   Element to delete
    */
    int deleteAll(int deletedElement)
  1. Modify HighArray class to create the following methods

long max()

Returns the maximum value in a

int maxIndex()

Returns the index of the maximum value

long min()

Returns the minimum value in a

int minIndex()

Returns the index of the minimum value

long range()

Calculates the range (max-min)

long sum()

Calculates the sum of elements of a

double avg()

Calculates the average of a

double std()

Calculates the standard deviation of a

long rank(int i)

Return the ith largest element of a

boolean checkOrdered()

Returns true if a is ordered otherwise returns false

boolean checkUnique()

Returns true if a has unique elements

void removeDuplicates()

Removes duplicates from a

void fillArrayRandom()

Fills a with Random numbers

  

Solutions

Expert Solution

import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;

public class HighArray {
   //private data member, an array of type long
   private long array[];
  
   /*
   * constructor
   * takes an array of type long
   * */
   public HighArray(long[] array){
       this.array = array;
   }
  
   /*
   * returns biggest (by value) element in array
   * */
   public long max(){
       long max = 0;
       for(int i = 0; i < this.array.length; i++)
       {
           max = Math.max(max,array[i]);
       }
      
       return max;
   }
  
   //returns the index of biggest element
   public int maxIndex()
   {
       long max = Long.MIN_VALUE;
       int index = 0;
       for(int i = 0; i < this.array.length; i++){
           if(Math.max(max, array[i]) > max){
               max = Math.max(max, array[i]);
               index = i;
           }
       }
      
       return index;
   }
  
   //returns the smallest element by value
   public long min(){
       long min = Long.MAX_VALUE;
       for(int i = 0; i < this.array.length; i++)
       {
           min = Math.min(min, this.array[i]);
       }
       return min;
   }
  
   //returns the index of smallest element
   public int minIndex(){
       long min = Long.MAX_VALUE;
       int Index = 0;
       for(int i = 0; i < this.array.length; i++)
       {
           if(Math.min(min, this.array[i]) < min){
               min = Math.min(min, this.array[i]);
               Index = i;
           }
       }
       return Index;
   }
  
   //returns range
   public long range()
   {
       return this.max() - this.min();
   }
  
   //returns the sum of all elements
   public long sum()
   {  
       long sum = 0;
       for(int i = 0; i < this.array.length; i++)
       {
           sum += this.array[i];
       }
      
       return sum;
   }
  
   //returns the average of all elements
   public double average()
   {
       return (this.sum() / this.array.length);
   }
   //returns standard deviation
   public double std()
   {   //standard deviation is a value that indicates how scattered numbers are
       //generally the point of reference is the mean, ie average
       double cstd = 0; //store the standard deviation
       double mean = this.average();
      
       //first substract all elements by the average(mean) of array
       for(int i = 0; i < this.array.length; i++){
           cstd += this.array[i] - mean;
           //squaring values will turn negative values into positive
           //this is essentially the distance from mean, ie how far from mean
          
           /*squaring eleminates the possibility of having zero standard deviation
           even when numbers are scattered
           */
           cstd = cstd*cstd;
       }
      
      
       cstd = cstd/(this.array.length - 1);
       //dividing with length of array minus one
       cstd = Math.sqrt(cstd);
       //square root
      
       return cstd;
   }
  
   //return's kth largest element
   public long rank(int i)
   {   //copy elements of array and sort
       long sortedA[] = Arrays.copyOf(this.array, this.array.length);
       Arrays.sort(sortedA);
       if(i <= array.length)
           return sortedA[i - 1];
       else return 0;
   }
  
   //checks the order in ascending
   public boolean checkOrdered(){
       long sortedA[] = Arrays.copyOf(this.array, this.array.length);
       Arrays.sort(sortedA);
       for(int i = 0; i < array.length; i++)
       {
           if(this.array[i] != sortedA[i])
               return false;
       }
      
       return true;
   }
  
   //check for duplicates
   public boolean checkUnique(){
       for(int i = 0; i < this.array.length; i++)
       {
           for(int j = 0; j < this.array.length; j++)
           {
               if(this.array[i] == this.array[j] && i != j)
                   return false;
           }
       }
      
       return true;
   }
  
   /*
   * checks unique with a linear time complexity
   public boolean checkUnique(){
       HashMap<Long,Long> map = new HashMap<Long,Long>();
       for(long i : this.array)
       {
           if(map.containsKey(i))
               return false;
           map.put(i, i);
       }
      
       return true;
   }
   */
  
   //removes duplicates
   public void removeDuplicates()
   {   int duplicates = 0;
  
       for(int i = 0; i < this.array.length - duplicates; i++){
           for(int j = 0; j < this.array.length - duplicates; j++){
               if(this.array[i] == this.array[j] && i != j)
               {  
                   duplicates++;
                   for(int k = j; k < this.array.length - duplicates; k++)
                   {
                       this.array[k] = this.array[k + 1];
                   }
                   this.array[this.array.length - duplicates] = 0;
               }
           }
       }
  
       long array[] = new long[this.array.length - duplicates];
      
       for(int i = 0; i < (this.array.length - duplicates); i++)
       {
           array[i] = this.array[i];
       }
      
       this.array = array;
   }
  
   public void PrintAll()
   {
       for(int i = 0; i < this.array.length; i++)
           System.out.print(" " + this.array[i]);
   }
  
   public void fillArrayRandom()
   {   Random rd = new Random();
       for(int i = 0; i < this.array.length; i++)
       {
           array[i] = rd.nextLong();
       }
   }
  
   public int findAll(long n)
   {
       int frequency = 0;
       for(long i : this.array){
       if(i == n)
           frequency++;
       }
      
       return frequency;
   }
  
   public int Delete(long n)
   {
       int frequency = this.findAll(n);
       long array[] = new long[this.array.length - frequency];
       for(int i = 0, j = 0; i < this.array.length; i++){
           if(this.array[i] != n)
               array[j++] = this.array[i];
       }
       this.array = array;
       return frequency;
   }
}


Related Solutions

[JAVA] Please make sure there are FIVE (5) different source codes. Do not copy your answers...
[JAVA] Please make sure there are FIVE (5) different source codes. Do not copy your answers or someone’s else answers from different Cheggs. I did not post this question to see the same answers. All information that was given is included. Thank you. I will upvote if you do it properly. Task 1: Add three methods: union, intersection, and difference to the interface BagInterface for the ADT bag. Task 2: Implement the three methods, union, intersection, and difference, for the...
Fundamentals of Programming USING JAVA Please copy here your source codes, including your input and output...
Fundamentals of Programming USING JAVA Please copy here your source codes, including your input and output screenshots. Please upload this document along with your source files (i.e., the .java files) on Blackboard by the due date. 1. (Display three messages) Write a program that displays Welcome to Java, Welcome to Computer Science, and Programming is fun. 2. (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the result. One foot...
Write the MIPS assembly codes to implement the following function: copy a block of words from...
Write the MIPS assembly codes to implement the following function: copy a block of words from one address to another. Assume that the starting address of the source block be in register $t1 and that the destination address be in $t2. The instruction also requires that the number of words to copy in $t3 (which is >0, that means how many words are needed to copy). Furthermore, assume that the values of these registers as well as register $t4 can...
Need R codes to compile and plots , no hand written. Step-1: Type the data in...
Need R codes to compile and plots , no hand written. Step-1: Type the data in R Step-2: Perform Least-Squares regression Step-3: Make a normal Probability Plot using rstudent residuals Step-4: Plotting residuals versus predicted response yhat Step-5: Plotting Residuals versus each regressor. Step-6: Partial regression plots of residuals vs. regressors Step-7: Partial regression plots of residuals vs. regressors data:  (p.555 y, x1 and x5). y <- c(271.8, 264,238.8,230.7,251.6,257.9,263.9,266.5,229.1,239.3,258, 257.6,267.3,267,259.6,240.4,227.2,196,278.7,272.3,267.4,254.5,224.7, 181.5,227.5,253.6,263,265.8,263.8) x1 <- c(783.35, 748.45,684.45,827.8,860.45,875.15,909.45,905.55,756,769.35,793.5,801.65,819.65,808.55,774.95,711.85,694.85,638.1,774.55,757.9,753.35,704.7, 666.8,568.55,653.1,704.05,709.6,726.9,697.15) x5 <- c(13.2, 14.11,15.68,10.53,11,11.31,11.96,12.58,10.66,10.85,11.41,11.91,12.85,13.58,14.21,15.56,15.83,16.41,13.1,13.63,14.51,15.38, 16.1,16.73,10.58,11.28,11.91,12.65,14.06)
Complete the attached program by adding the following: a) add the Java codes to complete the...
Complete the attached program by adding the following: a) add the Java codes to complete the constructors for Student class b) add the Java code to complete the findClassification() method c) create an object of Student and print out its info in main() method of StudentInfo class. * * @author * @CS206 HM#2 * @Description: * */ public class StudentInfo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application...
In JAVA, What is the difference between a shallow copy and a deep copy? Include a...
In JAVA, What is the difference between a shallow copy and a deep copy? Include a simple illustration of the difference.
. The following data ($B) are for the nation of Syldavia. Study the data carefully. Export...
. The following data ($B) are for the nation of Syldavia. Study the data carefully. Export of goods 110 Imports of services 60 Exports of services 35 Domestic purchases of foreign assets 5 Imports of goods 145 Receipts of income earned from foreign assets 10 Payments made for income from domestic assets owned by foreigners 12 Remittances received from overseas workers 30 Foreign purchases of domestic assets 15 Inward FDI 30 Outbound FDI 5 Statistical discrepancy 1 Net addition/subtraction to...
Do the assignment in R/RStudio. Copy or compile your results into Word, write any explanations and...
Do the assignment in R/RStudio. Copy or compile your results into Word, write any explanations and turn in the hard copy. You conduct a survey of New Yorkers. The survey uses a stratified sample of 100 employed college graduates and 100 employed high school graduates. Your survey concludes that 70% of college graduates are happy with their job compared to only 60% of high school graduates. a. What is the standard error and margin of error of the estimate for...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.    Below the main, write the function...
//Using Java language Write a copy instructor for the following class. Be as efficient as possible....
//Using Java language Write a copy instructor for the following class. Be as efficient as possible. import java.util.Random; class Saw {    private int x;    private Integer p; //------------------------------------------ public Saw() { Random r = new Random();        x = r.nextInt();        p = new Integer(r.nextInt()); } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT