7.7 Arrays of Objects, 7.8 The Sequential Search Algorithm

¡Supera tus tareas y exámenes ahora con Quizwiz!

An array of Strings , names , has been declared and initialized . Write the statements needed to determine whether any of the array elements are null or refer to the empty String . Set the variable hasEmpty to TRUE if any elements are null or empty-- otherwise set it to FALSE .

hasEmpty = false; for (int i = 0; i < names.length; i++) if (names[i] == null || names[i].length() == 0) hasEmpty = true;

search algorithm

is a method of locating a specific item in a larger collection of data. This section discusses the sequential search algorithm, which is a simple technique for searching the contents of an array.

Which search algorithm steps through an array, comparing each item with the search value?

sequential search

Is -1 a valid subscript?

no

Recall that we designed a Rectangle class in Chapter 6. Fill in the missing code in the /* */ comments to: (i) declare a Rectangle array named rectArray with five elements (ii) instantiate each element with a Rectangle object (iii) initialize each object with values for the length and width fields with the Rectangle constructor public class RectSpecs { public static void main(String[] args) { /* Declare a Rectangle array named rectArray with five elements here. */ for (/* Fill in the loop statement to iterate through rectArray here. */) { // Initialize each new rectangle with the values i and i+1 // for the length and width arguments. /* Fill in the initialization statement here. */ } } } /** Rectangle class, phase 5 */ class Rectangle { private double length; private double width; /** Constructor @param len The length of the rectangle. @param w The width of the rectangle. */ public Rectangle(double len, double w) { length = len; width = w; } /** The setLength method stores a value in the length field. @param len The value to store in length. */ public void setLength(double len) { length = len; } /** The setWidth method stores a value in the width field. @param w The value to store in width. */ public void setWidth(double w) { width = w; } /** The getLength method returns a Rectangle object's length. @return The value in the length field. */ public double getLength() { return length; } /** The getWidth method returns a Rectangle object's width. @return The value in the width field. */ public double getWidth() { return width; } /** The getArea method returns a Rectangle object's area. @return The product of length times width. */ public double getArea() { return length * width; } }

public class RectSpecs { public static void main(String[] args) { Rectangle[] rectArray = new Rectangle[5]; for (int i = 0; i < rectArray.length; i++) { rectArray[i] = new Rectangle(i, i+1); } } } /** Rectangle class, phase 5 */ class Rectangle { private double length; private double width; /** Constructor @param len The length of the rectangle. @param w The width of the rectangle. */ public Rectangle(double len, double w) { length = len; width = w; } /** The setLength method stores a value in the length field. @param len The value to store in length. */ public void setLength(double len) { length = len; } /** The setWidth method stores a value in the width field. @param w The value to store in width. */ public void setWidth(double w) { width = w; } /** The getLength method returns a Rectangle object's length. @return The value in the length field. */ public double getLength() { return length; } /** The getWidth method returns a Rectangle object's width. @return The value in the width field. */ public double getWidth() { return width; } /** The getArea method returns a Rectangle object's area. @return The product of length times width. */ public double getArea() { return length * width; } }


Conjuntos de estudio relacionados

EVOLVE REACH (HESI) A2 PRACTICE TEST - MODULE 4

View Set

Domain 1: Security and Risk Management: Business Continuity

View Set

Lewis Med-Surg Ch. 13 Altered Immune Responses and Transplantation

View Set

Chapter 14- Chemical Equilibrium

View Set

Ionic bonding and covalent bonding

View Set

Stutzman AP Euro Midterm (all review materials from folder)

View Set