Results 1 to 3 of 3
  1. #1
    carlyspade is offline Member
    Join Date
    Jun 2013
    Rep Power

    Angry MyProgrammingLab Questions

    I only have two problems from the MyProgrammingLab that I cannot get...I've tried different things 5 times and just can't get it.

    The first is:

    Suppose* you have two arrays* of ints*, arr1 and arr2, each containing ints* that are sorted in ascending order.

    Write a static* method* named* merge that receives these two arrays* as parameters* and returns a reference* to a new, sorted array* of ints* that is the result of merging the contents of the two arrays*, arr1 and arr2.

    This was my answer:
    Java Code:
    public static int[] merge(int[] arr1, int[] arr2) {
         int[] merged = new int[arr1.length + arr2.length];
         for (int k = 0; k < arr1.length; k++)
             merged[k] = arr1[k];
         for (int j = 0; j < arr2.length; j++)
                    merged[arr1.length j] = arr2[j];
    And why it said it was wrong: error: ']' expected
    We think you might want to consider using: if
    We think you might want to consider using: else
    We think you might want to consider using: while

    The 2nd one is:
    Assume* the existence of a Widget class* that implements the Comparable interface and thus has a compareTo method* that accepts an Object* parameter* and returns an int*. Write an efficient static* method*, getWidgetMatch, that has two parameters*. The first parameter* is a reference* to a Widget object*. The second parameter* is a potentially very large array* of Widget objects* that has been sorted in ascending order based on the Widget compareTo method*. The getWidgetMatch searches for an element* in the array* that matches the first parameter* on the basis of the equals* method* and returns true* if found and false* otherwise.

    My answer:
    Java Code:
    public static boolean getWidgetMatch(Widget a, Widget[] b){
     int lo=0;
     int hi=b.length-1;
     int m;
     int r;
     while (lo!=hi){
     if (r==0) return true;
     if (r>0) hi=m;
     else lo=m;
     return return a.equals(b[m]);
    Why they said it was wrong: error: illegal start of expression
    **⇒*****You almost certainly should be using: +
    *****⇒*****We think you might want to consider using: <
    *****⇒*****Solutions like yours don't usually use: !=

    Thank you!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: MyProgrammingLab Questions

    First, your syntax looks off since it appears you are missing some closing braces. Better formatting will help us help you.

    Second, it's time to play computer. Grab some paper and pencil and write down two sorted lists and a third one. The third one's size is the combined size of the other two. Assign a starting index to each one and start interating thru each sorted list and compare the current elements. Put the smaller one the third list and increment its pointer. Then continue incrementing, comparing, and merging, adjusting the various indices until you are done.

    See if that helps you visualize the problem.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    slider57 is offline Member
    Join Date
    Aug 2011
    Rep Power

    Default Re: MyProgrammingLab Questions

    On your first issue I think you are missing an addition sign on line 8.
    merged[arr1.length j] = arr2[j]; -- instead should look like...
    merged[arr1.length + j] = arr2[j]; -- the compiler wanted you to close the bracket because it didnt understand why j was there.

    Also... for your second issue look at line 13 in your code.
    Last edited by slider57; 07-25-2013 at 08:24 PM.

Similar Threads

  1. Two questions!
    By eranmc in forum New To Java
    Replies: 2
    Last Post: 11-09-2012, 04:35 PM
  2. questions
    By amaliutz in forum New To Java
    Replies: 13
    Last Post: 02-02-2011, 07:13 PM
  3. questions regarding XML
    By Sparky in forum New To Java
    Replies: 4
    Last Post: 02-01-2011, 08:10 PM
  4. I have 2 questions =P
    By santa in forum New To Java
    Replies: 4
    Last Post: 01-19-2011, 06:35 PM
  5. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 08:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts