In: Computer Science
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class.
public class DArray {
private int array[];
public DArray() {
}
private void expandArray() {
}
private void shrinkArray() {
}
}
---------------------------------------------------------------
public abstract class ArrayBP {
protected int numElements;
protected int numAllocations;
public abstract void storeAt(int item, int index) {
}
public abstract getFrom(int index) {
}
public abstract int len() {
}
public abstract void remove();{
}
public abstract void removeAt(int index) {
}
}
------------------------------------------------------------------
public interface DABehavior {
public void append(int item) {
}
public int getFirstItem();
public int getLastItem();
}
Following java program demonstrates usage of concrete class, abstract class and interface.
Following application creates dynamic array and provides following actions to perform :
1. append
2. get First Item
3. get Last Item
4. store At
5. get From
6. len
7. remove
8. removeAt
9. print array
0. exit
----------------------
Source code :
-----------------------
ArrayBP.java
-----------------------
public abstract class ArrayBP {
protected int numElements;
protected int numAllocations;
public abstract void storeAt(int item, int index);
public abstract int getFrom(int index);
public abstract int len();
public abstract void remove();
public abstract void removeAt(int index);
}
-----------------------
-----------------------
DABehavior.java
-----------------------
public interface DABehavior {
public void append(int item);
public int getFirstItem();
public int getLastItem();
}
-----------------------
-----------------------
DArray.java
-----------------------
import java.io.IOException;
import java.util.Arrays;
public class DArray extends ArrayBP implements DABehavior{
private int array[];
//Create empty array with 0 elements
public DArray() {
array = new int[0];
}
//Creates array with n number of elements
public DArray(int numElements) {
array = new int[numElements];
}
//Expand array
private void expandArray() {
//copying older array into bigger
int[] biggerArray = Arrays.copyOf(array, array.length+1);
array = null;
//assigning older array back with new length
array = new int[biggerArray.length];
//Copying all elements to original array
for(int i=0;i<biggerArray.length;i++){
array[i] = biggerArray[i];
}
biggerArray = null;
}
//Shrink array
private void shrinkArray() {
//copying older array into smaller
int[] smallerArray = Arrays.copyOf(array, array.length-1);
array = null;
//assigning older array back with new length
array = new int[smallerArray.length];
//Copying all elements to original array
for(int i=0;i<smallerArray.length;i++){
array[i] = smallerArray[i];
}
smallerArray = null;
}
//Append item to end of array
public void append(int item){
//Expanding before appending
expandArray();
array[array.length-1] = item;
}
//Get first item from array
public int getFirstItem(){
int result=0;
try {
if(array!=null && array.length>0){
result = array[0];
}else{
throw new IOException("array is empty!");
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//get last element
public int getLastItem(){
int result=0;
try {
if(array!=null && array.length>0){
result = array[array.length-1];
}else{
throw new IOException("array is empty!");
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//store element at index
public void storeAt(int item, int index){
try {
if(array!=null && array.length>index){
array[index] = item;
}else{
throw new IOException("array out of index!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
//get element from index
public int getFrom(int index){
int result=0;
try {
if(array!=null && array.length>index){
result = array[index];
}else{
throw new IOException("array out of index!");
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//get length of array
public int len(){
return array.length;
}
//remove element from last
public void remove(){
shrinkArray();
}
//remove element from index
public void removeAt(int index){
try {
if(array!=null && array.length>index){
for(int i=index;i<array.length-1;i++){
array[i] = array[i+1];
}
shrinkArray();
}else{
throw new IOException("array out of index!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
//print array
void print(){
System.out.print("Elements in array : ");
for(int i=0;i<array.length;i++){
System.out.print(array[i] + " ");
}
}
}
-----------------------
-----------------------
DriverClass.java
-----------------------
import java.util.Scanner;
//driver class
public class DriverClass {
public static void main(String[] args){
//to take menu choice from user
int choice;
//create array with n elements
int numElements=0;
Scanner sc = new Scanner(System.in);
//prompt user to enter array size
System.out.print("Enter the array initial size :");
numElements = sc.nextInt();
//creating array with numElements
DArray array = new DArray(numElements);
int item;
int index;
while(true){
System.out.println("\n------------------Menu------------------");
System.out.println("1. append");
System.out.println("2. get First Item");
System.out.println("3. get Last Item");
System.out.println("4. store At");
System.out.println("5. get From");
System.out.println("6. len");
System.out.println("7. remove");
System.out.println("8. removeAt");
System.out.println("9. print array");
System.out.println("0. exit");
System.out.print("Enter choice (0-8) :");
choice = sc.nextInt();
switch(choice){
case 1:
System.out.print("Enter element to append :");
item = sc.nextInt();
array.append(item);
break;
case 2:
item = array.getFirstItem();
System.out.print("First Item in array :" + item);
break;
case 3:
item = array.getLastItem();
System.out.print("Last Item in array :" + item);
break;
case 4:
System.out.print("Enter element to store :");
item = sc.nextInt();
System.out.print("Enter index to store at :");
index = sc.nextInt();
array.storeAt(item, index);
break;
case 5:
System.out.print("Enter index to get element from : ");
index = sc.nextInt();
item = array.getFrom(index);
System.out.print("Item at index " + index + " is " +
item);
break;
case 6:
System.out.print("Length of array :" + array.len());
break;
case 7:
System.out.print("Remove last element from array");
array.remove();
break;
case 8:
System.out.print("Enter index to remove element from : ");
index = sc.nextInt();
System.out.print("Remove last element from inded " + index);
array.removeAt(index);
break;
case 9:
array.print();
break;
case 0:System.out.print("Exiting!");
return;
default : System.out.print("Invalid choice!");
}
}
}
}
-----------------------
-----------------------
sample output
-----------------------
-----------------------
Let me know, if you face any issue.