Results 1 to 10 of 10
  1. #1
    cmps280CD is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default Need hmwk help with Arrays in jgrasp

    heres what I have so far, am i on the right track, here are my instructions and my code so far. Please help

    Make a class called HW1.java. In this class, write the following:
    A method called doubleArray that has an int array as an input parameter and returns an int array in which each element has been doubled. In the main method, call this doubleArray method with input {2,5,8}. Print out the values of the array returned by the method. (This should be 4, 10, 16.)
    A method called max that has an int array as an input parameter and returns an int representing the largest value in the array. Test this method from the main method for the values {20,5,15,40}. Print the result.


    public class HW1Makeup{

    public static void main(String[] args){
    int[] a = {2,5,8};
    int[] b = {4,10,16};
    print(b);

    }

    public static int print(int [] b){
    for (int i = 0; i < a.length; i++){
    System.out.println(b);



    }
    }
    }

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You don't declare the array b as you're doing but instead have b be the result of the doubleArray Method. e.g.,
    Java Code:
    int[] b = doubleArray(a);
    What you should be doing first and foremost is trying to create the method doubleArray.

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    It's also worth noting that this method is invalid:
    Java Code:
    public static int print(int [] b){
    for (int i = 0; i < a.length; i++){
       System.out.println(b);
    }
    a is never declared as a variable in this scope. Furthermore, your loop should not be printing b (as you will just get some weird output like [I@19c26f5) but instead print out the elements of b.

  4. #4
    cmps280CD is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    This is wat I have now but its not compiling


    public class HW1Makeup{

    public static void doubleArray(int[]){
    int[] a = {2,5,8};
    int[] b = doubleArray(a);
    print(b);




    }
    }

  5. #5
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    First of all you should give the variable in the function argument a name and not only a type.

    Also this: int[] b = doubleArray(a); cannot work, because your function does not return an int array.

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You really need to study your notes and read the chapter in your text on how to create methods before trying to create code as your method is totally borked. Guessing never works when programming.

    Look at examples of how the method looks, the return type (as noted above) the parameters (int[] wont work), and then try to build your method piece by piece using the knowledge you've gained. Also compile frequently and don't add any new code until your compile errors are fixed.

  7. #7
    cmps280CD is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    ok now I have this code:


    public class HW1Makeup{

    public static void main(String[] args){
    double[] mylist = {2,5,8};






    }
    }

    I need to double the elements of this array..and print out the values returned by the array...is this good so far, it compiles ok, im using the book now

  8. #8
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    Then define the signature of your doubleArray function next(public static...).

  9. #9
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Onra View Post
    Then define the signature of your doubleArray function next(public static...).
    public static? It looks like you're placing that in the method parameter list

    But by all means now try to do your method, but again a little bit at a time. I'll give you the signature....

    Java Code:
    public static int[] doubleArray(int[] originalArray) {
       // your code goes here.
    }

  10. #10
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    Ok, now think of an algorithm that takes your original array and writes all values*2 into a new array.

Similar Threads

  1. CMPS280 hmwk help please
    By cmps280CD in forum New To Java
    Replies: 3
    Last Post: 09-11-2010, 01:51 AM
  2. jGRASP debugger
    By TsAmE in forum New To Java
    Replies: 2
    Last Post: 02-23-2010, 10:35 PM
  3. Replies: 0
    Last Post: 11-24-2009, 05:49 PM
  4. JGrasp simple exercise?
    By Boomer1 in forum Other IDEs
    Replies: 2
    Last Post: 11-02-2009, 05:23 AM
  5. GUI with jGRASP
    By Matt Sakko in forum Other IDEs
    Replies: 0
    Last Post: 03-14-2009, 11:50 AM

Posting Permissions

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