Help writing this program!!!!

R

Rahsaan Page

Can anyone help me write this program, i dont know how i should go
about this:

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.
 
P

Patricia Shanahan

Rahsaan said:
Can anyone help me write this program, i dont know how i should go
about this:

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.

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
 
R

Rahsaan Page

Yes, the info was a is a bit confusing. so i guess ill ask my
instructor.
Thanks for the tip, Patricia :)
Patricia said:
Rahsaan said:
Can anyone help me write this program, i dont know how i should go
about this:

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.

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
 
R

Rahsaan Page

//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));
}


}
Patricia said:
Rahsaan said:
Can anyone help me write this program, i dont know how i should go
about this:

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.

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
 
P

Patricia Shanahan

Rahsaan Page wrote:
....
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
 
R

Rahsaan Page

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?
Patricia said:
Rahsaan Page wrote:
...
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
 
S

solaimadhan

Store the first element (array[0]) in a variable 'highestValue'. Then
go over each element (array) in the array and compare with
'highestValue', if the element (array) is greater than
'highestValue', then store this element (array) 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

Rahsaan said:
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?
Patricia said:
Rahsaan Page wrote:
...
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top