Listing 7.9, Calculator.java, is a simple commandline calculator. Note that the program terminates if any operand is
Question:
Listing 7.9, Calculator.java, is a simple commandline calculator. Note that the program terminates if any operand is nonnumeric. Write a program with an exception handler that deals with nonnumeric operands; then write another program without using an exception handler to achieve the same objective. Your program should display a message that informs the user of the wrong operand type before exiting (see Figure 12.12).
Listing
Transcribed Image Text:
Command Prompt c:\exercise>java Exercise12_01 4 + 5 4 + 5 : 9 c:\exercise>java Exercise12_01 4 - 5 4 - 5 = -1 e:\oxorcise>java Exorciso12_01 4x - 5 Wrong Input: 4x c:\exercise>. 1 public class Calculator { /** Main method */ public static void main(String[] args) { // Check number of strings passed if (args.length != 3) { System.out.printIn( "Usage: java Calculator operand1 operator operand2"); System.exit(0); 3 8. 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // The result of the operation int result = 0; // Determine the operator switch (args[1].charAt(0)) { case '+': result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]); break; case '-': result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]); break; case '.': result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]); break; case '/': result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]); 27 28 29 30 31 // Display result System.out.println(args [0] + + result); + args[1] + + args[2] 32 33 }
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 70% (10 reviews)
Program plan Enter two operators with one operand Convert string object to integer Enter into except...View the full answer
Answered By
Aysha Ali
my name is ayesha ali. i have done my matriculation in science topics with a+ . then i got admission in the field of computer science and technology in punjab college, lahore. i have passed my final examination of college with a+ also. after that, i got admission in the biggest university of pakistan which is university of the punjab. i am studying business and information technology in my university. i always stand first in my class. i am very brilliant client. my experts always appreciate my work. my projects are very popular in my university because i always complete my work with extreme devotion. i have a great knowledge about all major science topics. science topics always remain my favorite topics. i am also a home expert. i teach many clients at my home ranging from pre-school level to university level. my clients always show excellent result. i am expert in writing essays, reports, speeches, researches and all type of projects. i also have a vast knowledge about business, marketing, cost accounting and finance. i am also expert in making presentations on powerpoint and microsoft word. if you need any sort of help in any topic, please dont hesitate to consult with me. i will provide you the best work at a very reasonable price. i am quality oriented and i have 5 year experience in the following field.
matriculation in science topics; inter in computer science; bachelors in business and information technology
_embed src=http://www.clocklink.com/clocks/0018-orange.swf?timezone=usa_albany& width=200 height=200 wmode=transparent type=application/x-shockwave-flash_
4.40+
11+ Reviews
14+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Modify Listing 12.18 WebCrawler.java to search for the wordComputer Programming starting from the URL http://cs.armstrong.edu/liang . ?Your program terminates once the word is found. Display the URL...
-
Revise Listing 7.9, Calculator.java, to accept an expression as a string in which the operands and operator are separated by zero or more spaces. For example, 3+4 and 3 + 4 are acceptable...
-
Write a program similar to Listing 7.9, Calculator.java. Instead of using integers, use rationals, as shown in Figure 13.10a. You will need to use the split method in the String class, introduced in...
-
The 32-kg spool of outer radius r, = 420 mm has a centroidal radius of gyration k = 265 mm and a central shaft of radius r; = 155 mm. The spool is at rest on the incline when a tension T= 243 N is...
-
Compute the determinants in Exercises 1-2 using a cofactor expansion across the first row. In Exercises 1-2, also compute the 3. determinant by a cofactor expansion down the second column. 1. 2. 421...
-
Which of the following fields in the Therapist table would have the highest cardinality? a. TherapistID b. LastName c. FirstName d. Street
-
If the interest rate at B is 20 percent, then which of the following best describes the analysis of the investment? a. The IRR of the investment is less than 20 percent. b. The IRR of the investment...
-
The cash account for Interactive Systems at February 28, 2010, indicated a balance of $7,635. The bank statement indicated a balance of $13,333 on February 28, 2010. Comparing the bank statement and...
-
Lets summarize the analysis we carried out in the VC. What is driving economic (GDP) growth in India? What is driving it in China? As you look forward, how does this influence your expectations of...
-
A store maintains data on customers, products and purchase records in three tables: CUSTOMER, PRODUCT, PURCHASE. The store manager wants to know which product is on its maximum discount for each...
-
Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.
-
Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. Here are sample runs: Enter an integer m: 1500 The smallest number n...
-
Draw the products obtained when 3,3,6-trimethylcyclohexene is treated with NBS and irradiated with UV light.
-
Could you please explain why many nucleotide substitutions have no effect on phenotype, ever the change occurs in a gene that encodes a protein?
-
ABCLimited ("ABC") is a bigcompany listed on USstock market. Ithas been evaluating an investment proposal to manufacture Product XKX. Below showsthe investment proposal information for evaluation....
-
A bakery can sell 800 cupcakes if priced at $3.00 each, or 1,000 cupcakes if priced at $2.50 each.It would be profitable for the bakery to charge a price of $2.50 only if the marginal cost of...
-
The investor plans to invest 2000 UAH and chooses between two investment options: one-year bond that pays 12% upon maturity ( one-year bond with payment of 5% after redemption ) or a high-yield money...
-
In the context where protein serves the primary purpose of fostering growth and tissue development instead of being predominantly utilized for energy, which term accurately describes this phenomenon:...
-
Santana Rey created Business Solutions on October 1, 2017. The company has been successful, and Santana plans to expand her business. She believes that an additional $86,000 is needed and is...
-
Which of the ocean zones shown would be home to each of the following organisms: lobster, coral, mussel, porpoise, and dragonfish? For those organisms you identify as living in the pelagic...
-
a. Throughout the Case Problems in this course, you have been using a Rental class that obtains all the data for rentals from Sunshine Seashore Supplies, including details about the contract number,...
-
Create a multiple-choice quiz on any topic. Include at least 10 questions. Store the players highest score from any previous game in a file, and display the previous high score at the start of each...
-
a. Throughout the Case Problems in this course, you have been using an Event class to store data about events for Yummy Catering. Now, create an application that prompts the user for details about...
-
Provide an overview of the best practices in risk identification during the risk identification step.
-
Explain what role does "Lead Time and Process Speed" play in the implementation of Lean Six Sigma?
-
In recent years, some Walmart workers have staged public protests about inadequate wages and unfair treatment. A number of these employees claim that management has retaliated against them for...
Study smarter with the SolutionInn App