*** JAVA ASSIGNMENT *** PLEASE HELP CODE AND EXPLAIN THE STEPS ALONG THE WAY. WI
ID: 3742437 • Letter: #
Question
*** JAVA ASSIGNMENT ***
PLEASE HELP CODE AND EXPLAIN THE STEPS ALONG THE WAY.
WILL GIVE GOOD RATING
For this assignment we need to write our own array list class. We will call it "BackwardArrayList". This class will be different than a normal array list in these ways: The last box in the array will be the beginning of the list and the first box (0) will be the end of the list.
Your class must have the following methods:
theSize - this method receives nothing and returns an integer value of the number of integers that are in the list. (only returns how many positions are used)
addSomething - this method can receive only an integer value in which case the value will be added to the beginning of the list. Or it can receive a integer position and a integer value where the value will be placed in the position given. In either case it will return a Boolean value indicating if the value was added or not. (This adds a value one at a time) (If the user requests a place in the list then the rest of the numbers must shift)
removeSomething - this method will receive only an integer value for the position of value to be removed. It will return a Boolean value indicating if the value was removed or not. (shorten the list not the array)
findSomething - this method will receive the integer value you are searching for and will return the position of the first instance of that value found in the list. (Starts back to front)(Inverse translation)
makeBigger - this method will make the current array being used for the array list one and a half times the current size of the current array. i.e. if the current array is 10 it will make it 15. It will return a Boolean value indicating the task was done successfully. (this method will be called when the current array only has one position left)
(know how bif the array is, as the decimal will need to truncate)
(Once the array is full it will automatically make more boxes whether they will be used or not.)
+ Make a test driver to test all methods
+ Run from CMD line
+ Make sure you have exceptios
+ DONT USE JAVE LIST INTERFACE
+ DONT USE ANY ADDITIONAL METHODS
Upon completion of the "backwardArrayList" class you will also need to write a driver that test all these methods for all common cases that could occur.
You will need to upload both the "backwardArrayList" class and the driver that you used to test it.
Explanation / Answer
class BackwardArrayList{
//Use this array to store data
int[] arrList;
int index=10;
private int count=0;
//If size is not defined intial size is 10
public BackwardArrayList(){
arrList=new int[10];
}
//Pass the size as arguement
public BackwardArrayList(int size){
arrList=new int[size];
index=size;
}
//returns number of integer in list
public int theSize(){
return count;
}
public boolean addSomething (int data){
//If more than 2 space is available add the element
if(index>1){
count++;
arrList[index-1]=data;
index--;
return true;
}
//else make the container bigger
else
makeBigger();
return false;
}
public boolean removeSomething(int loc){
if(loc>=arrList.length){
throw new IllegalStateException();
//return false;
}
else
{
arrList[arrList.length-loc]=0;
return true;
}
}
//method find the index of data
//If data is not present return -1
public int findSomething(int data){
int loc=-1;
for(int i=arrList.length-1;i>=0;i--){
if(arrList[i]==data){
loc=(arrList.length-i);
break;
}
}
if(loc<0)
throw new IllegalStateException();
return loc;
}
//This method increases the container size
public void makeBigger(){
//Make it larger
int size=arrList.length*3/2;
int[] arr=new int[size];
//Copyu the value
for(int i=arrList.length-1;i>=0;i--){
arr[size-1]=arrList[i];
size--;
}
//Set the new index
index=size+1;
//make arrList to arr
arrList=arr;
}
}
//Driver class to test
public class MyClass {
public static void main(String args[]) {
BackwardArrayList a=new BackwardArrayList();
//Add the value and so the item inserted
for(int i=0;i<10;i++){
//When one item space is remaining then it will not insert
//In that case it will increase the size
System.out.println(a.addSomething(i));
System.out.println(a.theSize());
}
//Here size increased from 10 to 15 hence successfull add
System.out.println(a.addSomething(200));
System.out.println(a.theSize());
//Find element
System.out.println(" 200 Is present At Index: "+a.findSomething(200));
System.out.println(" Remove data at index 10: "+a.removeSomething(10));
// data is not present then throw Exception
System.out.println(" 200 Is present At Index: "+a.findSomething(200));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.