Explain the purpose of the program as detail as possible mention algorithms that
ID: 3750583 • Letter: E
Question
Explain the purpose of the program as detail as possible
mention algorithms that are used
List data structures that are used in solution.
Give a description of how to use the program and expected input/output
Explain the purpose of each class developed in the program.
Programming:
For each method, give the pre and post conditions and invariant, if any
Explain each component of the following code as if it were commented on every line:
ArrayList.java
==============
package com.sky.demo;
import java.util.Arrays;
public class ArrayList<T> {
private int length;
private Object[] arr;
// No Argument Constructor
public ArrayList() {
length=10;
arr = new Object[length];
}
// Parameterized Constructor
public ArrayList(int size) {
this.length = size;
arr = new Object[size];
}
// Method to add object at the end of ArrayList
public void add(T object) {
arr[length-1]=object;
}
// Method to add the value at given index
public void add(int index, T object) {
if(index<length)
arr[index] = object;
else
System.out.println("Entered position is out of range");
}
public T get(int index) throws ArrayIndexOutOfBoundsException{
return (T) arr[index];
}
//Method to get the size of ArrayList
public int size() {
return arr.length;
}
// Method to check ArrayList is empty or not
public boolean isEmpty(){
boolean empty = true;
for(int i=0; i<length;i++) {
if(!(arr[i]==null)) {
empty = false;
break;
}
}
return empty;
}
// Method to check an Object exist or not in the ArrayList
public boolean isIn(T obj){
boolean exist = false;
for(int i=0;i<length;i++) {
T obj1 = (T)arr[i];
if(obj1.toString().equals(obj.toString())) {
exist = true;
break;
}
}
return exist;
}
// Method to return position of first occurrence of an object in the ArrayList. If object does not exist in the Arraylist then it will return -1
public int find(T obj) {
int position = -1;
for(int i=0;i<length;i++) {
T obj1 = (T)arr[i];
if(obj1.toString().equals(obj.toString())) {
position=i;
break;
}
}
return position;
}
// Method to remove the first occurrence of the Object from ArrayList
public void remove(T obj) {
boolean exist = false;
for(int i=0;i<length;i++) {
T obj1 = (T)arr[i];
if(obj1.toString().equals(obj.toString())) {
exist = true;
arr[i]=null;
break;
}
}
if(exist==false)
System.out.println(obj+" not exist in the ArrayList");
}
@Override
public String toString() {
return Arrays.toString(arr);
}
}
TestArray.java with Default Size
============================
package com.sky.demo;
import java.util.Random;
public class TestArray {
public static void main(String[] args) {
//Creating Object with default size = 10
ArrayList arrayList = new ArrayList();
System.out.println("ArrayList Size = "+arrayList.size());
System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());
//Generating 10 random numbers and adding into the Arraylist
for(int i=0;i<arrayList.size();i++) {
Random random = new Random();
int randomNumber = random.nextInt(50)+1;
arrayList.add(i, randomNumber);
}
//Printing the ArrayList
System.out.println("Added 10 random numbers in the arrayList");
System.out.println("ArrayList : "+arrayList);
System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());
//Adding 200 in the last of ArrayList
arrayList.add(200);
System.out.println("ArrayList : "+arrayList);
// If you will enter the index > size java.lang.ArrayIndexOutOfBoundsException will be thrown
int element = (int) arrayList.get(9);
System.out.println("Element = "+element);
System.out.println("Value exist in the ArrayList = "+arrayList.isIn(200));
// Finding first occurrence of an Object
int pos = arrayList.find(200);
if(pos>=0)
System.out.println("Element exist at position : "+pos);
else
System.out.println("Element does not exist");
arrayList.remove(201);
System.out.println("Arraylist = "+arrayList);
}
}
TestArray.java with user input
=======================
package com.sky.demo;
import java.util.Random;
import java.util.Scanner;
public class TestArray {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the Size : ");
int len = in.nextInt();
//Creating Object with default size = 10
ArrayList arrayList = new ArrayList(len);
System.out.println("ArrayList Size = "+arrayList.size());
System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());
//Generating 10 random numbers and adding into the Arraylist
for(int i=0;i<arrayList.size();i++) {
Random random = new Random();
int randomNumber = random.nextInt(50)+1;
arrayList.add(i, randomNumber);
}
//Printing the ArrayList
System.out.println("Added 10 random numbers in the arrayList");
System.out.println("ArrayList : "+arrayList);
System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());
//Adding 200 in the last of ArrayList
arrayList.add(200);
System.out.println("ArrayList : "+arrayList);
// If you will enter the index > size java.lang.ArrayIndexOutOfBoundsException will be thrown
int element = (int) arrayList.get(9);
System.out.println("Element = "+element);
System.out.println("Value exist in the ArrayList = "+arrayList.isIn(200));
// Finding first occurrence of an Object
int pos = arrayList.find(200);
if(pos>=0)
System.out.println("Element exist at position : "+pos);
else
System.out.println("Element does not exist");
arrayList.remove(201);
System.out.println("Arraylist = "+arrayList);
}
}
Explanation / Answer
//package cheggSolution;
import java.util.Arrays;
// this is basically custom implementation of ArrayList class present in java
// this class stores list of objects of any type as it is Generic Class specified here <T>
// the size of elements which this class can hold is 10 by default
// user can also specify size
//the implementation is not dynamic but fixed size list
// no data structures other than array has been used
public class ArrayList<T> {
// this attribute stores the size of elements this list can hold
private int length;
// this attribute actually store the elemet in an array
private Object[] arr;
// No Argument Constructor
// this is a default constructor which creates array of 10 size
public ArrayList() {
length=10;
arr = new Object[length];
}
// Parameterized Constructor
//this constructor creates list of size specified by user
public ArrayList(int size) {
this.length = size;
arr = new Object[size];
}
// Method to add object at the end of ArrayList
// this method adds object at the end of array
// the implementation is wrong as it adds element to last of list always overwriting the previous
public void add(T object) {
arr[length-1]=object;
}
// Method to add the value at given index
// this method inserts the object in the list at specified index
// the implementation is wrong as it does not shift previous element to right
// but overwrites that index
public void add(int index, T object) {
if(index<length)
arr[index] = object;
else
System.out.println("Entered position is out of range");
}
// this method returns the element present in list at specified index
public T get(int index) throws ArrayIndexOutOfBoundsException{
return (T) arr[index];
}
//Method to get the size of ArrayList
// this returns the maximum size of list
// implementation wrong as it always returns the length of list can hold not the
// number of elements present
public int size() {
return arr.length;
}
// Method to check ArrayList is empty or not
// this method returns if list is empty or not
public boolean isEmpty(){
boolean empty = true;
for(int i=0; i<length;i++) {
if(!(arr[i]==null)) {
empty = false;
break;
}
}
return empty;
}
// Method to check an Object exist or not in the ArrayList
// this method checks if the elemet obj is present in list or not
// linear search algo is used in this
public boolean isIn(T obj){
boolean exist = false;
for(int i=0;i<length;i++) {
T obj1 = (T)arr[i];
if(obj1.toString().equals(obj.toString())) {
exist = true;
break;
}
}
return exist;
}
// Method to return position of first occurrence of an object in the ArrayList. If object does not exist in the Arraylist then it will return -1
// this method returns the index where the object obj ist occurs
// linear search is used in this also
public int find(T obj) {
int position = -1;
for(int i=0;i<length;i++) {
T obj1 = (T)arr[i];
if(obj1.toString().equals(obj.toString())) {
position=i;
break;
}
}
return position;
}
// Method to remove the first occurrence of the Object from ArrayList
// this method removes the elemet from list
public void remove(T obj) {
boolean exist = false;
for(int i=0;i<length;i++) {
T obj1 = (T)arr[i];
if(obj1.toString().equals(obj.toString())) {
exist = true;
arr[i]=null;
break;
}
}
if(exist==false)
System.out.println(obj+" not exist in the ArrayList");
}
@Override
public String toString() {
return Arrays.toString(arr);
}
}
//save below class as TestArray
// in seperate fille
//package cheggSolution;
import java.util.Random;
// this class is test driver for ArrayList class
// it randomly generates some integers and adds them to list
// use only one among two classes below
public class TestArray {
public static void main(String[] args) {
//Creating Object with default size = 10
ArrayList arrayList = new ArrayList();
System.out.println("ArrayList Size = "+arrayList.size());
System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());
//Generating 10 random numbers and adding into the Arraylist
// generates 10 random integer and adds them to arraylist
for(int i=0;i<arrayList.size();i++) {
Random random = new Random();
int randomNumber = random.nextInt(50)+1;
arrayList.add(i, randomNumber);
}
//Printing the ArrayList
System.out.println("Added 10 random numbers in the arrayList");
System.out.println("ArrayList : "+arrayList);
System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());
//Adding 200 in the last of ArrayList
arrayList.add(200);
System.out.println("ArrayList : "+arrayList);
// If you will enter the index > size java.lang.ArrayIndexOutOfBoundsException will be thrown
// gets elemet present at index 9
int element = (int) arrayList.get(9);
System.out.println("Element = "+element);
// checks if 200 is present in list or not
System.out.println("Value exist in the ArrayList = "+arrayList.isIn(200));
// Finding first occurrence of an Object
// returns the index at which 200 is stored
int pos = arrayList.find(200);
if(pos>=0)
System.out.println("Element exist at position : "+pos);
else
System.out.println("Element does not exist");
// remove 201 from list if present
arrayList.remove(201);
System.out.println("Arraylist = "+arrayList);
}
}
// or save this as TestArray
//package cheggSolution;
import java.util.Random;
import java.util.Scanner;
// this is also test driver for ArrayList class i,e it is same as above TestArray class
// the only difference is it asks user to enter size of list at run time
// run one of TestArray class to test ArrayList functionality
public class TestArray {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the Size : ");
int len = in.nextInt();
//Creating Object with default size = 10
ArrayList arrayList = new ArrayList(len);
System.out.println("ArrayList Size = "+arrayList.size());
System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());
//Generating 10 random numbers and adding into the Arraylist
for(int i=0;i<arrayList.size();i++) {
Random random = new Random();
int randomNumber = random.nextInt(50)+1;
arrayList.add(i, randomNumber);
}
//Printing the ArrayList
System.out.println("Added 10 random numbers in the arrayList");
System.out.println("ArrayList : "+arrayList);
System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());
//Adding 200 in the last of ArrayList
arrayList.add(200);
System.out.println("ArrayList : "+arrayList);
// If you will enter the index > size java.lang.ArrayIndexOutOfBoundsException will be thrown
int element = (int) arrayList.get(9);
System.out.println("Element = "+element);
System.out.println("Value exist in the ArrayList = "+arrayList.isIn(200));
// Finding first occurrence of an Object
int pos = arrayList.find(200);
if(pos>=0)
System.out.println("Element exist at position : "+pos);
else
System.out.println("Element does not exist");
arrayList.remove(201);
System.out.println("Arraylist = "+arrayList);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.