Question

In: Computer Science

Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the...

Create a program RandomFile.java that generates files with random numbers/characters.

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.

randomBinaryFile will generate a file containing a sequence of 0 or 1 of the specified length;
randomNumberFile will generate a file containing a sequence of digits 0, 1, 2, …, 9 of the specified length;
randomCharFile will generate a file containing a sequence of characters A, B, C, …, Z of the specified length;

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));

Solutions

Expert Solution

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

      }

}


Related Solutions

Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers...
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
Write a program in c++ that merges numbers from two files and writes all the numbers...
Write a program in c++ that merges numbers from two files and writes all the numbers into a third file in ascending order. Each input file contains a list of 50 sorted double floating-point numbers from the smallest to largest. After the program is run, the output file will contain all 100 numbers between in the two input files, also sorted from smallest to largest. Format the output into two columns – the first column contains the numbers 1-100, and...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A powerball ticket number consists of 5 integer numbers ( between 1-69) and One power play number( between 1-25).
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary search tree using the numbers of the sequence. The tree set should not have any nodes with same values and all repeated numbers of the random sequence must be stored in the node as a counter variable. For example, if there are five 20s’ in the random sequence then the tree node having...
Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
Create a program that generates random number between 0 and 50 and puts them into a...
Create a program that generates random number between 0 and 50 and puts them into a linked list. When it generates 49, instead of adding it to the list, it prints the list, freeing each node and then exits. Submit your .c file
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT