Collections Homework Answers


  1.  



    numElements = 4
    ok = true
    str = null
  2.  



    numElements = 4
    ok = true
    str = null
  3.    public boolean replace(T oldob, T newob) {
          boolean found = false;
          int i;
          for (i = 0 ; i < numElements ; i++)
             if (elements[i].equals(oldob)) {
                elements[i] = newob;
                found = true;
             }
          return found;
       }
    
  4.    public boolean replace(T oldob, T newob) {
          boolean found = false;
          LLNode<T> curr = head;
          for (i = 0 ; i < numElements ; i++)
             if (curr.getInfo().equals(oldob)) {
                curr.setInfo(newob);
                found = true;
             }
          return found;
       }
    
  5.    Scanner keybd = new Scanner(System.in);
       String str, oldstr, newstr;
       boolean repped;
       ArrayCollection<String> strcoll = new ArrayCollection<String>();
       System.out.print("Enter a string: ");
       str = keybd.nextLine();
       while (!str.equals("000") {
          strcoll.add(str);
          System.out.print("Enter a string: ");
          str = keybd.nextLine();
       }
       System.out.print("Enter the old string: ");
       oldstr = keybd.nextLine();
       System.out.print("Enter the new string: ");
       newstr = keybd.nextLine();
       repped = strcoll.replace(oldstr, newstr);
       if (repped)
          System.out.println(oldstr + " was replaced");
       else
          System.out.println(oldstr + " was not found");
       System.out.println("There are " + strcoll.size() + " elements");
    


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2017