[SOLVED] Lab exercise 10 Problems

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon Lab_10.zip (122.5 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
πŸ”’ Securely Powered by:
Secure Checkout
5/5 - (2 votes)
  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

Here’s 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: Drink
R: Refill
N: Get a brand new drink
E: Empty
X: 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 it’s 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 Aid
How much do you want?: 32
Your cup currently has 32 oz. of Kool Aid
Please select what you want to do with your drink/cup?:
D: Drink
R: Refill
N: Get a brand new drink
E: Empty
X: Exit
Selection: D
How much do you want to drink from the cup?: 16
Your cup currently has 16 oz. of Kool Aid
Please select what you want to do with your drink/cup?:
D: Drink
R: Refill
N: Get a brand new drink
E: Empty
X: Exit
Selection: D
How much do you want to drink from the cup?: 6
Your cup currently has 10 oz. of Kool Aid
Please select what you want to do with your drink/cup?:
D: Drink
R: Refill
N: Get a brand new drink
E: Empty
X: Exit
Selection: R
How much do you want to refill your cup?: 2
Your cup currently has 12 oz. of Kool Aid
Please select what you want to do with your drink/cup?:
D: Drink
R: Refill
N: Get a brand new drink
E: Empty
X: Exit
Selection: E
Emptying your cup
Your cup currently has 0 oz. of nothing
Please select what you want to do with your drink/cup?:
D: Drink
R: Refill
N: Get a brand new drink
E: Empty
X: Exit
Selection: N
What is the new drink you want?: Coke
What is the amount you want?: 16
Your cup currently has 16 oz. of Coke
Please select what you want to do with your drink/cup?:
D: Drink
R: Refill
N: Get a brand new drink
E: Empty
X: Exit
Selection: D
How much do you want to drink from the cup?: 8
Your cup currently has 8 oz. of Coke
Please select what you want to do with your drink/cup?:
D: Drink
R: Refill
N: Get a brand new drink
E: Empty
X: Exit
Selection: X
Thank 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 code’s 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 test
make stylecheck
make 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) Milk
2) Eggs
3) Flour

Destructor

Create a destructor that uses theΒ delete []Β keyword to delete the list of shopping items. Don’t 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) Milk
2) Eggs
3) Flour
4) Sugar
5) Cocoa Powder
6) 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 code’s 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 test
make stylecheck
make 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 player’s name, andΒ health_,Β strength_, andΒ defense_Β which are allΒ ints representing the character’s 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 player’s health, the player’s strength, the player’s defense, and the player’s 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 player’s health. If the player’s 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 player’s health based on the damage taken.

If the damage taken is greater than the player’s current health, set the player’s 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: Ash
Health: 10
Strength: 5
Defense: 2
At position: (10, 10)

Player: Les
Health: 20
Strength: 10
Defense: 6
At position: (0, 0)

Ash took 25 damage
Ash is dead

Player: Ash
Health: 0
Strength: 5
Defense: 2
At 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 code’s 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 test
make stylecheck
make 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 student’s 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 student’s 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 student’s 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 965137824
Test Grades:
95
88
92
77
84
Total 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 code’s 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 test
make stylecheck
make 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 it’s 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 20
2 is in the array
10 is in the array
16 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 code’s 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 test
make stylecheck
make formatcheck

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

make all

1 file Β· 122.5 KB

  • Lab_10.zip 122.5 KB