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: 72% (11 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...
-
Cost Estimation, Interpretation, and Analysis (Requires Computer Spreadsheet) (LO2, 3) Piedmont Table Company produces twa styles of tables, dining room and kitchen. Presented is monthly information...
-
How does the price of capital tend to change during a boom? During a recession? How does risk affect interest rates? AppendixLO1
-
Cash receipts budget. Scottsdale Co. has actual sales for July and August and forecast sales for September, October, November, and December as follows: Actual: July . . . . . . . . . . . . . . . . ....
-
3- If the minimum stock level and average stock level of raw material A are 8,000 and 18000 units respectively, find out its reorder quantity
-
As part of a forensic accounting engagement, you are asked to determine what factors influence the pricing of homes in the local community. Review the data in the following table that has been...
-
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...
-
Briefly explain whether each of the following is primarily a microeconomic issue or a macroeconomic issue. a. The effect of higher cigarette taxes on the quantity of cigarettes sold. b. The effect of...
-
4. Assume that G and T are exogenous, and C is determined by the standard. consumption function, but that investment is now endogenous and responds to income: I = b + bY. Assume c + b < 1. (a)...
-
4. You have decided it's time to buy a house, and you have found the one you want. The price is $500,000, and you will pay 10% in cash and will take a mortgage on the balance. The annual interest...
-
Differentiate. G(x) = (2x+3) (9x+ (x) G'(x)=
-
Consider the following C functions and assembly code: int fun4 (int *ap, int *bp) ( int a = *ap; int bbp; return a+b; }) pushl ebp movl esp, ebp int fun5 (int *ap, int *bp) { int bbp; *bp + *ap;...
-
The position of a particle moving along the x-axis is given by x(t) = = 4.2 2.5t m. (Assume t is in seconds.) (a) At what time (in s) does the particle cross the origin? 1.68 S (b) What is the...
-
Which of the following calls a void function named displayName, passing it no actual arguments? a. call displayName(); b. displayName; c. displayName() d. displayName();
-
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...
-
Kindly Provide brief answers to the following: a) Why do we add floatation costs in the calculations of individual components costs? b) List and briefly explain the qualitative and quantitative...
-
Which of the following statements is correct. On average, the most efficient strategy to create value for existing shareholders is: To buy back shares To offer new products in a growing market To...
-
Which investment should I choose? Bond X: AA Corporate bond, Par=$1,000, Coupon rate=5% (semiannual coupons), 5 years to maturity Bond Y: AA Corporate bond, Par=$5,000, Coupon rate=5.5% (semiannual...
Study smarter with the SolutionInn App