[Solved] Lab exercise 11 Problems

$25

File Name: Lab_exercise_11_Problems.zip
File Size: 226.08 KB

SKU: [Solved] Lab exercise 11 Problems Category: Tag:
5/5 - (1 vote)
  1. Write C++ programs
  2. Compile C++ programs
  3. Implement programs that use object composition.

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.

PetsBreed ClassCreate a Breed class with the following:

Data membersCreate the following data members: std::string species_, name_, and color_.

Default ConstructorCreate a default constructor for Breed that sets its species_ to Dog, name_ to Pug, and color_ to Fawn.

Non-Default ConstructorCreate a non-default constructor that receives an std::string for species_, name_, and color_; in that order. The values from the constructor should appropriately assign the data members.

Accessors and MutatorsCreate accessors and mutators for all data members.

Pet ClassCreate a Pet class with the following:

Data membersCreate private member variables std::string name_, Breed breed_ , and double weight_.

Default ConstructorCreate a default constructor for Pet that sets its name to Doug and weight to 15.6. The Breed object will automatically be created using its default constructor.

Non-Default ConstructorsCreate a non-default constructor that receives an std::string for name_, Breed for breed_, and a double for weight_ in that order. The values from the constructor should appropriately assign the data members.

Create another non-defaault constructor that receives an std::string for name_, std::string species_, name_, and color_ for the Breed constructor, and double for weight_. The values from the constructor should appropriately assign the data members.

Accessors and MutatorsCreate accessors and mutators for name_, breed_, and weight_.

set_breed overloadCreate a method overload for set_breed that accepts an std::string species_, name_, and color_ that will internally create a Breed object using the values provided and then assign it to the breed_ data member.

printCreate a member function called print that returns void and does not take in any parameters. Using the data members, this function should print out the name and weight of the Pet. It should also utilize accessors of the Breed class to get the species, breed name, and color.

Other instructionsComplete the main function as described. Place your classes in pet.hpp. Member functions that take more than five lines or use complex constructs should have their function prototype in pet.hpp and implementation in pet.cpp.

Sample Output:Please enter the pets name (q to quit): AirBudPlease enter the pets type: DogPlease enter the pets breed: Golden RetrieverPlease enter the pets color: BlondePlease enter the pets weight (lbs): 44.5Please enter the pets name (q to quit): CookiePlease enter the pets type: DogPlease enter the pets breed: English BulldogPlease enter the pets color: Brown & WhitePlease enter the pets weight (lbs): 31.2Please enter the pets name (q to quit): qPrinting Pets:Pet 1Name: AirBudSpecies: DogBreed: Golden RetrieverColor: BlondeWeight: 44.5 lbsPet 2Name: CookieSpecies: DogBreed: English BulldogColor: Brown & WhiteWeight: 31.2 lbsSubmission checklistCreated function prototype and stored in .hpp file.Created function implementation and stored in .cpp file (see reference).Call function in the driverCompiled and ran the driver (main).Manually checked for compilation and logical errors.Ensured no errors on the unit test (make test).Followed advice from the stylechecker (make stylecheck).Followed advice from the formatchecker to improve code readbility (make formatcheck).Code evaluationOpen 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-tuffyYou 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 prob01When 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 prob02Use 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 pet.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 pet.cpp -o main./mainYou 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 formatcheckA faster way of running all these tests uses the all parameter.

make all

Candy Shop

Create a class called CandyShop that will have the following:

Data members

CandyShop should have two private data members, a Candy array of size 10 and an int to keep count of the number of elements in the array, called size_.

NOTE: The code for the object Candy will be provided to you.

Default Constructor

Create a default constructor that sets size_ to 0.

Accessor

Create an accessor for size_.

add

Create a member function called add that passes in a Candy object. This member function should add the Candy object into the array as long as the size is less than 10. Otherwise, it should print this error message: Error, bag is full!.

print

Create a member function called print that displays the contents of the bag. Specifically, it shows the brand, flavor, and cost of every Candy object. Look at the output below for reference.

total_price

Create a member function called total_price that will return a double for the total cost of all the candy in the bag. Take note that the member function does not display anything on screen. The value is printed in the main function.

main

Complete the main function that should ask the user to enter in the brand, flavor, and cost of the candy, then use Candy and CandyShop objects to store and display the data as shown in the sample output below.

Other instructions

Complete the main function as described. Place your classes in candy_shop.hpp. Member functions that take more than five lines or use complex constructs should have their function prototype in candy_shop.hpp and implementation in candy_shop.cpp.

Sample output

Welcome to the Candy Shop!Enter the brand of candy (q to quit): SkittlesEnter the flavor of candy: FruityEnter the cost of candy: 4.25Enter the brand of candy (q to quit): SnickersEnter the flavor of candy: ChocolateEnter the cost of candy: 5.13Enter the brand of candy (q to quit): Sour Patch KidsEnter the flavor of candy: Sour-FruityEnter the cost of candy: 10.12Enter the brand of candy (q to quit): qContents of bag:Candy 1  Brand: Skittles  Flavor: Fruity  Cost: $4.25Candy 2  Brand: Snickers  Flavor: Chocolate  Cost: $5.13Candy 3  Brand: Sour Patch Kids  Flavor: Sour-Fruity  Cost: $10.12The total cost of all the candy in the bag is: $19.50

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 candy_shop.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 candy_shop.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

Car Class Composition

Create a class called Car that will utilize other objects.

Car

Car data members

Create two data members that are: (1) an instance of the Identifier class called identity_ and (2) an instance of the Date class called release_date_.

NOTE: Identifier and Date are classes that will be provided to you. You DO NOT need to create them.

Default Constructor

The default constructor will be EMPTY, so you do not have to initialize anything. Identifier and Dates respective constructors will initialize their default values.

Non-Default Constructors

  1. Create a non-default constructor that takes in an Identifier object. This will assign the parameter to the identity_ data member.
  2. Create a non-default constructor that takes in a Date object. This will assign the parameter to the release_date_ data member.
  3. Create a non-default constructor that takes in an Identifier and a Date object (in this order). This will assign the parameters to the identity_ and release_date_ parameters correspondingly.

Accessors and Mutators

Create accessors and mutators for identity_ and release_date_.

Other Member Functions

Create a void member function called print that takes in no parameters. print should print the name, id, license plate, and release date of the car. The release data should follow the format Month/Day/Year. See the output below for your reference.

Other instructions

Complete the main function as described. Place your classes in car_comp.hpp. Member functions that take more than five lines or use complex constructs should have their function prototype in car_comp.hpp and implementation in car_comp.cpp.

Your program does not need to account for the correct dates or license plates. For example: 13/41/1 will be acceptable for your program, even though it is not an acceptable date. 1111111111111111 will be acceptable in your program, even though it is not a valid license plate number.

Sample output

The name of the car is: FordThe id of the car is: 1The license plate of the car is: 123456The release date of the car is: 1/1/1885The name of the car is: HondaThe id of the car is: 3The license plate of the car is: 7B319X4The release date of the car is: 1/1/1885The name of the car is: FordThe id of the car is: 1The license plate of the car is: 123456The release date of the car is: 11/4/2018The name of the car is: HondaThe id of the car is: 3The license plate of the car is: 7B319X4The release date of the car is: 11/4/2018The name of the car is: FordThe id of the car is: 1The license plate of the car is: 123456The release date of the car is: 1/1/1885

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 car_comp.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 car_comp.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 11 Problems
$25