ConicalFrustum List Menu App Solution

$35.00

Description

Deliverables

Your project files should be submitted to Web-CAT by the due date and time specified. You may submit your files to the skeleton code assignment until the project due date but should try to do this much earlier. The skeleton code assignment is ungraded, but it checks that your classes and methods are named correctly and that methods and parameters are correctly typed. The files you submit to skeleton code assignment may be incomplete in the sense that method bodies have at least a return statement if applicable or they may be essentially completed files. In order to avoid a late penalty for the project, you must submit your completed code files to Web-CAT no later than 11:59 PM on the due date for the completed code. If you are unable to submit via Web-CAT, you should e-mail your files in a zip file to your TA before the deadline.

Files to submit to Web-CAT (all three files must be submitted together):

  • ConicalFrustum.java

  • ConicalFrustumList.java

  • ConicalFrustumListMenuApp.java

Specifications

Overview: You will write a program this week that is composed of three classes: the first class defines ConicalFrustum objects, the second class defines ConicalFrustumList objects, and the third, ConicalFrustumListMenuApp, presents a menu to the user with eight options and implements these:

  1. read input file (which creates a ConicalFrustumList object), (2) print report, (3) print summary,

  1. add a ConicalFrustum object to the ConicalFrustumList object, (5) delete a ConicalFrustum object from the ConicalFrustumList object, (6) find a ConicalFrustum object in the ConicalFrustumList object, (7) Edit a ConicalFrustum in the ConicalFrustumList object, and (8) quit the program. [You should create a new “Project 6” folder and copy your Project 5 files (ConicalFrustum.java, ConicalFrustumList.java, conical_frustum_data_1.txt, and conical_frustum_data_0.txt) to it, rather than work in the same folder as Project 5 files.]

A Conical Frustum is a Frustum created by slicing the top off a cone (with the cut made parallel to the base), forming a lower base and an upper base that are circular and parallel.

r1 radius of top

lu1617qrrmji tmp fcb88c9613017b84

r2 radius of bottom

  1. height

  1. slant height

  1. lateral surface area V volume

A total surface area

V =

π ∗ h

(r*

+

+ r+

+

+ (r* ∗ r+))

3

lu1617qrrmji tmp cd9ecc15a7fcf592

s = /(r* − r+)+ + h+

lu1617qrrmji tmp 190a1879c32093d1

S = π * (r1 + r2) * s

A = π * (r12 + r22 + (r1 + r2) * s)

Source for figures and formulas: https://www.calculatorsoup.com/images/frustum001.gif

  • ConicalFrustum.java (assuming that you successfully created this class in “Project: ConicalFrustum App” done previously, just copy the file to your new folder for this project and go on to ConicalFrustumList.java on page 4. Otherwise, you will need to create ConicalFrustum.java as part of this project.)

Page 1 of 11

Project: ConicalFrustum List Menu App

Page 2 of 11

Requirements: Create a ConicalFrustum class that stores the label, radius of top, radius of bottom, and height where the radii and height are non-negative. The ConicalFrustum class also includes methods to set and get each of these fields, as well as methods to calculate the volume, slant height, lateral surface area, and total surface area of a ConicalFrustum object, and a method to provide a String value that describes a ConicalFrustum object.

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

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

  1. Constructor: Your ConicalFrustum class must contain a public constructor that accepts four parameters (see types of above) representing the label, radius1, radius2, and height. Instead of assigning the parameters directly to the fields, the respective set method for each field (described below) should be called since they are checking the validity of the parameter. For example, instead of using the statement label = labelIn; use the statement setLabel(labelIn); Below are examples of how the constructor could be used to create ConicalFrustum 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.

ConicalFrustum example1 = new ConicalFrustum(“Small”, 0.5, 0.75, 0.25);

ConicalFrustum example2 = new ConicalFrustum(” Medium “, 5.1, 10.2, 15.9);

ConicalFrustum example3 = new ConicalFrustum(“Large”, 98.32, 199.0, 250.0);

  1. 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 ConicalFrustum, which should each be public, are described below. See the formulas in the figure above and the Code and Test section below for information on constructing these methods.

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 “trimmed” String is set to the label field and the method returns true. Otherwise, the method returns false and the label is not set.

o getRadius1: Accepts no parameters and returns a double representing the radius1 field.

o setRadius1: Takes a double parameter and returns a boolean. If the double parameter is non-negative, then the parameter is set to the radius1 field and the method returns true. Otherwise, the method returns false and the radius1 field is not set.

Page 2 of 11

Project: ConicalFrustum List Menu App

Page 3 of 11

  1. getRadius2: Accepts no parameters and returns a double representing the radius2 field.

  1. setRadius2: Takes a double parameter and returns a boolean. If the double parameter is non-negative, then the parameter is set to the radius2 field and the method returns true. Otherwise, the method returns false and the radius2 field is not set.

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

  1. setHeight: Accepts a double parameter and returns a boolean as follows. If the double parameter is non-negative, then the parameter is set to the height field and the method returns true. Otherwise, the method returns false and the height field is not set.

  1. volume: Accepts no parameters and returns the double value for the volume of the ConicalFrustum. [Be sure to avoid integer division in your expression.]

  1. slantHeight: Accepts no parameters and returns the double value for the slant height of the ConicalFrustum.

  1. lateralSurfaceArea: Accepts no parameters and returns the double value for the lateral surface area of the ConicalFrustum. Be sure to call your slantHeight method as appropriate.

  1. totalSurfaceArea: Accepts no parameters and returns the double value for the total surface area of the ConicalFrustum. Be sure to call your slantHeight method as appropriate.

  1. toString: Returns a String containing the information about the ConicalFrustum 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: volume(), slantHeight(), lateralSurfaceArea(), and totallSurfaceArea(). Each line should have no trailing spaces (e.g., there should be no spaces before a newline (\n) character). The toString value for example1, example2, and example3 respectively are shown below (the blank lines are not part of the toString values).

ConicalFrustum “Small” with radius1 = 0.5, radius2 = 0.75, and height = 0.25 has:

volume = 0.311 cubic units

slant height = 0.354 units

lateral surface area = 1.388 units

total surface area = 3.941 square units

ConicalFrustum “Medium” with radius1 = 5.1, radius2 = 10.2, and height = 15.9 has:

volume = 3,031.546 cubic units

slant height = 16.698 units

lateral surface area = 802.608 units

total surface area = 1,211.172 square units

ConicalFrustum “Large” with radius1 = 98.32, radius2 = 199.0, and height = 250.0 has:

volume = 18,020,568.788 cubic units

slant height = 269.512 units

lateral surface area = 251,739.485 units

total surface area = 406,518.914 square units

Page 3 of 11

Project: ConicalFrustum List Menu App

Page 4 of 11

Code and Test: As you implement your ConicalFrustum 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 ConicalFrustum in interactions (e.g., copy/paste the examples on page 2). 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 a ConicalFrustum 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 ConicalFrustum then prints it out. This would be similar to the ConicalFrustumApp class you will below, except that in the ConicalFrustumApp class you will read in the values and then create and print the object.

  • ConicalFrustumList.java – extend from “Project : Conical Frustum List App” by adding the last six methods below. (Assuming that you successfully created this class in the previous project, just copy ConicalFrustumList.java to your new folder for this project and then add the indicated methods. Otherwise, you will need to create all of ConicalFrustumList.java as part of this project.)

Requirements: Create a ConicalFrustumList class that stores the name of the list and an ArrayList of ConicalFrustum objects. It also includes methods that return the name of the list, number of ConicalFrustum objects in the ConicalFrustumList, total surface area, total volume, average surface area, and average volume for all ConicalFrustum objects in the ConicalFrustumList. The toString method returns a String containing the name of the list followed by each ConicalFrustum in the ArrayList, and a summaryInfo method returns summary information about the list (see below).

Design: The ConicalFrustumList class has two fields, a constructor, and methods as outlined below. These instance variables should be private, and they should be the only instance variables (fields) in the class.

    1. Fields (or instance variables): (1) a String representing the name of the list and (2) an ArrayList of ConicalFrustum objects. These are the only fields (or instance variables) that this class should have.

    2. Constructor: Your ConicalFrustumList class must contain a constructor that accepts a parameter of type String representing the name of the list and a parameter of type ArrayList< ConicalFrustum> representing the list of ConicalFrustum objects. These parameters should be used to assign the fields described above (i.e., the instance variables).

    1. Methods: The methods for ConicalFrustumList are described below.

o getName: Returns a String representing the name of the list.

o numberOfConicalFrustums: Returns an int representing the number of ConicalFrustum objects in the ConicalFrustumList. If there are zero ConicalFrustum objects in the list, zero should be returned.

Page 4 of 11

Project: ConicalFrustum List Menu App

Page 5 of 11

  1. totalSurfaceArea: Returns a double representing the total surface areas for all ConicalFrustum objects in the list. If there are zero ConicalFrustum objects in the list, zero should be returned.

  1. totalVolume: Returns a double representing the total volumes for all ConicalFrustum objects in the list. If there are zero ConicalFrustum objects in the list, zero should be returned.

  1. averageSurfaceArea: Returns a double representing the average for the total surface area for all ConicalFrustum objects in the list. If there are zero ConicalFrustum objects in the list, zero should be returned.

  1. averageVolume: Returns a double representing the average volume for all ConicalFrustum objects in the list. If there are zero ConicalFrustum objects in the list, zero should be returned.

  1. toString: Returns a String (does not begin with \n) containing the name of the list followed by each ConicalFrustum in the ArrayList. In the process of creating the return result, this toString() method should include a while loop that calls the toString() method for each ConicalFrustum object in the list (adding a \n before and after each). Be sure to include appropriate newline escape sequences. For an example, see lines 3 through 21 in the output from ConicalFrustumListApp (previous project) for the conical_frustum_data_1.txt input file. [Note that the toString result should not include the summary items in lines 24 through 29 of the example. These lines represent the return value of the summaryInfo method below which will be called from main.]

  1. summaryInfo: Returns a String (does not begin or end with \n) containing the name of the list (which can change depending of the value read from the file) followed by various summary items: number of ConicalFrustums, total surface area, total volume, average surface area, and average volume. Use “#,##0.0##” as the pattern to format the double values. For an example, see lines 24 through 29 in the output below from ConicalFrustumListApp for the conical_frustum_data_1.txt input file. The second example below shows the output from ConicalFrustumListApp for the conical_frustum_data_0.txt input file which contains a list name but no ConicalFrustum data.

The following six methods are new in Project 6:

  1. getList: Returns the ArrayList of ConicalFrustum objects (the second field above).

  1. readFile: Takes a String parameter representing the file name, reads in the file, storing the list name and creating an ArrayList of ConicalFrustum objects, uses the list name and the ArrayList to create a new ConicalFrustumList object, and then returns the ConicalFrustumList object. See note #2 under Important Considerations for the ConicalFrustumListMenuApp class to see how this method should be called.

  1. addConicalFrustum: Returns nothing but takes four parameters (label, radius1, radius2, and height), creates a new ConicalFrustum object, and adds it to the ConicalFrustumList object.

  1. findConicalFrustum: Takes a label of a ConicalFrustum as the String parameter and returns the corresponding ConicalFrustum object if found in the ConicalFrustumList object; otherwise returns null. Letter case should be ignored when attempting to match the label (e.g., “Small Example” and “sMaLL EXAMPLE” should be a match).

Page 5 of 11

Project: ConicalFrustum List Menu App

Page 6 of 11

  1. deleteConicalFrustum: Takes a String as a parameter that represents the label of the ConicalFrustum. Returns the ConicalFrustum object if it is found in the ConicalFrustumList and deleted; otherwise returns null. Letter case should be ignored when attempting to match the label; consider calling/using findConicalFrustum in this method.

  1. editConicalFrustum: Takes four parameters (label, radius1, radius2, and height), uses the label to find the corresponding the ConicalFrustum object. If found, sets the radius1, radius2, and height to the values passed in as parameters, and returns true. If not found, returns false.

Code and Test: You must import java.util.ArrayList, java.util.Scanner, java.io.File, java.io.FileNotFoundException as these classes will be needed in the readFile method, which will require a throws clause for FileNotFoundException. You may want to consider implementing the ConicalFrustumListMenuApp class below in parallel with the new methods in ConicalFrustumList. For example, after implementing the readFile method, you can implement the corresponding “case” in the switch for menu described below in the ConicalFrustumListMenuApp class that calls the method. This will allow you quickly test it by entering the ‘R’ and ‘P’ options to read in the file and print the list.

  • ConicalFrustumListMenuApp.java (replaces ConicalFrustumListApp class from previous project)

Requirements: Create a ConicalFrustumListMenuApp class with a main method that presents

the user with a menu with eight options, each of which is implemented to do the following: (1) read the input file and create a ConicalFrustumList object, (2) print the ConicalFrustumList object, (3) print the summary for the ConicalFrustumList object, (4) add a ConicalFrustum object to the ConicalFrustumList object, (5) delete a ConicalFrustum object from the ConicalFrustumList object, (6) find a ConicalFrustum object in the ConicalFrustumList object,

(7) Edit a ConicalFrustum object in the ConicalFrustumList object, and (8) quit the program.

Design: The main method should print a list of options with the action code and a short description followed by a line with just the action codes prompting the user to select an action. After the user enters an action code, the action is performed, including output if any. Then the line with just the action codes prompting the user to select an action is printed again to accept the next code. The first action a user would normally perform is ‘R’ to read in the file and create a ConicalFrustumList object. However, the other action codes should work even if an input file has not been processed. The user may continue to perform actions until ‘Q’ is entered to quit (or end) the program. Note that your program should accept both uppercase and lowercase action codes. Below is output produced after printing the action codes with short descriptions followed by the prompt with the action codes waiting for the user to make a selection.

Page 6 of 11

Project: ConicalFrustum List Menu App

Page 7 of 11

Line #

Program output

1

ConicalFrustum List System Menu

2

R – Read File and Create ConicalFrustum List

3

P – Print ConicalFrustum List

4

S – Print Summary

5

A – Add ConicalFrustum

6

D – Delete ConicalFrustum

7

F – Find ConicalFrustum

8

E – Edit ConicalFrustum

9

Q – Quit

10

Enter Code [R, P, S, A, D, F, E, or Q]:

Note that after an option code is entered, each sub-prompt has a leading tab (\t) (e.g., lines 2 and 3 below). The example below shows the screen after the user entered ‘r’ and when prompted, the file name. Notice the output from this action was “File read in and ConicalFrustum List created”. This is followed by the prompt with the action codes waiting for the user to make the next selection. Use conical_frustum_data_1.txt from Project 5 to test your program.

Line #

Program output

1

Enter Code [R, P, S, A, D, F, E, or Q]: r

2

File name: conical_frustum_data_1.txt

3

File read in and ConicalFrustum List created

4

Enter Code [R, P, S, A, D, F, E, or Q]:

5

The result of the user selecting ‘p’ to Print ConicalFrustum List is shown below and next page.

Line #

Program output

1

Enter Code [R, P, S, A, D, F, E, or Q]: p

2

Conical Frustum Test List

3

4

ConicalFrustum “Small” with radius1 = 0.5, radius2 = 0.75, and height = 0.25 has:

5

6

volume = 0.311 cubic units

7

slant height = 0.354 units

8

lateral surface area = 1.388 units

9

total surface area = 3.941 square units

10

ConicalFrustum “Medium” with radius1 = 5.1, radius2 = 10.2, and height = 15.9 has:

11

12

volume = 3,031.546 cubic units

13

slant height = 16.698 units

14

lateral surface area = 802.608 units

15

total surface area = 1,211.172 square units

16

ConicalFrustum “Large” with radius1 = 98.32, radius2 = 199.0, and height = 250.0 has:

17

18

volume = 18,020,568.788 cubic units

19

slant height = 269.512 units

20

lateral surface area = 251,739.485 units

21

total surface area = 406,518.914 square units

22

Enter Code [R, P, S, A, D, F, E, or Q]:

23

Page 7 of 11

Project: ConicalFrustum List Menu App

Page 8 of 11

The result of the user selecting ‘s’ to print the summary for the list is shown below.

Line #

Program output

1

Enter Code [R, P, S, A, D, F, E, or Q]: s

2

—– Summary for Conical Frustum Test List —–

3

4

Number of ConicalFrustum Objects: 3

5

Total Surface Area: 407,734.026

6

Total Volume: 18,023,600.645

7

Average Surface Area: 135,911.342

8

Average Volume: 6,007,866.882

9

Enter Code [R, P, S, A, D, F, E, or Q]:

10

The result of the user selecting ‘a’ to add a ConicalFrustum object is shown below. Note that after ‘a’ was entered, the user was prompted for label, radius1, radius2, and height. Then after the ConicalFrustum object is added to the ConicalFrustum List, the message “*** ConicalFrustum added ***” was printed. Note that lines 2 – 6 below have leading tabs (\t). This is followed by the prompt for the next action. After you do an “add”, you should do a “print” or a “find” to confirm that the “add” was successful.

Line # Program output

lu1617qrrmji tmp eb8aebfb96f45ff1

  1. Enter Code [R, P, S, A, D, F, E, or Q]: a

  1. Label: Test of Add

  2. Radius1: 5.0

  3. Radius2: 6.0

  4. Height: 7.0

  5. *** ConicalFrustum added ***

7

8 Enter Code [R, P, S, A, D, F, E, or Q]:

Here is an example of the successful “delete” for a ConicalFrustum object, followed by an attempt that was not successful (i.e., the ConicalFrustum object was not found). Do “p” to confirm the “d”.

Line # Program output

lu1617qrrmji tmp 9ac3b9f7cbefaa05

  1. Enter Code [R, P, S, A, D, F, E, or Q]: d

  1. Label: Medium

  2. “Medium” deleted

4

  1. Enter Code [R, P, S, A, D, F, E, or Q]: d

  2. Label: not a real label

  3. “not a real label” not found

8

9 Enter Code [R, P, S, A, D, F, E, or Q]:

Page 8 of 11

Project: ConicalFrustum List Menu App

Page 9 of 11

Here is an example of the successful “find” for a ConicalFrustum object, followed by an attempt that was not successful (i.e., the ConicalFrustum object was not found).

Line # Program output

lu1617qrrmji tmp b98f479ac594eba6

  1. Enter Code [R, P, S, A, D, F, E, or Q]: f

  1. Label: small

  2. ConicalFrustum “Small” with radius1 = 0.5, radius2 = 0.75, and height = 0.25 has:

  3. volume = 0.311 cubic units

  4. slant height = 0.354 units

  5. lateral surface area = 1.388 units

  6. total surface area = 3.941 square units

8

  1. Enter Code [R, P, S, A, D, F, E, or Q]: f

  2. Label: not a real label

  3. “not a real label” not found

12

13 Enter Code [R, P, S, A, D, F, E, or Q]:

Here is an example of the successful “edit” for a ConicalFrustum object, followed by an attempt that was not successful (i.e., the ConicalFrustum object was not found). In order to verify the edit, you should do a “find” for “wide example” or you could do a “print” to print the whole list.

Line #

Program output

1

Enter Code [R, P, S, A, D, F, E, or Q]: e

2

Label: small

3

Radius1: 1

4

Radius2: 2

5

Height: 3

6

“small” successfully edited

7

Enter Code [R, P, S, A, D, F, E, or Q]: e

8

9

Label: even smaller

10

Radius1: .5

11

Radius2: .5

12

Height: .5

13

“even smaller” not found

14

Enter Code [R, P, S, A, D, F, E, or Q]:

15

Finally, below is an example of entering an invalid code, followed by an example of entering a ‘q’ to quit the application with no message.

Line # Program output

lu1617qrrmji tmp b7b37b1d0ddeb3b0

  1. Enter Code [R, P, S, A, D, F, E, or Q]: c

  1. *** invalid code ***

3

4 Enter Code [R, P, S, A, D, F, E, or Q]: q

Page 9 of 11

Project: ConicalFrustum List Menu App

Page 10 of 11

Code and Test: Important considerations

  1. This class should import java.util.Scanner, java.util.ArrayList, and java.io.FileNotFoundException. Carefully consider the following information as you develop this class.

  1. At the beginning of your main method, you should declare and create an ArrayList of ConicalFrustum objects and then declare and create a ConicalFrustumList object using the list name and the ArrayList as the parameters in the constructor. This will be a

ConicalFrustumList object that contains no ConicalFrustum objects. For example:

String _______ = *** no list name assigned ***”;

ArrayList<ConicalFrustum> _________ = new ArrayList<ConicalFrustum>(); ConicalFrustumList _________ = new ConicalFrustumList(_________,_________);

The ‘R’ option in the menu should invoke the readFile method on your ConicalFrustumList object. This will return a new ConicalFrustumList object based on the data read from the file, and this new ConicalFrustumList object should replace (be assigned to) your original ConicalFrustumList object variable in main. Since the readFile method throws FileNotFoundException, your main method needs to do this as well.

  1. Very Important: You should declare only one Scanner on System.in for your entire program, and this should be done in the main method. That is, all input from the keyboard (System.in) must be done in your main method. Declaring more than one Scanner on System.in in your program will likely result in a very low score from Web-CAT.

  1. For the menu, your switch statement expression should evaluate to a char and each case should be a char; alternatively, your switch statement expression should evaluate to a String with a length of 1 and each case should be a String with a length of 1.

  1. After printing the menu of actions with descriptions, you should have a do-while loop that prints the prompt with just the action codes followed by a switch statement that performs the indicated action. The do-while loop ends when the user enters ‘q’ to quit. You should strongly consider using a for-each loop as appropriate in the new methods that require you to search the list. You should be able to test your program by exercising each of the action codes. After you implement the “Print ConicalFrustum List” option, you should be able to print the ConicalFrustumList object after operations such as ‘A’ and ‘D’ to see if they worked. You may also want to run in debug mode with a breakpoint set at the switch statement so that you can step-into your methods if something is not working. In conjunction with running the debugger, you should also create a canvas drag the items of interest (e.g., the Scanner on the file, your ConicalFrustumList object, etc.) onto the canvas and save it. As you play or step through your program, you’ll be able to see the state of these objects change when the ‘R’, ‘A’, and ‘D’ options are selected.

    1. For option P, when you print the ConicalFrustumList object (e.g., cList) be sure to append a leading “\n” in println:

Page 10 of 11

Project: ConicalFrustum List Menu App

Page 11 of 11

System.out.println(“\n” + cList);

  1. For option S, when you print the summary for ConicalFrustumList object (e.g., cList) be sure to append a leading and trailing “\n” in the .println:

System.out.println(“\n” + cList.summaryInfo() + “\n”);

Although your program may not use all of the methods in your ConicalFrustum and ConicalFrustumList classes, you should ensure that all of your methods work according to the specification. You can run your program in the canvas and then after the file has been read in, you can call methods on the ConicalFrustumList object in interactions or you can write another class and main method to exercise the methods. Web-CAT will test all methods to determine your project grade.

Page 11 of 11