Question: Please teach how to solve problems of SuperArray.java, problems are in comments of SuperArray.java Please showing the output Thank you There are some reference methods

Please teach how to solve problems of "SuperArray.java", problems are in comments of "SuperArray.java" Please showing the output Thank you

There are some reference methods in "SuperArrayTest.java"

(Please don't modify "Account.java" and "Main.java", after solve problems of "SuperArray.java", please open three java documents altogether and run with "Main.java" Please don't write other 4th extra java document Thank you )

"SuperArray.java"

import java.util.Arrays; public class SuperArray { private Object[] items; private int size = 0; private static final int INITIAL_SIZE = 3; public SuperArray() { this.items = new Object[INITIAL_SIZE]; } // put or update public void update(int i, T item) { rangeCheck(i); // Problem } // get item at index (read) public T get(int i) { rangeCheck(i); // every time we need to return an element in the array, we call elements(int i) return element(i); } // find item (search) // this method will return the index of the first occurrence of the item in the array public int find(T item) { // Problem: loop through the array up to this.size and as soon as find the item // return the index (i) // Problem: by the time get out of the loop, haven't found the item, therefore // return -1 } // This method will return the size which determines the size of the array public int size() { return size; } // This method returns the capacity (only for testing purposes) public int capacity() { return items.length; } //Problem: Please add items to the array at a specific index public void add(int i, T item) { // Problem: the trick here is to start at the last index, copy each element to the right and go down and finally store item at index i // Remind: call copyOf to make a copy of the current array if size >= items.length } public void push(T item) { // Problem: implement the push method // Remind: call copyOf to make a copy of the current array if size >= items.length } public Object pop() { // Problem: implement pop // return null if empty } // Problem: this method will copy everything from a to a new array of bigger size // and return it private Object[] copyOf(Object[] a, int newLength) { // Problem: newLenght determines the length of the new array return null; } // delete an item (by index or by object?) // -- by index // In the first overload would going to delete an item at the provided index // Problem: Plase write method that receives an index and "deletes" the item at that index // Problem: Please write public method that returns nothing (void) called delete which receives an index public void delete(int i) { // Problem: make sure the index is in range by calling rangeCheck(i); // Problem: Please write loop that starts at the index passed to the method, // then all the way up to "size - 1" // then it copies the next cell to the current one. This shifts all values in the array // Remind: arr[i] = arr[i + 1] rangeCheck(i); // Problem: Please create loop that starts at the index passed to the method, // then all the way up to "size - 1" // then it copies the next cell to the current one. This shifts all values in the array // Remind: arr[i] = arr[i + 1] // set the last cell's value to null // Problem: After the loop, set the last cell's value to null and decrement size } // -- by object // Problem: the thinking be delete the first occurrence of the item in the array // Already with methods public void delete(T item) { // Problem: call the find method to find the index then delete the item at that index // Remind: Already with all methods } // this method will check the range of the index and throw an exception if it's // not in range private void rangeCheck(int i) { if (i < 0 || i >= size) throw new ArrayIndexOutOfBoundsException(); } @Override public String toString() { return Arrays.toString(items); } // This method is used to return an element in the array as T @SuppressWarnings("unchecked") private T element(int i) { return (T) items[i]; } } 

"Main.java"

public class Main { public static void main(String[] args) { SuperArray accounts = new SuperArray<>(); accounts.push(new Account("user1", "1234")); accounts.push(new Account("user2", "abcd")); accounts.push(new Account("user1", "1234")); accounts.push(new Account("user3", "eeee")); accounts.push(new Account("user4", "aaaa")); System.out.println(accounts); System.out.println(accounts.get(1)); Account ac = new Account("user2", "abcd"); System.out.println(accounts.find(ac)); accounts.delete(3); System.out.println(accounts.get(3)); System.out.println(accounts.size()); Account tmpAccount = new Account("user1", "1234"); accounts.delete(tmpAccount); System.out.println(accounts); } }

"Account.java"

public class Account { private String userName; private String password; public Account(String userName, String password) { this.userName = userName; this.setPassword(password); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } // @Override is a Java annotation that hints the compiler that we are overriding an inherited method @Override public boolean equals(Object user) { Account otherUser = (Account) user; return this.getUserName().equals(otherUser.getUserName()) && this.getPassword().equals(otherUser.getPassword()); } @Override public String toString() { return String.format("{username: %s, password: %s}", this.getUserName(), this.getPassword()); } }

"SuperArrayTest.java"

import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.BeforeEach; public class SuperArrayTest { private SuperArray accounts; @BeforeEach private void init() { accounts = new SuperArray<>(); accounts.push(new Account("user1", "1234")); accounts.push(new Account("user2", "abcd")); accounts.push(new Account("user3", "eeee")); } @Test public void testPush() { accounts.push(new Account("user4", "tttt")); assertEquals(4, accounts.size()); assertEquals(4, accounts.capacity()); accounts.push(new Account("user5", "zzzz")); assertEquals(6, accounts.capacity()); } @Test public void testAdd() { accounts.add(1, new Account("user1.5", "bbbb")); assertEquals(4, accounts.size()); assertEquals(4, accounts.capacity()); assertEquals("user2", accounts.get(2).getUserName()); assertEquals("user3", accounts.get(3).getUserName()); accounts.add(3, new Account("user2.5", "cccc")); assertEquals(5, accounts.size()); assertEquals(6, accounts.capacity()); } @Test public void testPop() { Account ac = (Account) accounts.pop(); assertEquals(ac, new Account("user3", "eeee")); assertEquals(2, accounts.size()); } @Test public void testRead() { Account u2 = accounts.get(1); assertEquals(u2.getUserName(), "user2"); assertEquals(u2.getPassword(), "abcd"); assertThrows(ArrayIndexOutOfBoundsException.class, () -> accounts.get(7)); } @Test public void testUpdate() { Account acc = new Account("us", "pw"); accounts.update(2, acc); Account u3 = accounts.get(2); assertEquals(u3.getUserName(), "us"); assertEquals(u3.getPassword(), "pw"); assertThrows(ArrayIndexOutOfBoundsException.class, () -> accounts.update(8, acc)); } @Test public void testFind() { assertEquals(accounts.find(new Account("user3", "eeee")), 2); assertEquals(accounts.find(new Account("aa", "bb")), -1); } @Test public void testDeleteByIndex() { accounts.delete(0); assertEquals(-1, accounts.find(new Account("user1", "1234"))); assertEquals(1, accounts.find(new Account("user3", "eeee"))); assertThrows(ArrayIndexOutOfBoundsException.class, () -> accounts.get(4)); assertEquals(2, accounts.size()); } @Test public void testDeleteByAccount() { accounts.delete(new Account("user1", "1234")); assertEquals(2, accounts.size()); assertEquals(0, accounts.find(new Account("user2", "abcd"))); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!