Question
Given a java program, add more methods based on this import java.util.Arrays; public class SimpleList { private int list[]; private int count; public SimpleList() {
Given a java program, add more methods based on this
import java.util.Arrays;
public class SimpleList { private int list[]; private int count;
public SimpleList() { this.count = 0; list = new int[10]; }
public void add(int num) { if (count == 0) { list[count] = num; count++; } else if (count = 0; i--) { list[i + 1] = list[i]; } list[0] = num; count++; } else { list[count - 1] = 0; for (int i = count; i >= 0; i--) { list[i + 1] = list[i]; } list[0] = num; count++; } System.out.println("Number added successfully"); }
public void remove(int num) { int index = search(num);
if (index != -1) { // remove specific indexed element in array // left shift array for (int i = index; i
public int count() { return count; }
public int search(int num) { int index = -1; for (int i = 0; i
/* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { String str = ""; for (int i = 0; i
}
// Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) { int choice; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in); SimpleList sl=new SimpleList(); while (true) { System.out.println(" 1.Add"); System.out.println("2.Remove"); System.out.println("3.Search"); System.out.println("4.Print List"); System.out.println("5.Display Size"); System.out.println("6.Exit"); // Getting the input entered by the user System.out.print("Enter Choice :"); choice = sc.nextInt(); switch (choice) { case 1: { System.out.print("Enter Number to add :"); int num=sc.nextInt(); sl.add(num); continue; } case 2: { System.out.print("Enter Number to remove :"); int num=sc.nextInt(); sl.remove(num); continue; } case 3: { System.out.print("Enter Number to search :"); int num=sc.nextInt(); int indx=sl.search(num); if(indx==-1) { System.out.println(num+" not found in the list"); } else { System.out.println(num+" is found in the list index "+indx); } continue; } case 4: { System.out.println(sl); continue; } case 5: { System.out.println("List size is :"+sl.count()); continue; } case 6: { break; } default: { System.out.println("Invalid Choice"); continue; } } break; }
}
}
2. Make the following changes to current list. a. add (int) As before, add the parameter to the list at the beginning (index = 0). Move all the other integers in the list over so there is room. If the list was full, then increase the size by 50% so there will be room. Increment the count. b. remove (int) As before, if the parameter is in the list, then remove it. The other values in the list may need to be moved down. Adjust the count as needed. If the list has more than 25% empty places, the decrease the size of the list. 3. Create the second version a. append (int) The append method should append the parameter to the end of the list. If the list was full, then increase the size by 50% so there will be room. Increment the count. b. first (): int Return the first element in the list c. size (): int Return the current number of possible locations in the listStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started