Question

In: Computer Science

rite the code for a class named Funnel. A funnel is a cone-shaped object that is...

rite the code for a class named Funnel. A funnel is a cone-shaped object that
is used to pour liquids into small openings. For the purpose of this question,
you may assume that a Funnel is an inverted cone (see image at left) and has
a flap (a lid) at the bottom that can be opened and closed.





A Funnel has the following PUBLIC features:
a) A Funnel created without any data has a radius of 12, a height of 20,
   units measured in "millilitres", and  .

b) Alternatively, a Funnel can be created by accepting 2 whole numbers 
   representing the radius and height, and has units in "ounces" .
   
   All Funnels start out in a closed position and are empty (i.e. have a volume of
   0.00).The formula for calculating the volume of a cone is: V = 1/3 * Pi * R^2 * H
   where V = Volume, Pi = 3.14159, R^2 = radius squared, and H = height ($0.00)

c) A Funnel contains a function changeFlap( ) that opens a closed flap and
   closes an open flap and returns nothing (1 mark).

d) A Funnel also contains a function pour(int seconds) that empties the
   Funnel according to the formula: f = 2.5/R per second
   (where f = amount poured, R = radius), but ONLY FOR Funnel's that have
   OPEN FLAPS!
   The function also displays the amount poured for each second, and
   finally returns the total amount poured.
   So, for a default Funnel with a volume of 3015.9264, the function pour(2)
   would display:
   after 1 secs, poured 0.0075 millilitres, volume now at 3015.9189
   after 2 secs, poured 0.0075 millilitres, volume now at 3015.9114
   The function would return 0.015 for THIS EXAMPLE ONLY!

   If the Funnel's flap is closed, then only the line:
   "Sorry Funnel flap is closed!"
   is displayed, and 0.00 would be returned.

   This function permanently reduces the Funnel's volume .

e) A Funnel contains another function pour( ) that computes and returns the
   exact number of seconds (including fractional portions) required to
   completely empty the Funnel .


f) The postfix ++ operator increases the Funnel's radius by 1
   and returns the Funnel's new volume .

g) The prefix ++ operator increases the Funnel's height by 1
   and returns the Funnel's new volume .

h) A function called calcVolume( ) that computes and returns the
   Funnel's volume .

i) A function called getVolume( ) that computes and returns the
   Funnel's current volume .

For example, the following program:

int main( ) {
   Funnel f1, f2(6, 15);
   float amount, volumeF1, volumeF2, seconds;

   volumeF1 = f1++; // sets Funnel f1's radius to 13
   volumeF2 = ++f2; // sets Funnel f2's height to 16

   cout << "f1's volume is: " << volumeF1 << endl;
   cout << "f2's volume is: " << volumeF2 << endl;

   amount = f1.pour(5); // flap is closed, so does not pour anything
   f1.changeFlap( );

   amount = f1.pour(5);

   cout << "amount poured from Funnel f1: " << amount << endl;

   cout << "------------------------------------------------" << endl;

   seconds = f2.pour( );
   cout << "it takes " << seconds << " seconds to empty Funnel f2" << endl;

   f2.changeFlap( );
   f2.pour(3);

   cout << "f1's volume is: " << f1.getVolume( ) << endl;
   cout << "f2's volume is: " << f2.getVolume( ) << endl;

   return 0;
}

would display:
f1's volume is: 3539.52
f2's volume is: 603.185
Sorry Funnel flap is closed!
after 1 secs, poured 0.192308 millilitres, volume now at 3539.33
after 2 secs, poured 0.192308 millilitres, volume now at 3539.14
after 3 secs, poured 0.192308 millilitres, volume now at 3538.95
after 4 secs, poured 0.192308 millilitres, volume now at 3538.76
after 5 secs, poured 0.192308 millilitres, volume now at 3538.56
amount poured from Funnel f1: 0.961538
------------------------------------------------
it takes 1447.64 seconds to empty Funnel f2
after 1 secs, poured 0.416667 ounces, volume now at 602.769
after 2 secs, poured 0.416667 ounces, volume now at 602.352
after 3 secs, poured 0.416667 ounces, volume now at 601.935
f1's volume is: 3538.56
f2's volume is: 601.935


Solutions

Expert Solution

