Question

In: Computer Science

Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public...

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();

}

Solutions

Expert Solution

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.


Related Solutions

4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
Submit your complete implementation of our abstract list type using an array, ArrayList.java. public class ArrayList...
Submit your complete implementation of our abstract list type using an array, ArrayList.java. public class ArrayList { public static void main(String[] args) { ArrayList list = new ArrayList(); } private String[]a = new String[1000]; private int end = -1; // Throw an IndexOutOfBoundsException if the index is invalid public String get(int index); // Return the first index in the list with the given value, or -1 if it's not found public int find(String val); // Return true if the element...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
#1 a. When to use an Interface vs when to use an abstract class. For each...
#1 a. When to use an Interface vs when to use an abstract class. For each “when” provide extended example(s) (with class/interface codes). b. Suppose you have an interface Moveable. Think of some interface that can extend it. Implement this two interfaces. (java oop)-> laboratory work
C++ Programming. Create a class hierarchy to be used in a university setting. The classes are...
C++ Programming. Create a class hierarchy to be used in a university setting. The classes are as follows: The base class isPerson. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The classshould also have a static data member called nextID which is used to assignanID numbertoeach object created(personID). All data members must be private. Create the following member functions: Accessor functions to allow access to first name and last name (updates and retrieval). These functions should...
Skills needed to complete this assignment: dynamic arrays, classes. PROMPT: In mathematics, a polynomial is an...
Skills needed to complete this assignment: dynamic arrays, classes. PROMPT: In mathematics, a polynomial is an expression consisting of cariables and coefficients which involves only the operation of addition, subtraction, multiplication, and non-negative integer exponents of variables. A polynomial in a single variable can always be written in the form: anxn + an-1xn-1+ ....... + a2x2 + a1x + a0 Where a0 ....., an are coefficients and x is the variable. In this assignment, you will complete a polynomial class...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT