1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Marina86 [1]
3 years ago
10

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 outp

uts 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.)
Computers and Technology
1 answer:
Oksi-84 [34.3K]3 years ago
7 0

Answer:

The program to this question can be given as:

Program:

//define header file.

#include<iostream>

#include<string>

using namespace std;

class invalidDay              //define class.

{

string msg;                   //define variable as private.

public:

invalidDay()               //constructor.

{

msg="Day input is wrong";            

}

void showException()           //define function.

{

cout<<msg<<endl;

}

};

class invalidMonth              //define class.

{

string msg;             //define variable as private.

public:

invalidMonth()              //constructor.

{

msg="Month input is wrong";

}

void showException()           //define function.

{

cout<<msg<<endl;

}

};

class leapYear           //define class            

{

string msg;         //define variable as private.

public:

leapYear()                  //define constructor

{

msg="year input is wrong";

}

void showException()         //define function.

{

cout<<msg<<endl;

}

};

void read_date(int &day,int &month,int &year);         //declaration of function.

void read_date(int &d,int &m,int &y)                 //defination of function.

{    

  //function body.

cout<<"Enter day"<<endl;

cin>>d;

if(d<=0 ||d>31)

throw invalidDay();               //Exception.

cout<<"Enter month"<<endl;

cin>>m;

if(m<=0 ||m>=13)

throw invalidMonth();

cout<<"Enter year"<<endl;

cin>>y;

if(y%400==0||(y!=100&&y%4==0))

if(d>=30)

throw leapYear();

}

int main()                     //main function

{

   //define variable

int day,month,year;                  

string months[12]={"January","February","March",            

"April","May","June","July","August","September",

"October","November","December"};

//Exception handling.

try                               //try block

{

read_date(day,month,year);

cout<<"Date of Birth "<<months[month-1]<<" "<<day<<","<<year;

}  

//catch bolck.

catch(invalidDay id)

{

id.showException();

}

catch(invalidMonth im)

{

im.showException();

}

catch(leapYear ly)

{

ly.showException();

}

return 0;

}

Output:

Enter day

12

Enter month

12

Enter year

2019

Date of Birth December 12,2019.

Explanation:

In the above program firstly we define 3 class that is invalidDay, invalidMonth, and leapYear. In these classes, we define a method, variable, and constructor, and in the constructor, we holding the value of the msg(variable). Then we define a function that is read_data. This function collects all values from the user and throws into leap year function. In the last, we define the main function this function used exception handling. In the exception handling, we use multiple catch block with try block. After handling all the exception we called all function of the class.

You might be interested in
(Palindrome integer) Write the methods with the following headers // Return the reversal of an integer, i.e., reverse(456) retur
Debora [2.8K]

Answer:

import java.util.Scanner;

public class PalindromeInteger {

public static void main(String[] args) {

 // Create an object of the Scanner class to allow for user's inputs

 Scanner input = new Scanner(System.in);

 // Create a prompt to display the aim of the program.

 System.out.println("Program to check whether or not a number is a palindrome");

 // Prompt the user to enter an integer number.

 System.out.println("Please enter an integer : ");

 // Receive user's input and store in an int variable, number.

 int number = input.nextInt();

 // Then, call the isPalindrome() method to check if or not the

 // number is a  palindrome integer.

 // Display the necessary output.

 if (isPalindrome(number)) {

  System.out.println(number + " is a palindrome integer");

 }

 else {

  System.out.println(number + " is a not a palindrome integer");

 }

}

// Method to return the reversal of an integer.

// It receives the integer as a parameter.

public static int reverse(int num) {

 // First convert the number into a string as follows:

 // Concatenate it with an empty quote.

 // Store the result in a String variable, str.

 String str = "" + num;

 // Create and initialize a String variable to hold the reversal of the  

 // string str. i.e rev_str

 String rev_str = "";

 // Create a loop to cycle through each character in the string str,

 // beginning at  the last character down to the first character.

 // At every cycle, append the character to the rev_str variable.

 // At the end of the loop, rev_str will contain the reversed of str

 for (int i = str.length() - 1; i >= 0; i--) {

  rev_str += str.charAt(i);

 }

 // Convert the rev_str to an integer using the Integer.parseInt()

 // method.  Store the result in an integer variable reversed_num.

 int reversed_num = Integer.parseInt(rev_str);

 // Return the reversed_num

 return reversed_num;

}

// Method to check whether or not a number is a palindrome integer.

// It takes in the number as parameter and returns a true or false.

// A number is a palindrome integer if reversing the number does not

//  change the  number itself.

public static boolean isPalindrome(int number) {

 // check if the number is the same as its reversal by calling the

 //reverse()  method declared earlier. Return true if yes, otherwise,

               // return false.

 if (number == reverse(number)) {

  return true;

 }

 return false;

}

}

Explanation:

The source code file for the program has also been attached to this response for readability. Please download the file and go through the comments in the code carefully as it explains every segment of the code.

Hope this helps!

Download java
7 0
3 years ago
Which ribbon tab is not a default in Outlook 2016's main interface?
IRISSAK [1]

Answer:

view

Explanation:

i just know it

8 0
3 years ago
Read 2 more answers
Which path needs to be followed to change the currency settings of Windows Operating System?
PIT_PIT [208]

Answer:

To configure operating system settings, in Control Panel, click Performance And Maintenance. To view operating system performance configuration options, in the Performance And Maintenance window, click System, and then click the Advanced tab.

4 0
3 years ago
Write a programme to find the sum of first three cube number?
aleksandrvk [35]

// Simple Java program to find sum of series

// with cubes of first n natural numbers

import java.util.*;

import java.lang.*;

class GFG {

/* Returns the sum of series */

public static int sumOfSeries(int n)

{

int sum = 0;

for (int x = 1; x <= n; x++)

sum += x * x * x;

return sum;

}

// Driver Function

public static void main(String[] args)

{

int n = 5;

System.out.println(sumOfSeries(n));

}

}

// Code Contributed by Mohit Gupta_OMG <(0_o)>

7 0
3 years ago
What is the name of the technology that is typically implemented on switches to avoid Ethernet connectivity problems when the wr
Andrew [12]

Answer:  Medium Dependent Interface Crossover (MDIX)

Explanation:

The technology that is typically implemented on switches to avoid Ethernet connectivity problems is Medium Dependent Interface Crossover(MDIX) For switches with Auto (MDIX) the connection is configured automatically and a crossover or straight-through cable can be used to connect two switches. When this MDIX interface is connected it corrects the connectivity speed and the cabling for proper device functionality.

4 0
3 years ago
Other questions:
  • Why will the standard replacement algorithms (LRU, FIFO, clock) not be effective in handling this workload for a page allocation
    11·1 answer
  • which of the following are used on cable trays to protect the cable insulation from direct sunlight? a. barrier strips b. solid
    5·1 answer
  • What is the difference between chrome and chromium browser?
    9·1 answer
  • Does anyone watch The Office?
    14·1 answer
  • What can be done to improve the security of business uses of the Internet? Give several examples of ecurity measures and technol
    9·1 answer
  • Types of operating systems
    5·2 answers
  • Write any four difference between email and effects​
    14·1 answer
  • Let x = ["Red", 2.55,"Green", 3,"Black","false"], then solve the following:
    7·1 answer
  • What is a type of transition used for music in Earsketch
    11·1 answer
  • What is boot sector virus​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!