Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 16, Problem 2PP
Program Plan Intro
- Include required library files.
- Define a “checkDay()” function to check invalid date.
- Check day and month using “if” statement.
- Define a “convertToString()” function to convert string.
- Create an array of months and declare the empty string for date.
- Check the numerical month and print the corresponding month in alphabetic format.
- Define a “convertDate()” function to convert date for the given format.
- Check the given format and convert the valid string.
- Define a “main()” function.
- Call the “convertDate()” function with parameter inside the “try” block.
- Any problem converting date, the exception is thrown to the “catch” block.
- The “catch” will print the error message if the given date is invalid.
- Check day and month using “if” statement.
- Create an array of months and declare the empty string for date.
- Check the numerical month and print the corresponding month in alphabetic format.
- Check the given format and convert the valid string.
- Call the “convertDate()” function with parameter inside the “try” block.
- Any problem converting date, the exception is thrown to the “catch” block.
- The “catch” will print the error message if the given date is invalid.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle a leap year.)
Write a program that prompts the user to enter a person’s date of birthin numeric form such as 8-27-1980. The program then outputs thedate of birth in the form: August 27, 1980. Your program must containat least two exception classes: invalidDay and invalidMonth.If the user enters an invalid value for day, then the program shouldthrow and catch an invalidDay object. Follow similar conventions forthe invalid values of month and year. (Note that your program musthandle a leap year.)
in C# i need to
Write the program BookExceptionDemo for the Peterman Publishing Company. Create a BookException class that is instantiated when a Book’s price exceeds 10 cents per page and whose constructor requires three arguments for title, price, and number of pages. Create an error message that is passed to the Exception class constructor for the Message property when a Book does not meet the price-to-pages ratio. For example, an error message:
For Goodnight Moon, ratio is invalid. ...Price is $12.99 for 25 pages.
Next, create a Book class that contains fields for title, author, price, and number of pages. Include properties for each field. Throw a BookException if a client program tries to construct a Book object for which the price is more than 10 cents per page.
Finally, using the Book class, create an array of five Books. Prompt the user for values for each Book. To handle any exceptions that are thrown because of improper or invalid data entered by the user, set the Book’s…
Chapter 16 Solutions
Problem Solving with C++ (10th Edition)
Ch. 16.1 - Prob. 1STECh. 16.1 - What would be the output produced by the code in...Ch. 16.1 - Prob. 3STECh. 16.1 - What happens when a throw statement is executed?...Ch. 16.1 - In the code given in Self-Test Exercise 1, what is...Ch. 16.1 - Prob. 6STECh. 16.1 - Prob. 7STECh. 16.1 - What is the output produced by the following...Ch. 16.1 - What is the output produced by the program in...Ch. 16.2 - Prob. 10STE
Knowledge Booster
Similar questions
- We have if statement, and we want to use an exception to check the right input with if-else and how it worksarrow_forwardin c # i need to Write the program BookExceptionDemo for the Peterman Publishing Company. Create a BookException class that is instantiated when a Book’s price exceeds 10 cents per page and whose constructor requires three arguments for title, price, and number of pages. Create an error message that is passed to the Exception class constructor for the Message property when a Book does not meet the price-to-pages ratio. Next, create a Book class that contains fields for title, author, price, and number of pages. Include properties for each field. Throw a BookException if a client program tries to construct a Book object for which the price is more than 10 cents per page. Finally, using the Book class, create an array of five Books. Prompt the user for values for each Book. To handle any exceptions that are thrown because of improper or invalid data entered by the user, set the Book’s price to the maximum of 10 cents per page. At the end of the program, display all the entered, and…arrow_forwardIn Java Attachedarrow_forward
- I need to know how to do this in python: Write a program that calculates an adult's fat-burning heart rate, which is 70% of 220 minus the person's age. Complete fat_burning_heart_rate() to calculate the fat burning heart rate. The adult's age must be between the ages of 18 and 75 inclusive. If the age entered is not in this range, raise a ValueError exception in get_age() with the message "Invalid age." Handle the exception in __main__ and print the ValueError message along with "Could not calculate heart rate info." Ex: If the input is: 35 the output is: Fat burning heart rate for a 35 year-old: 129.5 bpm If the input is: 17 the output is: Invalid age. Could not calculate heart rate info.arrow_forwardin C# i need to Write the program BookExceptionDemo for the Peterman Publishing Company. Create a BookException class that is instantiated when a Book’s price exceeds 10 cents per page and whose constructor requires three arguments for title, price, and number of pages. Create an error message that is passed to the Exception class constructor for the Message property when a Book does not meet the price-to-pages ratio. my errors are Test Case IncompleteComplete program walkthrough, test 1 InputBook1Author110.99200Book2Author219.99100Book3Author225.00600Book4Author35.0020Book5Author48.00120OutputBook1 by Author1 Price $10.99 200 pages.Book2 by Author2 Price $19.99 100 pages.Book3 by Author2 Price $25.00 600 pages.Book4 by Author3 Price $5.00 20 pages.Book5 by Author4 Price $8.00 120 pages. ResultsFor Book2, ratio is invalid....Price is $19.99 for 100 pages.For Book4, ratio is invalid....Price is $5.00 for 20 pages.Book1 by Author1 Price $10.99 200 pages.Book2 by Author2 Price $10.00 100…arrow_forwardin c # i need to Write the program BookExceptionDemo for the Peterman Publishing Company. Create a BookException class that is instantiated when a Book’s price exceeds 10 cents per page and whose constructor requires three arguments for title, price, and number of pages. Create an error message that is passed to the Exception class constructor for the Message property when a Book does not meet the price-to-pages ratio. my errors are Test Case IncompleteComplete program walkthrough, test 1 InputBook1Author110.99200Book2Author219.99100Book3Author225.00600Book4Author35.0020Book5Author48.00120OutputEnter information for book #1:Title: Author: Price: Number of pages: Unhandled Exception:System.DivideByZeroException: Value was either too large or too small for a Decimal. at System.Decimal+DecCalc.VarDecDiv (System.Decimal& d1, System.Decimal& d2) [0x0003e] in <7b90a8780ac4414295b539b19eea7eea>:0 at System.Decimal.Divide (System.Decimal d1, System.Decimal d2) [0x00000] in…arrow_forward
- Write a Month class that holds information about the month. Write exception classes for the following error conditions:• A number less than 1 or greater than 12 is given for the month number.• An invalid string is given for the name of the month.Modify the Month class so that it throws the appropriate exception when either of these errors occurs. Demonstrate the classes in a program.arrow_forwardWrite a program that uses an exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. You will create the StringTooLongException.java file first based on the information presented in the chapter. In the StringError.java program, read strings from the user until the user enters "DONE". If a string is entered that has too many characters (say 10), throw the exception. StringError should catch and handle the exception if it is thrown. Handle the exception by printing an appropriate message, and then continue processing more strings.arrow_forwardWrite a class TimeOfDay that uses the exception classes defined in the previous exercise. Give it a method setTimeTo(timeString) that changes the time if timeString corresponds to a valid time of day. If not, it should throw an exception of the appropriate type. [Java]arrow_forward
- write a code in javaarrow_forwardA pedometer treats walking 2,000 steps as walking 1 mile. Write a stepsToMiles() method that takes the number of steps as an integer parameter and returns the miles walked as a double. The stepsToMiles() method throws an Exception object with the message "Exception: Negative step count entered." when the number of steps is negative. Complete the main() method that reads the number of steps from a user, calls the stepsToMiles() method, and outputs the returned value from the stepsToMiles() method. Use a try-catch block to catch any Exception object thrown by the stepsToMiles() method and output the exception message. Output each floating-point value with two digits after the decimal point, which can be achieved as follows:System.out.printf("%.2f", yourValue); Ex: If the input of the program is: 5345 the output of the program is: 2.67 Ex: If the input of the program is: -3850 the output of the program is: Exception: Negative step count entered. import java.util.Scanner; public class…arrow_forwardA pedometer treats walking 2,000 steps as walking 1 mile. Write a stepsToMiles() method that takes the number of steps as an integer parameter and returns the miles walked as a double. The stepsToMiles() method throws an Exception object with the message "Exception: Negative step count entered." when the number of steps is negative. Complete the main() method that reads the number of steps from a user, calls the stepsToMiles() method, and outputs the returned value from the stepsToMiles() method. Use a try-catch block to catch any Exception object thrown by the stepsToMiles() method and output the exception message. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: System.out.printf("%.2f", yourValue);arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,