#include <iostream>
using namespace std;
int flap=0;
class Funnel {
   public:
          Funnel(){                     //default constructor
             radius=12;
         height=20;
         volume= ((3.14*radius*radius*height)/3)/29.5735;   //unit in millilitres
      }
      Funnel(int rad, int hght){    //parametrized constructor
         radius=rad;
         height=hght;
         volume= (3.14*radius*radius*height)/3;   //unit in ounces
      }
    //function to open and close flap
      void changeFlap(){
         if(flap==0)
            flap=1;
         else
            flap=0;
      }
//calculates the volume after every second, the liquid is poured out
      float pour(int seconds){
         float f= 0.00;
            int i=1;
         if(flap==0){
            cout<<"Sorry, Flap is closed!";
            return f;
         }
         else {
            while(i <= seconds){
              f= (2.5/ radius)/29.5735;
              volume= volume-f;
              cout<<"after "<<i<<" seconds, poured"<<f<<" millilitres, volume now at"<<volume;
              return (f*seconds);
         }   
      }
 //calculates the time required to empty the funnel
    float pour(){
      float time;
      time= (radius * volume) /2.5;
      return time;
    }
      // Overload postfix ++ operator 
    friend Funnel operator++(Funnel &ob)
    {   
        ob.radius++;
        return ( (3.14*radius*radius*height)/3);
    }
      // Overload postfix ++ operator 
    friend Funnel operator++(Funnel &ob, int h)
    {
        ++ob.height;
        return ( (3.14*radius*radius*height)/3);
    }
     //calculates the volume
     float calcVolume(){
         return ( (3.14*radius*radius*height)/3);
    }
    //returns the current volume
    float getVolume(){
         return volume;
    }
       
   private:
      int radius;      // radius of the funnel
      int height;      // Height of the funnel
      float volume;   // volume of the funnel
};




Related Solutions

CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a price of $50.99, prints the name and price of the product, reduces its price by 4.0, and then prints its price again. public class Product { private String name; private double price; public Product(String productName, double productPrice) { name = productName; price = productPrice; } public String getName() { return name; } public double getPrice() { return price; } public void reducePrice(double amount) {...
Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers (using one or a pair of for or while loops): 0 1 2 3 4 0 1 2 3 4 3.2 Consider the ArrayList object named list containing these Integers: list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 } What are the contents of list after this loop completes? for (int i = 1; i < 10; ++i) {...
CODE IN C++ PLEASE Create a structure (object) named Person that has the following characteristics: •...
CODE IN C++ PLEASE Create a structure (object) named Person that has the following characteristics: • full name • age • family members full names represent by an array of size 10. Write a program that creates an array of Person of size 5. Populate the array of objects with the information given by user input according to the following specifications: • With input from the user i) Populate only 3 positions of the array of Person object. ii) Populate...
Problem: A bottle is shaped with a right cone placed on top of a right cylinder....
Problem: A bottle is shaped with a right cone placed on top of a right cylinder. The radius of the cone and the cylinder is 1.5 inches. The height of the cone is 3 inches and the height of the cylinder is 5 inches. Find the volume of the bottle. About how much paper or plastic is needed to make a label for the cylindrical part of your bottle? Explain.
Create a class named RemoveDuplicates and write code: a. for a method that returns a new...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list) b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0) c. to output the original array that displays all integers entered d. to output the new array that displays the list with duplicates removed Use this TEST...
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
The cone shaped sensor at the base of the semicircular canals involved with balance? Saccule Otoliths...
The cone shaped sensor at the base of the semicircular canals involved with balance? Saccule Otoliths Utricle scala media Cupola
Instructions 1. Create a class named after your favorite object in the whole world. For example,...
Instructions 1. Create a class named after your favorite object in the whole world. For example, if you love pizza, then create a class called Pizza.java. This class should not have a main method (1 point) 2. Create Instance Variables (attributes) (2 point) Create at least 3 private instance fields (attributes) for your class You must use at least 3 different data types for your fields 3. Create getter (accessor) and setter (mutator) methods   Create a getter (accessor) method for...
Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                            &n
Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                                                              - meanings: String + DictionaryWord (String word, String meanings) + getWord(): String + setWord (String word): void + getMeanings(): String + setMeanings(String meanings): void Write a program with the following requirements: Creates 8 DictionaryWord objects with: Word and meanings as the table: word meanings bank robber Steals money from a bank burglar Breaks into a home to steal things forger Makes an illegal copy of something...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT