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
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 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...
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 complete Java program that does the following: Open an input file named data.txt that...
Write a complete Java program that does the following: Open an input file named data.txt that consists of a series of unknown number of integers. If data.txt does not exist, give an appropriate error message and terminate the program. Define a constant MAX of value 100 and create an array of size MAX to hold items from the input file. Make sure your program will not generate ArrayIndexOutOfBounds exception. Open an output file named result.txt and write the array elements...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
Write the following java program: Desc Output the name and time of the runner who came...
Write the following java program: Desc Output the name and time of the runner who came in first, as well as the name and time of the runner who came in last in a marathon race (assuming there are no ties). Input A text file named marathon.txt containing the name and time of each participant in the following format (the file has at least 1 participant, name is just 1 word with no space, and name and time are separated...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT