Programming with C++ - Day3 || Wong Edition || B.Tech Diaries

Lab #3 - Classes

Question 1 - Rectangle

Implement the Rectangle class according to the given Class Diagram.

Rectangle
- length : int
- width: int
- colour : string
+ getArea() : int
+ print() : string
+ compare(Rectangle r1) : int

Write a constructor which takes as input the length and width of a Rectangle instance. In the default constructor, initalise the length and width to 0;

The function compare returns

    0, if the Rectangle instances are of the same size,
    1, if r1 is larger
    -1, if r1 is smaller 

The function print gives the dimensions, colour and area of the Rectangle instance.

In the main function of Rectangle, obtain and validate user input for the Rectangle's dimensions and colour. Create 2 Rectangle instances and compare their area. [Hint: check for type and negative numbers]

Solution:


    #include <iostream>
    #include <sstream>

    class Rectangle
    {
    private:
        int length, breadth;
        std::string colour;

    public:
        Rectangle();
        Rectangle(int, int, std::string);
        ~Rectangle();
        int getArea();
        std::string print();
        int compare(Rectangle);
    };

    Rectangle::Rectangle()
    {
        length = 0;
        breadth = 0;
        colour = "";
    }

    Rectangle::Rectangle(int length, int breadth, std::string colour)
    {
        this->length = length;
        this->breadth = breadth;
        this->colour = colour;
    }

    Rectangle::~Rectangle()
    {
    }

    int Rectangle::getArea()
    {
        return this->length * this->breadth;
    }

    std::string Rectangle::print()
    {
        std::stringstream ps;
        ps << "nLength: " << this->length
        << "nBreadth: " << this->breadth
        << "nColour: " << this->colour
        << "nArea: " << this->getArea() << std::endl;
        return ps.str();
    }

    int Rectangle::compare(Rectangle rect)
    {
        if (this->getArea() > rect.getArea())
            return 1;
        else if (this->getArea() < rect.getArea())
            return -1;
        return 0;
    }

    int main(int argc, char const *argv[])
    {
        int len, br;
        std::string col;

        // std::cout << "Enter dimensions for Rectangle 1:n"
        //           << "Length:";
        while (std::cin >> len)
        {
            if (len > 0)
                break;
            if (len <= 0)
                std::cout << "Length should be greater than 0n";
        }
        // std::cout << "Breadth:";
        while (std::cin >> br)
        {
            if (br > 0)
                break;
            if (br <= 0)
                std::cout << "Breadth should be greater than 0" << std::endl;
        }
        // std::cout << "Color:";
        std::cin.ignore();
        std::cin >> col;
        Rectangle rect1(len, br, col);

        // std::cout << "Enter dimensions for Rectangle 2:n"
        //           << "Length:";
        while (std::cin >> len)
        {
            if (len > 0)
                break;
        }
        // std::cout << "Breadth:";
        while (std::cin >> br)
        {
            if (br > 0)
                break;
        }
        // std::cout << "Color:";
        std::cin.ignore();
        std::cin >> col;
        Rectangle rect2(len, br, col);

        std::cout << "Rectangle 1n"
                << rect1.print() << std::endl;
        std::cout << "Rectangle 2n"
                << rect2.print() << std::endl;

        if (rect1.compare(rect2) == 1)
            std::cout << "Rectangle 1 is bigger than Rectangle 2" << std::endl;
        else if (rect1.compare(rect2) == -1)
            std::cout << "Rectangle 2 is bigger than Rectangle 1" << std::endl;
        else
            std::cout << "Rectangle 1 and Rectangle 2 are equal" << std::endl;

        return 0;
    }  

Output:


    12
    54
    Red
    24
    32
    Green
  
    Rectangle 1
    
    Length: 12
    Breadth: 54
    Colour: Red
    Area: 648
    
    Rectangle 2
    
    Length: 24
    Breadth: 32
    Colour: Green
    Area: 768
    
    Rectangle 2 is bigger than Rectangle 1

Question 2 - Student Records

In a UML Diagram, design a Student class with the following attributes:-

    name (max 30 characters)
    rollNumber (between 1 and 260)
    mobileNumber (10 digits)
    emailAddress (must contain '@' and then '.')
    attendance (between 0 and 1, where 1 is 100% attendance)

Provide the following functions:-

    getName, getRollNumber, ...
    setName, setRollNumber, ...
    print, which outputs the Student details in a comma-delimited string

    e.g. Donald Duck, 123, 1234567890, donald@duck.com, 0.00

The Student records are managed by the StudentManager class, which holds the main() loop.

StudentManager
- studentList:Student[10]
* getStudentInput:Student
* sortByRollNumber:Student[]
* sortByAttendance:Student[]
* printAttendanceReport(filename, sortBy:bool) : bool

The function printAttendanceReport outputs the list of students to a user-specified filename. If sortBy is true, the student list will be sorted by roll number, else the student list will be sorted by attendance (followed by roll number).

Provide the following user menu:

  1. Enter Student Details
  2. Update Student Mobile Number
  3. Update Student Email Address
  4. Print Attendance Report by Roll Number
  5. Print Attendance Report by Attendance
  6. Exit

Solution:


#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>

class Student
{
private:
    std::string name;
    int rollNumber;

public:
    long mobileNumber;
    std::string emailAddress;
    double attendance;

    Student();
    ~Student();
    std::string getName();
    int getRollNumber();
    void setName(std::string);
    void setRollNumber(int);
    std::string print();
};

Student::Student()
{
    this->name = "";
    this->rollNumber = 0;
    this->mobileNumber = 0;
    this->emailAddress = "";
    this->attendance = 0;
}

Student::~Student()
{
}

std::string Student::getName()
{
    return name;
}

int Student::getRollNumber()
{
    return rollNumber;
}

void Student::setName(std::string name)
{
    this->name = name;
}

void Student::setRollNumber(int rollNumber)
{
    this->rollNumber = rollNumber;
}

std::string Student::print()
{
    std::stringstream ps;
    ps << name << "," << rollNumber << "," << mobileNumber << "," << emailAddress << "," << attendance;
    return ps.str();
}

class StudentManager
{
private:
    std::vector<Student> studentList;

public:
    StudentManager();
    ~StudentManager();
    Student getStudentInput();
    void addStudent(Student);
    bool isEmpty();
    Student getStudent(int);
    bool setStudent(int, Student);
    std::vector<Student> sortByRollNumber();
    std::vector<Student> sortByAttendance();
    bool printAttendanceReport(std::string, bool);
};

StudentManager::StudentManager()
{
}

StudentManager::~StudentManager()
{
}

void StudentManager::addStudent(Student student)
{
    studentList.push_back(student);
}

Student StudentManager::getStudent(int rollno)
{
    for (int i = 0; i < studentList.size(); i++)
    {
        if (studentList[i].getRollNumber() == rollno)
        {
            return studentList[i];
        }
    }
    return Student();
}

bool StudentManager::setStudent(int rollno, Student student)
{
    for (int i = 0; i < studentList.size(); i++)
    {
        if (studentList[i].getRollNumber() == rollno)
        {
            studentList[i] = student;
            return true;
        }
    }
    return false;
}

bool StudentManager::isEmpty()
{
    return studentList.empty();
}

Student StudentManager::getStudentInput()
{
    Student student;
    std::string name;
    int rollNumber;
    long mobileNumber;
    std::string emailAddress;
    double attendance;
    std::cout << "Enter name: ";
    std::cin.ignore();
    std::getline(std::cin, name);
    std::cout << "Enter roll number: ";
    std::cin >> rollNumber;
    std::cout << "Enter mobile number: ";
    std::cin >> mobileNumber;
    std::cout << "Enter email address: ";
    std::cin.ignore();
    std::cin >> emailAddress;
    std::cout << "Enter attendance: ";
    std::cin >> attendance;
    student.setName(name);
    student.setRollNumber(rollNumber);
    student.mobileNumber = mobileNumber;
    student.emailAddress = emailAddress;
    student.attendance = attendance;
    return student;
}

std::vector<Student> StudentManager::sortByRollNumber()
{
    int N = this->studentList.size();
    std::vector<Student> studentList(N);
    for (int i = 0; i < N; i++)
    {
        studentList[i] = this->studentList[i];
    }
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N - i - 1; j++)
        {
            if (studentList[j].getRollNumber() > studentList[j + 1].getRollNumber())
            {
                std::swap(studentList[j], studentList[j + 1]);
            }
        }
    }
    return studentList;
}

std::vector<Student> StudentManager::sortByAttendance()
{
    int N = this->studentList.size();
    std::vector<Student> studentList(N);
    for (int i = 0; i < N; i++)
    {
        studentList[i] = this->studentList[i];
    }
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N - i - 1; j++)
        {
            if (studentList[j].attendance > studentList[j + 1].attendance)
            {
                std::swap(studentList[j], studentList[j + 1]);
            }
            else if ((studentList[j].attendance == studentList[j + 1].attendance) && (studentList[j].getRollNumber() > studentList[j + 1].getRollNumber()))
            {
                std::swap(studentList[j], studentList[j + 1]);
            }
        }
    }
    return studentList;
}

bool StudentManager::printAttendanceReport(std::string filename, bool sortBy)
{
    std::ofstream report;
    report.open(filename);

    std::vector<Student> studentList;

    if (sortBy)
    {
        studentList = sortByRollNumber();
    }
    else
    {
        studentList = sortByAttendance();
    }

    for (Student stud : studentList)
    {
        report << stud.print() << std::endl;
    }

    report.close();

    return true;
}

int main(int argc, char const *argv[])
{
    StudentManager mngr;
    int ch = 0;
    int rollNumber;
    Student stud;
    std::string filename;

    while (ch != 5)
    {
        std::cout << "n======Student Manager======" << std::endl;
        std::cout << "0.  Enter Student Detailsn"
                  << "1.  Update Student Mobile Numbern"
                  << "2.  Update Student Email Addressn"
                  << "3.  Print Attendance Report by Roll Numbern"
                  << "4.  Print Attendance Report by Attendancen"
                  << "5.  Exitn"
                  << std::endl;
        std::cout << "Enter your choice: ";
        std::cin >> ch;

        switch (ch)
        {
        case 0:
            mngr.addStudent(mngr.getStudentInput());
            break;
        case 1:
            if (mngr.isEmpty())
            {
                std::cout << "No students entered yet"
                          << std::endl;
                break;
            }
            std::cout << "Enter roll number: ";
            std::cin >> rollNumber;
            stud = mngr.getStudent(rollNumber);
            if (stud.getRollNumber() == 0)
            {
                std::cout << "Student not found"
                          << std::endl;
                break;
            }
            std::cout << "Enter Mobile number: ";
            std::cin >> stud.mobileNumber;
            mngr.setStudent(rollNumber, stud);
            break;
        case 2:
            if (mngr.isEmpty())
            {
                std::cout << "No students entered yet"
                          << std::endl;
                break;
            }
            std::cout << "Enter roll number: ";
            std::cin >> rollNumber;
            stud = mngr.getStudent(rollNumber);
            if (stud.getRollNumber() == 0)
            {
                std::cout << "Student not found"
                          << std::endl;
                break;
            }
            std::cout << "Enter Email address: ";
            std::cin.ignore();
            std::cin >> stud.emailAddress;
            mngr.setStudent(rollNumber, stud);
            break;

        case 3:
            if (mngr.isEmpty())
            {
                std::cout << "No students entered yet"
                          << std::endl;
                break;
            }

            std::cout << "Enter filename: ";
            std::cin.ignore();
            std::getline(std::cin, filename);
            mngr.printAttendanceReport(filename, true);
            break;

        case 4:
            if (mngr.isEmpty())
            {
                std::cout << "No students entered yet"
                          << std::endl;
                break;
            }

            std::cout << "Enter filename: ";
            std::cin.ignore();
            std::getline(std::cin, filename);
            mngr.printAttendanceReport(filename, false);
            break;

        case 5:
            std::cout << "Exiting..." << std::endl;
            break;

        default:
            std::cout << "Invalid choice" << std::endl;
            break;
        }
    }

    return 0;
}

Output:


    ======Student Manager======
    0.  Enter Student Details
    1.  Update Student Mobile Number
    2.  Update Student Email Address
    3.  Print Attendance Report by Roll Number
    4.  Print Attendance Report by Attendance
    5.  Exit
    
    Enter your choice: 0
    Enter name: Bhola
    Enter roll number: 45
    Enter mobile number: 32
    Enter email address: bholaeatschola@gmail.com
    Enter attendance: 78
    
    ======Student Manager======
    0.  Enter Student Details
    1.  Update Student Mobile Number
    2.  Update Student Email Address
    3.  Print Attendance Report by Roll Number
    4.  Print Attendance Report by Attendance
    5.  Exit
    
    Enter your choice: 3
    Enter filename: report
    
    ======Student Manager======
    0.  Enter Student Details
    1.  Update Student Mobile Number
    2.  Update Student Email Address
    3.  Print Attendance Report by Roll Number
    4.  Print Attendance Report by Attendance
    5.  Exit
    
    Enter your choice: 5
    Exiting...

File Contents:

Bhola,45,32,bholaeatschola@gmail.com,78
Share:

0 comments:

Post a Comment