Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignments you are given 2 classes (Item, Bag) and 1 main() that uses

ID: 3722172 • Letter: F

Question

For this assignments you are given 2 classes (Item, Bag) and 1 main() that uses them.

For simplicity sake, everything is in one .cpp file ( main.cpp in Clion)

First write the Item class:

It has a couple of constructors, and a few functions that let you change or see the variables: name , cost.

First thing to do is, test the Item class to see if it works from the main():

Now add another class (in the same file, before the main) called Bag:

The Bag class creates an array of items:

Provides two different ways to insert an item into the Bag:

Lets you show everything in the Bag:

Lets you delete the first element (index 0), then shift all other to the left on top of it to avoid a gap

Checks to see if the Bag contains a particular Item name

=== WRITE and TEST the Functions below:

void most_expensive ( ) (shows the most expensive item)

void show_reverse ( ) (shows everything in the bag in reverse order of showItems() )

void get_frequency (Item item) (show how many times an Item (the name) appears in the bag)

bool deleteItem (Item item) (delete item(based on the name), shift array elements to the left, return true if successful

int get_index_of (Item item) (returns the index of an item (based on the name))

double sum_of_all ( ) (returns the sum of all Items)

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

1. Defined the class Item according to the instructions , added constructor and getter/setter methods.

2. Defined the Bag class exactly as per the needs, implemented all the required methods and constructors, working of each method is well explained.

3. Defined a main method, defined a Bag, created and added a few items, demonstrated various operations and verified the output.

4. Comments are included, If you have any doubts, feel free to ask, Thanks

EDIT: I’m getting troubles while trying to paste the answer without losing the formatting .It says character limit exceeded error, Forgive me if the code indentation is messed up.

//code.cpp

#include<iostream>

using namespace std;

class Item{

//attributes

string name;

double cost;

public:

//constructor with two args

Item(string n, double c){

name=n;

cost=c;

}

//constructor with no args

Item(){

name="";

cost=0;

}

//constructor with one argument

Item(string n){

name=n;

cost=0;

}

//getters and setters

string getName(){

return name;

}

double getCost(){

return cost;

}

void setName(string n){

name=n;

}

void setCost(double c){

cost=c;

}

};

class Bag{

//a constant for storing the max capacity of bag

static const int MAX_CAPACITY=20;

//array of items

Item items[MAX_CAPACITY];

//count of items currently in bag

int numItems;

public:

//default constructor

Bag(){

numItems=0;

}

/* method to insert an element at the end */

void insert(Item itm){

if(numItems==MAX_CAPACITY){

cout<<"Bag is full!"<<endl;

}else{

items[numItems]=itm;//adding to the array

numItems++;//incrementing the count of items

cout<<itm.getName()<<" added to the bag!"<<endl;

}

}

/* method to insert an element at the specified index */

void insert(Item itm, int pos){

if(numItems==MAX_CAPACITY){

cout<<"Bag is full!"<<endl;

}else{

if(pos>=0 && pos<numItems){

/*shifting elements to the right to make space*/

for(int i=numItems;i>pos;i--){

items[i]=items[i-1];

}

/*adding element at specified position*/

items[pos]=itm;

//incrementing the count of items

numItems++;

cout<<itm.getName()<<" added to the bag at position: "<<pos<<endl;

}else{

cout<<"Invalid position"<<endl;

}

}

}

/*method to display all items in the bag*/

void showItems(){

cout<<endl;

cout<<"...Items currently in the bag..."<<endl;

for(int i=0;i<numItems;i++){

cout<<items[i].getName()<<", cost: $"<<items[i].getCost()<<endl;

}

cout<<endl;

}

/*method to delete the item at front position in the bag*/

void deleteFirst(){

if(numItems==0){

cout<<"Bag is Empty!"<<endl;

}else{

for(int i=0;i<numItems-1;i++){

items[i]=items[i+1];

}

numItems--;

cout<<"One item deleted from the front!"<<endl;

}

}

/*method to check if an item exist in the bag*/

bool contains(Item itm){

for(int i=0;i<numItems;i++){

if(items[i].getName()==itm.getName()){

//found

return true;

}

}

return false;

}

/*method to display the most expensive item in the bag*/

void most_expensive (){

if(numItems==0){

cout<<"Bag is empty!"<<endl;

}else{

int index=0;//assuming index of most expensive item is 0

double cost=items[0].getCost();

/*looping through all items*/

for(int i=1;i<numItems;i++){

/*checking if it has higher cost than what we have assumed*/

if(items[i].getCost()>cost){

//making the current object as the most expensive

index=i;

cost=items[i].getCost();

}

}

cout<<"Most expensive item is "<<items[index].getName()<<", with cost: $"<<items[index].getCost()<<endl;

}

}

/*method to display all items in the bag in reverse order*/

void show_reverse(){

cout<<endl;

cout<<"...Items currently in the bag (reversed order)..."<<endl;

for(int i=numItems-1;i>=0;i--){

cout<<items[i].getName()<<", cost: $"<<items[i].getCost()<<endl;

}

cout<<endl;

}

/*method to display the frequency of occurrance of an item in the bag*/

void get_frequency(Item itm){

if(contains(itm)){

int count=0;

for(int i=0;i<numItems;i++){

if(items[i].getName()==itm.getName()){

count++;

}

}

cout<<"The item "<<itm.getName()<<" appear "<<count<<" times"<<endl;

}else{

cout<<"Bag doesn't contain the specified item"<<endl;

}

}

/*method to return the index of an item in the bag*/

int get_index_of(Item itm){

if(contains(itm)){

for(int i=0;i<numItems;i++){

if(items[i].getName()==itm.getName()){

//found

return i;

}

}

}else{

//not found

return -1;

}

}

/*method to delete a specified item from the bag if exist*/

bool deleteItem (Item itm){

if(contains(itm)){

int index=get_index_of(itm);//getting the index of item

/*shifting elements to the left to occupy the vacant space*/

for(int i=index;i<numItems-1;i++){

items[i]=items[i+1];

}

numItems--;

return true;

}else{

//doesnt exist

return false;

}

}

/*method to find the sum of all items cost*/

double sum_of_all(){

double sum=0;

for(int i=0;i<numItems;i++){

sum+=items[i].getCost();

}

return sum;

}

};

int main(){

//defining a Bag

Bag bag;

//defining a few items

Item item1("water",2.33);

Item item2("snacks",5.20);

Item item3("book",10);

Item item4("gold",123.66);

Item item5("battery",7.50);

Item item6("water",2.33);

Item item7("water",3);

//inserting each items into the bag

bag.insert(item1);

bag.insert(item2);

bag.insert(item3,1); //adding to index 1

bag.insert(item4);

bag.insert(item5,3);//adding to index 3

bag.insert(item6);

bag.insert(item7,4);//adding to index 4

//displaying the bag

bag.showItems();

//finding the count of item1 (water)

bag.get_frequency(item1);

//deleting the first item

bag.deleteFirst();

//displaying current list

bag.showItems();

//trying to delete item5 (battery)

if(bag.deleteItem(item5)){

cout<<item5.getName()<<" is deleted"<<endl;

}else{

cout<<item5.getName()<<" is not deleted"<<endl;

}

//displaying in reverse order

bag.show_reverse();

//displaying the sum total

cout<<"Sum cost of all items= $"<<bag.sum_of_all()<<endl;

//displaying the most expensive item

bag.most_expensive();

return 0;

}

/*OUTPUT*/

water added to the bag!

snacks added to the bag!

book added to the bag at position: 1

gold added to the bag!

battery added to the bag at position: 3

water added to the bag!

water added to the bag at position: 4

...Items currently in the bag...

water, cost: $2.33

book, cost: $10

snacks, cost: $5.2

battery, cost: $7.5

water, cost: $3

gold, cost: $123.66

water, cost: $2.33

The item water appear 3 times

One item deleted from the front!

...Items currently in the bag...

book, cost: $10

snacks, cost: $5.2

battery, cost: $7.5

water, cost: $3

gold, cost: $123.66

water, cost: $2.33

battery is deleted

...Items currently in the bag (reversed order)...

water, cost: $2.33

gold, cost: $123.66

water, cost: $3

snacks, cost: $5.2

book, cost: $10

Sum cost of all items= $144.19

Most expensive item is gold, with cost: $123.66

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote