Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 4-Loop and Files During this lab, students will learn how to Declare variabl

ID: 3746893 • Letter: L

Question

Lab 4-Loop and Files During this lab, students will learn how to Declare variables and select the appropriate data type Read from and Write into a teat ile Apply loops for repeating processes Test the program to validate the logic. Introduction: a) In lab 4 you will implement the following Write a program in Java that Task 1: Ask the user to input a positive number N. Keep going asking a positive number if the number entered by the user is not positive. Task 2:Create N random numbers (positive integers) in a loop and save them in a text file called Numbers.txt. Task 3: For each number in Numbers.txt print the same number ofsymbols (on the same line) into an output file called Histegram.txt. The following shows an example of run. Important your program should run for any number (integer) provided by the user not only the scenario below. Example: Task 1: Please enter a positive number Task 21 Numbers.txt (fle Numbers.txt is created with 5 numbers. In the example 7. 2. 5. 10 and 20 are generated 10 20 Task 3: Histogram.txt (file showing the numbers of stars associated to each number from Numbera.xt Tasks 1-3 Word document (with pseudo code and Numbers.tat and Histogram.bxt files Java source code (comments, naming convention etc)

Explanation / Answer

import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Random; import java.util.Scanner; public class NumbersAndHistogram { public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(System.in); int num; while (true) { System.out.print("Enter a positive number: "); num = in.nextInt(); if(num > 0) break; } PrintWriter numbers = new PrintWriter("Numbers.txt"); PrintWriter histogram = new PrintWriter("Histogram.txt"); Random random = new Random(); for(int i = 0; i