In: Computer Science
You are required to write the following three methods in the class RandomFile:
public static void randomBinaryFile(String fileName, int length)
public static void randomNumberFile(String fileName, intlength)
public static void randomCharFile(String fileName, int length)
The parameter “fileName” specifies the file name, and “length” specifies the length of the sequence in the file.
Also write a main() method to test your code. In the main() method, you may ask the user to type in a file name, a type of randomness (binary, digit or character), and the length of the file.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The following code generates a random character between A and Z:
Random r = new Random();
char a = (char) (r.nextInt(26) + 'A');
Another way is as below:
Random r = new Random();
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char a = alphabet.charAt(r.nextInt(26));
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// RandomFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class RandomFile {
// method to generate a file containing random binary values
public static void randomBinaryFile(String fileName, int length) {
try {
// opening file in write mode
PrintWriter writer = new PrintWriter(new File(fileName));
// initializing random number generator
Random random = new Random();
// looping for length times
for (int i = 0; i < length; i++) {
// generating a number between (0 or 1) and writing to file.
writer.print(random.nextInt(2));
}
// closing file, saving changes
writer.close();
} catch (FileNotFoundException e) {
// invalid file name
System.err.println(e);
}
}
// method to generate a file containing random digits
public static void randomNumberFile(String fileName, int length) {
try {
// opening file in write mode
PrintWriter writer = new PrintWriter(new File(fileName));
// initializing random number generator
Random random = new Random();
for (int i = 0; i < length; i++) {
// generating a number between (0 to 9) and writing to file.
writer.print(random.nextInt(10));
}
writer.close();
} catch (FileNotFoundException e) {
System.err.println(e);
}
}
// method to generate a file containing random alphabets
public static void randomCharFile(String fileName, int length) {
try {
// opening file in write mode
PrintWriter writer = new PrintWriter(new File(fileName));
// initializing random number generator
Random random = new Random();
// creating a string of alphabets
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < length; i++) {
// generating an index value between 0 and 25, choosing the
// character at that index from alphabet and appending to file.
writer.print(alphabet.charAt(random.nextInt(alphabet.length())));
}
writer.close();
} catch (FileNotFoundException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
// setting up a Scanner, reading file name, randomness type and length
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a filename: ");
String file = scanner.nextLine();
System.out
.print("Enter type of randomness (b: binary, d: digit or c: character): ");
char type = scanner.next().toLowerCase().charAt(0);
System.out.print("Enter length: ");
int length = scanner.nextInt();
// based on the chosen type, generating random file. and exiting
if (type == 'b') {
randomBinaryFile(file, length);
} else if (type == 'd') {
randomNumberFile(file, length);
} else if (type == 'c') {
randomCharFile(file, length);
}
// this program does not open the file once it is created, you have to
// check your folder after running this program
}
}