Starting Out with C++ from Control Structures to Objects (9th Edition)
9th Edition
ISBN: 9780134498379
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 16, Problem 1PC
Program Plan Intro
Date Exceptions
Program Plan:
- Include the required header files.
- Declare a class named “Date”. Inside the class
- Inside the “private” access specifier.
- Declare private variables “month”, “day”, “year”.
- Declare an array named “month_Names[]” and a private member function named “set_month_Names()”.
- Inside the “public” access specifier,
- Declare the default and parameterized constructor.
- Declare the functions to set date, month and year.
- Declare the functions to print the date formats.
- Inside the “private” access specifier.
- Inside the default constructor “Date()”,
- Call the function to set the month names.
- Inside the parameterized constructor “Date (int m, int d, int y)”,
- Call the function to set the month
- Call the function to set the day
- Call the function to set the year
- Call the function to set the month names
- Give the function definition set month names
- Inside the “set_month_Names()” function,
- Assign month names to the array “month_Names []”
- Give the function definition set month
- Inside the “set_Month()” function
- Use “if” statement to check whether the “month” value is greater than or equal to 1 and less than or equal to 12
- If the condition is true, then set the month.
- If the condition is false, then throw an exception to indicate invalid month.
- Use “if” statement to check whether the “month” value is greater than or equal to 1 and less than or equal to 12
- Give the function definition set day
- Inside the “set_Day()” function
- Use “if” statement to check whether the “day” value is greater than or equal to 1 and less than or equal to 31
- If the condition is true, then set the day.
- If the condition is false, then throw an exception to indicate invalid day.
- Use “if” statement to check whether the “day” value is greater than or equal to 1 and less than or equal to 31
- Give the function definition set year
- Inside the “set_Year()” function
- Set the year.
- Function definition to print the “date_Format1()”.
- Print the statement
- Function definition to print the “date_Format2()”.
- Print the statement
- Function definition to print the “date_Format3()”.
- Print the statement
- Define the “main()” function.
- Declare and initialize the variables
- Create an object for the class and instantiate the constructor
- Inside the try block
- Call the functions to print the “date_Format1()”, “date_Format2()”, “date_Format3()”.
- Construct the catch blocks to handle the invalid day and month exceptions.
- Return the statement.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
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.
Question 3
}
public class WeekDay {
static void daysFromSunday(int day) {
String dayName = "";
try {
}
}
if (day > 7) {
throw new Exception("Invalid Day! Too high!");
}
else if(day < 1){
throw new Exception("Invalid Day! Too low!");
}
switch(day){
case 1: dayName="Sun";
break;
case 2: dayName="Mon";
break;
case 3: dayName="Tue";
break;
case 4: dayName="Wed";
break;
}
▬▬
}
case 5: dayName="Thu";
Dreak;
case 5: dayName="Thu";
break;
case 6: dayName="Fri";
break;
default: dayName="Sat";
System.out.println(dayName);
}
catch (Exception excpt) {
System.out.println("Oops");
System.out.println(excpt.getMessage());
}finally{
System.out.println("Done!");
public static void main(String[] args) {
daysFromSunday(10);
daysFromSunday(3);
daysFromSunday(0);
OOops
Invalid Day! Too high!
Tue
Oops
Invalid Day! Too low!
OOops
Invalid Day! Too high!
Done!
Tue
Done!
Oops
Invalid Day! Too low!
Done!
O Oops
Invalid Day! Too high!
Done!
Oops
Invalid Day! Too high!
Done!
Oops
Invalid Day! Too low!
Done!
O Oops…
C++ Visual Studio 2019
Complete #13. Dependent #1 Employee and ProductionWorker classes showing below. Modify the Employee and ProductionWorker classes so they throw exceptions when the following errors occur:
The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999.
The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift.
The ProductionWorker class should throw an exception named InvalidPayRate when it receives a negative number for the hourly pay rate.
Write a driver program that demonstrates how each of these exception conditions works.
#1 Employee and ProductionWorker classes
#include <string>#include <iostream>#include <iomanip>using namespace std;
class Employee{private: string name; // Employee name string number; // Employee number string hireDate; // Hire date
public: // Default…
Chapter 16 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Ch. 16.1 - Prob. 16.1CPCh. 16.1 - Prob. 16.2CPCh. 16.1 - Prob. 16.3CPCh. 16.1 - Prob. 16.4CPCh. 16.1 - Prob. 16.5CPCh. 16.3 - Prob. 16.6CPCh. 16.3 - The following function accepts an i nt argument...Ch. 16.3 - Prob. 16.8CPCh. 16.3 - Prob. 16.9CPCh. 16.4 - Prob. 16.10CP
Ch. 16.4 - Prob. 16.11CPCh. 16 - Prob. 1RQECh. 16 - Prob. 2RQECh. 16 - Prob. 3RQECh. 16 - Prob. 4RQECh. 16 - What is unwinding the stack?Ch. 16 - What happens if an exception is thrown by a classs...Ch. 16 - How do you prevent a program from halting when the...Ch. 16 - Why is it more convenient to write a function...Ch. 16 - Why must you be careful when writing a function...Ch. 16 - The line containing a throw statement is known as...Ch. 16 - Prob. 11RQECh. 16 - Prob. 12RQECh. 16 - Prob. 13RQECh. 16 - The beginning of a template is marked by a(n)...Ch. 16 - Prob. 15RQECh. 16 - Prob. 16RQECh. 16 - Write a function that searches a numeric array for...Ch. 16 - Write a function that dynamically allocates a...Ch. 16 - Make the function you wrote in Question 17 a...Ch. 16 - Write a template for a function that displays the...Ch. 16 - Prob. 21RQECh. 16 - Prob. 22RQECh. 16 - Prob. 23RQECh. 16 - Prob. 24RQECh. 16 - T F All type parameters defined in a function...Ch. 16 - Prob. 26RQECh. 16 - T F A class object passed to a function template...Ch. 16 - Prob. 28RQECh. 16 - Prob. 29RQECh. 16 - Prob. 30RQECh. 16 - Prob. 31RQECh. 16 - T F A class template may not be derived from...Ch. 16 - T F A class template may not be used as a base...Ch. 16 - Prob. 34RQECh. 16 - Prob. 35RQECh. 16 - try { quotient = divide(num1, num2); } cout The...Ch. 16 - template class T T square(T number) { return T T;...Ch. 16 - template class T int square(int number) { return...Ch. 16 - Prob. 39RQECh. 16 - Assume the following definition appears in a...Ch. 16 - Assume the following statement appears in a...Ch. 16 - Prob. 1PCCh. 16 - Prob. 2PCCh. 16 - Prob. 3PCCh. 16 - Prob. 4PCCh. 16 - Prob. 5PCCh. 16 - IntArray Class Exception Chapter 14 presented an...Ch. 16 - TestScores Class Write a class named TestScores....Ch. 16 - Prob. 8PCCh. 16 - Prob. 9PCCh. 16 - SortableVector Class Template Write a class...Ch. 16 - Inheritance Modification Assuming you have...Ch. 16 - Prob. 12PCCh. 16 - Prob. 13PC
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- 1.class Nothing {public static void main(String args[]){int x = 0;int y = 20;int fraction = y/x;System.out.println("End Of Main");}}1. If there will occur exception, write the code that will handle the exception.arrow_forwarduse C++ programing language 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.)arrow_forwardJava Exception method withdraw throws an exception if amount is greater than balance. For example: Test Result Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(5500.0); System.out.println("Balance: "+account.getBalance()); Insufficient: Insufficient funds. Balance: 5000.0 Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(500.0); System.out.println("Balance: "+account.getBalance()); Balance: 4500.0arrow_forward
- Instructions: Exception handing Add exception handling to the game (try, catch, throw). You are expected to use the standard exception classes to try functions, throw objects, and catch those objects when problems occur. At a minimum, you should have your code throw an invalid_argument object when data from the user is wrong. File GamePurse.h: class GamePurse { // data int purseAmount; public: // public functions GamePurse(int); void Win(int); void Loose(int); int GetAmount(); }; File GamePurse.cpp: #include "GamePurse.h" // constructor initilaizes the purseAmount variable GamePurse::GamePurse(int amount){ purseAmount = amount; } // function definations // add a winning amount to the purseAmount void GamePurse:: Win(int amount){ purseAmount+= amount; } // deduct an amount from the purseAmount. void GamePurse:: Loose(int amount){ purseAmount-= amount; } // return the value of purseAmount. int GamePurse::GetAmount(){ return purseAmount; } File…arrow_forwardCpp program Write a Garage class that has a Car that is having troubles with its Motor. Use a function-level try block in the Garage class constructor to catch an exception (thrown from the Motor class) when its Car object is initialized. Throw a different exception from the body of the Garage constructor s handler and catch it in main().arrow_forwardWAP to demonstrateuser defne exception create InsufficientBalanceException class create cust class if(withdrawlamt > avabal)---->throw InsufficentBalException else------------>collect the amtarrow_forward
- PLZ help with the following IN JAVA If a method throws an exception, and the exception is not caught inside the method, then the method invocation: terminates transfers control to the catch block transfers control to the exception handler none of the abovearrow_forwardChallenge 1: Division.java You have to implement a Division class that divides two numbers given by the user. The program should handle exceptions that could be thrown during operation: If the second number is 0 If any of the numbers is not a valid number, such as a letter.arrow_forwardJavaarrow_forward
- True or False: IOException serves as a superclass for exceptions that are related to programming errors, such as an out-of-bounds array subscript.arrow_forwardCreate a flowchart and modify the code. INSTRUCTION: Create a new class called CalculatorWithMod. This class should be a sub class of the base class Calculator. This class should also have an additional method for calculating the modulo. The modulo method should only be seen at the sub class, and not the base class! Include exception handling for instances when dividing by 0 or calculating the modulo with 0. You would need to use throw, try, and catch. The modulo (or "modulus" or "mod") is the remainder after dividing one number by another.Example: 20 mod 3 equals 2Because 20/3 = 6 with a remainder of 2 CODE TO COPY: #include <iostream> using namespace std; class Calculator{public:Calculator(){printf("Welcome to my Calculator\n"); } int addition(int a, int b);int subtraction(int a, int b);int multiplication(int a, int b);float division(int a, int b);}; int Calculator::addition(int a, int b){return (a+b);} int Calculator::subtraction(int a, int b){return (a-b);} int…arrow_forwardExceptions are caught and handled by usingarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,