Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, Everybody I just finished adding the test case 2. I need a test 3 in which the grades of lowest student will swap with

Hello, Everybody

I just finished adding the test case 2. I need a test 3 in which the grades of lowest student will swap with student with the highest grade and return student with a new grade.

Please do not modify test 1 and 2. If you feel that it is needed, a create a new function in the StudentGrades.cpp, to be called in new function into StudentGrades.h and main.cpp. Thank you

Here is the 5 parts of my code.

(1) Student.h

#include

#include

using namespace std;

#ifndef STUDENT_H

#define STUDENT_H

class Student

{

public:

Student(); // default constructor

Student(std::string n, int g); // constructor

~Student(); // destructor

void setName(std::string name); // mutator function

void setGrade(int g); // mutator function

std::string getName() const; // accessor function

int getGrade() const; // accessor function

private:

std::string name; // holds the student name

int grade; // holds the student grage

};

#endif

(2) Student.cpp

#include

#include

#include "Student.h"

using namespace std;

Student::Student()

{

}

Student::Student(std::string n, int g) : name(n), grade(g)

{}

Student::~Student()

{}

void Student::setName(std::string name)

{

this->name = name;

}

void Student::setGrade(int g)

{

grade = g;

}

std::string Student::getName() const

{

return name;

}

int Student::getGrade() const

{

return grade;

}

(3) StudentGrades.h

#include

#include

#include "Student.h"

#ifndef STUDENTGRADES_H

#define STUDENTGRADES_H

class StudentGrades

{

public:

StudentGrades(); // default constructor

~StudentGrades(); // destructor

StudentGrades& operator = (const StudentGrades &S);//assignment

StudentGrades(StudentGrades &S);//copy Construction

Student getStudentWithHighestGrade() const; // accessor function

void addStudentRecord(std::string name, int grade);

void resetList();

private:

const static int MAX_STUDENTS = 5;

int numberOfStudents;

Student *students;

};

#endif

(4) StudentGrades.cpp

#include

#include

#include "StudentGrades.h"

using namespace std;

StudentGrades::StudentGrades() : numberOfStudents(0)

{

students = new Student[MAX_STUDENTS];

}

StudentGrades::StudentGrades(StudentGrades &S) : numberOfStudents(S.numberOfStudents), students(S.students)

{

}

StudentGrades & StudentGrades ::operator = (const StudentGrades &S)

{

this->numberOfStudents = S.numberOfStudents;

this->students = new Student[MAX_STUDENTS];

for (int i = 0; i

{

this->students[i] = S.students[i];

}

return *this;

}

StudentGrades::~StudentGrades()

{

delete[] students;

}

Student StudentGrades::getStudentWithHighestGrade() const

{

Student returnValue = students[0];

for (int i = 1; i < (numberOfStudents - 1); i++)

{

if (returnValue.getGrade() < students[i].getGrade())

{

returnValue = students[i];

}

}

return returnValue;

}

void StudentGrades::addStudentRecord(std::string name, int grade)

{

if (numberOfStudents < MAX_STUDENTS)

{

students[numberOfStudents].setName(name);

students[numberOfStudents].setGrade(grade);

numberOfStudents++;

}

}

void StudentGrades::resetList()

{

students[0].setName("No Entry");

students[0].setGrade(0);

for (int i = 1; i < numberOfStudents; i++)

{

students[i].setName("No Entry");

students[i].setGrade(0);

}

numberOfStudents = 0;

}

(5) Main.cpp

#include

#include

#include "StudentGrades.h"

using namespace std;

int main()

{

int count = 1;

StudentGrades a;

StudentGrades b; // object b will be used for the tests that require b = a;

StudentGrades c;

Student s;

Student r;

Student t;

a.addStudentRecord("Wade", 82);

b.addStudentRecord("Logan", 90);

c.addStudentRecord("Jean", 95);

// Test Case 1

s = a.getStudentWithHighestGrade();

r = b.getStudentWithHighestGrade();

t = c.getStudentWithHighestGrade();

// get the student with highest grade at initialization

if ((s.getName().compare("No Entry") == 0) && (s.getGrade() == 0) || (r.getName().compare("No Entry") == 0) && (r.getGrade() == 0) ||(t.getName().compare("No Entry") == 0) && (t.getGrade() == 0))

{

// if the test case is meet then Pass

std::cout << "Test Case 1 Passes" << std::endl;

}

else // else test case fails

{

if ((s.getGrade() > r.getGrade() && t.getGrade()))

{

std::cout << "Student " << s.getName() << " has highest grade of " << s.getGrade() << std::endl;

}

else if ((s.getGrade() && t.getGrade() < r.getGrade()))

{

std::cout << "Student " << r.getName() << " has highest grade of " << r.getGrade() << std::endl;

}

else

{

std::cout << "Student " << t.getName() << " has highest grade of " << t.getGrade() << std::endl;

}

std::cout << "Test Case 1 Fails" << std::endl;

}

//Test Case 2

// get the student with lowest grade at initialization

if(s.getGrade() < r.getGrade() && t.getGrade())

{

std::cout << "Student " << s.getName() << " has lowest grade of " << s.getGrade() << std::endl;

std::cout << "Test Case 2 Passes" << std::endl;

}

else if (t.getGrade() < s.getGrade() && r.getGrade())

{

std::cout << "Student " << t.getName() << " has lowest grade of " << t.getGrade() << std::endl;

std::cout << "Test Case 2 Passes" << std::endl;

}

else if (r.getGrade() < s.getGrade() && t.getGrade())

{

std::cout << "Student " << r.getName() << " has lowest grade of " << r.getGrade() << std::endl;

std::cout << "Test Case 2 Passes" << std::endl;

}

else

{

std::cout << "No Entry " << " score of " << std::endl;

std::cout << "Test Case 2 Fails" << std::endl;

}

//Test Case 3 Swaps the Grade value of Students (Need to be added)

}

Thank you

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions