#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Smile Homework Help THX


    Hi,
    i am new here, and would be very grateful for some help in Java !
    I think it is a simple problem, but i've been looking around a bit, andstill have problems with it :
    it's a short program with arrays, and we have to find the different values of the variables once the program has finished running, here it is:

    public class d{
    public static int func(int[] a, int z){
    int tmp = a[0];
    a[0] = a[2];
    a[2] = tmp;
    tmp = z;
    z = a[1];
    a[1] = tmp;
    return z;
    }
    public static void main(String[] argv){
    int[] a = {4,95,2};
    int[] a1 = {5,34,90};
    int a2 = 8;
    int[] b1 = {8,5,432};
    func(a, a[1]);
    a2 = func(a1,func(b1, a2));
    b1[2] = a2;
    int c = func(b1, 19);
    }
    }

    and here are the solutions for the different variables:
    a: 2 95 4
    a1: 90 5 5
    a2: 34
    b1: 34 19 432
    c: 8

    i'm just not sure how to solve this,
    your help will be much apreciated,
    thanks in advance !!!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    my problem is actually with the functions:

    func(a, a[1]);
    a2 = func(a1,func(b1, a2));
    c = func(b1, 19);

    i'm not sure how they work ...
    thx in advance
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Here you go: http://www.lmgtfy.com/?q=java+method+tutorial

    Read up about methods and if you have more specific questions I'd be more than happy to answer them.

    Also, please surround the code you post with [code] tags.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    Thanks for your answer, I will look at these links,
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    hi,
    i've looked at some sites,
    and i think i understand the program a bit better,
    it passes the arrays as parameters through the functions,
    however i am still having problems,
    with the fisrt part of the program that swaps the different values in the array around ...
    could you maybe give me an example of how the get the value of one of the variables ?
    thanks
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Nice job on following through and learning Your right, this program does pass around an Array. It passes another value as well.

    Your code as it currently stands is hard to read. The reason for that is because the variable names that were chosen for a lot of the variables, methods and classes aren't meaningful. When naming variables its very important to name them something that describes what they are.

    e.g. If we are computing the sum of two numbers, then I would write the program as follows.
    Code:
    int sum = 0, num1 = 6, num2 = 8;
    sum = num1 + num2;
    instead of
    int a = 0, b = 0, c = 0;
    a = b + 1;
    It's pretty obvious which program is easier to read.
    Your question is a little vague. Can you elaborate a bit on what you are asking? Maybe break it into a several questions.

IMN logo majestic logo threadwatch logo seochat tools logo