The Name Game Lab objective To read, search and sort, an Array of strings Assign
ID: 3781197 • Letter: T
Question
The Name Game Lab objective To read, search and sort, an Array of strings Assignment Download the following 2 files from Blackboard. Make sure you save both of the files in the same folder. period2.ixt or period3.txt depending on which class you are in. Name Game java The text file contains a list of all the students in your class (first name only). The java program will read the list of names from the text file and store each name in an array. Your assignment is to write the body of the 4 methods defined in the NamoG java file. Below is a description of the task of each method. ame print List Public static void printlist (Stringt) array) This method should print a list of all the names in the array numbered For example 2 Sally 3 Harry shuffle public static void shuffle (Stringti array) This method should mix up all the elenents in the array one way to shuttle a list is to pick 2 random elements in your list and swap them Repeat thia proceno 20 time or search public static int search (string 1 array, string key This method the array the You should perform a linear or tial search on the starting from the element position to the end of the array. If you find the "key" in the array, then you should return the position of the key. If you do not find the "key" in the array then you should return a value of (negative one averageLength public static double averageLength string (I array This method should return the average length of the names in the array.Explanation / Answer
NameGame.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;
public class NameGame {
public static void main(String[] args) throws FileNotFoundException { // throwing exceptions
Scanner s = new Scanner(new FileInputStream("D:\period2.txt")); // Scanner for reading from file
StringBuffer sb = new StringBuffer(); // Creating StringBuffer for Storing Strings
while(s.hasNextLine()) {
sb.append(s.nextLine()+" "); // Appending to Buffer
}
String arrayNames[] = sb.toString().split(" "); // Splitting with and storing into array
System.out.println("************NAMES IN ARRAY**************");
printList(arrayNames);
System.out.println("************SHUFFLED STRINGS***********");
shuffleList(arrayNames);
System.out.println("***********SEARCH STRING*********");
System.out.println("Enter the key:");
String key = new Scanner(System.in).next();
searchList(arrayNames,key);
System.out.println("************AVERAGE LENGTH************");
averageLength(arrayNames);
}
//Method for average String length of arrays
private static void averageLength(String[] arrayNames) {
int sum=0;
for(int i=0;i<arrayNames.length;i++) {
sum = sum+ arrayNames[i].length();
}
int average = sum/arrayNames.length;
System.out.println("The average Length is:"+average);
}
//Method for searching the String in the array
private static void searchList(String[] arrayNames, String key) {
int pos = -1;
for(int i=0;i<arrayNames.length;i++) {
if(key.equals(arrayNames[i])){
pos = i;
}
}
if(pos>-1)
System.out.println(key+" found at position"+" "+pos);
}
//Method for shuffling Strings in the array
private static void shuffleList(String[] arrayNames) {
Random r = new Random(); // Creating Random object for random positions
for(int j=0;j<arrayNames.length;j++) {
int p1 = r.nextInt(26); // One random position
int p2 = r.nextInt(26); //Second Random Position
String temp; // swapping p1 and p2 using temp
temp = arrayNames[p1];
arrayNames[p1] = arrayNames[p2];
arrayNames[p2] = temp;
}
System.out.println("String after Shuffling:");
//Printing after shuffling
for(int k=0;k<arrayNames.length;k++) {
System.out.println(arrayNames[k]);
}
}
//Method for print Strings in the array
private static void printList(String[] arrayNames) {
System.out.println("The names in the array are:");
for(int i=0;i<arrayNames.length;i++) {
System.out.println(arrayNames[i]);
}
}
}
Sample Input File period2.txt
Andre
Michael
AJ
Callista
Jacob
Josh
Charlie
Nathan
John K.
Jonathan
Jared
Min Jae
Scott
Liam
Kareem
Geordan
Naweed
Omar
Juliana
Ashley
David
Alycia
Taylor
Ben
John W.
Connor
Blake
Output:
************NAMES IN ARRAY**************
The names in the array are:
Andre
Michael
AJ
Callista
Jacob
Josh
Charlie
Nathan
John K.
Jonathan
Jared
Min Jae
Scott
Liam
Kareem
Geordan
Naweed
Omar
Juliana
Ashley
David
Alycia
Taylor
Ben
John W.
Connor
Blake
************SHUFFLED STRINGS***********
String after Shuffling:
Jonathan
Ben
Scott
Juliana
John W.
Josh
Jacob
Michael
David
John K.
Jared
Taylor
Liam
Omar
Kareem
Geordan
Callista
Connor
Nathan
Alycia
Andre
Charlie
Min Jae
Ashley
AJ
Naweed
Blake
***********SEARCH STRING*********
Enter the key:
Connor
Connor found at position 17
************AVERAGE LENGTH************
The average Length is:5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.