Java Main topics: Arrays ArrayLists Exercise This week we will be practicing wri
ID: 3724219 • Letter: J
Question
Java
Main topics:
Arrays
ArrayLists
Exercise
This week we will be practicing writing a portion of a class, that emulates some of the features of a Java ArrayList. You will be writing five of the methods from this week’s homework (Assignment 5).
Getting Started
To start this exercise, you should:
1. Open eclipse and start a new Java project named Lab05
2. Add a Class (named DynArray) to this project, and copy the contents of the DynArray.java file provided into it.
3. Add a Class (named DynArrayDriver) to this project, the contents of which you will be writting from scratch.
Requirements
DynArray.java
A very simple class which models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such:
1. Write the method body for the default constructor
2. Write the method body for the method arraySize()
3. Write the method body for the method elements()
4. Write the method body for the method grow()
5. Write the method body for the method shrink()
DynArrayDriver.java
A simple driver class to test your DynArray class, you must write this class from scratch
--------------------------------------------------------------------------
DynArray Class
public class DynArray
{
private double[] array;
private int size;
private int nextIndex;
public int arraySize()
{
}
public int elements()
{
}
public DynArray()
{
}
private void grow()
{
}
private void shrink()
{
}
}
Explanation / Answer
ScreenShot:-
-------------------------------------------------------------------------------------------------------------------------------
Program:-
DriverClass
/** Driver class **/
class DynArrayDriver
{
//main method to call functions
public static void main (String[] args) throws java.lang.Exception
{
//Scan the user input or choice
Scanner scan = new Scanner(System.in);
DynamicArray dArray = new DynamicArray(); //object creation
char ch;
/* Perform Dynamic Array operations according to use choice */
do
{
//menu for user to choose
System.out.println(" Dynamic Array Operations ");
System.out.println("1. Find size ");
System.out.println("2. Display Elements");
System.out.println("3. Grow array");
System.out.println("4. Shrink array");
//User choice
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println(" Size = "+ dArray.arraySize() );
break;
case 2 :
dArray.elements();
break;
case 3 :
System.out.println("Enter value to insert");
dArray.grow(scan.next() );
dArray.elements();
break;
case 4 :
System.out.println("Enter value");
dArray.shrink(scan.next() );
dArray.elements();
break;
default :
System.out.println("Wrong Entry ");
break;
}
//ask user to continue or not their operations
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Dynamic Array Class:-
class DynamicArray
{
private ArrayList<String> al;
/** constructor **/
public DynamicArray()
{
al = new ArrayList<String>();
}
/** Array size **/
public int arraySize()
{
return al.size();
}
/** Array expansion**/
public void grow(String key)
{
al.add(key);
}
/** Array shrinking **/
public void shrink(String key)
{
al.remove(key);
}
/** Display elements in the Array **/
public void elements()
{
System.out.println(" Dynamic Array : "+ al);
System.out.println();
}
}
----------------------------------------------------------------------------------------------------------------------------------
note:-
I assume you need 5 methods incuding constructor(methods-array growth,array shrink,array size and array display).
If more than anything apart from this method,let me know
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.