Note:
import java.util.Scanner;
class EnhancedArray{
public int[] array = null; // An array that is not
initialized has a null value.
public int size;
/* Define your methods here */
private void updateSize(){
if(array!=null) { // This checks if the array is
already initialized or not.
this.size = array.length;
}else {
this.size = 0;
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
/* Test your code here. */
}
}
EnhancedArray.java
import java.util.Scanner;
public class EnhancedArray {
public int[] array = null; // An array that is not
initialized has a null value.
public int size;
/* Define your methods here */
//default constructor
public EnhancedArray() {
// initialize an array of 0
size
array = new int[0];
}
//parameterized constructor
public EnhancedArray(int[] arr) {
this.array = arr;
updateSize();
}
// Method that updates the size of the array
private void updateSize() {
if(array != null) { // This checks
if the array is already initialized or not.
this.size =
array.length;
} else {
this.size =
0;
}
}
// Method that correctly receives an array of a
certain size and update the internal array.
public void setArray(int[] arr) {
//copy the array and update the
size of the array
this.array = arr;
updateSize();
}
// Method that correctly receives a comma separated
string and update the internal array.
public void setArray(String arr) {
//split the string based on
","
String[] items =
arr.split(",");
//initialize the array
this.array = new
int[items.length];
updateSize();
//store the items into the
array
for(int i = 0; i < items.length;
i++) {
array[i] =
Integer.parseInt(items[i]);
}
}
// Method that correctly creates a copy of the
array and returns that copy.
public int[] getArray() {
//return the copy of the
array
int[] arr = this.array;
return arr;
}
// Method that can correctly add its input
parameter at the end of the array.
public void addItem(int item) {
//create an new array with size 1
greater than the original array
int arr[] = new int[this.size +
1];
//copy the array
for(int i = 0; i < size; i++)
{
arr[i] =
this.array[i];
}
//add the item at the end of the
array
arr[arr.length - 1] = item;
//replace the array and update its
size
this.array = arr;
updateSize();
}
// Method that correctly inserts its first input
parameter at the index indicated in its second input parameter, and
then shift the rest of elements already in the array to right
public void addItem(int item, int index) {
//display error message if index is
out of bounds
if(index > size-1)
System.out.println("Index is out of bounds.");
else
{
//create an new
array with size 1 greater than the original array
int arr[] = new
int[this.size + 1];
//copy the items
before the index specified
for(int i = 0; i
< index; i++) {
arr[i] = this.array[i];
}
//add the item
at the specified index
arr[index] =
item;
//move the rest
of the items to the right
for(int i =
index; i < size; i++) {
arr[i + 1] = array[i];
}
//replace the
array and update the size
this.array =
arr;
updateSize();
}
}
// Method that correctly removes and returns the
item at the end of the array.
public int removeItem() {
//store the item that is
removed
int itemRemoved =
this.array[size-1];
//create a new array with size 1
less than the original array
int[] arr = new int[size -1];
//copy the items from the original
array leaving the last item
for(int i = 0; i < size-1; i++)
{
arr[i] = this.
array[i];
}
//replace the array and update the
size
this.array = arr;
updateSize();
return itemRemoved;
}
// Method that correctly removes and returns the
item at the index indicated by the input parameter, shifting the
rest of the elements to left.
public int removeItem(int index) {
int itemRemoved = -1;
//display error if index specified
is out of bounds
if(index > size-1)
System.out.println("Index is out of bounds.");
else
{
//store the item
that is removed
itemRemoved =
this.array[index];
//create a new
array with size 1 less than the original array
int[] arr = new
int[size -1];
//copy the
elements before the index elements
for(int i = 0; i
< index; i++) {
arr[i] = this. array[i];
}
//copy the rest
of the items leaving the item to be removed
for(int i =
index + 1; i < size; i++) {
arr[i - 1] = this. array[i];
}
//replace the
array and update the size
this.array =
arr;
updateSize();
}
return itemRemoved;
}
// Method that returns true if the array is
empty
public boolean isEmpty() {
if(this.size == 0)
return
true;
else
return
false;
}
// Method that prints the array to console
public void printArray() {
for(int i = 0; i < size; i++)
{
if(i == 0)
System.out.print("{" + this.array[i]);
else if(i ==
size -1)
System.out.print(", " + this.array[i] +
"}\n");
else
System.out.print(", " + this.array[i]);
}
}
}
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scnr = new
Scanner(System.in);
/* Test your code here. */
//create an object to the
EnhacedArray class
EnhancedArray myArray = new
EnhancedArray();
//set the elements in the array
using a string
myArray.setArray("1,2,3,4,5,6,7,8,9");
//print the elements in the
array
System.out.println("The items in
the array are: ");
myArray.printArray();
//get the items in the array
int[] copy =
myArray.getArray();
//create an object to the
EnhancedArray class using the array returned from getArray
EnhancedArray copyOfArray = new
EnhancedArray(copy);
//display the array
System.out.println("The items in
the copy of the array are: ");
copyOfArray.printArray();
//Read the item to added to the
array
System.out.print("\nEnter an item
to be added into the array: ");
int item = scnr.nextInt();
//add the item to the array
myArray.addItem(item);
//display the array
System.out.println("\nThe items in
the array after adding an item: ");
myArray.printArray();
//Read the item to added to the
array
System.out.print("\nEnter an item
to be added into the array: ");
item = scnr.nextInt();
//read the index where the item has
to be inserted
System.out.print("Enter the index
where the item is to be added: ");
int index = scnr.nextInt();
//add the item
myArray.addItem(item, index);
//display the array
System.out.println("\nThe items in
the array after adding an item: ");
myArray.printArray();
//remove an item from the array and
display the array
int itemRemoved =
myArray.removeItem();
System.out.println("\nRemoving an
item from the array...");
System.out.println("The item
removed is: " + itemRemoved);
System.out.println("\nThe items in
the array after removing an item: ");
myArray.printArray();
//read the index of the item to be
removed
System.out.print("\nEnter the index
of the item to be removed: ");
index = scnr.nextInt();
//remove the item and display the
array
itemRemoved =
myArray.removeItem(index);
System.out.println("The item
removed is: " + itemRemoved);
System.out.println("\nThe items in
the array after adding an item: ");
myArray.printArray();
if(myArray.isEmpty())
System.out.println("\nThe array is empty");
else
System.out.println("\nThe array is not empty");
scnr.close();
}
}
Output:
Program Screenshot:
Let me know if you have any concerns with the above solution.
+array:int[] This is provided to facilitate testing and grading. Use it as your internal array. +size:int...
1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...
Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int virtualArrayLength; // the number of elements in the dynamic array // Throws an IndexOutOfBoundsException if i is not a valid index // for adding to the dynamic array, otherwise inserts s at index i. // Elements can be added from index 0 to this.size(). public void add(int i, String s) { // If there is no room for s in data, create a new...
A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...
Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a new method called displayAll, that takes an ArrayList (of your base class) as a parameter, and doesn't return anything [25 pts] The displayAll method will loop through the ArrayList, and call the display (or toString) method on each object [25 pts] In the main method, create an ArrayList containing objects of both your base class and subclass. (You should have at least 4 objects) [25...
The last element in each array in a 2D array is incorrect. It’s your job to fix each array so that the value 0 is changed to include the correct value. In the first array, the final value should be the length of the first array. In the second array, the final value should be the sum of the first value, and the second to last value in the array. In the third array, the final value should be the...
Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft Visual Studio described here. As you work on the below project, demonstrate to the instructor the usage of this feature. Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting array of doubles described in the previous lab as a class. You should use this class definition. The class attributes are a pointer to the dynamically allocated array dAarray and the array size size This...
I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array. //Array implementation of stacks. import java.util.Arrays; public class ArrayStack implements Stack { //Declare a class constant called DEFAULT_STACK_SIZE with the value 10. private static final int DEFAULT_STACK_SIZE = 10; /* Declare two instance variables: 1. An integer called...
I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...
DESCRIPTION You have to design an e-commerce shopping cart. These require classes Item, Electronics, Food, Dress, Cart and Main. ITEM CLASS Your Item class should contain: Attributes (protected) String name - name of the Item double price - price of the item Methods (public) void setName(String n) - sets the name of Item void setPrice(double p) - sets the price of Item String getName() - retrieves name double getPrice() - retrieves price String formattedOutput() returns a string containing detail of...
In C++, create a class, called DynamicCharArray, which wraps around the standard char array and offers the following features: • Default constructor: initializes an internal array of 8 elements • Copy constructor: called implicitly when making a copy • int size(): returns the length of the array • void expand(int amount): increases the capacity of the array by the specified amount. It will need to create a new internal array and copy the elements over to accomplish this correctly, but...