Lab exercise 11 Problems Solved

35.00 $

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

You'll get a download link with a: . zip solution files instantly, after Payment

Description

5/5 - (2 votes)
  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

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

Pets
Breed Class
Create a Breed class with the following:

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

Default Constructor
Create a default constructor for Breed that sets its species_ to “Dog”, name_ to “Pug”, and color_ to “Fawn”.

Non-Default Constructor
Create 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 Mutators
Create accessors and mutators for all data members.

Pet Class
Create a Pet class with the following:

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

Default Constructor
Create 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 Constructors
Create 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 Mutators
Create accessors and mutators for name_, breed_, and weight_.

set_breed overload
Create 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.

print
Create 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 instructions
Complete 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 pet’s name (q to quit): AirBud
Please enter the pet’s type: Dog
Please enter the pet’s breed: Golden Retriever
Please enter the pet’s color: Blonde
Please enter the pet’s weight (lbs): 44.5
Please enter the pet’s name (q to quit): Cookie
Please enter the pet’s type: Dog
Please enter the pet’s breed: English Bulldog
Please enter the pet’s color: Brown & White
Please enter the pet’s weight (lbs): 31.2
Please enter the pet’s name (q to quit): q
Printing Pets:
Pet 1
Name: AirBud
Species: Dog
Breed: Golden Retriever
Color: Blonde
Weight: 44.5 lbs
Pet 2
Name: Cookie
Species: Dog
Breed: English Bulldog
Color: Brown & White
Weight: 31.2 lbs
Submission checklist
Created function prototype and stored in .hpp file.
Created function implementation and stored in .cpp file (see reference).
Call function in the driver
Compiled 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 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 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
./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

 

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): Skittles
Enter the flavor of candy: Fruity
Enter the cost of candy: 4.25
Enter the brand of candy (q to quit): Snickers
Enter the flavor of candy: Chocolate
Enter the cost of candy: 5.13
Enter the brand of candy (q to quit): Sour Patch Kids
Enter the flavor of candy: Sour-Fruity
Enter the cost of candy: 10.12
Enter the brand of candy (q to quit): q
Contents of bag:
Candy 1
  Brand: Skittles
  Flavor: Fruity
  Cost: $4.25
Candy 2
  Brand: Snickers
  Flavor: Chocolate
  Cost: $5.13
Candy 3
  Brand: Sour Patch Kids
  Flavor: Sour-Fruity
  Cost: $10.12

The 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 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

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

NOTEIdentifier 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 Date‘s 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: Ford
The id of the car is: 1
The license plate of the car is: 123456
The release date of the car is: 1/1/1885

The name of the car is: Honda
The id of the car is: 3
The license plate of the car is: 7B319X4
The release date of the car is: 1/1/1885

The name of the car is: Ford
The id of the car is: 1
The license plate of the car is: 123456
The release date of the car is: 11/4/2018

The name of the car is: Honda
The id of the car is: 3
The license plate of the car is: 7B319X4
The release date of the car is: 11/4/2018

The name of the car is: Ford
The id of the car is: 1
The license plate of the car is: 123456
The 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 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
  • Lab_11.zip