Starting from:

$25

COMP1210-Project 4 Ice Cream Cone App Solved

You will write a program this week that is composed of two classes: (1) one named IceCreamCone that defines IceCreamCone objects, and (2) the other, IceCreamConeApp, which has a main method that reads in data, creates an IceCreamCone object, and then prints the object.  

 

An ice cream cone is a cone with a hemisphere on top as depicted below with cone height h and cone radius r, where r is also the radius of the hemisphere. The formulas are provided to assist you in computing return values for the respective methods in the IceCreamCone class described in this project.   
 
 

radius (r) 

height (h) 

 

Cone Side Area (cA) 

Hemisphere Area (hA) 

Surface Area (A) 

 

Cone Volume (cV) 

Hemisphere Volume (hV) 

Volume (V) 

 

 
 

 𝑐𝐴 =              𝜋    𝑟             ’ℎ) + 𝑟)                     

 

ℎ𝐴 = 2  𝜋𝑟)

 

𝐴 = 𝑐𝐴 + ℎ𝐴

 

𝑐𝑉 = ℎ𝜋𝑟)⁄3  

 

ℎ𝑉 = 2𝜋𝑟/⁄3        

      

𝑉 = 𝑐𝑉 + ℎ𝑉      
 

        •    IceCreamCone.java 

 

Requirements: Create an IceCreamCone class that stores the label, radius, and height.  The radius and height must be greater than zero. The IceCreamCone class also includes methods to set and get each of these fields, as well as methods to calculate the surface area and volume of the cone, hemisphere, and the IceCreamCone object, and a method to provide a String value of an IceCreamCone object (i.e., a class instance).

 

Design:  The IceCreamCone class has fields, a constructor, and methods as outlined below.

 

(1)   Fields (instance variables): label of type String, radius of type double, and height of type double.  Initialize the String to "" and the double to 0 in their respective declarations. These instance variables should be private so that they are not directly accessible from outside of the IceCreamCone class, and these should be the only instance variables in the class.    

 

(2)   Constructor: Your IceCreamCone class must contain a public constructor that accepts three parameters (see types of above) representing the label, radius, and height.  Instead of assigning the parameters directly to the fields, the respective set method for each field (described below) should be called. For example, instead of the statement label = labelIn; use the statement setLabel(labelIn); Below are examples of how the constructor could be used to create IceCreamCone objects.  Note that although String and numeric literals are used for the actual parameters (or arguments) in these examples, variables of the required type could have been used instead of the literals.    

 

IceCreamCone ex1 = new IceCreamCone("Ex 1", 1, 2); 

 

IceCreamCone ex2 = new IceCreamCone(" Ex 2   ", 12.3, 25.5);  

IceCreamCone ex3 = new IceCreamCone("Ex 3", 123.4, 900);  

(3)   Methods: Usually a class provides methods to access and modify each of its instance variables (known as get and set methods) along with any other required methods.  The methods for IceCreamCone, which should each be public, are described below.  See formulas in Code and Test below. o getLabel:  Accepts no parameters and returns a String representing the label field.

o   setLabel: Takes a String parameter and returns a boolean. If the string parameter is not null, then the label field is set to the “trimmed” String and the method returns true. Otherwise, the method returns false and the label field is not set.

o   getRadius:  Accepts no parameters and returns a double representing the radius field.

o   setRadius:  Accepts a double parameter and returns a boolean as follows. If the double is greater than zero, sets the radius field to the double passed in and returns true.  Otherwise, the method returns false and does not set the radius field.  

o   getHeight:  Accepts no parameters and returns a double representing the height field.

o   setHeight:  Accepts a double parameter and returns a boolean as follows. If the double is greater than zero, sets the height field to the double passed in and returns true.  Otherwise, the method returns false and does not set the height field.  

o   surfaceArea:  Accepts no parameters and returns the double value for the total surface area calculated using formula above and the values of the radius and height fields.   

o   volume:  Accepts no parameters and returns the double value for the volume calculated using formula above and the values of the radius and height fields.

o   toString:  Returns a String containing the information about the IceCreamCone object formatted as shown below, including decimal formatting ("#,##0.0######") for the double values.  Newline and tab escape sequences should be used to achieve the proper layout.  In addition to the field values (or corresponding “get” methods), the following methods should be used to compute appropriate values in the toString method:

surfaceArea() and volume().  Each line should have no trailing spaces (e.g., there should be no spaces before a newline (\n) character).  The toString value for ex1, ex2, and ex3 respectively are shown below (the blank lines are not part of the toString values).

IceCreamCone "Ex 1" with radius = 1.0 and height = 2.0 units has:    surface area = 13.308 square units    volume = 4.1887902 cubic units 

 

IceCreamCone "Ex 2" with radius = 12.3 and height = 25.5 units has:    surface area = 2,044.5837657 square units    volume = 7,937.3689278 cubic units 

 

IceCreamCone "Ex 3" with radius = 123.4 and height = 900.0 units has:    surface area = 447,847.2056927 square units    volume = 18,287,175.0307675 cubic units  

Code and Test: As you implement your IceCreamCone class, you should compile it and then test it using interactions.  For example, as soon you have implemented and successfully compiled the constructor, you should create instances of IceCreamCone in interactions (e.g., copy/paste the examples above).  Remember that when you have an instance on the workbench, you can unfold it to see its values.  You can also open a viewer canvas window and drag the instance from the Workbench tab to the canvas window.  After you have implemented and compiled one or more methods, create an IceCreamCone object in interactions and invoke each of your methods on the object to make sure the methods are working as intended.  You may find it useful to create a separate class with a main method that creates an instance of IceCreamCone then prints it out.  This would be similar to the IceCreamConeApp class you will create below, except that in the IceCreamConeApp class you will read in the values and then create and print the object.  

 

        •     IceCreamConeApp.java 

 

Requirements: Create an IceCreamConeApp class with a main method that reads in values for label, radius, and height.  After the values have been read in, the main method creates an IceCreamCone object and then prints a new line and the object.   

       

Design:  The main method should prompt the user to enter the label, radius, and height.  After a value is read in for radius, if the value is less than or equal to zero, an appropriate message (see examples below) should be printed followed by a return from main.  Similarly, after a value is read in for height, if the value is less than or equal to zero, an appropriate message (see examples below) should be printed followed by a return from main. Assuming that radius and height are positive, an IceCreamCone object should be created and printed.    

Below is an example where the user has entered a non-positive value for radius. Your program input/output should be exactly as follows.

Line #
Program input/output









Enter label, radius, and height for an ice cream cone.    label: Bad radius    radius: -7 

Error: radius must be greater than 0. 

 
 

Below is an example where the user has entered a non-positive value for height.  Your program input/output should be exactly as follows.

Line #
Program input/output











Enter label, radius, and height for an ice cream cone.    label: Bad height    radius: 11.5    height: 0 

Error: height must be greater than 0. 

 
 

Below is an example where the user has the values from ex1, the first example, above for label, radius, and height.  Your program input/output should be exactly as follows.

Line #
Program input/output

















Enter label, radius, and height for an ice cream cone. 

   label: Ex 1    radius: 1    height: 2 

 

IceCreamCone "Ex 1" with radius = 1.0 and height = 2.0 units has: 

   surface area = 13.308 square units    volume = 4.1887902 cubic units  
 

Code:  Your program should use the nextLine method of the Scanner class to read user input.  Note that this method returns the input as a String.  Whenever necessary, you can use the Double.parseDouble method to convert the input String to a double.  For example:  

Double.parseDouble(s1) will return the double value represented by String s1.  For the printed lines requesting input for label, radius, and height, use a tab "\t" rather than three spaces.  After you have created the object, it can be printed simply by using its variable name; i.e., when the object’s variable name is evaluated in the println statement, its toString() method is automatically called.  Thus, printing the object reference variable myObj is equivalent to printing the return value of the myObj.toString() method call. In your println statement, be sure to prepend the newline character (e.g., "\n" + myObj) to skip the line as shown in the examples.

More products