In this exercise you will be creating a Student ADT which keeps track of some ba
ID: 3665675 • Letter: I
Question
In this exercise you will be creating a Student ADT which keeps track of some basic information about a student in a particular course, using the given student record type: In this question, we want you to design an ADT called Student similar to the book example covered in class, to represent some of the information that a class list of students might need.
It won’t be completely realistic, but it will help you put into practice the concepts of ADTs.
Using pseudo-code, complete the following steps:
1. Build a student record. A Student record should include:
(a) a student’s name (first and last), 4
(b) a student number,
(c) an NSID (aaannn format e.g abd123),
(d) an array of 10 assignment grades,
(e) a midterm exame grade and a final exam grade. Design a record type to contain all of this data. A
dd any additional fields that you think might be necessary to make this record type complete or manage it more effectively.
2. Design and implement the following operations: • createStudentRecord() - creates a new blank student record on the heap and returns a reference to the record Correction: you may add parameters to this method which will contain a given student’s information to be stored in the record initially. This is optional. •destroyStudentRecord(s) - de-allocates the student record s
• displayStudentRecord(s) - displays the student record s on the console
• readStudentRecordFromConsole() - obtains data for a student record from the input stream and returns a reference to the new populates student record.
• changeAssignmentGradeForStudents(s, a, g) - adds a given number g to the grade of a given assignment number a for a given student record s
• changeExamGradeForStudent(s, x, e) - adds a given number e to the x (’M’=midterm, ’F’=final) exam grade of a given student record s
• calculateAverageGrade(s) - calculates average grade for student s and returns the answer.
Assume that the assignments count for 30%, the midterm counts for 25% and the final counts for 45%.
The parameters and details of these operations are not precisely states, so make some reasonable assumptions to assist your design. Your records should be allocated on the heap, and you should use references to records as parameters.
Every operation should have a full algorithm header, including pre-, post- and return descriptions.
Every function should have a body that describes the function in enough detail that you can simply translate it to C++ when the time comes.
3. In a main, write an algorithm that uses the above operations to do the following:
• create an array of 10 students with initial values received from the user
• print out each student to the console, one at a time
• calculate the class average and print out your result
Note: The array for 10 students is an array of references
Explanation / Answer
testing.cpp
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
#include "Student.h"
// Algorithm copy(s)
// Pre: s :: refToChar
// Post: memory allocated on heap to store a copy
// Return: reference to new string
char *copy(char *s) {
char *temp = new char[strlen(s)+1];
strcpy(temp,s);
return temp;
}
// Pre: none
// Post: reads string from console, allocates memory on heap
// Return: reference to string
char *readAndCopyCString() {
char temp[100];
cin >> temp;
char *result = new char[strlen(temp)+1];
strcpy(result,temp);
return result;
}
// Algorithm readStudentRecordFromConsole()
// Pre: none
// Post: data for a valid Student record has been read from the input console
// Returns: reference to the newly created student record
Student *readStudentRecordFromConsole() {
//Student *s = createStudentRecord();
cout << "First Name: ";
//s->firstName = readAndCopyCString(); // used from previous assignment
char *firstName = readAndCopyCString();
cout << "Last Name: ";
//s->lastName = readAndCopyCString();
char *lastName = readAndCopyCString();
cout << "Student Number: ";
//cin >> s->studentNumber; // integer read directly
int studentNumber;
cin >> studentNumber;
cout << "NSID: ";
//s->NSID = readAndCopyCString(); // could now check if length entered was only 6 characters
char *nsid = readAndCopyCString();
float grades[10];
for (int i = 0; i < 10; i++) {
cout << "Student grade " << i << ": ";
//cin >> s->getGrade(i);
cin >> grades[i];
}
cout << "Midterm exam grade: ";
float mtmark;
//cin >> s->getMT() ;
cin >> mtmark;
cout << "Final exam grade: ";
//cin >> s->getFinal() ;
float fmark;
cin >> fmark;
Student *dude = new Student(firstName, lastName, studentNumber, nsid, grades, mtmark, fmark);
return dude;
}
// Algorithm create_TestStudentRecord()
// Post: creates a student record for testing other functions
// Return: the reference
Student *create_TestStudentRecord() {
char f[] = "FIRSTNAME";
char l[] = "LASTNAME";
char n[] = "abc123";
float g[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
Student *testS = new Student(f, l, 12345678, n, g, 66, 88);
return testS;
}
void testStudentRecordOperations() {
int before, before2;
// does a lot of testing!
Student *testS = NULL;
cout << "=============================" << endl;
cout << "Testing createStudentRecord()" << endl;
testS = new Student();
if (testS == NULL) {
cout << "Create student test failed." << endl;
return;
}
else {
cout << "Create student test succeeded." << endl;
}
cout << "=============================" << endl;
cout << "deallocating record" << endl;
delete testS;
cout << "deallocation did not fail horribly." << endl;
// you can't really test deallocation without
// looking into the memory manager, so that's
// the best we can do
cout << "=============================" << endl;
cout << "Testing displayStudentRecord()" << endl;
cout << "1. Testing call with hard coded data" << endl;
testS = create_TestStudentRecord();
testS->displayStudentRecord();
cout << "does everything look right?" << endl;
delete testS;
cout << "=============================" << endl;
cout << "To test readStudentRecordFromConsole()" << endl;
cout << "enter data for a student" << endl;
testS = readStudentRecordFromConsole();
cout << "Now compare to what gets displayed." << endl;
testS->displayStudentRecord();
cout << "does everything look right?" << endl;
delete testS;
cout << "=============================" << endl;
cout << "Testing changeAssignmentGradeForStudent()" << endl;
cout << "1. Testing call with hard coded data" << endl;
for (int a = 0; a < 10; a++) {
testS = create_TestStudentRecord();
cout << "testing changes to assignment grade at index " << a << endl;
before = testS->getGrade(a);
testS->changeAssignmentGradeForStudent(a,1);
if (testS->getGrade(a) == before + 1) {
cout << "test passed" << endl;
}
else {
cout << "test did not pass for some reason" << before << " " << testS->getGrade(a) << endl;
}
delete testS;
}
cout << "=============================" << endl;
cout << "Testing changeExamGradeForStudent()" << endl;
cout << "1. Testing with hard coded data" << endl;
testS = create_TestStudentRecord();
cout << "testing changes to midterm grade" << endl;
before = testS->getMT();
testS->changeExamGradeForStudent('M',1);
if (testS->getMT() == before + 1) {
cout << "test passed" << endl;
}
else {
cout << "test did not pass for some reason" << before << " " << testS->getMT() << endl;
}
cout << "testing changes to final exam grade" << endl;
before = testS->getFinal();
testS->changeExamGradeForStudent('F',1);
if (testS->getFinal() == before + 1) {
cout << "test passed" << endl;
}
else {
cout << "test did not pass for some reason" << before << " " << testS->getFinal() << endl;
}
cout << "testing nonsense command " << endl;
before2 = testS->getMT();
before = testS->getFinal();
testS->changeExamGradeForStudent('Q',-1);
if (testS->getFinal() == before && testS->getMT() == before2) {
cout << "test passed" << endl;
}
else {
cout << "test did not pass for some reason" << testS->getMT() << " " << testS->getFinal() << endl;
}
delete testS;
cout << "Done testing!" << endl;
}
int main () {
testStudentRecordOperations();
return 0;
}
Student.h
// file: Student.h
#ifndef _STUDENT_H_
#define _STUDENT_H_
class Student {
private:
char *firstName; // string
char *lastName; // string
int studentNumber;
char *NSID; // string format aaannn
float grades[10]; // array
float mtExam; // midterm exam grade
float finalExam; // final exam grade
public:
// Algorithm createStudentRecord()
// Pre: none
// Post: none
// Returns: refToStudent, a valid reference to Student c/w all internal references allocated
Student() ;
// Algorithm destroyStudentRecord(s)
// Pre: s :: refToStudent, a valid Student record reference
// Post: s :: refToStudent, deleted
// Returns: nothing
Student(char* , char* , int , char*, float[10] , float, float) ;
~Student() ;
// Algorithm displayStudentRecord(s)
// Pre: s :: refToStudent, a valid Student record reference
// Post: All data held in s is displayed on the console screen
// Returns: nothing
//
void displayStudentRecord() ;
// Algorithm changeAssignmentGradeForStudent(s,a,g)
// Pre: s :: refToStudent is a valid reference to a student record;
// a :: integer between 0 and 9;
// g :: float, a valid grade change amount (negative or positive)
// Post: The grade for the assignment of the student record has been adjusted by the amount.
// Returns: nothing.
void changeAssignmentGradeForStudent(int a, float g);
// Algorithm changeExamGradeForStudent(s,x,e)
// Pre: s :: refToStudent, a valid reference to a student record;
// x :: character, is either 'M' or 'F';
// e :: float, is a valid exam grade change amount (negative or positive)
// Post: The grade for the exam of the student record has been adjusted by the amount.
// Returns: nothing.
void changeExamGradeForStudent(char x, float e) ;
//Algorithm getGrade
//Pre: none
//Post: Does nothing but retrive a value
//Return: returns the Value requested
float getGrade(int) ;
//Algorithm getMTD
//Pre: none
//Post: Does nothing but retrive a value
//Return: returns the Value requested
float getMT() ;
//Algorithm getFinal
//Pre: none
//Post: Does nothing but retrive a value
//Return: returns the Value requested
float getFinal() ;
};
#endif
Student.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "Student.h"
// Algorithm createStudentRecord()
// Pre: none
// Post: none
// Returns: refToStudent, a valid reference to Student c/w all internal references allocated
Student::Student() {
//Student *sr = new Student;
firstName = NULL;
lastName = NULL;
studentNumber = -99; // invalid number, but recognizable
NSID = NULL; // it will be allocated somewhere else
mtExam = 0;
finalExam = 0;
for (int i = 0; i < 10; i++) {
grades[i] = 0;
}
}
// Algorithm destroyStudentRecord(s)
// Pre: s :: refToStudent, a valid Student record reference
// Post: s :: refToStudent, deleted
// Returns: nothing
Student::~Student() {
/* removed cuz objects
if (s==NULL) {
return;
}
*/
if (firstName != NULL) {
delete[] firstName ;
}
if (lastName != NULL) {
delete[] lastName;
}
if (NSID != NULL) {
delete[] NSID ;
}
}
Student::Student(char* _firstName, char* _lastName, int _studentNumber, char* _NSID, float _grades[10], float _mtExam, float _finalExam) {
firstName = strdup(_firstName);
lastName = strdup(_lastName);
studentNumber = _studentNumber;
NSID = strdup(_NSID);
for (int i = 0; i < 10; i++) {
grades[i] = _grades[i];
}
mtExam = _mtExam;
finalExam = _finalExam;
}
// Algorithm displayStudentRecord(s)
// Pre: s :: refToStudent, a valid Student record reference
// Post: All data held in s is displayed on the console screen
// Returns: nothing
//
void Student::displayStudentRecord() {
/* removed cuz objects
if (s==NULL) {
return;
}
*/
cout << "+++++++++++++++++++++++++++++++++++++++++++++++" << endl;
cout << "First Name: " << firstName << endl;
cout << "Last Name: " << lastName << endl;
cout << "Student Number: " << studentNumber << endl;
cout << "NSID: " << NSID << endl;
for (int i = 0; i < 10; i++) {
cout << "Student grade: " << i << " = " << grades[i] << endl;
}
cout << "Midterm exam grade: " << mtExam << endl;
cout << "Final exam grade: " << finalExam << endl;
cout << "+++++++++++++++++++++++++++++++++++++++++++++++" << endl;
}
// Algorithm changeAssignmentGradeForStudent(s,a,g)
// Pre: s :: refToStudent is a valid reference to a student record;
// a :: integer between 0 and 9;
// g :: float, a valid grade change amount (negative or positive)
// Post: The grade for the assignment of the student record has been adjusted by the amount.
// Returns: nothing.
void Student::changeAssignmentGradeForStudent(int a, float g) {
/* removed cuz objects
if (s==NULL || a < 0 || a > 9) {
return;
}
*/
grades[a] = grades[a] + g;
}
// Algorithm changeExamGradeForStudent(s,x,e)
// Pre: s :: refToStudent, a valid reference to a student record;
// x :: character, is either 'M' or 'F';
// e :: float, is a valid exam grade change amount (negative or positive)
// Post: The grade for the exam of the student record has been adjusted by the amount.
// Returns: nothing.
void Student::changeExamGradeForStudent(char x, float e) {
/* removed cuz objects
if (s==NULL) {
return;
}
*/
if (x =='M') {
mtExam = mtExam + e;
}
else if (x == 'F') {
finalExam =finalExam + e;
}
}
//Algorithm getGrade
//Pre: none
//Post: Does nothing but retrive a value
//Return: returns the Value requested
float Student::getGrade(int index) {
return grades[index];
}
//Algorithm getMTD
//Pre: none
//Post: Does nothing but retrive a value
//Return: returns the Value requested
float Student::Student::getMT() {
return mtExam;
}
//Algorithm getFinal
//Pre: none
//Post: Does nothing but retrive a value
//Return: returns the Value requested
float Student::getFinal() {
return finalExam;
}
// eof
sample output
Testing createStudentRecord()
Create student test succeeded.
=============================
deallocating record
deallocation did not fail horribly.
=============================
Testing displayStudentRecord()
1. Testing call with hard coded data
+++++++++++++++++++++++++++++++++++++++++++++++
First Name: FIRSTNAME
Last Name: LASTNAME
Student Number: 12345678
NSID: abc123
Student grade: 0 = 10
Student grade: 0 = 10
Student grade: 1 = 20
Student grade: 2 = 30
Student grade: 3 = 40
Student grade: 4 = 50
Student grade: 5 = 60
Student grade: 6 = 70
Student grade: 7 = 80
Student grade: 8 = 90
Student grade: 9 = 100
Midterm exam grade: 66
Final exam grade: 88
// due to limited number of characters i cant able to submit entire sample output but program working fine
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.