Question

Project Lists and Object-oriented Programming Note: using c++ This project is an individual assignment that focuses...

Project

Lists and Object-oriented Programming

Note: using c++

This project is an individual assignment that focuses on object-oriented programming and lists. The list will be a doubly-linked list and it will be implemented using nodes and pointers. The data contained in the list will be objects from a class.

  1. Project data: You will choose one of the following classes to define: Boat, Earthquake, Game, Sport, State, Website, House, Phone, Activist, Plant, Fish, Parrot, and Course. Your class code will include a .h class interface file and a .cpp class definition file. You will choose the data attributes of your class. There should be at least 3 data attributes. At least one of these attributes will be a string, and at least one attribute will be numeric.

  1. Project data structure: Additionally, you will create classes for a doubly-linked list that will manage a collection of your class objects (e.g., Boats). The list will use dynamically allocated nodes.
  2. For a user to work with your data and list, you should write a fully functional menu with the following options:
    1. Adding to the front of the list
    2. Adding to the back of the list
    3. Searching for and displaying a specific item in the list
    4. Editing a specified item from the list
    5. Removing an identified item from the list
    6. Displaying the entire list
    7. Ending the program
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// course.h

#ifndef COURSE_H_
#define COURSE_H_

#include <iostream>
using namespace std;

class Course
{
private:
   string name;
   int id;
   double marks;
   static int nextID;
public:
   Course();
   Course(string name, double marks);
   void setMarks(double marks);
   int getID();
   string getName();
   double getMarks();
   void print();
};

#endif

//end of course.h

// course.cpp

#include "course.h"

// static variable used to populate the course id
int Course::nextID = 1;

Course::Course()
{
   id = 0;
   name = "";
   marks=0;
}

// constructors
Course::Course(string name, double marks)
{
   this->id = nextID;
   nextID++;
   this->name = name;
   this->marks = marks;
}

// setters
void Course::setMarks(double marks)
{
   this->marks = marks;
}

// getters
int Course::getID()
{
   return id;
}

string Course::getName()
{
   return name;
}

double Course::getMarks()
{
   return marks;
}

// function to print the course details
void Course::print()
{
   cout<<"ID: "<<id<<" Name: "<<name<<" Marks: "<<marks;
}


//end of course.cpp

// node.h

#ifndef NODE_H_
#define NODE_H_

#include "course.h"

class Node
{
private:
   Course course;
   Node *next;
   Node *prev;
public:
   Node(Course course);
   Node(Course course, Node *next, Node *prev);
   void setCourse(Course course);
   void setNext(Node *next);
   void setPrev(Node *prev);
   Course getCourse();
   Node *getNext();
   Node* getPrev();
};

#endif

//end of node.h

// node.cpp

#include "node.h"

// constructors
Node::Node(Course course)
{
   this->course = course;
   next = NULL;
   prev = NULL;
}


Node::Node(Course course, Node *next, Node *prev)
{
   this->course = course;
   this->next = next;
   this->prev = prev;
}

// setters
void Node::setCourse(Course course)
{
   this->course = course;
}

void Node::setNext(Node *next)
{
   this->next = next;
}

void Node::setPrev(Node *prev)
{
   this->prev = prev;
}

// getters
Course Node::getCourse()
{
   return course;
}

Node* Node::getNext()
{
   return next;
}

Node* Node::getPrev()
{
   return prev;
}

//end of node.cpp

// doubleLinkedList.h

#ifndef DOUBLELINKEDLIST_H_
#define DOUBLELINKEDLIST_H_

#include "node.h"

class DoubleLinkedList
{
private:
   Node *front;
   Node *back;
public:
   DoubleLinkedList();
   ~DoubleLinkedList();
   void addFront(Course course);
   void addBack(Course course);
   void search(int ID);
   void editCourse(int ID, double marks);
   void removeCourse(int ID);
   void displayList();
};

#endif

//end of doubleLinkedList.h

// doubleLinkedList.cpp

#include "doubleLinkedList.h"

// constructor
DoubleLinkedList::DoubleLinkedList()
{
   front = NULL;
   back = NULL;
}

// destructor
DoubleLinkedList::~DoubleLinkedList()
{
   while(front != NULL)
   {
       Node *temp = front;
       front = front->getNext();
       delete(temp);
   }

   back = NULL;
}

// function to add a course at the front
void DoubleLinkedList::addFront(Course course)
{
   Node *node = new Node(course);
   if(front == NULL)
   {
       front = node;
       back = node;
   }else
   {
       node->setNext(front);
       front->setPrev(node);
       front = node;
   }
   cout<<"Course: ";
   course.print();
   cout<<" added to the front of the list"<<endl;
}

// function to add a course at the back
void DoubleLinkedList:: addBack(Course course)
{
   Node *node = new Node(course);
   if(front == NULL)
   {
       front = node;
       back = node;
   }else
   {
       node->setPrev(back);
       back->setNext(node);
       back = node;
   }
   cout<<"Course: ";
   course.print();
   cout<<" added to the back of the list"<<endl;
}

// function to search for a course
void DoubleLinkedList:: search(int ID)
{
   Node *node = front;
   bool found = false;

   while(node != NULL)
   {
       if(node->getCourse().getID() == ID)
       {
           node->getCourse().print();
           cout<<endl;
           found = true;
           break;
       }

       node = node->getNext();
   }

   if(!found)
   {
       cout<<"Course with ID: "<<ID<<" not found "<<endl;
   }
}

// function to edit a course
void DoubleLinkedList::editCourse(int ID, double marks)
{
   Node *node = front;
   bool found = false;

   while(node != NULL)
   {
       if(node->getCourse().getID() == ID)
       {
           Course course = node->getCourse();
           course.setMarks(marks);
           node->setCourse(course);

           cout<<"Course with ID: "<<ID<<" edited successfully"<<endl;
           found = true;
           break;
       }

       node = node->getNext();
   }

   if(!found)
   {
       cout<<"Course with ID: "<<ID<<" not found "<<endl;
   }

}

// function to remove a course
void DoubleLinkedList::removeCourse(int ID)
{
   Node *node = front;
   bool found = false;

   while(node != NULL)
   {
       if(node->getCourse().getID() == ID)
       {

           if(node == front)
           {
               front = front->getNext();

               if(front != NULL)
                   front->setPrev(NULL);
               else
                   back = NULL;
           }else if(node == back)
           {
               back = back->getPrev();
               if(back != NULL)
                   back->setNext(NULL);
               else
                   front = NULL;
           }else
           {
               node->getPrev()->setNext(node->getNext());
               node->getNext()->setPrev(node->getPrev());
           }

           cout<<"Course with ID: "<<ID<<" removed successfully"<<endl;
           found = true;
           break;
       }

       node = node->getNext();
   }

   if(!found)
       cout<<"Course with ID: "<<ID<<" not found "<<endl;

}

// function to display all the courses
void DoubleLinkedList:: displayList()
{
   if(front == NULL)
       cout<<"No courses present"<<endl;
   else
   {
       cout<<"Courses: "<<endl;
       Node *node = front;

       while(node != NULL)
       {
           node->getCourse().print();
           cout<<endl;
           node = node->getNext();
       }
   }
}


//end of doubleLinkedList.cpp


// main.cpp : C++ driver program to implement the DoubleLinkedList class

#include "doubleLinkedList.h"
#include <iostream>
using namespace std;

int main()
{
   DoubleLinkedList list;
   int ID, choice;
   string name;
   double marks;
   // loop that continues till the user exits
   do
   {
       // display the menu
       cout<<"Menu"<<endl;
       cout<<"1. Adding a course to the front of the list"<<endl;
       cout<<"2. Adding a course to the back of the list"<<endl;
       cout<<"3. Searching for and displaying a specific course in the list"<<endl;
       cout<<"4. Editing a specified course from the list"<<endl;
       cout<<"5. Removing an identified course from the list"<<endl;
       cout<<"6. Displaying the entire list"<<endl;
       cout<<"7. Quit"<<endl;
       cout<<"Enter your choice(1-7): ";
       cin>>choice;

       // validate the choice and re-prompt until valid
       while(choice < 1 || choice > 7)
       {
           cout<<"Invalid choice"<<endl;
           cout<<"Enter your choice(1-7): ";
           cin>>choice;
       }

       switch(choice)
       {
           case 1:
               cin.ignore(100,'\n');
               cout<<"Enter the course name: ";
               getline(cin,name);
               cout<<"Enter the course marks: ";
               cin>>marks;
               list.addFront(Course(name,marks));
               break;

           case 2:
               cin.ignore(100,'\n');
               cout<<"Enter the course name: ";
               getline(cin,name);
               cout<<"Enter the course marks: ";
               cin>>marks;
               list.addBack(Course(name,marks));
               break;

           case 3:
               cout<<"Enter the course id to search: ";
               cin>>ID;
               list.search(ID);
               break;

           case 4:
               cout<<"Enter the course id to edit: ";
               cin>>ID;
               cout<<"Enter the new marks: ";
               cin>>marks;
               list.editCourse(ID,marks);
               break;

           case 5:
               cout<<"Enter the course id to remove: ";
               cin>>ID;
               list.removeCourse(ID);
               break;

           case 6:
               list.displayList();
       }
       cout<<endl;

   }while(choice != 7);

   return 0;
}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
Project Lists and Object-oriented Programming Note: using c++ This project is an individual assignment that focuses...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java is an object-oriented programming language that enables us to define classes and to instantiate them...

    Java is an object-oriented programming language that enables us to define classes and to instantiate them into objects. These objects then call each other’s methods to implement the behavior of the application. The Unified Modeling Language (UML) is an object-oriented visual notation to document the design of object-oriented classes. For this discussion, you will practice designing a Java class called Course, drawing a UML class diagram for the Course class, and then implementing the Course class in Java code. Review...

  • Purpose: The main goal of this assignment is to practice Object Oriented Programming by creating classes,...

    Purpose: The main goal of this assignment is to practice Object Oriented Programming by creating classes, writing methods, and creating inheritance type relationships. Task: Create a Java project using the IDE Write a program using Object Oriented Programming Plan a program where two classes inherit from a single class and theme it in a way that makes sense. Create a super class with a non-constructor method Create two sub classes with their own non-constructor methods and have them inherit from...

  • *Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented...

    *Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented programming using classes to create objects. PROBLEM DEFINITION: Write a class named Employee that holds the following data about an employee in attributes: name, IDnumber, department, jobTitle The class should have 8 methods as follows:  For each attribute, there should be a method that takes a parameter to be assigned to the attribute. This is known as a mutator method.  For each...

  • Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic me...

    Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic memory allocation, pointers, recursion, and debugging Mandatory Instructions Develop a C++ object oriented solution to the Towers of Hanoi puzzle. Your solution will involve designing two classes one to represent individual Disk and another to represent the TowersOfHanoi game. TowersOfHanoi class will implement the game with three linked lists representing disks on each...

  • In object-oriented programming, the object encapsulates both the data and the functions that operate on the...

    In object-oriented programming, the object encapsulates both the data and the functions that operate on the data. True False Flag this Question Question 101 pts You must declare all data members of a class before you declare member functions. True False Flag this Question Question 111 pts You must use the private access specification for all data members of a class. True False Flag this Question Question 121 pts A private member function is useful for tasks that are internal...

  • Programming Assignment 6: Object Oriented Programming Due date: Check Syllabus and Canvas Objectives: After successfully completing...

    Programming Assignment 6: Object Oriented Programming Due date: Check Syllabus and Canvas Objectives: After successfully completing this assignment, students will practice Object Oriented design and programming by creating a Java program that will implement Object Oriented basic concepts. RetailItem Class: Part 1: Write a class named RetailItem that holds data about an item in a retail store. The class should have the following fields: • description. The description field references a String object that holds a brief description of the...

  • In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now...

    In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now that you have successfully created Trivia objects, you will continue 6B by creating a linked list of trivia objects. Add the linked list code to the Trivia class. Your linked list code should include the following: a TriviaNode class with the attributes: 1. trivia game - Trivia object 2. next- TriviaNode 3. write the constructor, accessor, mutator and toString methods. A TriviaLinkedList Class which...

  • Linked Lists implementation on Lists Executive Summary: Linked list implementation on lists includes two parts: Data...

    Linked Lists implementation on Lists Executive Summary: Linked list implementation on lists includes two parts: Data part - stores an element of the list Next part- stores link/pointer to next element You are asked to implement Lists through Linked Lists to perform basic ADT functions. Project Objective: in completing this project, you will Enhance your ability to understand Lists Familiar with the idea of linked list Enable yourself to perform Linked Lists programming skills Due Dates and Honor: This project...

  • The purpose of this project is to give students more exposure to object oriented design and...

    The purpose of this project is to give students more exposure to object oriented design and programming using classes and polymorphism in a realistic application that involves arrays of objects and sorting arrays containing objects A large veterinarian services many pets and their owners. As new pets are added to the population of pets being serviced, their information is entered into a flat text file. Each month the vet requests and updates listing of all pets sorted by their "outstanding...

  • python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item,...

    python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item, left, and rightL. Left link points to the previous node in the list, right link points to the next node in the list. You can also add the display method to this class (like we did it in class for the ListNode class). Then test your class. For example, create a linked list of 5 values: 34,1, 23, 7, and 10. Display it. Then...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT