Question

In: Computer Science

C++ program homework question 1 1. Create and implement a class called clockType with the following...

C++ program homework question 1

1. Create and implement a class called clockType with the following data and methods (60 Points.):

  • Data:
    • Hours, minutes, seconds
  • Methods:
    • Set and get hours
    • Set and get minutes
    • Set and get seconds
    • printTime(…) to display time in the form of hh:mm:ss
    • default and overloading constructor
  1. Overloading Operators:
    • << (extraction) operator to display time in the form of hh:mm:ss
    • >> (insertion) operator to get input for hours, minutes, and seconds
    • operator+=(int x) (increment operator) to increment by x seconds
    • operator<=(…) which returns true if time of the clock is less than or equal to another clock
    • operator>=(…) which returns true if time of the clock is greater than or equal to another clock

Derive a new class called extClockType from the class clockType by adding a member variable to store the time zone. Add the necessary member functions and constructors and implement them

Solutions

Expert Solution

Please find the code , output below.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
// Class declaration
class clockType {
  
public:
int hours, minutes, seconds;
clockType (int hours1, int minutes1, int seconds1){
hours=hours1;
minutes=minutes1;
seconds=seconds1;
}
void sethours(int hours1){
hours=hours1;
}
void setminutes(int minutes1){
minutes=minutes1;
}
void setseconds(int seconds1){
seconds=seconds1;
}
int gethouors(){
return hours;
}
int getminutes(){
return minutes;
}
int getseconds(){
return seconds;
}
// Printing the output
void printTime(){
if(hours<10 && minutes>=10 && seconds >=10){
cout<<"The time is 0"<<hours<<" : "<< minutes<< " : "<<seconds<<endl;
}
else if (hours>=10 && minutes < 10 && seconds>=10){
cout<<"The time is "<<hours<<" : 0"<< minutes<< " : "<<seconds<<endl;
}
else if (hours>=10 && minutes >= 10 && seconds <10){
cout<<"The time is "<<hours<<" : "<< minutes<< " : 0"<<seconds<<endl;
}
else if (hours>=10 && minutes < 10 && seconds <10){
cout<<"The time is "<<hours<<" : 0"<< minutes<< " : 0"<<seconds<<endl;
}
else if (hours <10 && minutes >= 10 && seconds <10){
cout<<"The time is 0"<<hours<<" : "<< minutes<< " : 0"<<seconds<<endl;
}
else if (hours <10 && minutes < 10 && seconds >=10){
cout<<"The time is 0"<<hours<<" : 0"<< minutes<< " : "<<seconds<<endl;
}
else if (hours<10 && minutes < 10 && seconds <10){
cout<<"The time is 0"<<hours<<" : 0"<< minutes<< " : 0"<<seconds<<endl;
}
else{
cout<<"The time is "<<hours<<" : "<< minutes<< " : "<<seconds<<endl;
}
}
};
// Child class
class extClockType : public clockType {
public:
vector<string> time_zone;
extClockType(int hours1, int minutes1, int seconds1,string timezone): clockType(hours1,minutes1,seconds1){
time_zone.push_back(timezone);
}
void printTimezone(){
cout<<"\nThe timezone is "<<time_zone[0];
}
};

int main()
{   
// Getting input from user
int hours, minutes, seconds;
cout<<"Enter the hours : ";
cin>>hours;
if(hours<0 || hours>24){
cout<< "Invalid hours !!!";
exit(0);
}
cout<<"Enter the minutes : ";
cin>>minutes;
if(minutes<0 || minutes>60){
cout<< "Invalid minutes !!!";
exit(0);
}
cout<<"Enter the seconds : ";
cin>>seconds;
if(seconds<0 || seconds>60){
cout<< "Invalid seconds !!!";
exit(0);
}
// Creating object of clockType class
clockType ct(hours,minutes,seconds);
  
//ct.printTime();
extClockType ect(hours,minutes,seconds,"Pacific");
// Calling the function
ect.printTime();
ect.printTimezone();
return 0;
}


Related Solutions

1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
The following program will be written in JAVA. Create a class called complex performing arithmetic with...
The following program will be written in JAVA. Create a class called complex performing arithmetic with complex numbers. Write a program to test your class.                         Complex numbers have the form:                         realPart + imaginaryPart * i                                               ___                         Where i is sqrt(-1)                                                 Use double variables to represent data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in...
JAVA Homework 1) Create a die class. This is similar to the coin class , but...
JAVA Homework 1) Create a die class. This is similar to the coin class , but instead of face having value 0 or 1, it now has value 1,2,3,4,5, or 6. Also instead of having a method called flip, name it roll (you flip a coin but roll a die). You will NOT have a method called isHeads, but you will need a method (call it getFace ) which returns the face value of the die. Altogether you will have...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables:...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables: StudentName (string), SchoolYear (int), YearsUntilGraduation(int) * Method YTK() = 12 - SchoolYear; 2. Main *Enter name *Enter age *You will attend school:____ years before graduating.
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT