1) Test Scores Class Write a class named TestScores. The class constructor shoul
ID: 3820793 • Letter: 1
Question
1) Test Scores Class Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program.
2).
Month Class Exceptions
Programming Challenge 5 of Chapter 8 required you to write a Month class that holds information about a 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 class in a program.
programming challenge 5 ( Month Class Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods: • A no-arg constructor that sets the monthNumber field to 1. • A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than 1 or greater than 12 is passed, the constructor should set monthNumber to 1. • A constructor that accepts the name of the month, such as “January” or “February” as an argument. It should set the monthNumber field to the correct corresponding value. • A setMonthNumber method that accepts an int argument, which is assigned to the monthNumber field. If a value less than 1 or greater than 12 is passed, the method should set monthNumber to 1. • A getMonthNumber method that returns the value in the monthNumber field. • A getMonthName method that returns the name of the month. For example, if the monthNumber field contains 1, then this method should return “January”. • A toString method that returns the same value as the getMonthName method. • An equals method that accepts a Month object as an argument. If the argument object holds the same data as the calling object, this method should return true. Otherwise, it should return false.
Explanation / Answer
1)
#include<iostream>
//declare how many test_scores are there , change this if you want more test scores
#define MAX 5
using namespace std;
class TestScores :exception
{
int test_scores[MAX];
public:
TestScores(int scores[])
{
exception e("IllegalArgumentException");
for (int i = 0; i < MAX; i++)
{
try
{
test_scores[i] = scores[i];
if (test_scores[i] < 0 || test_scores[i] > 100)
throw e;
}
catch (exception &e)
{
cout << e.what() << endl;
return ;
}
}
}
float average()
{
float avg = 0;
exception e("IllegalArgumentException");
for (int i = 0; i < MAX; i++)
{
try
{
if (test_scores[i] < 0 || test_scores[i] > 100)
throw e;
avg += test_scores[i];
}
catch (exception &e)
{
cout << e.what() << endl;
return -1;
}
}
avg /= MAX;
return avg;
}
};
int main()
{
//set scores in array and create object of type TestScores
int marks[] = { -78, 90, 67, 89, 96 };
TestScores score(marks);
cout << "Test score Average = " << score.average()<<endl;
}
---------------------------------------
//output
Test score Average = 84
for test_score invalid value , output is as below
IllegalArgumentException
IllegalArgumentException
Test score Average = -1
--------------------------------------
//pg2
#include<iostream>
using namespace std;
class MonthException:exception
{
public:
virtual const char* what() const throw() // call to the std exception class function
{
return "Invalid month ..."; // my error message
}
};
class Month : public MonthException
{
int monthNumber;
public:
Month(int m)
{
MonthException myexception;
try
{
if (m < 1 || m > 12)
throw myexception;
monthNumber = m;
}
catch (MonthException &e)
{
cout << e.what() << endl;;
}
}
};
int main()
{
Month m(0);
}
------------------------------------
//output for month = 0
Invalid month ...
---------------------------------------
//pg3
class MonthException:exception
{
public:
virtual const char* what() const throw() // call to the std exception class function
{
return "Invalid month ..."; // my error message
}
};
string Months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
class Month : public MonthException
{
int monthNumber;
public:
Month()
{
monthNumber = 1;
}
Month(int m)
{
MonthException myexception;
try
{
if (m < 1 || m > 12)
throw myexception;
monthNumber = m;
}
catch (MonthException &e)
{
cout << e.what() << endl;;
}
}
Month(string name)
{
for (int i = 0; i < 12; i++)
{
if (name == Months[i])
{
monthNumber = i + 1;
return;
}
}
}
void setMonthNumber(int m)
{
MonthException myexception;
try
{
if (m < 1 || m > 12)
throw myexception;
monthNumber = m;
}
catch (MonthException &e)
{
cout << e.what() << endl;;
}
}
int getMonthNumber()
{
return monthNumber;
}
string getMonthName()
{
return Months[monthNumber - 1];
}
string toString()
{
return Months[monthNumber - 1];
}
bool operator==(Month &obj)
{
if (monthNumber == obj.monthNumber)
return true;
else
return false;
}
};
int main()
{
Month m;
Month m1(5);
Month m2("April");
cout << "m month number= " << m.getMonthNumber() << "m1 month number= " << m1.getMonthNumber() << "m2 month number= " << m2.getMonthNumber() << endl;
cout << "m month name= " << m.getMonthName() << "m1 month number= " << m1.getMonthName() << "m2 month number= " << m2.getMonthName() << endl;
m1.setMonthNumber(4);
if (m1 == m2)
{
cout << "m1 and m2 are equal" << endl;
}
else
{
cout << "m1 and m2 are not equal" << endl;
}
//check string method
cout<<"Month name of m1 = "<<m1.toString();
}
//output
m month number= 1m1 month number= 5m2 month number= 4
m month name= Januarym1 month number= Maym2 month number= April
m1 and m2 are equal
Month name of m1 = April
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.