using C++ program Write a program to determine, and output to the screen, the cl
ID: 3842246 • Letter: U
Question
using C++ program
Explanation / Answer
Ans 1.
#include <iostream>
#include<string> // for string class
using namespace std;
class Earthquake{
int magnitude;
string earthquakeClass;
public:
/* this function will determine the class of earthquake based on the magnitude set for the object which is used to call this method */
void determineEarthquakeClass(){
if(magnitude >= 0 && magnitude <4){
setEarthquakeClass("MINOR");
} else if(magnitude >= 4 && magnitude <5){
setEarthquakeClass("LIGHT");
} else if(magnitude >= 5 && magnitude <6){
setEarthquakeClass("MODERATE");
} else if(magnitude >= 6 && magnitude <7){
setEarthquakeClass("STRONG");
} else if(magnitude >= 7 && magnitude <8){
setEarthquakeClass("MAJOR");
} else if(magnitude>=8){
setEarthquakeClass("GREAT");
}
}
// getters and setters for data member variables
int getMagnitude() {
return magnitude;
}
void setMagnitude(int magni) {
magnitude = magni;
}
string getEarthquakeClass() {
return earthquakeClass;
}
void setEarthquakeClass(string eClass) {
earthquakeClass = eClass;
}
};
int main() {
int magnitude;
//declaring object of class Earthquake
Earthquake earthquake;
cout<<"Enter the magnitude";
cin>>magnitude;
//set value of magnitude for object earthquake
earthquake.setMagnitude(magnitude);
if(magnitude<0){
cout<<" Please enter a valid magnitude value!";
} else{
//call member function determineEarthquakeClass
earthquake.determineEarthquakeClass();
cout<<" This earthquake is rated as: "<<earthquake.getEarthquakeClass();
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.