Description
Files to submit to Web-CAT (both files must be submitted together):
• Octahedron.java
• OctahedronApp.java
Specifications
Overview: You will write a program this week that is composed of two classes: (1) one named Octahedron that defines Octahedron objects, and (2) the other, OctahedronApp, which has a main method that reads in data, creates an Octahedron object, and then prints the object.
An Octahedron is a is a polyhedron with eight faces, twelve edges, and six vertices. The term is most commonly used to refer to the regular octahedron, a Platonic solid composed of eight equilateral triangles, four of which meet at each vertex. The formulas are provided to assist you in computing return values for the respective methods in the Octahedron class described in this project. Source: Wikipedia and Merriam-Webster.
Formulas for surface area (A) and volume (V) are shown below where 𝑎 is the length of an edge.
𝐴 (
𝑉
• Octahedron.java
Requirements: Create an Octahedron class that stores the label, color, and edge length, which must be non-negative. The Octahedron class also includes methods to set and get each of these fields, as well as methods to calculate the surface area, volume, and surface to volume ratio of the Octahedron object, and a method to provide a String value of an Octahedron object (i.e., a class instance).
Design: The Octahedron class has fields, a constructor, and methods as outlined below.
(1) Fields (instance variables): label of type String, color of type String, and edge of type double. Initialize the Strings to “” and the double to zero in their respective declarations. These instance variables should be private so that they are not directly accessible from outside of the Octahedron class, and these should be the only instance variables in the class.
(2) Constructor: Your Octahedron class must contain a public constructor that accepts three parameters (see types of above) representing the label, color, and edge. 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 Octahedron 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.
Octahedron ex1 = new Octahedron (“Ex 1”, “orange”, 5.2);
Octahedron ex2 = new Octahedron (” Ex 2 “, “blue”, 20.4);
Octahedron ex3 = new Octahedron (“Ex 3”, “orange and blue”, 104.5);
(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 Octahedron, 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 getColor: Accepts no parameters and returns a String representing the color field.
o setColor: Takes a String parameter and returns a boolean. If the string parameter is not null, then the “trimmed” String is set to the color field and the method returns true. Otherwise, the method returns false and the label is not set.
o getEdge: Accepts no parameters and returns a double representing the edge field.
o setEdge: Accepts a double parameter and returns a boolean as follows. If the edge is non-negative, sets the edge field to the double passed in and returns true. Otherwise, the method returns false and the edge is not set.
o surfaceArea: Accepts no parameters and returns the double value for the surface area calculated using the value for edge.
o volume: Accepts no parameters and returns the double value for the volume calculated using the value for edge.
o surfaceToVolumeRatio: Accepts no parameters and returns the double value calculated by dividing the surface area by the volume.
o toString: Returns a String containing the information about the Octahedron object formatted as shown below, including decimal formatting (“#,##0.0###”) for the double values. The newline ( ) and tab ( ) escape sequences should be used to achieve the proper layout for the indented lines (use rather than three spaces for the indentation). 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(), volume(), and surfaceToVolumeRatio(). 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).
Octahedron “Ex 1” is “orange” with 12 edges of length 5.2 units.
surface area = 93.6693 square units volume = 66.2832 cubic units surface/volume ratio = 1.4132
Octahedron “Ex 2” is “blue” with 12 edges of length 20.4 units.
surface area = 1,441.6205 square units volume = 4,002.066 cubic units surface/volume ratio = 0.3602
Octahedron “Ex 3” is “orange and blue” with 12 edges of length 104.5 units. surface area = 37,828.8557 square units volume = 537,950.8703 cubic units surface/volume ratio = 0.0703
• OctahedronApp.java
Requirements: Create an OctahedronApp class with a main method that reads in values for label, color, and edge. After the values have been read in, main creates an Octahedron object and then prints a new line and the object.
Design: The main method should prompt the user to enter the label, color, and edge.
After a value is read in for edge, if the value is less than zero, an appropriate message
(see examples below) should be printed followed by a return from main. Assuming that edge is non-negative, an Octahedron object should be created and printed.
Below is an example where the user has entered a negative value for edge followed by an example using the values from the first example above for label, color, and edge. Your program input/output should be exactly as follows.
Line # Program input/output
1
2
3
4
5 Enter label, color, and edge length for an octahedron. label: Negative Edge Value color: red edge: -10.5
Error: edge must be non-negative.
Line # Program input/output
1
2
3
4
5
6
7
8
9 Enter label, color, and edge length for an octahedron.
label: Ex 1 color: orange edge: 5.2
Octahedron “Ex 1” is “orange” with 12 edges of length 5.2 units.
surface area = 93.6693 square units volume = 66.2832 cubic units surface/volume ratio = 1.4132
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, even when it appears to be numeric value. 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, assuming s1 represents a numeric value. For the printed lines requesting input for label, color, and edge, use a tab ” ” rather than three spaces. After reading in the values, create the new Octahedron, say octa, then print
it: System.out.println(” ” + octa);
General Notes
1. All input from the keyboard and all output to the screen should done in the main method. Only one Scanner object on System.in should be created and this should be done in the main method. All printing (i.e., using the System.out.print and System.out.println methods) should be in the main method. Hence, none of your methods in the Octahedron class should do any input/output (I/O).
2. When a method has a return value, you can ignore the return value if it is no interest in the current context. For example, when setEdge(3.5) is invoked, it returns true to let the caller know the edge field was set; whereas setEdge(-3.5) will return false since the edge field was not set. So, if the caller knows that x is positive, then the return value of setEdge(x) can safely be ignored since it can be assumed to be true.
Reviews
There are no reviews yet.