Question
Assign4 class // Test program public static void main(String[]) // main of test program SortedStringArray class // an array of Strings with entries in sorted
Assign4 class // Test program
public static void main(String[]) // main of test program
SortedStringArray class // an array of Strings with entries in sorted order, with no duplicates, that grows as needed
private String[] stringArray // Array to contain sorted Strings
private int currentSize // Current number of Strings
--------------------------------------------------------------
public SortedStringArray() // Default constructor with initial capacity of 10
public SortedStringArray(int) // Initial constructor with the given capacity
public boolean add(String) // Add an entry in the correct sorted location
public void print() // Print the sorted array
public String get(int) // Get the entry at the sorted position
public int getIndex(String) // Get the index of the given entry
public boolean delete(int) // Delete the entry at the given index
public boolean delete(String) // Delete the given entry
public int size() // Get the current size
public int capacity() // Get the current capacity
Assign4 class // Test program
public static void main(String[]) // main of test program
SortedStringArray class // an array of Strings with entries in sorted order, with no duplicates, that grows as needed
private String[] stringArray // Array to contain sorted Strings
private int currentSize // Current number of Strings
--------------------------------------------------------------
public SortedStringArray() // Default constructor with initial capacity of 10
public SortedStringArray(int) // Initial constructor with the given capacity
public boolean add(String) // Add an entry in the correct sorted location
public void print() // Print the sorted array
public String get(int) // Get the entry at the sorted position
public int getIndex(String) // Get the index of the given entry
public boolean delete(int) // Delete the entry at the given index
public boolean delete(String) // Delete the given entry
public int size() // Get the current size
public int capacity() // Get the current capacity
public class Assign4 {
public static void main(String[] args) {
boolean testSucceeds;
// Test that the default constructor works properly
testSucceeds=true;
SortedStringArray ssa = new SortedStringArray();
if(ssa.size()!=0)
{
System.err.println("Default constructor: initial size is wrong");
testSucceeds=false;
}
if(ssa.capacity()!=10)
{
System.err.println("Default constructor: initial capacity is wrong");
testSucceeds=false;
}
if(testSucceeds)
{
System.out.println("Default constructor: working correctly");
}
// Test that the initial constructor works properly
// Test that the add method works properly
// Test that the get method works properly
// Test that the getIndex method works properly
// Test that the delete method works properly
}
Here is my code, but it does not work properly
public class SortedStringArray { // an array of String with entries in sorted order, with no duplicates, that
// grows as needed
private String[] stringArray; // Array to contain sorted Strings
private int currentSize; // current number of Strings
public SortedStringArray() { // Default constructor with initial capacity 10
stringArray = new String[10];
}
public SortedStringArray(int numbers) { // Initial constructor with the given capacity
stringArray = new String[numbers];
}
public boolean add(String s) {// Add an entity in the correct sorted location
if (currentSize == stringArray.length) {
for (int i =0; i< currentSize; i++) {
if(stringArray[i]==s) {
return false;
}
}
String temp [] = new String [stringArray.length +1]; // create temporary array with one element larger
for (int i =0; i < stringArray.length; i++ ) {
temp[i] = stringArray[i];
}
stringArray=temp;
}
stringArray[currentSize] = s;
currentSize++;
int i, j;
String temp;
for (i=0; i < currentSize - 1; i++) {
for (j=i+1; j < currentSize; j++) {
if (stringArray[i].compareToIgnoreCase(stringArray[j]) > 0){
temp = stringArray[i];
stringArray[i] =stringArray[j];
stringArray[j] = temp;
}
}
}
return false;
}
public void print() { // Print the sorted array
for (String arrStr : stringArray) {
if (arrStr == null) {
System.out.print("");
} else
System.out.println(arrStr + " " + getIndex(arrStr)+ " "+ size()+ " / "+capacity());
}
}
public String get(int index) { // Get the entry at the sorted position
if (stringArray[index] != null)
return stringArray[index];
else
return "Error";
}
public int getIndex(String entry) { // Get the index of the given entry
for (int i = 0; i < stringArray.length; i++) {
if (stringArray[i] == entry)
return i;
}
return -1;
}
public boolean delete(int i) { // Delete the entry at the given index
if (stringArray[i] != null) {
stringArray[i] = null;
currentSize--;
int num = 0;
for (int j = 0; j < stringArray.length; j++) {
if (stringArray[j] == null) {
num = j;
break;
}
}
}
return false;
}
public boolean delete(String entry) {// Delete the given entry
for (int i = 0; i < stringArray.length; i++) {
if (stringArray[i] == entry) {
stringArray[i] = null;
return true;
}
}
return false;
}
public int size() { // Get the current size
return currentSize;
}
public int capacity() { // Get the current capacity
return stringArray.length;
}
}
public class Assign4 { // main
public static void main(String[] args) {
boolean testSucceeds;
testSucceeds = true; //Test that the default constructor works properly
SortedStringArray ssa = new SortedStringArray();
if (ssa.size() !=0)
{System.out.println("Default constructor: initial size is wrong");
testSucceeds=false;
}
if (ssa.capacity() !=10)
{
System.out.println("Default constructor: initial capacity is wrong");
testSucceeds=false;
}
if (testSucceeds)
{
System.out.println("Default constructor: working correctly");
}
if (ssa.size() !=0) // Test that the initial constructor works properly
{System.out.println("Initial constructor: initial size is wrong");
testSucceeds=false;
}
if (ssa.capacity() !=10)
{
System.out.println("Initial constructor: initial capacity is wrong");
testSucceeds=false;
}
if (testSucceeds)
{
System.out.println("Initial constructor: working correctly");
}
ssa.add("Astra");
ssa.add("Glodiolus");
ssa.add("Lilac");
ssa.add("Rose");
ssa.add("Geosint");
ssa.print();
System.out.println();
ssa.delete(1);
ssa.print();
}
}
Step 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