1. Member
Join Date
Dec 2008
Location
North America
Posts
29
Rep Power
0

## BigElement-HELP

Assignment number 2:
Create a program called BigElement that will allow the user to input five integers. The program will then output the largest of the five numbers. Include a function-type method that will return to the main method the value of the largest element in the array (Hint: the flowchart below shows the algorithm that could be used for the method). Use the length method to help determine the number of elements in the array.

This is assignment 2 that I need to complete quickly and all I have is this:
// The "BiggestElement" class.
import java.awt.*;
import hsa.Console;

public class BiggestElement
{
static Console c; // The output console

public static void main (String[] args)
{
c = new Console ();
int biggest[] = new int [5];
//i know this part is wrong, im not sure what to do exactly
c.println ("Please input five integers: ");
biggest = c.readInt[] ();

} // main method
} // BiggestElement class

2. Member
Join Date
Mar 2009
Posts
6
Rep Power
0
if you have 5 elements, make a "for" loop, initialize a int variable max=0
in each time, you compare max with each element, if max is smaller, than assign max as that element,
hope it helps

3. Member
Join Date
Dec 2008
Location
North America
Posts
29
Rep Power
0
Sorry not quite sure I got it....would you happen to have msn so I can ask a few questions quicker? ...I aprreciate the help.
c = new Console ();
int biggest[] = new int [5];
c.println ("Please input five integers: ");
biggest = c.readInt ();
for (int max = 0 ; max < biggest ; max++)
{

}
} // main method

4. ## pseudo code

It would llook something like this:

Java Code:
```create array of 5 elements
start loop
ask user for int
end loop
call method (sortMethod?) with array as a parameter
print the int retunred from the method

begining of sort method
do sort stuff
return the biggest element (int)
end method```
Luck,
CJSL

5. Member
Join Date
Dec 2008
Location
North America
Posts
29
Rep Power
0
Ok so this is what I have now, im not sure if my loop in the sortMethod is correct, also i get an error when i try to print the method out from the main method

Java Code:
```public static void main (String[] args)
{
c = new Console ();
//create array of 5 elements
int fiveNumber[] = new int [5];
int max = 0;
//ask the user for five integers
c.println ("Please input five integers: ");
//start a loop so that the user can enter five integers
for (int i = 0 ; i < 5 ; i++)
{
fiveNumber [i] = c.readInt ();
}
c.println ("The largest number entered was " + sortMethod (max));
} // main method

public static int sortMethod (int fiveNumber, int max)
{
for (int i = 0 ; i < 5 ; i++)
{
if (fiveNumber > max)
max = fiveNumber;
}
return max;
}```

6. Member
Join Date
Dec 2008
Location
North America
Posts
29
Rep Power
0
nevermind i got the program to run!:
Java Code:
```c = new Console ();
//create array of 5 elements
int fiveNumber[] = new int [5];
//declare a variable to determine the largest number
int max = 0;
//ask the user for five integers
c.println ("Please input five integers: ");
//start a loop so that the user can enter five integers
for (int i = 0 ; i < 5 ; i++)
{
fiveNumber [i] = c.readInt ();
}
//print the largest number, that has been returned from the sortMethod method
c.println ("The largest number entered was " + sortMethod (fiveNumber, max));
} // main method

//create a new method called SorthMethod to determine which of the five numbers is the largest
public static int sortMethod (int[] fiveNumber, int max)
{
//use a loop to determine out of five numbers which is the largest
for (int i = 0 ; i < 5 ; i++)
{
if (fiveNumber [i] > max)
max = fiveNumber [i];
}
//return the max integer, which is now the largest integer in the given array
return max;
}```

7. Great... glad it works !!!
Java Code:
`public static int sortMethod (int[] fiveNumber, [B][COLOR="Red"]int max[/COLOR][/B])`
a comment... there's no need to pass variable max as a parameter (it's value is zero at that point). Just declare it and initialize it in the sort method.

Luck,
CJSL

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•