can someone explain this to me... coz i cant understand.. im trying to this one but i cant do it.

Implement and test this method:
static double max(double[] x) {
// returns the maximum of the elements in the array x

All 15 Replies

If you can use it, Arrays.sort and return the last element. Otherwise, save the first value and compare it to the others always saving the larger value and then return that.

What part of all that are you having problems with?

sorry man.. i really dont understand that one.. ill just read again my notes.. my teacher didnt discuss that part..-_-

i also dont understand this one..

Static Boolean hasDuplicates (int[] a) {
// returns true if the array a[] has any duplicate elements

Same concept, but just checking for equality.

Hi ronghel,

If I gave you a list of numbers and asked you which one was the biggest, how would you do that? (Not in code, just in life...)

For your second one, if I gave you a list of numbers and asked you if there were any duplicates in the list, how would you know?

For both just think about the steps you would take to solve each problem. Then worry about coding and testing after and it will be much easier :)

``````public class Problem8
{
public static void main(String[]args)
{
int a=0;

System.out.print(max(a));
System.out.println();

}

public static double max(double[]x)
{
int a,b,c,d=0;
double high=0;
double [] check = {1,2,3,4,5,6,7,8,9};
for(a=0;a<check.length;a++)
{
if(check[a]>high)
high=check[a];
}
return x;
}

}``````

this is the code but it doesnt work tsktsk.... can someone enlighten me.. hehe

>public static double max(double[]x)
This signature suggests that you're supposed to pass an array of doubles to the function and the function returns the largest of them. Your code doesn't do that at all. Use this as your driver:

``````public class Problem8
{
public static void main(String[] args)
{
double[] check = {1,2,3,4,5,6,7,8,9};

System.out.println(max(check));
}

public static double max(double[] x)
{
// Find the largest value in x and return it
}
}``````
commented: Your class name indicates you have tried 7 times before and failed :) +13
``````public class Problem7

{

public static void main(String[] args)

{

int [] check = {1,2,3,4,5,6,7,8,9,10};

System.out.println(max(check));

}

public static Boolean max(int[] c)
{
// returns true if the array a[] has any duplicate elements

int high=0, b=0;
for(int a=0;a<c.length;a++)
{
if(c[a]==c.length)
System.out.print(c[a]);
}
return true;

}
}``````

how about this one... i want to find the duplicated entry and return true.. but i think its not correct

You aren't doing any element comparisons at all. Think about what operation would be needed to see if any element in the array was equal to another element.

``````import java.util.*;

public class Problem7
{
public static void main(String[]args);
private static Random random = new Random();
{
// *WILL I PUT DECLERATION HERE??**
I DONT KNOW WHAT TO PUT HERE

}

public static boolean isSorted(int[] a)
{
// returns false unless a[0] <= a[1] <= a[2] <= a[2] <= . . .
for (int i = 1; 1<a.length; i++)
if (a[i]  <  a[i-1])
return false;
return true;
}

public static void printed(int[] a)
{
// prints the elements of the specified array in sequence.
if ( a == null | a.length == 0)
return;
System.out.print("{" + a[0]);
for (int i=1; i<a.length; i++)
System.out.print("," + a[i]);
System.out.print("}");

}

public static int[] randomIntarray( int length, int range)
{
// returns a new int array of the specified length whose elements
// are randomly distributed in the range 0 to range-1;
int[] a = new int[length];
for (int i=0; i<length; i++)
a[i] = random.nextInt(range);
return a;
}

public static int[] resized(int[] a, int length )
{
// returns a new array with the specified length
// containing the same elements as the specified array;
int[] aa = new int[length];
int n = Math.min(a.length, length);
System.arraycopy(a, 0, aa, 0, n);
return aa;
}

public static void swap(int[] a, int i, int j)
{
// interchanges elements a[i] and a[j];
int ai=a[i], aj=a[j];
a[i] = aj;
a[j] = ai;
}

Static Boolean hasDuplicates (int[] a)
{
// returns true if the array a[] has any duplicate elements
}
}``````

can someone try to explain this one...? coz i got always error when i try to run it..

This is probably only the first thing (I haven't looked very close), but, from the following two lines, remove the semi-colon from the first, and move the second to a line before the first.

``````public static void main(String[]args);
private static Random random = new Random();``````

Edit: As far as what to put in there, think about it. What does the program need to do, then insert those steps.

ok i i done what you've said... but when i tried to run it shows error "Excpetion in thread "main" java.lang.Arrayindexoutofboundexception: 9"

error in array i guess...

yup. You're trying to access an element either before the beginning or after the end of the array.

You do realise that arrays in Java are 0-based?

what do you mean sir???
so i will not declare the array??

ill just declare the size of array???

He means an array size of 10 elements is indexed from 0 to 9. The exception you're receiving basically means you're trying to access an index outside of that 0-9 range, or whatever the range is of your array.

still i cant manage to run this one...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.