Write a code fragment that determines and prints the number of times the character 'z' appears in a String object called name.

What will be an ideal response?


```
int count = 0;
for (int index = 0; index < name.length(); index++)
if (name.charAt(index) == 'z')
count++;
System.out.println("The character \'z\' appears "
+ count + " time(s)");
```

Computer Science & Information Technology

You might also like to view...

A block:

a. Must contain exactly three statements. b. Cannot contain declarations. c. Is a compound statement. d. Is represented by placing a semicolon (;) where a statement would normally be.

Computer Science & Information Technology

A(n) __________ statement provides a default section to validate a user's menu selection.

a. If-Then b. If-Then-Else c. While d. Case

Computer Science & Information Technology

The means by which IP traffic is related to specific SAs is the _________ .

A) TRS B) SPD C) SAD D) SPI

Computer Science & Information Technology

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 ```

Computer Science & Information Technology