Starting from:

$34.99

COMP1210 Deliverables Solution


Files to submit to Web-CAT (all three files must be submitted together):
• PentagonalPyramid.java
• PentagonalPyramidList2.java
• PentagonalPyramidList2MenuApp.java

Specifications – Use arrays in this project; ArrayLists are not allowed!
Overview: The objective is to modify your Project 6 to use arrays instead of ArrayList objects. You will write a program this week that is composed of three classes: the first class defines
PentagonalPyramid objects, the second class defines PentagonalPyramidList2 objects, and the third, PentagonalPyramidList2MenuApp, presents a menu to the user with eight options and implements these: (1) read input file (which creates an PentagonalPyramidList2 object), (2) print report, (3) print summary, (4) add an PentagonalPyramid object to the PentagonalPyramidList2 object, (5) delete an PentagonalPyramid object from the PentagonalPyramidList2 object, (6) find an PentagonalPyramid object in the PentagonalPyramidList2 object, (7) Edit an PentagonalPyramid in the
PentagonalPyramidList2 object, and (8) quit the program. [You should create a new “Project 7” folder and copy your Project 6 files (PentagonalPyramid.java, PentagonalPyramidList.java,
PentagonalPyramidListMenuApp.java, PentagonalPyramid_data_1.txt, and
PentagonalPyramid_data_0.txt) to it, rather than work in the same folder as Project 6 files.]

To rename an existing .java file, open the file in jGRASP, change the name of the class and the name of the constructor (if it has one) in the source file, and then click the Save button. In the dialog that pops up, click the “Rename and Save” button. This will rename and save the .java file and delete the old associated .class file.

• PentagonalPyramid.java (assuming that you successfully created this class in Project 4, 5, or 6, just copy the file to your new Project 7 folder and go on to
PentagonalPyramidList2.java on page 4. Otherwise, you will need to create PentagonalPyramid.java as part of this project.)

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

A PentagonalPyramid has a pentagon base and five isosceles triangle faces, six vertices, and 10 edges as depicted below with base edge length a and height h. The formulas are provided to assist you in computing return values for the respective methods in the PentagonalPyramid class described in this project.
Ref: Do a “Google” search on “pentagonal pyramid surface area” for details and an interactive solver.

Surface Area
(A)

Volume (V)

Edge length
(a)

Height (h)



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

(1) Fields (instance variables): label of type String, base edge 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 PentagonalPyramid class, and these should be the only instance variables in the class.

(2) Constructor: Your PentagonalPyramid class must contain a public constructor that accepts three parameters (see types of above) representing the label, base edge, 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 PentagonalPyramid 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.

PentagonalPyramid ex1 = new PentagonalPyramid("Ex 1", 1, 2);
PentagonalPyramid ex2 = new PentagonalPyramid(" Ex 2 ", 12.3, 25.5);
PentagonalPyramid ex3 = new PentagonalPyramid("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 PentagonalPyramid, 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 getBaseEdge: Accepts no parameters and returns a double representing the base edge field.
o setBaseEdge: Accepts a double parameter and returns a boolean as follows. If the double is greater than zero, sets the base edge field to the double passed in and returns true. Otherwise, the method returns false and does not set the base edge 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 base edge and height fields. See code and test below regarding the Math.tan(x) method.
o volume: Accepts no parameters and returns the double value for the volume calculated using formula above and the values of the base edge and height fields.
o toString: Returns a String containing the information about the PentagonalPyramid 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 ( ) character). The toString value for ex1, ex2, and ex3 respectively are shown below (the blank lines are not part of the toString values).
PentagonalPyramid "Ex 1" with base edge = 1.0 and height = 2.0 units has: surface area = 7.008203 square units volume = 1.1469849 cubic units

PentagonalPyramid "Ex 2" with base edge = 12.3 and height = 25.5 units has: surface area = 1,086.4892066 square units volume = 2,212.4737204 cubic units

PentagonalPyramid "Ex 3" with base edge = 123.4 and height = 900.0 units has: surface area = 305,081.9691528 square units volume = 7,859,601.8538338 cubic units
Code and Test: Math.tan(x) expects x to be in radians rather than degrees. In the formula, 54 degrees can be converted to radians using Math.toRadians(54). The following combines the two methods: Math.tan(Math.toRadians(54))

• PentagonalPyramidList2.java – You must use arrays instead of ArrayList objects.
(Assuming that you successfully created this class in Project 6, just copy
PentagonalPyramidList.java to your new Project 7 folder and then open in jGRASP and rename as PentagonalPyramidList2. To rename an existing .java file, open the file in jGRASP, change the name of the class and the name of the constructor in the source code, and then click the Save button. In the dialog that pops up, click the “Rename and Save” button. This will rename and save the .java file and then delete old associated .class file. Otherwise, you will need to create all of PentagonalPyramidList2.java as part of this project.) In the requirements below, PentagonalPyramidList has been changed to PentagonalPyramidList2. Be sure to make these changes in your methods as necessary.

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

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

(1) Fields (or instance variables): (1) a String representing the name of the list, (2) an array of PentagonalPyramid objects, and (3) an int representing the number of PentagonalPyramid objects in the PentagonalPyramid array. These are the only fields (or instance variables) that this class should have.
(2) Constructor: Your PentagonalPyramidList2 class must contain a constructor that accepts a parameter of type String representing the name of the list, a parameter of type PentagonalPyramid[], representing the list of PentagonalPyramid objects, and a parameter of type int representing the number of PentagonalPyramid objects in the PentagonalPyramid array. These parameters should be used to assign the fields described above (i.e., the instance variables).

(3) Methods: Methods: The methods for PentagonalPyramidList are described below.
o getName: Returns a String representing the name of the list.
o numberOfPentagonalPyramids: Returns an int representing the number of PentagonalPyramid objects in the PentagonalPyramidList. If there are zero PentagonalPyramid objects in the list, zero should be returned.
o totalSurfaceArea: Returns a double representing the total surface areas for all PentagonalPyramid objects in the list. If there are zero PentagonalPyramid objects in the list, zero should be returned.
o totalVolume: Returns a double representing the total volumes for all
PentagonalPyramid objects in the list. If there are zero PentagonalPyramid objects in the list, zero should be returned.
o averageSurfaceArea: Returns a double representing the average surface area for all PentagonalPyramid objects in the list. If there are zero PentagonalPyramid objects in the list, zero should be returned.
o averageVolume: Returns a double representing the average volume for all
PentagonalPyramid objects in the list. If there are zero PentagonalPyramid objects in the list, zero should be returned.
o toString: Returns a String (does not begin with ) containing the name of the list followed by each PentagonalPyramid in the array. In the process of creating the return result, this toString() method should include a while loop that calls the toString() method for each PentagonalPyramid object in the list (adding a before and after each). Be sure to include appropriate newline escape sequences. For an example, see lines 2 through 16 in the output from PentagonalPyramidListApp for the PentagonalPyramid_data_1.txt input file. [Note that the toString result should not include the summary items in lines 18 through 24 of the example. These lines represent the return value of the summaryInfo method.]
o summaryInfo: Returns a String (does not begin with ) containing the name of the list (which can change depending of the value read from the file) followed by various summary items: number of PentagonalPyramid objects, 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 18 through 24 in the output from PentagonalPyramidList2App for the PentagonalPyramid_data_1.txt input file. The second example shows the output from PentagonalPyramidList2App for the PentagonalPyramid_data_0.txt input file which contains a list name but no PentagonalPyramid data.
o getList: Returns the array of PentagonalPyramid objects (the second field above).
o readFile: Takes a String parameter representing the file name, reads in the file, storing the list name and creating an array of PentagonalPyramid objects, uses the list name, the array, and number of PentagonalPyramid objects in the array to create a PentagonalPyramidList2 object, and then returns the PentagonalPyramidList2 object. See note #1 under Important Considerations for the PentagonalPyramidList2MenuApp class (last page) to see how this method should be called.
o addPentagonalPyramid: Returns nothing but takes three parameters (label, base edge, and height), creates a new PentagonalPyramid object, and adds it to the PentagonalPyramidList2 object. Finally, the number of elements field must be incremented.
o findPentagonalPyramid: Takes a label of a PentagonalPyramid as the String parameter and returns the corresponding PentagonalPyramid object if found in the PentagonalPyramidList2 object; otherwise returns null. Case should be ignored when attempting to match the label.
o deletePentagonalPyramid: Takes a String as a parameter that represents the label of the PentagonalPyramid and returns the PentagonalPyramid if it is found in the PentagonalPyramidList2 object and deleted; otherwise returns null. Case should be ignored when attempting to match the label; consider calling/using
findPentagonalPyramid in this method. When an element is deleted from an array, elements to the right of the deleted element must be shifted to the left. After shifting the items to the left, the last PentagonalPyramid element in the array should be set to null. Finally, the number of elements field must be decremented.
o editPentagonalPyramid: Takes three parameters (label, base edge, and height), uses the label to find the corresponding the PentagonalPyramid object. If found, sets the base edge and height to the values passed in as parameters, and returns true. If not found, returns false. This method should not change the label.

Code and Test: Remember to import java.util.Scanner, java.io.File,

• PentagonalPyramidList2MenuApp.java (replaces PentagonalPyramidListMenuApp class from Project 6; the file and class name in the file must be renamed to reflect
PentagonalPyramidList2MenuApp). To rename an existing .java file, open the file in jGRASP, change the name of the class in the source code, and then click the Save button. In the dialog that pops up, click the “Rename and Save” button. This will rename the .java file, save it, and delete the old associated .class file. Be sure to make these changes in your main method as necessary.

Requirements: Create a PentagonalPyramidList2MenuApp 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 PentagonalPyramidList2 object, (2) print the PentagonalPyramidList2 object, (3) print the summary for the PentagonalPyramidList2 object, (4)
add a PentagonalPyramid object to the PentagonalPyramidList2 object, (5) delete a
PentagonalPyramid object from the PentagonalPyramidList2 object, (6) find a
PentagonalPyramid object in the PentagonalPyramidList2 object, (7) edit a PentagonalPyramid object in the PentagonalPyramidList2 object, and (8) quit the program.


Line # Program output
1
2
3
4
5
6
7
8
9
10 PentagonalPyramid List System Menu
R - Read File and Create PentagonalPyramid List
P - Print PentagonalPyramid List
S - Print Summary
A - Add PentagonalPyramid
D - Delete PentagonalPyramid
F - Find PentagonalPyramid E - Edit PentagonalPyramid
Q - Quit
Enter Code [R, P, S, A, D, F, E, or Q]:

Below shows the screen after the user entered ‘r’ and then (when prompted) the file name. Notice the output from this action was “File read in and PentagonalPyramid List created”. This is followed by the prompt with the action codes waiting for the user to make the next selection. You should use the PentagonalPyramid_data_1.txt file from Project 5 to test your program.

Line # Program output
1
2
3
4
5 Enter Code [R, P, S, A, D, F, E, or Q]: r
File Name: PentagonalPyramid_data_1.txt
File read in and PentagonalPyramid List created

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

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

Line # Program output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Enter Code [R, P, S, A, D, F, E, or Q]: p
PentagonalPyramid Test List

PentagonalPyramid "Ex1" with base edge = 1.0 and height = 2.0 units has:
surface area = 7.008203 square units volume = 1.1469849 cubic units

PentagonalPyramid "Ex 2" with base edge = 12.3 and height = 25.5 units has: surface area = 1,086.4892066 square units volume = 2,212.4737204 cubic units

PentagonalPyramid "Ex 3" with base edge = 123.4 and height = 900.0 units has: surface area = 305,081.9691528 square units volume = 7,859,601.8538338 cubic units

Enter Code [R, P, S, A, D, F, E, or Q]:
The result of the user selecting ‘s’ to print the summary for the list is shown below.

Line # Program output
1
2
3
4
5
6
7
8
9
10
Enter Code [R, P, S, A, D, F, E, or Q]: s

----- Summary for PentagonalPyramid Test List -----
Number of PentagonalPyramid: 3
Total Surface Area: 306,175.467
Total Volume: 7,861,815.475
Average Surface Area: 102,058.489
Average Volume: 2,620,605.158

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


The result of the user selecting ‘a’ to add a PentagonalPyramid object is shown below. Note that after ‘a’ was entered, the user was prompted for label, base edge, and height. Then after the
PentagonalPyramid object is added to the PentagonalPyramid List, the message “***
PentagonalPyramid added ***” was printed. 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
1
2
3
4
5
6
7
Enter Code [R, P, S, A, D, F, E, or Q]: a
Label: Ex 4
Base Edge: 10.5
Height: 1000
*** PentagonalPyramid added ***

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


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

Line # Program output
1
2
3
4
5
6
7
8
9
Enter Code [R, P, S, A, D, F, E, or Q]: d
Label: ex 2
"Ex 2" deleted

Enter Code [R, P, S, A, D, F, E, or Q]: d
Label: Fake
"Fake" not found

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




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

Line # Program output
1
2
3
4
5
6
7
8
9
10
11
Enter Code [R, P, S, A, D, F, E, or Q]: f
Label: ex 3
PentagonalPyramid "Ex 3" with base edge = 123.4 and height = 900.0 units has:
surface area = 305,081.9691528 square units volume = 7,859,601.8538338 cubic units

Enter Code [R, P, S, A, D, F, E, or Q]: f
Label: Another Fake
"Another Fake" not found

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

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

Line # Program output
1
2
3
4
5
6
7
8
9
10
11
12
13
Enter Code [R, P, S, A, D, F, E, or Q]: e
Label: Ex 3
Base Edge: 250
Height: 1000
"Ex 3" successfully edited

Enter Code [R, P, S, A, D, F, E, or Q]: e
Label: Ex 33
Base Edge: 1.5
Height: 4.5
"Ex 33" not found

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


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
1
2
3
4
5 Enter Code [R, P, S, A, D, F, E, or Q]: k
*** invalid code ***

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






Page
Code and Test:
Important considerations: This class should import java.util.Scanner 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 array of PentagonalPyramid objects and then declare and create a PentagonalPyramidList2 object using the list name, the array, and 0 as the parameters in the constructor. This will be an PentagonalPyramidList2 object that contains no PentagonalPyramid objects. For example:
PentagonalPyramid[] _________ = new PentagonalPyramid[100];
PentagonalPyramidList2 _________ = new
PentagonalPyramidList2(_________,_________,_________);

The ‘R’ option in the menu should invoke the readFile method on your

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

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

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 PentagonalPyramid List” option, you should be able to print the

a. For option P, when you print the PentagonalPyramidList2 object (e.g., myList) be sure to append a leading “ ” in println:

System.out.println(" " + myList);
Page
Page

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

System.out.println(" " + myList.summaryInfo() + " ");

Page

More products