Google Answers Logo
View Question
 
Q: Java code ( No Answer,   1 Comment )
Question  
Subject: Java code
Category: Computers > Programming
Asked by: tobymac8-ga
List Price: $2.00
Posted: 02 Nov 2005 18:35 PST
Expires: 03 Nov 2005 12:17 PST
Question ID: 588235
This program generates an ArrayList having 100 elements. All but one
reference String objects with the value "XXX". One references "Waldo".
Your job is to fill-in the blanks to find the index of "Waldo",
replace that element with a reference to "XXX", and prove that "Waldo"
no longer exists. Do NOT modify any existing code.

import java.util.*;
public class Lab23C {
  public static void main(String[] args) {
    ArrayList names = new ArrayList();
    for (int i = 0, n = (int)(Math.random()*100); i < 100; i++) {
      if (i == n) {
        names.add("Waldo");
      }
      else {
        names.add("XXX");
      }
    }
    int index = _________________;    // Find the index of "Waldo"
    System.out.println("\"Waldo\" at index: " + index);
    ____________________;             // Replace the element with a
reference to "XXX"
    if (!_______________________) {   // Prove that "Waldo" no longer exists
      System.out.println("Waldo is gone");
    }
  }
} 

      When successful, the program should display output similar to
the following (the index will vary):

 

     "Waldo" at index: 43
  Waldo is gone
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java code
From: karuneeru-ga on 03 Nov 2005 11:42 PST
 
Here is the code:

import java.util.*;
public class Lab23C {
  public static void main(String[] args) {
    ArrayList names = new ArrayList();
    for (int i = 0, n = (int)(Math.random()*100); i < 100; i++) {
      if (i == n) {
        names.add("Waldo");
      }
      else {
        names.add("XXX");
      }
    }
    int index = names.indexOf("Waldo");    // Find the index of "Waldo"
    System.out.println("\"Waldo\" at index: " + index);

    names.set(index, "XXX");             // Replace the element with a
reference to "XXX"

    if (!names.contains("Waldo")) {   // Prove that "Waldo" no longer exists
      System.out.println("Waldo is gone");
    }
  }
}

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy