Question

In: Computer Science

Write a program to perform the following actions: the language is Java – Open an output...

Write a program to perform the following actions: the language is Java

– Open an output file named “Assign14Numbers.txt”

– Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive.

– After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is generated with one integer per line (no extra spaces, tabs, or lines).

– Close output file – open and view it in the IDE editor if desired.

– In program, add code to reopen the same file for input.

– Using an EOF while-loop to detect the end of data, input and display all data items from the file formatted as stated in the next instruction – do NOT use a counting loop to input the data!

– Display the random integers from the file to the console with 15 numbers per line formatted with right-alignment in columns that are exactly 4 positions wide, there must be exactly 1 blank line above and below the numbers displayed on the console.

– Close the input file.

Solutions

Expert Solution

Complete code in java:-

import java.util.*;
import java.io.*;

class random {
   public static void main(String ... args) {
       // Creating scanner object to take input from an input stream.
       Scanner sc = new Scanner(System.in);
       // Creating Random object to generate random numbers.
       Random rand = new Random();
       int count;
       // Opening file to write into.
       try {
           FileWriter fw = new FileWriter("Assign14Numbers.txt");
           // Taking number of numbers that will be printed intot he opened file.
           do {
               System.out.print("Enter a number in range 35-150 (inclusive) : ");
               count = sc.nextInt();
           }while(count < 35 || count > 150);
           // Generating random numbers and writting them into file.
           for(int i = 0; i < count; i++) {
               int num = rand.nextInt(100);
               fw.write(num+"\n");
           }
           // Closing file.
           fw.close();
       } catch (IOException e) {
           System.out.println("IO Error has occured, try again...");
       }
       // Again opening file into read mode, to read from
       try {
           // Creating Input stream object for file.
           FileInputStream fr = new FileInputStream("Assign14Numbers.txt");
           // Creating scanner object to read from file stream stream
           sc = new Scanner(fr);
           // Count will keep track the number of numbers to be printed in a line, specified 15.
           count = 0;
           // Printing formatted data.
           System.out.println();
           String output = "";
           // Readin from file until end of file (EOF).
           while(sc.hasNextLine()) {
               if(count == 15) {
                   System.out.printf("%s\n", output);
                   output = "";
                   count = 0;
               }
               // I am using tab "\t" to make columns 4 position wide,
               // because "\t" is equal to 4 spaces.
               output += ("\t"+sc.nextLine());
               count++;
           }
           System.out.printf("%s\n\n", output);
           sc.close();
       } catch (IOException e) {
           System.out.println("IO Error has occured try again...");
       }
   }
}

Screenshot of output:-


Related Solutions

Write a program in MIPS assembly language to perform the calculation of the following equation and...
Write a program in MIPS assembly language to perform the calculation of the following equation and save the result accordingly:    f = 5x + 3y + z Assumptions: - Registers can be used to represent variables x, y, z, and f - Initialize x, y, and z to values of your choice. f can be initialized to zero. - Use comments to specify your register usage and explain your logic
Java language (a) Write code segments to perform the following: (i) declare and create an integer...
Java language (a) Write code segments to perform the following: (i) declare and create an integer array freqArray of size 8 (ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68 (iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement (b) A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3};...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
Write a program in a language of your choice to perform a search using the A*...
Write a program in a language of your choice to perform a search using the A* algorithm for the eight puzzle problem, in which numbers may be shifted one space at a time to transform the initial state into the goal state (see p. 103 – 3rd Ed., pp. 105-106 – 2nd Ed. of the text). 2. a) Use the start state-goal state combination given in pp. 103, Figure 3.28 (3rd Ed.), [pp. 105, Figure 4.7 (2rd Ed.)], as (start_1,...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
Language is Java Design and write a Java console program to estimate the number of syllables...
Language is Java Design and write a Java console program to estimate the number of syllables in an English word. Assume that the number of syllables is determined by vowels as follows. Each sequence of adjacent vowels (a, e, i, o, u, or y), except for a terminal e, is a syllable. However, the minimum number of syllables in an English word is one. The program should prompt for a word and respond with the estimated number of syllables in...
Write a machine language program to output your name on the output device. The name you...
Write a machine language program to output your name on the output device. The name you output must be longer than two characters. Write it in a format suitable for the loader and execute it on the Pep/9 simulator. my name is Kevin
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results
Write a simple JAVA program to understand natural language. The user will enter the input following...
Write a simple JAVA program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Chris came to Bangkok, Thailand in 2009. The user will follow the exactly the same formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay in City for X year(s). City is in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT