Question

Write a Java class called FlexArray that has/does the following: Has a private variable array that...

Write a Java class called FlexArray that has/does the following:

Has a private variable array that holds integers as int[] array. Has a private variable capacity that is the maximum number of integers the array can hold. Has a private variable size that shows the number of currently occupied locations. Obviously, capacity >= size. Capacity tells you how many values the array can hold, not how many it has currently, which is available via the variable size.

Has a default constructor that sets capacity=10 and allocates array to new int[capacity]. Then sets size=0 because there is currently no value stored in the array yet.

Has a non-default constructor having parameter (int maxEntries) that sets up array to hold maxEntries number of integers, sets capacity=maxEntries and size=0. Make sure maxEntries is >0.

This array has the property that only consecutive entries starting from location 0 can contain integer values. In other words, the array contains values in locations 0 through size-1, which is equal to a total of size number of values.

A method called public void add(int val) adds the integer val at location size of the array as long as size<capacity. If size is equal to capacity, there should be an error message saying, Array is full, values cannot be added. add(..) method when successful will also increase the size by 1.

A method called public int set(int val, int location) that sets the value at location of array to val, returning the old value to the caller. (the old value is replaced with the new value val). The method should throw an error, if location >=size or location <0, meaning that you can only modify existing values in the array to new values.

A method called public int get(int location) that returns the value stored at location of the array. The method should throw an error if location >=size or location <0, meaning that you can only modify existing values in the array to new values.

Homework Answers

Answer #1

//Java code

public class FlexArray {
    private int[] array;
    private int capacity;
    private int size;

    //default constructor
    public FlexArray()
    {
        capacity =10;
        array = new int[capacity];
        size=0;
    }
    //non-default constructor
    public FlexArray(int maxEntries)
    {
        if(maxEntries>0)
        capacity = maxEntries;
        array = new int[capacity];
        size =0;
    }
    public void add(int val)
    {
        if(size<capacity)
        {
            array[size] = val;
            size++;
        }
        else
        {
            System.err.println("Array is full");
        }
    }
    public int set(int val, int location)
    {
        int oldvalue=-1;
        if(location>=size || location<0)
            System.err.println("Wrong location");
        else
        {
            oldvalue = array[location];
            array[location]= val;
        }
        return oldvalue;
    }

    @Override
    public String toString() {
        String result ="";
        for (int i=0;i<size;i++)
        {
            result+= array[i]+" ";
        }
        return result;
    }
}

//====================================

public class FlexArrayDemo {

    public static void main(String[] args)
    {
        FlexArray flexArray = new FlexArray(5);
        flexArray.add(56);
        flexArray.add(33);
        flexArray.add(78);
        flexArray.add(12);
        flexArray.add(13);
        System.out.println(flexArray);
        flexArray.set(77,2);
        System.out.println("After set new value at index 2: \n"+flexArray);

    }
}

//Output

//If you need any help regarding this solution .......... please leave a comment ........ thanks

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method that constructs a new arrayDeque called "public ArrayDeque()" Q2: write a method called "public void addFirst(T data)" that adds an element...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method called "public void addLast(T data)" which adds an element to the back of a Deque. If sufficient space is not available...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
Code a valid copy constructor for the GrabBag class below: class GrabBag { private: int* values;...
Code a valid copy constructor for the GrabBag class below: class GrabBag { private: int* values; int size; public: GrabBag(); }; #include "GrabBag.h" GrabBag::GrabBag() { size = rand() % 100 + 1;   values = new int[size]; //random array size   for (int i = 0; i < size; i++)   values[i] = rand()% 10 + 1 ;// random array contents   }   
[Multiple correct answers] Write a line of Java code to create an array of integers called...
[Multiple correct answers] Write a line of Java code to create an array of integers called studentIDs that can hold 1200 values.    int [] studentIDs = new int[1200];     int studentIDs [] = new int[1200];     int studentIDs = new int[1200];     int [] studentIDs = new int(1200);     int studentIDs [] = new int(1200);     int [] studentIDs = int[1200];     int studentIDs [] = int[1200];
Write a class called Item that has a string field called name and a double field...
Write a class called Item that has a string field called name and a double field called price and an integer field called quantity. In main, create a bag of type Item and place several item objects in the bag. Then in main calculate the total price of items purchased. You may remove each item and add the price to a total variable in a loop. The loop should end when the bag is empty. Using bag algorithms public final...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...