Question

In: Computer Science

Create a program with the Calculator.java code RUN the program with the invalid data arguments 1,...

  1. Create a program with the Calculator.java code
    1. RUN the program with the invalid data arguments 1, 3, 5x
    2. Take a screenshot of the output and describe what you think happened.
    3. MODIFY the code as in listing 14.2, NewCalculator.java , RUN it again with the same invalid input
    4. Take a screenshot of the new output and describe in your word document why you think the exception handling is a 'better' programming technique than just letting the program crash.

__________________________________________________________________________

The Calculator.java code

1: package com.java24hours;
2:
3: public class Calculator {
4: public static void main(String[] arguments) {
5: float sum = 0;
6: for (String argument : arguments) {
7: sum = sum + Float.parseFloat(argument);
8: }
9: System.out.println("Those numbers add up to " + sum);
10: }
11: }

__________________________________________________________________________

The NewCalculator.java code

1: package com.java24hours;
2:
3: public class NewCalculator {
4: public static void main(String[] arguments) {
5: float sum = 0;
6: for (String argument : arguments) {
7: try {
8: sum = sum + Float.parseFloat(argument);
9: } catch (NumberFormatException e) {
10: System.out.println(argument + " is not a number.");
11: }
12: }
13: System.out.println("Those numbers add up to " + sum);
14: }
15: }

Solutions

Expert Solution

//if you are satisfied with the answer kindly leave a like, comment to clear doubts

1. In Calculator.java it leads to an error since "5x" cannot be float parsed hence it gives an error. There is no float value corresponding to "5x" like "8" is 8.0 and "1" is 1.0

2. Exception handling is a better programming technique since while deploying applications, it may lead to the application crashing and pausing the whole process of working on the application and leads to bad user experience.

Handling errors leads to a smooth flow of programs.

Screenshot:

Calculator.java:

NewCalculator.java:


Related Solutions

Create two arguments, one valid and one invalid. Demonstrate with a truth table the validity of...
Create two arguments, one valid and one invalid. Demonstrate with a truth table the validity of each
create two arguments, one valid and one invalid. Demonstrate with a truth table the validity of...
create two arguments, one valid and one invalid. Demonstrate with a truth table the validity of each.
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input entry by a user. a. Write a program that prompts a user to enter a length in feet and inches. The length values must be positive integers. b. Calculate and output the equivalent measurement in centimeters 1 inch = 2.54 centimeters c. Write the code to handle the following exceptions: If the user enters a negative number, throw and catch an error that gives...
Run the following R code (copy and paste) to create some data: out1 <- rep( c(0,0,1),...
Run the following R code (copy and paste) to create some data: out1 <- rep( c(0,0,1), 3 ) out2 <- rep( c(1,0,1), 3 ) counts <- c(18,17,15,20,10,20,25,13,12) This is a variation on the data in the first example on the “glm” help page in R. The counts variable is our response variable and will be modeled as a Poisson variable, the out1 predictor variable will measure the difference between outcomes 2 (baseline) and 3, and out2 will measure the difference...
Determine each of the following arguments’ forms to be valid or invalid. You may use the...
Determine each of the following arguments’ forms to be valid or invalid. You may use the Venn Diagram proof method, the rules/fallacies method, or any other method, of your choice. 1. Some athletes are not baseball players and some baseball players are not basketball players. Therefore, some athletes are not basketball players.2. All creationists are fundamentalists because all fundamentalists are religious people and all creationists are religious people. 3. As no conservationists are litterers, no environmentalists are litterers, because all...
Hello, How can I make the program print out "Invalid data!!" if the file has a...
Hello, How can I make the program print out "Invalid data!!" if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student grade. Smith Kelly 438975 98.6 A Johnson Gus 210498 72.4 C Reges...
Using the main function’s arguments, create a program that takes in a person’s name, their home...
Using the main function’s arguments, create a program that takes in a person’s name, their home town/location, and cell number then prints out the following message: Sample run 1: Java lab01_task03 Sililo Uis 0819876543 Output:   Contact successfully saved !! Contact Name : Sililo @ Uis Home number: 0819876543
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an error message if the input cannot be converted I can give an thumbs up! :)
use VISUAL STUDIO CODE to write this javascript program Exercise 1 (a) Create a HTML file...
use VISUAL STUDIO CODE to write this javascript program Exercise 1 (a) Create a HTML file that uses createElement and appendChild to dynamically insert three paragraphs when a button is clicked. (b) Create a HTML file that includes JavaScript that is similar to: let recs = [“my item …1”,”my item…2”, …] i.e. an array that contains several CSV item records. When the user clicks a button, each array element will be rendered as a list element. Use a HTML list....
Create a schematic of a circuit using and write C program to run on a PIC...
Create a schematic of a circuit using and write C program to run on a PIC 16F88 that will flash between a red and green LED at 5Hz with the green on 75% of the time and the red on 25% of the time.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT