[Solved] Lab exercise 10 Problems

$25

File Name: Lab_exercise_10_Problems.zip
File Size: 226.08 KB

SKU: [Solved] Lab exercise 10 Problems Category: Tag:
5/5 - (1 vote)
  1. Write C++ programs
  2. Compile C++ programs
  3. Implement programs that use advanced class concepts such as constructors, destructors, and member functions.

Additional Reading

This lab exercise requires an understanding of some concepts to solve the problems. You are strongly encouraged to read the following tutorials to help you answer the problems.

  1. Organizing C++ files: function prototypes, implementations, and drivers.
  2. Using objects as parameters and return values in functions
  3. Passing arrays as parameters to functions
  4. File reading and writing (also includes dealing with arrays)

Instructions

Answer the programming problems sequentially (i.e., answer prob01 before prob02). If you have questions let your instructor or the lab assistant know. You can also consult your classmates.

When you answer two programming problems correctly, let your instructor know and wait for further instruction.

Lab exercise guide

Heres a link to the Lab exercise guide in case you need to review the lab exercise objectives, grading scheme, or evaluation process.

Digicup

This program simulates interactions with a Cup object for getting a drink, refilling a drink, emptying a drink, and drinking from it.

Output

All of the output statements (std::cout) should be in main and are mostly provided for you. You will only need to complete the menu functionality by calling the appropriate member functions from the Cup object. Your member functions are designed to only perform calculations and return values.

The Cup object will be used to ask the user for the type of drink they prefer and the amount they want to drink.

The menu options are shown below for your reference:

D: DrinkR: RefillN: Get a brand new drinkE: EmptyX: Exit

The Cup class

Data members

Create a class called Cup with the following member variables:

  1. drink_type_ which is an std::string that will be the name of the drink.
  2. fluid_oz_ which is a double that will be the amount of fluid in the cup.

Constructors

Default constructor

The default constructor should initialize drink_type_ to "Water" and initialize fluid_oz_ to 16.0

Non-default constructor

The non-default constructor should take in an std::string parameter that will be the name of the drink type and a double parameter that will be the amount of drink in the cup. It should set the passed parameter values to the corresponding data members.

Member functions

drink

Drinking reduces the amount of liquid in the Cup based on a given amount that is passed as a parameter. Take note that fluid_oz_ should never be negative such that if you drink an amount that is greater that fluid_oz_, then fluid_oz_ should be set to 0.

refill

Refilling the cup increases the amount of liquid in the Cup based on the given parameter, amount. Assume the cup is bottomless.

new_drink

Throw out your current drink and replace it with a new drink. The function accepts two parameters. The first is the name of the new drink type and the second is the amount of the new drink type.

empty

Empties out the contents of the cup in its entirety. fluid_oz_ should be set to 0, and _drink_type should be set to "nothing".

Accessors

Create the accessors for fluid_oz_ and drink_type_.

Other information

Place the Cup class in cup.hpp and complete the code in main.cpp.

Sample Output

What kind of drink can I get you?: Kool AidHow much do you want?: 32Your cup currently has 32 oz. of Kool AidPlease select what you want to do with your drink/cup?:D: DrinkR: RefillN: Get a brand new drinkE: EmptyX: ExitSelection: DHow much do you want to drink from the cup?: 16Your cup currently has 16 oz. of Kool AidPlease select what you want to do with your drink/cup?:D: DrinkR: RefillN: Get a brand new drinkE: EmptyX: ExitSelection: DHow much do you want to drink from the cup?: 6Your cup currently has 10 oz. of Kool AidPlease select what you want to do with your drink/cup?:D: DrinkR: RefillN: Get a brand new drinkE: EmptyX: ExitSelection: RHow much do you want to refill your cup?: 2Your cup currently has 12 oz. of Kool AidPlease select what you want to do with your drink/cup?:D: DrinkR: RefillN: Get a brand new drinkE: EmptyX: ExitSelection: EEmptying your cupYour cup currently has 0 oz. of nothingPlease select what you want to do with your drink/cup?:D: DrinkR: RefillN: Get a brand new drinkE: EmptyX: ExitSelection: NWhat is the new drink you want?: CokeWhat is the amount you want?: 16Your cup currently has 16 oz. of CokePlease select what you want to do with your drink/cup?:D: DrinkR: RefillN: Get a brand new drinkE: EmptyX: ExitSelection: DHow much do you want to drink from the cup?: 8Your cup currently has 8 oz. of CokePlease select what you want to do with your drink/cup?:D: DrinkR: RefillN: Get a brand new drinkE: EmptyX: ExitSelection: XThank you for using Digicup!

Submission checklist

  1. Created function prototype and stored in .hpp file.
  2. Created function implementation and stored in .cpp file (see reference).
  3. Call function in the driver
  4. Compiled and ran the driver (main).
  5. Manually checked for compilation and logical errors.
  6. Ensured no errors on the unit test (make test).
  7. Followed advice from the stylechecker (make stylecheck).
  8. Followed advice from the formatchecker to improve code readbility (make formatcheck).

Code evaluation

Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy and you are currently in /home/student you can issue the following commands

cd labex02-tuffy

You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.

cd prob01

When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02 for example.

cd ..cd prob02

Use the clang++ command to compile your code and the ./ command to run it. The sample code below shows how you would compile code saved in main.cpp, and into the executable file main. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.

clang++ -std=c++17 main.cpp -o main./main

You can run one, two, or all the commands below to test your code, stylecheck your codes design, or formatcheck your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.

make testmake stylecheckmake formatcheck

A faster way of running all these tests uses the all parameter.

make all

Shopping List

Create a ShoppingList class that has three data members: a std::string* list_, an int num_items_, and an int list_size_.

ShoppingList

Default constructor

The default constructor should dynamically allocate list_ to an array of std::string objects with a size 10, initialize num_items_ to 0, and set list_size to 10.

Non-default constructor

The non-default constructor should receive one parameter: an int that is the size of the dynamically-allocated array of std::string assigned to list_. The size from the parameter should also be assigned to list_size_ and num_items should be set to 0.

Member functions

Accessors

Create accessor functions for num_items_ and list_size_.

add_item

Create a function add_item that receives an std::string and adds it to the first available location in the list.

Adding an item should also increment num_items_ which is used to track the total number of items added to the array and, conveniently, stores the index for the next item.

If you attempt to add an item into a full list, you should display an error message Error! Shopping List full!.

remove_last

Create a function remove_last that removes the last item from the list. Specifically, it sets the value of the last element to an empty string "" and decrements num_items_.

print_list

Create a functionprint_list that prints all elements in the list. It provides the numbering for items. For example, given a list of three items, it might show:

1) Milk2) Eggs3) Flour

Destructor

Create a destructor that uses the delete [] keyword to delete the list of shopping items. Dont forget to set the list pointer to nullptr to avoid dangling references.

Other instructions

Store your ShoppingList class in list.hpp. Member functions that take more than five lines or use complex constructs should have their function prototype in list.hpp and implementatiion in list.cpp.

In main.cpp, create a ShoppingList object using the non-default constructor where you pass it the value of 10. Add items to the shopping list according to the output below. Call the print_list function to display all items. The values are hard-coded and do not need to be retrieved from the user (no need for std::cin). See main.cpp for more details.

Sample Output:

Shopping List:1) Milk2) Eggs3) Flour4) Sugar5) Cocoa Powder6) Vanilla

Submission checklist

  1. Created function prototype and stored in .hpp file.
  2. Created function implementation and stored in .cpp file (see reference).
  3. Call function in the driver
  4. Compiled and ran the driver (main).
  5. Manually checked for compilation and logical errors.
  6. Ensured no errors on the unit test (make test).
  7. Followed advice from the stylechecker (make stylecheck).
  8. Followed advice from the formatchecker to improve code readbility (make formatcheck).

Code evaluation

Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy and you are currently in /home/student you can issue the following commands

cd labex02-tuffy

You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.

cd prob01

When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02 for example.

cd ..cd prob02

Use the clang++ command to compile your code and the ./ command to run it. The sample code below shows how you would compile code saved in list.cpp and main.cpp, and into the executable file main. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.

clang++ -std=c++17 main.cpp list.cpp -o main./main

You can run one, two, or all the commands below to test your code, stylecheck your codes design, or formatcheck your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.

make testmake stylecheckmake formatcheck

A faster way of running all these tests uses the all parameter.

make all

Player

Create a Player class with the following data members to represent a game character: xpos_ that is an int representing their x position, ypos_ that is an int representing their y position, name_ that is an std::string representing the players name, and health_, strength_, and defense_ which are all ints representing the characters attributes.

Default Constructor

Create a default constructor that initializes the following values to your data members: 0 for the x position, 0 for the y position, Ash for the name, 10 for the health, 5 for the strength and 2 for the defense.

Non-Default Constructor

Create a non-default constructor that takes in 6 values: the name, the health, the strength, the defense, the x position, and the y position respectively. These should be used to initialize the corresponding data members.

Accessors & Mutators

Create accessors and mutators for all data members

Member functions

display_stat

Create a member function called display_stat that takes in no parameters and does not return anything. This member function should display the name of the player, the players health, the players strength, the players defense, and the players coordinates (x and y position). Look at the output provided below for an example.

player_move

Create a member function called player_move that takes in no parameters and does not return anything. This member function should increment the x position and the y position by a value of 1.

is_player_dead

Create a member function called is_player_dead that takes in no parameters and returns a bool value. This member function should check the players health. If the players health is equal to 0, then return true, otherwise return false.

take_damage

Create a member function called take_damage that takes in an int value for the damage taken and returns nothing. This member function should display a statement saying that the player took damage, and decrease the players health based on the damage taken.

If the damage taken is greater than the players current health, set the players health to 0 (the player cannot have negative health). This member function should call the is_player_dead function to check whether the player survives the attack. If the player is dead, display a statement saying that the player is dead. Look at the output provided below for an example.

Other instructions

Place the Player class inside player.hpp. Member functions that take more than five lines or use complex constructs should have their function prototype in list.hpp and implementatiion in list.cpp.

The main function is already provided for you. Do not edit the main.cpp file.

Sample output

Player: AshHealth: 10Strength: 5Defense: 2At position: (10, 10)Player: LesHealth: 20Strength: 10Defense: 6At position: (0, 0)Ash took 25 damageAsh is deadPlayer: AshHealth: 0Strength: 5Defense: 2At position: (10, 10)

Submission checklist

  1. Created function prototype and stored in .hpp file.
  2. Created function implementation and stored in .cpp file (see reference).
  3. Call function in the driver
  4. Compiled and ran the driver (main).
  5. Manually checked for compilation and logical errors.
  6. Ensured no errors on the unit test (make test).
  7. Followed advice from the stylechecker (make stylecheck).
  8. Followed advice from the formatchecker to improve code readbility (make formatcheck).

Code evaluation

Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy and you are currently in /home/student you can issue the following commands

cd labex02-tuffy

You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.

cd prob01

When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02 for example.

cd ..cd prob02

Use the clang++ command to compile your code and the ./ command to run it. The sample code below shows how you would compile code saved in player.cpp and main.cpp, and into the executable file main. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.

clang++ -std=c++17 main.cpp player.cpp -o main./main

You can run one, two, or all the commands below to test your code, stylecheck your codes design, or formatcheck your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.

make testmake stylecheckmake formatcheck

A faster way of running all these tests uses the all parameter.

make all

Advanced Student Class

Create a Student class that has four data members: an std::string name_, an int id_, an int[] grades_, and an int num_grades_. The grades array will have a capacity of 10 elements.

Create a default constructor that sets name_ to Stu Dent, id to 123456789, grades_ empty, and num_grades_ to 0.

Create a non-default constructor that receives an std::string and an int for the students name and id, respectively. Remember to set num_grades_ to 0 as well.

Create accessor and mutator functions for name_, id_, and num_grades_.

Create a function called add_grade that receives an int and adds it to the grades_ array if there is space. The grade should not be added to the array if it exceeds 10 and displays the error message: Array full, unable to add grade.

Create another function called calculate_grade that returns the average of the grades as a double. Take note that if the user added fours grades, then the function should return the average of those four grades only.

Finally create a function called print_student that prints the students name and all their grades.

The main.cpp has already been created for you. It creates a Student object, adds grades to it, calls the print_student function, then prints the students total grade. You do not need to modify main.cpp.

Place your class in student.hpp. Member functions that take more than five lines or use complex constructs should have their function prototype in student.hpp and implementation in student.cpp.

Sample Output:

Lonnie Hansen 965137824Test Grades:9588927784Total grade = 87.20

Submission checklist

  1. Created function prototype and stored in .hpp file.
  2. Created function implementation and stored in .cpp file (see reference).
  3. Call function in the driver
  4. Compiled and ran the driver (main).
  5. Manually checked for compilation and logical errors.
  6. Ensured no errors on the unit test (make test).
  7. Followed advice from the stylechecker (make stylecheck).
  8. Followed advice from the formatchecker to improve code readbility (make formatcheck).

Code evaluation

Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy and you are currently in /home/student you can issue the following commands

cd labex02-tuffy

You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.

cd prob01

When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02 for example.

cd ..cd prob02

Use the clang++ command to compile your code and the ./ command to run it. The sample code below shows how you would compile code saved in student.cpp and main.cpp, and into the executable file main. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.

clang++ -std=c++17 main.cpp student.cpp -o main./main

You can run one, two, or all the commands below to test your code, stylecheck your codes design, or formatcheck your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.

make testmake stylecheckmake formatcheck

A faster way of running all these tests uses the all parameter.

make all

Find Number

Create a class called Numbers. Create two data members, an int* values_ that points to a dynamically allocated array of numbers and an int capacity_ that stores the total number of elements that the array can hold.

Default Constructor

Create a default constructor that initializes (sets) the value of 10 to capacity_ and initializes a new dynamically allocated array with a capacity of 10. It should call the init function inside the body of the constructor.

Non-default Constructor

Create a non-default constructor that accepts an int parameter that is assigned to capacity_ that is used as the capacity of the dynamically allocated array. It should also call the init function inside the body of the constructor.

Destructor

Create a destructor that deletes the dynamically allocated array and initializes the pointer to nullptr.

Accessor

Create an accessor for capacity_.

Member functions

init

The implementation of the init member function is already provided for you. You only need to create its function prototype in find_number.hpp. This function should be a private member function that sets initial values of your dynamically allocated array according to the capacity.

display_array

Create a member function display_array that displays the contents of the array, as shown in the output below.

2 4 6 8 10 12 14 16 18 20

find_number

Create a member function find_number that takes in an int parameter representing the number you want to find. It should check to see if the array contains the number that is passed. If the number is present, then display a statement saying that the number is in the array as shown in the the output below.

2 is in the array

Other instructions

The main function is already given to you. Do not edit main.cpp, but place your Numbers class in find_number.hpp. Member functions that take more than five lines or use complex constructs should have their function prototype in find_number.hpp and implementation in find_number.cpp.

Sample Output

2 4 6 8 10 12 14 16 18 202 is in the array10 is in the array16 is in the array

Submission checklist

  1. Created function prototype and stored in .hpp file.
  2. Created function implementation and stored in .cpp file (see reference).
  3. Call function in the driver
  4. Compiled and ran the driver (main).
  5. Manually checked for compilation and logical errors.
  6. Ensured no errors on the unit test (make test).
  7. Followed advice from the stylechecker (make stylecheck).
  8. Followed advice from the formatchecker to improve code readbility (make formatcheck).

Code evaluation

Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy and you are currently in /home/student you can issue the following commands

cd labex02-tuffy

You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.

cd prob01

When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02 for example.

cd ..cd prob02

Use the clang++ command to compile your code and the ./ command to run it. The sample code below shows how you would compile code saved in find_number.cpp and main.cpp, and into the executable file main. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.

clang++ -std=c++17 main.cpp find_number.cpp -o main./main

You can run one, two, or all the commands below to test your code, stylecheck your codes design, or formatcheck your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.

make testmake stylecheckmake formatcheck

A faster way of running all these tests uses the all parameter.

make all

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] Lab exercise 10 Problems
$25