• October 29th, 2011, 05:21 PM
tripline
a. Calculate the largest of 2, 3, or 4 integer values.

int val(int a, int b, int c ,int d)

if (a > b && b > c && b > d)
{return a + b;}

else if (a > c && c > b && b > d)
{return a + c;}

Is it asking for something similar to this? What do they mean by calculate the largest?
• October 29th, 2011, 06:24 PM
Herah
what you have would be appropriate for 4 value inputs, but sometimes you'll only have 2 or 3, according to the problem. Overload the method name by making the same method again, but only giving it values a & b, then again with values a, b & c. Obviously, rewrite the body to reflect the number of inputs. Then, the correct method will be called depending on how many values are passed in.

correct your body for the 4 inputs first, right now it's not going to work. something like:

Code Java:

```public int vals(int a, int b, int c, int d){ int highest = a; if(b>highest) highest = b; if(c>highest) highest = c; if(d>highest) highest = d; return highest; }```
• October 29th, 2011, 06:48 PM
tripline
public class ttt
{
public int vals(int a, int b, int c, int d)
{
a=3; b=2; c=10; d=4;
int highest = a;
if(b>highest)
highest = b; System.out.println(highest);
if(c>highest)
highest = c; System.out.println(highest);
if(d>highest)
highest = d;
System.out.println(highest);
return highest;
}
}

There are no errors but nothing shows up. Can you tell me why?
• October 29th, 2011, 07:36 PM
Herah
What you have listed above is a fragment, not a program, so it wont run. If you want to test it you could do something like this:
Code Java:

```  public class ttt {   /** * @param args */ public static void main(String[] args) {   vals(3, 2, 10, 4); // calls vals method using 4 arguments } public static int vals(int a, int b, int c, int d){   int highest = a; if(b>highest) highest = b; System.out.println(highest); if(c>highest) highest = c; System.out.println(highest); if(d>highest) highest = d; System.out.println(highest); return highest; } }```

your print statements are just giving you an idea of what's happening to your highest value, so are good for debugging. You don't need them for the actual program, of couse.
• October 29th, 2011, 08:58 PM
tripline