Question

In: Computer Science

Write a C++ program for storing information on on a series of balls collected by a...

Write a C++ program for storing information on on a series of balls collected by a person. The balls should have these characteristics:

1. Diameter in mm

2. Color

3. if the texture of the surface is smooth or rough

4. an identification id/number

The program should allow the person to enter the values for the balls' attributes as they are entered in a database. The program should then offer the choice to save all new data into a file.

Next, the program should offer these operations:

1. List all balls with diameters above 10mm (Show all attribute values)

2. List all balls with less or equal to 10mm (Show all attribute values)

3. Show all balls that are smooth (Show all attribute values as a list)

4. Show all balls that are rough (Show all attribute values as a list)

5. Find a specific ball based on its id (Show all attributes)

6. How many balls of a specific color are in the database? (Show a total)

Program requirements:

1. The program must be OOP

2. Use of an array as temporary buffer to store ball objects

3. Move content of array to file

4. Retrieve all objects in file to an array at run time

5. Use of menus to direct user on the various available options for the program.

6. Use of header files for the classes need for this program (Use a project setup rather than a single app).

Delivery requirements:

1. Source code

2. Sample files generated by the program

3. A word document that summarizes your work with screenshots of your program running and generating outputs based on your menu selections. All menu options must be tested!

4. A conclusion within the above document about your performance in completing this task. This is going to be important for me to read and assess what needs to be done at the beginning of this course to bring each one of you to a level where CS230 will be performed without struggles.

Solutions

Expert Solution

// ball.h

#ifndef BALL_H_

#define BALL_H_

#include <iostream>

using namespace std;

class Ball

{

private:

       double diameter;

       string color;

       string texture;

       int id;

public:

       Ball();

       Ball(double diameter, string color, string texture, int id);

       void setDiameter(double diameter);

       double getDiameter();

       void setColor(string color);

       string getColor();

       void setTexture(string texture);

       string getTexture();

       void setId(int id);

       int getId();

};

#endif /* BALL_H_ */

//end of ball.h

// ball.cpp

#include "ball.h"

Ball::Ball()

{

       diameter=0;

       color="";

       texture="";

       id=0;

}

Ball::Ball(double diameter, string color, string texture, int id)

{

       this->diameter = diameter;

       this->color = color;

       this->texture = texture;

       this->id = id;

}

void Ball::setDiameter(double diameter)

{

       this->diameter = diameter;

}

double Ball::getDiameter()

{

       return diameter;

}

void Ball::setColor(string color)

{

       this->color = color;

}

string Ball::getColor()

{

       return color;

}

void Ball::setTexture(string texture)

{

       this->texture = texture;

}

string Ball::getTexture()

{

       return texture;

}

void Ball::setId(int id)

{

       this->id = id;

}

int Ball::getId()

{

       return id;

}

//end of ball.cpp

// main.cpp : Driver program to implement the main program

#include <iostream>

#include <fstream>

#include "ball.h"

using namespace std;

