> Can anyone help me write this program, i dont know how i should go
> about this:
[quoted text clipped - 8 lines]
> which receives an integer array array1 as parameter and returns an
> duplicate integer array. (4) Write a main program to test it.
Well, you are going to need to start by writing a class to contain all
this. After that, I would do it in order (4), (2), (3), (1), testing (2)
and (3) with internally supplied data before trying to do the input
task. Start with a skeleton main method, then add a test, then add the
code to make that test work. Repeat as needed.
I find the description of (1) a bit confusing. It seems to be intended
to read in an array, but is defined as taking an array reference
parameter, rather than returning an array as result. Can you clarify? If
you don't know what it means, it might be something you should check
with your instructor.
In addition to the methods you have been told to write, it might be a
good idea to have a method that prints the contents of an array, so that
you can see what you are working with.
Patricia
Rahsaan Page - 17 Oct 2006 00:00 GMT
Yes, the info was a is a bit confusing. so i guess ill ask my
instructor.
Thanks for the tip, Patricia :)
> > Can anyone help me write this program, i dont know how i should go
> > about this:
[quoted text clipped - 26 lines]
>
> Patricia
Rahsaan Page - 17 Oct 2006 00:29 GMT
//Author: Rahsaan Page
//Date:10/16/06
//Lab #5 10/16/2006 Array Practice
//(1)Write a method, getValue(int [ ] array) to have user enter the
size (length)
//of an integer array, then user is able to continuously enter the
integer values.
//(2)Write a method int getHighestValue() and int getLowestValue()
which returns the
//highest value and lowest in the array respectively. (3) Write a
method
//int[ ] geArray( int[ ] array1) which receives an integer array array1
as
//parameter and returns an duplicate integer array.
//(4) Write a main program to test it.
import javax.swing.JOptionPane;
import java.util.*;
public class Array {
public static int[] getValue() {
String input,v;
int array_size = 0;
int [] array;
int n = 0;
input = JOptionPane.showInputDialog("Enter the size of array:
");
array_size = Integer.parseInt(input);
array = new int[array_size];
while(array_size>0){
v = JOptionPane.showInputDialog("Enter an integer> ");
array[n++] = Integer.parseInt(v);
array_size--;
}
return array;
}
public static int getHighest(int[] array) {
Arrays.sort(array);
return array[array.length-1];
}
public static int getLowestValue(int [] array) {
Arrays.sort(array);
for(int n=0;n<array.length;n++) {
System.out.println(array[n]);
}
return array[0];
}
public static void main(String[] args) {
int array[] = getValue();
JOptionPane.showMessageDialog(null,"Highest: " +
getHighest(array));
JOptionPane.showMessageDialog(null,"Lowest: " +
getLowestValue(array));
}
}
> > Can anyone help me write this program, i dont know how i should go
> > about this:
[quoted text clipped - 26 lines]
>
> Patricia
Patricia Shanahan - 17 Oct 2006 04:06 GMT
...
> public static int getHighest(int[] array) {
> Arrays.sort(array);
> return array[array.length-1];
> }
...
This will find the maximum in an array, but it has a couple of problems:
1. It is destructive. The order of the items in the array is part of its
value, and your getHighest changes that order.
2. It is unnecessarily slow.
As a hint, there is a way of finding the highest element in an array in
a single scan, without changing any element of the array.
Patricia
Rahsaan Page - 17 Oct 2006 15:17 GMT
how can i find the highest element in an array in a single scan,
without changing any element of the array, i would love to know?
> ...
> > public static int getHighest(int[] array) {
[quoted text clipped - 14 lines]
>
> Patricia
solaimadhan@gmail.com - 17 Oct 2006 15:26 GMT
Store the first element (array[0]) in a variable 'highestValue'. Then
go over each element (array[i]) in the array and compare with
'highestValue', if the element (array[i]) is greater than
'highestValue', then store this element (array[i]) in 'highestValue'
and continue till the end of the array
At the end of the scan, 'highestValue' will have the highest value :)
hope this helps
Maddy
> how can i find the highest element in an array in a single scan,
> without changing any element of the array, i would love to know?
[quoted text clipped - 16 lines]
> >
> > Patricia