Question

ise 1. Create a new project named lab6 1. You will be implementing an IceCream class. For the class attributes, lets stick tBrror: This is not a valid number of scoops. 2 scoop (s) of strawberry ice cream costs 198 cents.

ise 1. Create a new project named lab6 1. You will be implementing an IceCream class. For the class attributes, let's stick to flavor, number ofscoops, and price (in cents). Note that the price per scoop of any flavor is 99 cents. Therefore, allow the flavor and scoops to be set directly, but not the price! The price should be set automatically based on the number of scoops entered. Some other requirements i. A default constructor and a constructor with parameters for your flavor and number of scoops member variables. You should also include setters and getters for these private member variables. However, make sure that the setter for price is private! You don't want anyone outside the class modifying price ii. Your constructor needs to call a set method which allows you to set the two member variables, thus takes two arguments. In turn, the set method will call the individual setters for each member variable iii. You number of scoops setter needs to throw an exception if a valid number for scoops is not used (only positive numbers should work). This means that back in your main, you will also have a try...catch block to catch this exception. Don't forget to include the stdexcept library. Use a general run-time exception like shown before if (some condition) do some stuff; else throw runtime error (" This is not a valid number of scoops."); Which means the catch should be catching a runtime error obiect iv. You will also make use of the const keyword to indicate which methods are not modifying the object v. Demonstrate the use of your IceCream class by creating objects of your class type, setting them to different values, and then printing out their information. Make sure to also show ain example of an incorrect value being assigned to a member variable, and the program throwing an exception. Potential sample run 1 scoop (s) of vanilla ice cream costs 99 cents. 2 scoop (s) of cookie dough ice cream costs 198 cents
Brror: This is not a valid number of scoops. 2 scoop (s) of strawberry ice cream costs 198 cents.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <sstream>
using namespace std;

class Icecream
{
private :
       string flavour;
   int noOfScoops;
   int price;
public :  
Icecream()
{
}
Icecream(string flavour, int noOfScoops) {
       setFlavour(flavour);
       setNoOfScoops(noOfScoops);
       setPrice(noOfScoops * 99);
   }

string getFlavour() const {
       return flavour;
   }

   void setFlavour(string flavour) {
       this->flavour = flavour;
   }

int getNoOfScoops() const{
       return noOfScoops;
   }

   void setNoOfScoops(int noOfScoops) {
       if (noOfScoops < 0)
           throw runtime_error("This is not a valid number of scoops");
       else
           this->noOfScoops = noOfScoops;
   }

   int getPrice() {
       return price;
   }

   void setPrice(int price) {
       this->price = price;
   }

  
   string toString() {
      
stringstream ss;
       ss<<noOfScoops<<" Scoop(s) of " << flavour << " ice cream cost " << getPrice()<< " cents.";
       return ss.str();
   }
};
int main()
{
Icecream ic1("Vanilla", 1);
Icecream ic2("cookie dough", 2);

try {

cout<<ic1.toString()<<endl;
cout<<ic2.toString()<<endl;
ic1.setNoOfScoops(-1);
} catch(runtime_error& x) {
cout << x.what() << endl;
}
ic2.setFlavour("strawberry");
cout<<ic2.toString()<<endl;
return 0;
}

___________________________

Output:

CAProgram Files (x86)\Dev-CppMinGW64 binceCreamClassException.exe Scoop<s> of Uanilla ice cream cost 99 cents Scoop(s) of coo


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
ise 1. Create a new project named lab6 1. You will be implementing an IceCream class. For the class attributes, let&#39...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab, you will be writing a complete class from scratch, the IceCreamCone class. You...

    In this lab, you will be writing a complete class from scratch, the IceCreamCone class. You will also write a driver to interact with a user and to test all of the IceCreamCone methods. Lab: • Cone • Ice Cream o Flavor o Topping • Ice Cream Cone • Driver Part I: Cone An ice cream cone has two main components, the flavor of ice cream and the type of cone. Write a Cone class that takes an integer in...

  • JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The ...

    JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The starter code has been provided below, just needs to be altered. For this project you will implement the Memento Design Pattern. To continue on with the food theme, you will work with Ice Cream Cones. You will need to use AdvancedIceCreamCone.java. Create at least three Ice Cream Cones - one with chocolate ice cream, one with vanilla ice cream and one with strawberry ice cream....

  • I need the following written in Java please, thank you ' We want you to implement...

    I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string...

    Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string +Contact()                     //default constructor sets all attribute strings to “unknown”; no other constructor +setName(string name) : void +setPhone(string pn) : void +setEmail(string em) : void +getName() : string +getPhone() : string +getEmail() : string +printContact() : void         //print out the name, phone numbers and email address Create New Project Name your project: CIS247_Lab7_YourLastName. For this exercise, you may use a header file or one file...

  • its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task an...

    its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task and Appointment which inherit from the Event class The Event Class This will be an abstract class. It will have four private integer members called year, month, day and hour which designate the time of the event It should also have an integer member called id which should be a...

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named...

    JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT