1. Create a RandomString class (starting from the template on Blackboard) and im
ID: 3546993 • Letter: 1
Question
1. Create a RandomString class (starting from the template on Blackboard) and implement the following:
b. A constructor that receives the name of a file to get string values from. The constructor should read in the phrases from the file and store them for later use using a private loadStringsFromFile method.
c. next a method that returns a random string value from the file; this value shouldnt be repeated until all guess phrases in the file have been used.
d. shuffle
e. Create a main method to test that next is working correctly by repeatedly calling next & printing the result you should not have any repeats, and the phrases should not be in the same order as in the file.
f. Once youve verified your RandomString class is working by testing it in main, run the ListSpinner class to verify your wheel will spin properly.
----------------------------------------------------------------
Template: Given
/**
* RandomString gets a set of strings from a file. You can use next()
* to get a random string from the set. It will reload the strings from
* the file after every string is used.
*
public class RandomString {
public RandomString(String filename) {
// TODO Auto-generated constructor stub
}
/**
* This gets the strings from the file.
*/
private void loadStringsFromFile()
{
// TODO
}
/**
* Shuffles the strings.
*/
public void shuffle()
{
// TODO
}
/**
* Returns the strings read in from the file.
* @return
*/
public ArrayList<String> getStrings() {
// TODO Auto-generated method stub
return null;
}
/**
* Returns a randomly chosen string from the set.
* It will not repeat a string until it has used all of them.
*/
public String next()
{
// TODO
return "";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Explanation / Answer
/**
* RandomString gets a set of strings from a file. You can use next()
* to get a random string from the set. It will reload the strings from
* the file after every string is used.
*/
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class RandomString {
private ArrayList<String> strings;
private String filename;
private int current;
public RandomString(String filename) {
this.strings = new ArrayList<String>();
this.filename = filename;
this.current = -1;
loadStringsFromFile();
shuffle();
}
/**
* This gets the strings from the file.
*/
private void loadStringsFromFile() {
try {
Scanner file = new Scanner(new File(filename));
while (file.hasNextLine()) {
String line = file.nextLine().trim();
if (!line.isEmpty())
strings.add(line);
}
file.close();
}
catch (Exception e){ }
}
/**
* Shuffles the strings.
*/
public void shuffle() {
Collections.shuffle(strings);
}
/**
* Returns the strings read in from the file.
* @return
*/
public ArrayList<String> getStrings() {
return strings;
}
/**
* Returns a randomly chosen string from the set.
* It will not repeat a string until it has used all of them.
*/
public String next() {
if (current < 0)
current = 0;
else if (++current >= strings.size()) {
shuffle();
current = 0;
}
return strings.get(current);
}
public static void main(String[] args) {
// test that next is working correctly by repeatedly
// calling next & printing the result
// need to create the wheel_values.txt
RandomString rnd = new RandomString("wheel_values.txt");
int size = rnd.getStrings().size();
for (int i = 0; i < size; i++) {
System.out.println(rnd.next());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.