In: Computer Science
//// HighArray.java
//// HighArrayApp.java
Study carefully the design and implementation HighArray class and note the attributes and its methods.
/**
* 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)
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 |
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;
}
}