JAVA PROGRAMMING PLEASE
This lab has three parts:
Task 1 – ArrayList Class
Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default constructor creates an empty array and the overloaded constructor creates one with a size specified by the parameter.
There should also be a method that adds new elements to the array, which handles resizing and creation of the array if needed. As well as a method to remove elements from the array, which also handles resizing if needed. Finally, there should be a method that is able to retrieve an element at a given position in the internal array.
Task 2 – LinkedList Class
Create a LinkedList class. This is a class that uses references to create a list like structure. This class should have a default constructor that creates an empty list.
We will also need a method that adds elements to the list and handles resizing and creation if needed. There should be a removal method and a method that gets elements at any given point in the list.
Task 3 – The Tester Class
Create a Tester Class, with a Main method. In the Main method, create one ArrayList and LinkedList object each. Use their addition, removal and get methods and show the results by printing the lists before and after each method call. Use user input to populate the lists, with at most ten inputs. After testing the methods, create two new methods, one that takes an ArrayList and one that takes in a LinkedList, and returns the sum of all the elements inside of each.
∃ Some Sample Output:
ArrayList Portion:
Enter a number: 5
Are you done entering numbers (y/n)? n
Enter a number: 9
Are you done entering numbers (y/n)? n
Enter a number: 11
Are you done entering numbers (y/n)? n
Enter a number: 7
Are you done entering numbers (y/n)? y
Initial contents of the list: 5, 9, 11, 7
Contents before adding another item: 5, 9, 11, 7
Please enter another number to add: 21
Contents after adding another item: 5, 9, 11, 7, 21
Contents before removing an item: 5, 9, 11, 7, 21
Please enter the index of the element to remove: 2
Contents after removing an item: 5, 9, 7, 21
Contents before retrieving a value from the list: 5, 9, 7, 21
Please enter the index of the element to retrieve: 0
Element at position 0 is: 5
Contents after retrieving a value from the list: 5, 9, 7, 21
Assume similar input for the LinkedList Portion.
Sum of all items in the ArrayList: 42
Sum of all items in the LinkedList: 42
IList.java
public interface IList<T> {
void Add(T value);
void Add(T value, int index);
T Get(int index);
T Remove();
T Remove(int index);
void Set(T value, int index);
int getSize();
}
ArrayList.java
import java.util.Arrays;
public class ArrayList<T> implements IList<T> {
private Object[] array;
public int getSize() {
return array.length;
}
public ArrayList() {
array = new Object[0];
}
public ArrayList(int size) {
array = new Object[size];
}
public void Add(T value) {
Add(value, array.length);
}
public void Add(T value, int index) {
if (index > array.length)
throw new IndexOutOfBoundsException();
array = Arrays.copyOf(array, array.length + 1);
for (int i = index; i < array.length - 1; i++) {
// shifting ahead
array[i + 1] = array[i];
}
array[index] = value;
}
public void Set(T value, int index) {
if (index >= array.length)
throw new IndexOutOfBoundsException();
array[index] = value;
}
public T Get(int index) {
@SuppressWarnings("unchecked")
final T t = (T) array[index];
return t;
}
public T Remove(int index) {
if (index >= array.length)
throw new IndexOutOfBoundsException();
var value = Get(index);
var lastValue = array[array.length - 1];
array = Arrays.copyOf(array, array.length - 1);
for (int i = index; i < array.length - 1; i++) {
// shifting behind
array[index] = array[index + 1];
}
array[array.length - 1] = lastValue;
return value;
}
public T Remove() {
return Remove(array.length - 1);
}
}
LinkedList.java
public class LinkedList<T> implements IList<T> {
class Value<E> {
public E value;
public Value<E> next;
}
Value<T> head;
private int Size = 0;
public int getSize() {
return Size;
}
public LinkedList() {
}
public LinkedList(int size) {
head = new Value<T>();
var last = head;
for (int i = 0; i < size - 1; i++) {
last.next = new Value<T>();
last = last.next;
}
Size = size;
}
private Value<T> gotoPosition(int index) {
int currentIndex = 0;
var last = head;
while (last.next != null && currentIndex < index) {
last = last.next;
currentIndex++;
}
if (currentIndex != index) {
throw new IndexOutOfBoundsException();
}
return last;
}
public void Add(T value) {
Add(value, Size);
}
public void Add(T value, int index) {
if (head == null && index == 0) {
head = new Value<T>();
head.value = value;
Size++;
return;
}
if (index == Size) {
index--;
}
var last = gotoPosition(index);
var newNode = new Value<T>();
newNode.value = value;
newNode.next = last.next;
last.next = newNode;
Size++;
}
public T Get(int index) {
var last = gotoPosition(index);
return last.value;
}
public T Remove() {
return Remove(Size - 1);
}
public T Remove(int index) {
int currentIndex = 0;
var last = head;
var secondLast = last;
while (currentIndex != index && last.next != null) {
secondLast = last;
last = last.next;
currentIndex++;
}
if (currentIndex == index) {
var value = last.value;
secondLast.next = last.next;
Size--;
return value;
} else {
throw new IndexOutOfBoundsException();
}
}
public void Set(T value, int index) {
var last = gotoPosition(index);
last.value = value;
}
}
TesterClass.java
import java.text.MessageFormat;
import java.util.Scanner;
public class TesterClass {
static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
System.out.println("Testing array list");
ArrayList<Integer> arrayList = new ArrayList<>();
TestList(arrayList);
System.out.println(MessageFormat.format("Sum of all items in the ArrayList: {0}", getSum(arrayList)));
System.out.println("Testing linked list");
LinkedList<Integer> linkedList = new LinkedList<>();
TestList(linkedList);
System.out.println(MessageFormat.format("Sum of all items in the LinkedList: {0}", getSum(linkedList)));
in.close();
}
public static void TestList(IList<Integer> list) {
getUserInput(list);
System.out.print("Initial content of list: ");
printList(list);
System.out.print("Contents before adding another item: ");
printList(list);
System.out.print("Please enter another number to add: ");
list.Add(Integer.parseInt(in.nextLine()));
System.out.print("Contents after adding another item: ");
printList(list);
System.out.print("Contents before removing an item: ");
printList(list);
System.out.print("Please enter the index of the element to remove: ");
list.Remove(Integer.parseInt(in.nextLine()));
System.out.print("Contents after removing an item: ");
printList(list);
System.out.print("Contents before retrieving a value from the list: ");
printList(list);
System.out.print("Please enter the index of the element to retrieve: ");
int index = Integer.parseInt(in.nextLine());
System.out.println(MessageFormat.format("Element at position {0} is: {1}", index, list.Get(index)));
System.out.print("Contents after retrieving a value from the list: ");
printList(list);
}
static void printList(IList<Integer> list) {
for (int i = 0; i < list.getSize(); i++) {
System.out.print(list.Get(i) + " ");
}
System.out.println();
}
static void getUserInput(IList<Integer> list) {
boolean doneEntering = false;
int count = 0;
while (!doneEntering && count < 10) {
System.out.print("Enter a number:");
list.Add(Integer.parseInt(in.nextLine()));
System.out.print("Are you done entering numbers (y/n)? ");
doneEntering = in.nextLine().equals("y") ? true : false;
++count;
}
}
static int getSum(IList<Integer> list) {
int sum = 0;
for (int i = 0; i < list.getSize(); i++) {
sum += list.Get(i);
}
return sum;
}
}
Output
Testing array list
Enter a number:5
Are you done entering numbers (y/n)? n
Enter a number:9
Are you done entering numbers (y/n)? n
Enter a number:11
Are you done entering numbers (y/n)? n
Enter a number:7
Are you done entering numbers (y/n)? y
Initial content of list: 5 9 11 7
Contents before adding another item: 5 9 11 7
Please enter another number to add: 21
Contents after adding another item: 5 9 11 7 21
Contents before removing an item: 5 9 11 7 21
Please enter the index of the element to remove: 2
Contents after removing an item: 5 9 7 21
Contents before retrieving a value from the list: 5 9 7 21
Please enter the index of the element to retrieve: 0
Element at position 0 is: 5
Contents after retrieving a value from the list: 5 9 7 21
Sum of all items in the ArrayList: 42
Testing linked list
Enter a number:5
Are you done entering numbers (y/n)? n
Enter a number:9
Are you done entering numbers (y/n)? n
Enter a number:11
Are you done entering numbers (y/n)? n
Enter a number:7
Are you done entering numbers (y/n)? y
Initial content of list: 5 9 11 7
Contents before adding another item: 5 9 11 7
Please enter another number to add: 21
Contents after adding another item: 5 9 11 7 21
Contents before removing an item: 5 9 11 7 21
Please enter the index of the element to remove: 2
Contents after removing an item: 5 9 7 21
Contents before retrieving a value from the list: 5 9 7 21
Please enter the index of the element to retrieve: 0
Element at position 0 is: 5
Contents after retrieving a value from the list: 5 9 7 21
Sum of all items in the LinkedList: 42
Hope this helps :)
JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....
Solve the following LinkedList problems. You can use the LinkedList class from the previous lab. Write the following methods for the LinkedList class Problem 1: Write a method that deletes the first half of the list. Problem 2: Write a method that deletes the second half of the list. Problem 3: Write a method that deletes every other element in the list. Problem 4: Write a method that, given a second LinkedList, creates and returns a third list containing all...
Java Create a class named OrderedList. It will use a LinkedList of integers as its attribute. The constructor will simply create an empty list. This will be a different LinkedList, as all of the items will be in ascending numerical order. That means items will not necessarily be placed at the end of the list, but placed where it should be located. Note that the iterator simply uses the next() method, so if you use the iterator to find the...
Create a LIFO class with following methods in java - public LifoList() The default constructor for LifoList class which will initialize your class variables maxSize to 0 and the int array to null. public LifoList(int maxSize) The constructor for LifoList class which takes the int input maxSize to initialize the size of the array. You should NOT reinitialize the array elsewhere. For example, creating an object as new LifoList(5) should create a LifoList whose maximum size is 5. If the...
PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is, if the list is full when an item is being added to this list, the elements will be moved to a larger array. The new array should have twice the size of the original array. Using the new class ArrayList, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should...
Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...
JAVA HELP (ARRAYS)
Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...
Step 1 Your text has an example of LinkedList class. Convert the class so that the Link objects have a single long instance variable -- call it "data" -- and the Link instance variable.. Step 2 Then add the following capabilities to the class. A constructor that takes an array of longs as an argument. The contents of the array are use to create Link objects that become objects in the LinkedList. A "search" method that takes a long argument...
C# Create a “Main” method that contains a 1-Dimensional ArrayList of integers. Randomly fill the ArrayList with at least 10 integers using a recursive method. Then create another method that calculates the sum of that ArrayList using recursion as well. Only use TWO (2) parameters, at most, in these two recursive methods. Finally, print the array and the sum of ArrayList. Sample Output: ArrayList contents: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 The sum of the ArrayList...
JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...
Your text has an example of LinkedList class. Convert the class so that the Link objects have a single long instance variable -- call it "data" -- and the Link instance variable.. Step 2 Then add the following capabilities to the class. A constructor that takes an array of longs as an argument. The contents of the array are use to create Link objects that become objects in the LinkedList. A "search" method that takes a long argument and returns...