Question

In: Computer Science

(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...

(1) Create three files to submit.

  • ContactNode.h - Class declaration
  • ContactNode.cpp - Class definition
  • main.cpp - main() function

(2) Build the ContactNode class per the following specifications:

  • Parameterized constructor. Parameters are name followed by phone number.

  • Public member functions

    • InsertAfter() (2 pts)
    • GetName() - Accessor (1 pt)
    • GetPhoneNumber - Accessor (1 pt)
    • GetNext() - Accessor (1 pt)
    • PrintContactNode()
  • Private data members

    • string contactName
    • string contactPhoneNum
    • ContactNode* nextNodePtr


Ex. of PrintContactNode() output:

Name: Roxanne Hughes
Phone number: 443-555-2864


(3) In main(), prompt the user for three contacts and output the user's input. Create three ContactNodes and use the nodes to build a linked list. (2 pts)

Ex:

Person 1
Enter name:
Roxanne Hughes
Enter phone number:
443-555-2864
You entered: Roxanne Hughes, 443-555-2864

Person 2
Enter name:
Juan Alberto Jr.
Enter phone number:
410-555-9385
You entered: Juan Alberto Jr., 410-555-9385

Person 3
Enter name:
Rachel Phillips
Enter phone number:
310-555-6610
You entered: Rachel Phillips, 310-555-6610


(4) Output the linked list. (2 pts)

Ex:

CONTACT LIST
Name: Roxanne Hughes
Phone number: 443-555-2864

Name: Juan Alberto Jr.
Phone number: 410-555-9385

Name: Rachel Phillips
Phone number: 310-555-6610

main.cpp

#include <iostream>
using namespace std;

int main() {

/* Type your code here. */

return 0;
}

ContactNode.cpp

ContactNode.h

How do you write this code?

Solutions

Expert Solution

SOLUTION-
I have solve the problem in C++ code with comments and screenshot for easy understanding :)

CODE-

ContactNode.h file

#ifndef CONTACTNODE_H
#define CONTACTNODE_H

using namespace std;

class ContactNode{
private:
string contactName;
string contactPhoneNum;
ContactNode* nextNodePtr;
  
public:
ContactNode();
ContactNode(string name, string phone);
void InsertAfter(ContactNode*);
string GetName() const;
string GetPhoneNumber() const;
ContactNode* GetNext();
void PrintContactNode();
};
#endif

ContactNode.cpp file:

#include <iostream>
#include "ContactNode.h"

using namespace std;

/**
* Default constructor
* Assigns nextNodePtr to NULL
*/
ContactNode::ContactNode(){
nextNodePtr = NULL;
}

/**
* Parameterized constructor
* @param name a string represents contact name
* @param phone a string represents phone number
*/
ContactNode::ContactNode(string name, string phone){
contactName = name;
contactPhoneNum = phone;
nextNodePtr=NULL;
}

/**
* Method Name: InsertAfter
* Makes connection between this node and the nextnode
*/
void ContactNode::InsertAfter(ContactNode *nextNode){
  
//If the next pointer is null just assign the nextnode
if(nextNodePtr == NULL)
nextNodePtr=nextNode;
else{
//Define a temp node to store current next pointer
ContactNode *temp = nextNodePtr;
  
//Then loop through the nextnodes and adjust the pointers
while(temp->nextNodePtr != NULL){
temp = temp->GetNext();
}
//Assign the nextnode to temp node
temp->nextNodePtr = nextNode;
}
}

/** Getter for Name **/
string ContactNode::GetName() const{
return contactName;
}

/** Getter for Phone number **/
string ContactNode::GetPhoneNumber() const{
return contactPhoneNum;
}

/** Getter for next pointer **/
ContactNode* ContactNode::GetNext(){
return nextNodePtr;
}

/**
* Print contact Node - Name and Phone
*/
void ContactNode::PrintContactNode(){
//Go through the next pointer and display name and phonenumber
if(nextNodePtr != NULL) {
cout << "Name: " << nextNodePtr->GetName() << endl;
cout << "Phone number: " << nextNodePtr->GetPhoneNumber() << endl;
cout << endl;
GetNext()->PrintContactNode();
}
}

main.cpp file:

#include <iostream>
#include "ContactNode.h"

using namespace std;

//Define a constant for total number of contacts
int TOTAL_CONTACTS = 3;

int main()
{
//Declare two nodes to keep track of both the head and tail nodes
ContactNode *head, *tail;
head = new ContactNode;
  
//Get the contact node for TOTAL_CONTACTS times
for(int i=0; i < TOTAL_CONTACTS; i++){
//Display headings
cout << "\nPerson " << i+1 <<endl;
//Get Person name
cout<<"Enter name: "<<endl ;
string name;
getline(cin, name);
//Get Phone number
cout<<"Enter phone number: "<<endl ;
string phonenum;
getline(cin, phonenum);
//Display enetered values
cout << "You entered: "<< name << ", " << phonenum << endl;
//Create a node
tail = new ContactNode(name, phonenum);
//Insert after the head
head->InsertAfter(tail);
}
  
cout << endl;
cout << "CONTACT LIST" << endl;
//Print contacts from head
head->PrintContactNode();
return 0;
}


SCREENSHOT-

main.cpp file:

ContactNode.h file

ContactNode.cpp file:

Sample Run:

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------



Related Solutions

(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp - main() function Build the ItemToPurchase class with the following specifications: Default constructor Public class functions (mutators & accessors) SetName() & GetName() (2 pts) SetPrice() & GetPrice() (2 pts) SetQuantity() & GetQuantity() (2 pts) Private data members string itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 (2)...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the main module that creates and manipulates CVehicle objects cars.dat -- a text file that contains name data for the main module 4th file is cvehicle.cpp and it needs to be created from scratch, and cvehicle.h needs to be filled in This was the test drive: carOne = Hyundai Sonata carTwo = Hyundai Sonata carThree = Enter the make and model of a vehicle: toyota...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main()...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main() method Build the ItemToBuy class with the following specifications: Private fields String itemName - Initialized in the nor-arg constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 No-arg Constructor (set the String instance field to "none") Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() toString()...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method Build the Payroll class with the following specifications: 4 private fields String name - Initialized in default constructor to "John Doe" int ID - Initialized in default constructor to 9999 doulbe payRate - Initialized in default constructor to 15.0 doulbe hrWorked - Initialized in default constructor to 40 2 constructors (public) Default constructor A constructor that accepts the employee’s name, ID, and pay rate...
C (1) Create three files to submit: ItemToPurchase.h - Struct definition and related function declarations ItemToPurchase.c...
C (1) Create three files to submit: ItemToPurchase.h - Struct definition and related function declarations ItemToPurchase.c - Related function definitions main.c - main() function Build the ItemToPurchase struct with the following specifications: Data members (3 pts) char itemName [ ] int itemPrice int itemQuantity Related functions MakeItemBlank() (2 pts) Has a pointer to an ItemToPurchase parameter. Sets item's name = "none", item's price = 0, item's quantity = 0 PrintItemCost() Has an ItemToPurchase parameter. Ex. of PrintItemCost() output: Bottled Water...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class Definition ShoppingCartPrinter.java – Contains main() method Build the ItemToPurchase class with the following specifications: Specifications Description ItemToPurchase(itemName) itemName – The name will be a String datatype and Initialized in default constructor to “none”. ItemToPurchase(itemPrice) itemPrice – The price will be integer datatype and Initialized in default constructor to 0. ItemToPurchase(itemQuantity) itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0....
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter)...
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For...
C++ Create an ArrayBag template class from scratch. This will require you to create two files:...
C++ Create an ArrayBag template class from scratch. This will require you to create two files: ArrayBag.hpp for the interface and ArrayBag.cpp for the implementation. The ArrayBag class must contain the following protected members: static const int DEFAULT_CAPACITY = 200; // max size of items_ ItemType items_[DEFAULT_CAPACITY]; // items in the array bag int item_count_; // Current count of items in bag /** @param target to be found in items_ @return either the index target in the array items_ or...
(1)Discuss the syntax for combining the definition and declaration of an enumeration type into one statement....
(1)Discuss the syntax for combining the definition and declaration of an enumeration type into one statement. Provide some examples. (2) Describe the syntax of a namespace statement. Discuss the syntax for accessing a namespace member. Also, review the syntax for the using namespace statement and discuss how this simplifies the process of accessing namespace members.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT