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

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....
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...
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...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor with parameters, a copy constructor, a destructor, and an overloaded assignment operator (=). //main.cpp #include <iostream> using namespace std; #include "Party.h" int main() { return 0; } //party.h #ifndef PARTY_H #define PARTY_H class Party { private: string location; string *attendees; int maxAttendees; int numAttendees;    public: Party(); Party(string l, int num); //Constructor Party(/*parameters*/); //Copy constructor Party& operator=(/*parameters*/); //Add destructor void addAttendee(string name); void changeAttendeeAt(string...
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...
// Base class for game configuration public abstract class DataSource {     private Graph map;    ...
// Base class for game configuration public abstract class DataSource {     private Graph map;     private HashMap <String,Room> rooms;     private ArrayList <Entity> entities;     private Player player;     protected HashMap < String, List<String[]> > tables;     // constructor     public DataSource() {     }         // Connect to the data source. Override if source is a database     public void connect() {     };         // Load the configuration tables required to build the game world...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    *...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    * Gets the current number of entries in this set.    *    * @return The integer number of entries currently in the set.    */    public int getSize();    /**    * Sees whether this set is empty.    *    * @return True if the set is empty, or false if not.    */    public boolean isEmpty();    /**    *...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The MyDynamicArray class should manage the storage of an array that can grow and shrink. The public methods of your class should already be the following: MyDynamicArray(); Default Constructor. The array should be of capacity 2. MyDynamicArray(int s); For this constructor the array should be of capacity and size s. int& operator[](int i); Traditional [] operator. Should print a message if i is out of...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT