What does the following code do? The first code listing contains the declaration of class Shape. Each Shape object represents a closed shape with a number of sides (a closed shape must have three or more sides). The second code listing contains a method (mystery) declared by a client of class Shape. What does this method do?
```
1 public class Shape
2 {
3 // integer for storing number of sides
4 private int sides;
5
6 // Shape constructor, number of sides supplied
7 public Shape( int numSides )
8 {
9 setSides( numSides );
10
11 } // end constructor
12
13 // return sides value
14 public int getSides()
15 {
16 return sides;
17
18 } // end method getSides
19
20 // set sides value
21 public void setSides( int numSides )
22 {
23 if ( numSides > 2 )
24 {
25 sides = numSides;
26 }
27 else // set invalid input to 0
28 {
29 sides = 0;
30 }
31
32 } // end method setSides
33
34 } // end class Shape
```
```
1 public String mystery( Shape shape)
2 {
3 String shapeText;
4
5 // switch with number of shape’s sides
6 switch ( shape.getSides() )
7 {
8 case 3:
9 shapeText = "Triangle";
10 break;
11
12 case 4:
1 public String mystery( Shape shape)
2 {
3 String shapeText;
4
5 // switch with number of shape’s sides
6 switch ( shape.getSides() )
7 {
8 case 3:
9 shapeText = "Triangle";
10 break;
11
12 case 4:
13 shapeText = "Quadrilateral";
14 break;
15
16 default:
17 shapeText = "Other polygon";
18
19 } // end switch
20
21 return shapeText;
22
23 } // end method mystery
```
Method mystery returns a String containing text indicating what type of shape the Shape object passed to it is. Method mystery will determine whether the Shape object is a triangle, quadrilateral or other polygon by the number of sides the Shape object has.
You might also like to view...
Maps allocate keys to values and cannot contain duplicate keys, i.e., the key-to-value mapping is a __________ mapping.
a. many-to-many. b. many-to-one. c. one-to-many. d. one-to-one.
The ____ component can display the progress of loading content, such as a photo file, into the UILoader instance.
A. LoadGraph B. ProgressGraph C. ProgressBar D. LoadBar
Every problem has an algorithmic solution.
Answer the following statement true (T) or false (F)
A(n) ____________________ object is an object that can be positioned at a specific location in a document or in a layer over or behind text in a document.
Fill in the blank(s) with the appropriate word(s).