int main() {

       int n;

       double diameter;

       string color, texture;

       int id, texture_choice;

       // input of number of ball objects to create

       cout<<"Enter the number of balls you want to enter : ";

       cin>>n;

       Ball balls[n]; // create an array of ball objects

       // loop to input details of ball objects

       for(int i=0;i<n;i++)

       {

             cout<<"\nEnter details for ball-"<<(i+1)<<" : "<<endl;

             cout<<"Diameter (in mm) : ";

             cin>>diameter;

             cout<<"Color : ";

             cin>>color;

             cout<<"Texture of the surface( 1. rough , 2. smooth ) : ";

             cin>>texture_choice;

             cout<<"Identification number(ID) : ";

             cin>>id;

             balls[i].setDiameter(diameter);

             balls[i].setColor(color);

             balls[i].setId(id);

             if(texture_choice == 1)

                    balls[i].setTexture("rough");

             else

                    balls[i].setTexture("smooth");

       }

       // open an output file

       ofstream fout("balls.txt");

       // loop to write the data of balls in output file (each field is separated by a single space)

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

       {

             fout<<balls[i].getId()<<" "<<balls[i].getDiameter()<<" "<<balls[i].getColor()<<" "<<balls[i].getTexture()<<endl;

       }

       fout<<balls[n-1].getId()<<" "<<balls[n-1].getDiameter()<<" "<<balls[n-1].getColor()<<" "<<balls[n-1].getTexture();

       fout.close(); //close the file

       ifstream fin("balls.txt"); // open the file as input file

       n=0;

       // read till the end of file

       while(!fin.eof())

       {

             // load the data into the array

             fin>>id>>diameter>>color>>texture;

             balls[n].setDiameter(diameter);

             balls[n].setId(id);

             balls[n].setColor(color);

             balls[n].setTexture(texture);

             n++;

       }

       fin.close(); //close the file

       int choice;

       // loop that continues till the user wants

       do

       {

             cout<<endl;

             cout<<"1. List all balls with diameters above 10mm"<<endl;

             cout<<"2. List all balls with less or equal to 10mm"<<endl;

             cout<<"3. Show all balls that are smooth"<<endl;

             cout<<"4. Show all balls that are rough "<<endl;

             cout<<"5. Find a specific ball based on its id"<<endl;

             cout<<"6. How many balls of a specific color are in the database?"<<endl;

             cout<<"7. Exit"<<endl;

             cout<<"Enter a choice(1-7) : ";

             cin>>choice;

             switch(choice)

             {

             case 1: // list all balls with diameter > 10

                    for(int i=0;i<n;i++)

                    {

                           if(balls[i].getDiameter() > 10)

                                 cout<<"ID : "<<balls[i].getId()<<" Diameter : "<<balls[i].getDiameter()<<"mm Color : "<<balls[i].getColor()<<" Texture : "<<balls[i].getTexture()<<endl;

                    }

                    break;

             case 2: // list all balls with diameter <= 10

                    for(int i=0;i<n;i++)

                    {

                           if(balls[i].getDiameter() <= 10)

                                 cout<<"ID : "<<balls[i].getId()<<" Diameter : "<<balls[i].getDiameter()<<"mm Color : "<<balls[i].getColor()<<" Texture : "<<balls[i].getTexture()<<endl;

                    }

                    break;

             case 3: //list all smooth balls

                    for(int i=0;i<n;i++)

                    {

                           if(balls[i].getTexture() == "smooth")

                                 cout<<"ID : "<<balls[i].getId()<<" Diameter : "<<balls[i].getDiameter()<<"mm Color : "<<balls[i].getColor()<<" Texture : "<<balls[i].getTexture()<<endl;

                    }

                    break;

             case 4: // list all rough balls

                    for(int i=0;i<n;i++)

                    {

                           if(balls[i].getTexture() == "rough")

                                 cout<<"ID : "<<balls[i].getId()<<" Diameter : "<<balls[i].getDiameter()<<"mm Color : "<<balls[i].getColor()<<" Texture : "<<balls[i].getTexture()<<endl;

                    }

                    break;

             case 5: // find and display the details of the ball whose id is given

             {

                    bool found=false;

                    cout<<"Enter the id of the ball : ";

                    cin>>id;

                    // loop to find the ball with the given id

                    for(int i=0;i<n;i++)

                    {

                           if(balls[i].getId() == id)

                           {

                                 cout<<"ID : "<<balls[i].getId()<<" Diameter : "<<balls[i].getDiameter()<<"mm Color : "<<balls[i].getColor()<<" Texture : "<<balls[i].getTexture()<<endl;

                                 found = true;

                                 break;

                           }

                    }

                    if(!found) //check if ball was found

                           cout<<"No ball with id "<<id<<" found"<<endl;

                    break;

             }

             case 6: // count the number of balls of a specific color

                    {

                           int count = 0;

                           cout<<"Enter the color of the ball : ";

                           cin>>color;

                           for(int i=0;i<n;i++)

                           {

                                 if(balls[i].getColor() == color)

                                        count++;

                           }

                           cout<<"Number of balls of color "<<color<<" : "<<count<<endl;

                           break;

                    }

             case 7: // exit the loop

                    break;

             default: // wring choice

                    cout<<"Invalid choice"<<endl;

             }

       }while(choice != 7);

       return 0;

}

//end of program

Output:

File:


Related Solutions

Write a C++ program for storing information on on a series of balls collected by a...
Write a C++ program for storing information on on a series of balls collected by a person. The balls should have these characteristics: 1. Diameter in mm 2. Color 3. if the texture of the surface is smooth or rough 4. an identification id/number The program should allow the person to enter the values for the balls' attributes as they are entered in a database. The program should then offer the choice to save all new data into a file....
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
Use C language Write a program that reads in a series of lines of input character...
Use C language Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1, • to convert decimal number to binary number using the function2, • to check whether a number is a prime number or not using the function3, • to check whether two given strings are an anagram using the function4. important must do in (Multi-Filing) of c++
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT