Question

Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling...

Assignment 11 –
Exceptions, Text Input/Output - Random Numbers

Revised 9/2019

Chapter 12 discusses Exception Handling and Input/Output. Using try/catch/finally statement to handle exceptions, or declare/throw an exception as needed, create the following program:

Create an array of 25 random numbers between 0 & 250 formatted to a field width of 10 & 4 decimal places (%10.4f). Use the formula Math.random() * 250. Display the array of random numbers on the console and also write to a file.

Prompt the user for an integer, and then display the random number located at that index of the array. If the specified index is out of bounds, display the message The index is out of bounds in addition to the Java compiler out of bounds exception message. The random numbers and all results should be written to the console as well as to a file. Also, display the file path using the File method getAbsolutePath.

*Note: for those already familiar with Java programming, please use only concepts discussed up to this point.

Before starting this lab, be sure to refer to the “How to Submit Assignments” document in eLearn for proper documentation, indention, naming conventions, etc. Points may be deducted if submitted incorrectly.

REMINDER: Any sources you use besides the textbook, class demo files, or the instructor must be documented in your program with details such as the scope of help received & URL.

Required project name: LastnameFirstname11 Required package name: chap12
Required class names: RandomNumbers

Your program and the text in the file should look similar to the 3 program runs below (user input in bold):

24.5294

80.8706 147.1332

...(additional random numbers edited out) 72.8478

224.3029 89.4630

Enter an index: 0

The element at index 0 is 24.5294
(finally clause) Scanner object is closed
File path: C:\Users\John\Desktop\myJavaWorkspace\SmithJohn11\randNums.txt

--------------

(run the program again to get another set of random numbers) Enter an index: 24

The element at index 24 is 120.3913
(finally clause) Scanner object is closed
File path: C:\Users\John\Desktop\myJavaWorkspace\SmithJohn11\ randNums.txt

-----------------

(run the program again to get another set of random numbers) Enter an index: 25
The index chosen is out of bounds java.lang.ArrayIndexOutOfBoundsException: 25

(finally clause) Scanner object is closed
File path: C:\Users\John\Desktop\myJavaWorkspace\SmithJohn11\ randNums.txt

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.FileWriter;
import java.util.Scanner;

public class Main {
    public static void main(String args[]) throws Exception {
        double arr[] = new double[25];
        FileWriter writer = new FileWriter("randNums.txt");
        Scanner in = new Scanner(System.in);

        for (int i = 0; i < 25; i++) {
            arr[i] = Math.random() * 250;
            System.out.printf("%10.4f   ", arr[i]);
            writer.write(String.format("%10.4f   ", arr[i]));
        }
        System.out.print("\n\nEnter an index: ");
        int index = in.nextInt();
        try {
            double element = arr[index];
            System.out.println("The element at index " + index + " is " + String.format("%10.4f   ", element));
            writer.write("The element at index " + index + " is " + String.format("%10.4f   ", element));
        } catch (IndexOutOfBoundsException e) {
            System.out.println("The index chosen in out of bounds " + e);
        }

        in.close();
        writer.close();
    }
}
Add a comment
Know the answer?
Add Answer to:
Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1....

    please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1. Write a program for the following. NOTE that some of these steps are not dependent on each other. Using methods is mandatory. Make sure to use methods where it makes sense. a. Ask the user for a series of integers entered from the keyboard. Use a sentinel value such as 999 to end the input process. If the entered values are not integers, throw...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • Fibtester: package fibtester; // Imported to open files import java.io.File; // Imported to use the exception...

    Fibtester: package fibtester; // Imported to open files import java.io.File; // Imported to use the exception when files are not found import java.io.FileNotFoundException; // Imported to write to a file import java.io.PrintWriter; // Imported to use the functionality of Array List import java.util.ArrayList; // Imported to use the exceptions for integer values import java.util.NoSuchElementException; // Imported to use when the array is out of bounds import java.lang.NullPointerException; // Imported to take input from the user import java.util.Scanner; public class FibTester...

  • I NEED HELP with this. please create a UML diagram. I need a simple code to...

    I NEED HELP with this. please create a UML diagram. I need a simple code to solve the problem.   The ADT Bag is a group of items, much like what you might have with a bag of groceries. In a software development cycle, specification, design, implementation, test/debug, and documentation are typical activities. The details are provided in the rest of the document. ADT Bag Specification: (Note: You should not change the names of the operations in your program. This should...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

  • I'm not getting out put what should I do I have 3 text files which is...

    I'm not getting out put what should I do I have 3 text files which is in same folder with main program. I have attached program with it too. please help me with this. ------------------------------------------------------------------------------------ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an...

    Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an order at a fast-food burger joint. This class will be used in Part B, when we work with a list of orders. As vou work through this part and Part B, draw a UML diagram of each class in using the UML drawing tool 1) Create a new Lab5TestProject project in Netbeans, right-click on the lab5testproject package and select New>Java Class 2) Call your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT