Question

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 an exception and ask the user again making sure that only integers are allowed as input. Note that the user could enter a floating point number or a number that has characters in it.

b. Create an array with 10 random integers between 100 and 1000, inclusive. Prompt the user to enter the index of the array and display the corresponding element value, in addition to the random integers in the array. If the specified index is out of bounds, throw an exception and display the message out of bounds. HINT: IndexOutOfBoundsException will catch such errors.

c. Create an array with 10 elements and populate it with 10 random numbers between 1000.0 and 2000.0. Note these include decimal numbers. Ask the user for the name of an output file. Write your own full name (do not ask for input) as the first line of the file and the contents of this array to this file. If the file exists, tell the user that the file exists and do not write to the file. Make sure to also ask the user to enter the file name.

d. Write the code to create a text file named text.txt. Write the following text to the file: “Never trust a computer you can't throw out a window.”

e. Write code to verify if the above file exists using try/catch block. If the file exists, read it, and display the contents of the text file on the screen. If the file does not exist, display an error message.

f. Write code to display the number of vowels (a, e, i, o, u – case insensitive), the number of upper case letters, the number of lower case letters, the number of digits, and the number of words in a file. The user should be able to enter any file name for processing. HINT: A word is separated by one space, so number of spaces should give you a clue.

2 Write a JavaFX program to display 5 labels and 3 buttons. You can use any kind of Pane you like to use. Display 3 red circles and 2 blue rectangles (pick your own size). Use the Image and ImageView classes (explained in section 14.9 of your textbook) to show 2 images of your choice. Include a name "Lucus Night" somewhere on the frame, label, button, etc. We won’t discuss handing events where one would be able to click buttons and expect results.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a) PROGRAM


import java.util.Scanner;


public class MainClass{
  
public static void inputNumbers(){
int value = 0;
Scanner scan = new Scanner(System.in);
do{
try{
value = scan.nextInt(); //input number
}catch(Exception ex){ //execption is number is not entered
System.out.println("Please enter only integers"); //print error
scan.nextLine();
}
}while(value!=999);
}
  
public static void main(String[] args) {
inputNumbers();
}
}

OUTPUT
12 kcvk Please enter only integers 689 8.90 Please enter only integers BUILD SUCCESSFUL (total time 14 seconds)

b) PROGRAM


import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;


public class MainClass{
  
public static Scanner scan = new Scanner(System.in);
  
public static int inputNumber(){
int value = 0;
while(true){
try{
value = scan.nextInt(); //input number
return value;
}catch(InputMismatchException ex){ //execption is number is not entered
System.out.println("Please enter only integers"); //print error
scan.nextLine();
}
}
}
  
public static void displayValues(int arr[]){
  
System.out.print("Enter index>> ");
int index = inputNumber();
try{
int value = arr[index];
System.out.println("Value at index " + index + " is " + value);
}catch(IndexOutOfBoundsException ex){
System.out.println("Out of Bounds");
}
}
  
public static void main(String[] args) {
int arr[] = new int[10];
Random rand = new Random();
for(int i=0; i<10; i++){
arr[i] = rand.nextInt(900) + 100;
}
  
displayValues(arr);
}
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1....
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
  • 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...

  • JavaFX zodiac program

    Create a GUI that will display the zodiac sign for a given day of the year, using IntelliJ and JavaFX. It should look like the image, please show code in IntelliJ and make sure there are at least two classes other than the main class as well as implementing an exception handling error that only allows the user to input integers and special characters for '/'. The zodiac list and day of the year should be in an array. Lastly, some...

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

  • (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly...

    (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly chosen integers. ■ Prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds. Following modifications: * Use either a Scanner object or JOptionPane.showInputDialog() for input. * Use either System.out.println() or JOptionPane.showMessageDialog() for output. * In addition to ArrayIndexOutOfBoundsException, also catch InputMismatchException or NumberFormatException, and...

  • THIS IS A C++ QUESTION! PLEASE ANSWER THE QUESTION CORRECTLY! Read the question thoroughly! THIS QUESTION WANTS THE A...

    THIS IS A C++ QUESTION! PLEASE ANSWER THE QUESTION CORRECTLY! Read the question thoroughly! THIS QUESTION WANTS THE A USER INPUT! SO FOR EXAMPLE THE CODE MUST MAKE IT SO THE USER HAS TO INPUT A TEMPERATURE. AND WHEN THE USER INPUTS A TEMP IT MUST OUTPUT IF THE TEMP ENTERED IS TO HIGH OR LOW. Again please read the question carefully and READ the notes I left also! Write a Thermostat class such that the user of the Thermostat...

  • you need to use javafx for following problem these instructions is for JavaFX program so you...

    you need to use javafx for following problem these instructions is for JavaFX program so you need to follow the instructions and according to images you have design the GUI Write a program to design the following interface and use event handling and File 10 to perform the given operation. Make sure you have a background image and different color for labels and buttons for this interface. Input Information Course Quiz Grade Assignment Grade Fal Grade Submit View Grade Info...

  • CM245 Lab 4 (Chapter 12) Submit online through D2L Implement solutions using the appropriate concepts from...

    CM245 Lab 4 (Chapter 12) Submit online through D2L Implement solutions using the appropriate concepts from Chapter 12. There is no need for any UML for this lab. Provide your implementation (java source code) and the output for each of the tests. There should be one class for exercises 12.2 and 12.3 and two classes for 12.4. I prefer one output file per programming exercise. For each problem you must handle the exception. 12.2 (InputMismatchException) Write a program that prompts...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • Write a program that reads a file containing an arbitrary number of text integers that are...

    Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts how many fall into each of eleven ranges. The ranges are 0-9, 10-19, 20-29,..., 90-99 and an eleventh range for "out of range." Each range will correspond to a cell of an array of ints. Find the correct cell for each input integer using integer division. After reading in the file and counting the integers, write...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

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