Summer Fun Coding Practice In Preparation For CSC 205 Completing a Program Using
ID: 3918051 • Letter: S
Question
Summer Fun Coding Practice In Preparation For CSC 205 Completing a Program Using Class Methods, Arrays, and Strings Type in the program which follows and complete the body of the method FindMin so that it takes as input an array of size numItems of strings and returns to the calling function the minimum string. That is, the string that would appear first alphabetically. For example, consider the following sample input and output Sample Input (from a user-created external text file States.dat) Georgia Florida North Carolina Tennessee Alabama Sample Output (to screen Alabama Method FindMin is a value-returning class method so you'll need a return statement at the end of your method. Be sure and notice how this method is invoked in main. Also. notice how both methods in main actually send the array 1ist off as a parameter, and then notice how this array parameter is defined in the headers for the two methods it is being sent to. In writing FindMin, remember that when you compare two string objects you must use the compareTo method rather than aExplanation / Answer
Code:
import java.io.*;
import java.util.*;?
public class MinString {
private static final int SIZE = 10;
public static void main(String[] args) {
String [] list = new String[SIZE];
int numItems;
numItems = Initialize(list);
System.out.println(FindMin(list,numItems));
}
private static int Initialize(String[] list)
{
String filename, stateInput;
int i=0, numItems=0;
try
{
System.out.print("Input File : ");//Accept File name
Scanner stdin = new Scanner(System.in);
filename = stdin.nextLine();
stdin = new Scanner(new File(filename));//Open File
while((stdin.hasNext()) && (i<list.length))//Extract names one by one and store in array
{
stateInput = stdin.nextLine();
System.out.println("S = "+stateInput);
list[i]=stateInput;
i++;
}
numItems = i;
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
return numItems;
}
private static String FindMin(String list[],int numItems)//Finds Minimum String
{
int i;//Declaration
String minString = list[0];//Initially we assume the first string to be minimum
for(i=0;i<numItems;i++)//Iterate through the list
{
if(minString.compareTo(list[i]) > 0)//Compare each element of list to minString
{
minString = list[i];//If smaller string found we replace the value of minString
}
}
return minString;//Return final minString Value
}
}
Output:
Input File : States.dat
S = Georgia
S = Florida
S = North Carolina
S = Tennessee
S = Alabama
Alabama?
Added the FindMin function. No changes made in the rest of the code.
Please comment if you need further help or clarification.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.