Hey, I am currently looking for the best solution to this assignment: Please let
ID: 3903419 • Letter: H
Question
Hey,
I am currently looking for the best solution to this assignment:
Please let me know if you have any questions. I will need this by 5:00 US Pacific Time 6/11/2018.
Thank you
General Requirements .All files you are turning in must be signed. Please add your name to the source code in comments. Unsigned work automatically loses 5 pts Add comments to the source code you are writing . o Describe the purpose of every variable o Explain the algorithm you are using for solution o Add proper comments for all methods. Include @param, @return, and @throws tags Uncommented work loses up to 25% of the grade. Archive the entire project using ZIP or RAR utility. Turn it in into the digital drop box. Dynamic Array of Integers Class Create a class named DynamicArray that will have convenient functionality similar to JavaScript's Array object and Java's ArrayList class. The class allows to store array of integers that can grow and shrink as needed, search and sort itself, add and remove elements You are not allowed to use ArrayList object as well as any methods from java.util.Arrays class. Please see the list of required features and methods below . private int array[] field. You MUST store the data internally in a regular partially-filled array of integers. Please DO NOT USE ArrayList. The size 2. private int size field. This variable stores the number of "occupied 3. Constructor with parameter. The parameter defines the capacity of initial array 4. No-argument constructor. Allocates array of size 10, sets size field to 0 of the allocated array is its capacity and will be discussed below elements in the array. Set to 0 in the constructor Allocates array of given capacity, sets size field to 0. In case the parameter given to constructor is less than 0, IllegalArgumentException is being thrown. 5. Copy constructor. The constructor takes an object of type DynamibcArray as a parameter and copies it into the object it creates. The constructor throws IllegalArgumentException if the object that was passed to copy from is null. 6. int getSize () returns the size a number of occupied elements in the array 7. int [] toArray ) accessor returns the array. Make sure you DO NOT return the private array field. Instead, allocate memory for the new array, copy your array field into that new object, and return the new arrayExplanation / Answer
Implemented Source Code:-
---------------------------------
package com.ibm;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class ArrayDemo
{
private int Array[];
private int size;
public ArrayDemo()
{
this.size=6;
}
public ArrayDemo(int size)
{
this.size=size;//Saves the Maximum Capacity of array
Array=new int[size];//Allocate The Dynamic memory for Array
}
int getSize()
{
return size;//Return the Array Size
}
int [] toArray()
{
int NewArray[]=new int[Array.length];//Copying Array to New Array
NewArray=Array;
NewArray[0]++;
return NewArray;
}
public void push(int num)
{
size++;//first Increase the Size
Array[size]=num;//Add the Element in New Position
}
int get(int index)throws IndexOutOfBoundsException
{
return Array[index];//Return Element of Index
}
int Indexof(int key)
{
for(int i=0;i<size;i++)
{
if(key==Array[i])//Check if key is Matching or Not
{
return i; //Return index
}
}
return -1;//Key Not Found
}
void add(int index,int num) throws IndexOutOfBoundsException
{
Array[index]=num;
}
int remove(int index)throws IndexOutOfBoundsException
{
for(int i=index;i<Array.length;i++)
{
Array[i]=Array[i+1];//OverWrite the Last Position of Array
}
size--;
return Array[index];
}
void Sort()
{
int temp;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(Array[i]<Array[j])//Exchange the Data Elements Applying Bubble Sort
{
temp=Array[i];
Array[i]=Array[j];
Array[j]=temp;
}
}
}
}
void Shuffle()
{
Random r = new Random();//To Shuffle the Array Elements Declare Random Class
for (int i=Array.length-1;i>0;i--)
{
int j = r.nextInt(i);
int temp = Array[i];
Array[i] = Array[j];
Array[j] = temp;
}
System.out.println(Arrays.toString(Array));
}
int FinMin() throws RuntimeException
{
int min=0;
for(int i=0;i<Array.length;i++)
{
if(min<Array[i])
{
min=Array[i];
}
}
return min;
}
int FinMax() throws RuntimeException
{
int max=0;
for(int i=0;i<Array.length;i++)
{
if(max<Array[i])
{
max=Array[i];
}
}
return max;
}
public String toString()
{
return Array.toString();
}
boolean isEmpty()
{
if(Array.length==0)
return true;
else
return false;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
ArrayDemo obj=new ArrayDemo(6);
System.out.println("The Size of Array is:"+obj.getSize());
obj.add(0,12);
obj.add(1,34);
obj.add(2,55);
//obj.push(88);
obj.add(3,77);
//obj.Array[4]=34;
obj.add(4,67);
System.out.println(" The 77 is Added at the 3 Location");
System.out.println(" The Index of 55 is"+obj.get(55));
System.out.println(" The first Occurence of 34 is at :"+obj.Indexof(34)+" Location");
System.out.println(" The Maximum element in the Array is:"+obj.FinMax());
System.out.println(" The Minimum element in the Array is:"+obj.FinMin());
obj.Sort();
System.out.println(" After Performing Bubble Sort"+obj.toString());
System.out.println(" Shuffling the Elements ");
obj.Shuffle();
sc.close();
}
}
Sample Output:-
-------------------
The Size of Array is:6
The 77 is Added at the 3 Location
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 55
at com.ibm.ArrayDemo.get(ArrayDemo.java:36)
at com.ibm.ArrayDemo.main(ArrayDemo.java:139)