1. Member Join Date
Nov 2008
Posts
20
Rep Power
0

## Simple Program

What I am trying to code is a simple program to do the following:

1) Ask the user to input a number, lets call this (x)
2) Ask the user to input (x) numbers
3) Store these numbers in an array.
4) Find the largest number in the array and display it.
5) Find the smallest number in the array and display it.
6) Find average of the numbers in the array and and display it.

This also needs to be split up 4 different methods, one get the numbers, the second to find the largest, the third to find the smallest and the fifth to calculate the average.

Can anyone help me?  Reply With Quote

2. Senior Member Join Date
Nov 2008
Posts
286
Rep Power
12

## Well... which is the bit you're having trouble with...?  Reply With Quote

3. Member Join Date
Nov 2008
Posts
20
Rep Power
0

## The first 3 steps.

Java Code:
```import javax.swing.*;
public class Arrays
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog(
"Enter a number");
int n = Integer.parseInt(input);
int[] marks = new int [n];
getMarks(n);
}

public static int getMarks(int n)
{
while(n > 0)
{
String inputMark = JOptionPane.showInputDialog(
"Enter a number");
int marks = Integer.parseInt(inputMark);
n--;
}
return n;
}
}```  Reply With Quote

4. Member Join Date
Nov 2008
Posts
20
Rep Power
0

## Here is what I have so far, I need to split into the 4 methods though and I just can't seem to be able to do it! :( Help!

Java Code:
```import javax.swing.*;
public class Array
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog(
"Enter a number");
int n = Integer.parseInt(input);
int[] num = new int[n];
int temp = 0;
int temp1 = Integer.MAX_VALUE;

for(; temp < num.length; temp++)
{

num[temp] = Integer.parseInt
(JOptionPane.showInputDialog("Enter number:" ));

//Find Smallest
if (temp1 > num[temp])
temp1=num[temp];
}

//Find Largest
int maximum = num;
int index = 0;
for (int i=1; i<num.length; i++)
{
if (num[i] > maximum)
{
maximum = num[i];
index = i;
}
}

//Find Average
double result=0.0;
int j=0;
for(j=0; j < num.length; j++)
{
result=result + num[j];
}

//Display All 3
JOptionPane.showMessageDialog(null,
"The number with the smallest value is " + temp1
+ "\nThe number with the largest value is " + maximum
+ "\nThe average of the numbers is " + result/num.length);
}
}```  Reply With Quote

5. ## you already have it seperated within your main which makes things easier for you.

create 3 other methods called public int findLargest(), public int findAverage(), public void display()

then just take the code you have for each of those and toss it into the method.

Your find largest should return maximum and find average should return result.  Reply With Quote

6. Member Join Date
Nov 2008
Posts
20
Rep Power
0

## It needs to be split up 4 different methods, one get the numbers, the second to find the largest, the third to find the smallest and the fourth to calculate the average.

The main would then be used to call on these methods and output the display at the end of the main, is this possible? I may of explained it wrong.  Reply With Quote

7. ## oh ok i assumed 3 + the main. You know where different pieces of code perform a certain function(you comment out what the piece of code does, which is good) now you just need to move those chunks of code into their own method.
your main will end up being

Java Code:
```public static void main(String[] args){
getInput();
findLargest();
findSmallest();
calculateAVG();
}```  Reply With Quote

8. Member Join Date
Nov 2008
Posts
20
Rep Power
0

## That makes perfect sense but when I just tried, it came up with 36 errors. Could you just do it on the code put up so I could see how it is done for future reference. Thanks a lot.  Reply With Quote

9. ## Post the error messages and show us the code that you changed.  Reply With Quote

10. Member Join Date
Nov 2008
Posts
20
Rep Power
0

## Ok I tried moving it all but I couldn't separate the Inputting of numbers and get smallest. Could you help?

Java Code:
```       //Get Marks + Find Smallest
for(; temp < num.length; temp++)
{
num[temp] = Integer.parseInt
(JOptionPane.showInputDialog("Enter number:" ));

if (temp1 > num[temp])
{
temp1=num[temp];
}
}```  Reply With Quote

11. ## take your if statement out and have that loop only add numbers to your array. then in your findSmallest() method you send in the array and loop through it comparing the values.  Reply With Quote

12. Member Join Date
Nov 2008
Posts
20
Rep Power
0

## Ok I did that and then got this error:

'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Array.main(Array.java:19)'

Code Below:

Java Code:
```import javax.swing.*;
public class Array
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog("Enter a number");
int n = Integer.parseInt(input);
int[] num = new int[n];
int temp = 0;
int temp1 = Integer.MAX_VALUE;

//Get Marks + Find Smallest
for(; temp < num.length; temp++)
{
num[temp] = Integer.parseInt
(JOptionPane.showInputDialog("Enter number:" ));
}

if (temp1 > num[temp])
{
temp1=num[temp];
temp++;
}

//Find Largest
int maximum = num;
int index = 0;
for (int i=1; i<num.length; i++)
{
if (num[i] > maximum)
{
maximum = num[i];
index = i;
}
}

//Find Average
double result=0.0;
int j=0;
for(j=0; j < num.length; j++)
{
result=result + num[j];
}

//Display All 3
JOptionPane.showMessageDialog(null,
"The number with the smallest value is " + temp1
+ "\nThe number with the largest value is " + maximum
+ "\nThe average of the numbers is " + result/num.length);
}
}```  Reply With Quote

13. Member Join Date
Dec 2008
Posts
30
Rep Power
0

## Solution

I probably just did your school work for you, but I had fun.

It feels like there is a simpler solution to this problem. Anyone else?

Java Code:
```import javax.swing.JOptionPane;

public class main {

/**
* @param args
*/
static int   numberOf;
static int[] values;

public static void main(String[] args) {

getInput();
int smallest = findSmallest();
int largest = findLargest();
;
float average = findAverage();
;

// Display All 3
JOptionPane.showMessageDialog(null,
"The number with the smallest value is " + smallest
+ "\nThe number with the largest value is " + largest
+ "\nThe average of the numbers is " + average);

}

// Find the Largest
private static int findLargest() {
int largest = values;
for (int x : values) {
if (largest < x)
largest = x;
}
return largest;
}

// Find the smallest
private static int findSmallest() {
int smallest = values;
for (int x : values) {
if (smallest > x)
smallest = x;
}
return smallest;
}

// Find the Average
private static float findAverage() {
float sum = 0;
for (int x : values) {
sum += x;
}
return (float) (sum / values.length);
}

// Retrieve the input and store in an array
private static void getInput() {
String input = JOptionPane.showInputDialog("Enter a number");
numberOf = Integer.parseInt(input);
values = new int[numberOf];
int small = Integer.MAX_VALUE;

for (int i = 0; i < values.length; i++) {
values[i] = Integer.parseInt(JOptionPane
.showInputDialog("Enter number:"));
}
}

}```  Reply With Quote

14. ## I'm just curious as to the extra semi-colons?
Java Code:
```        int largest = findLargest();
;
float average = findAverage();
;```
I don't see why it would throw an error but I don't see the point to it either. Is there something I'm missing here?  Reply With Quote

15. Member Join Date
Dec 2008
Posts
30
Rep Power
0

## No, just a typo that Eclipse didn't catch.  Reply With Quote

16. Member Join Date
Dec 2008
Posts
1
Rep Power
0

## Java Using MySql Back UP

Hi,
I am new to form.I would like to evey one Happy New Year.
:)

How to take up the MySql Database backup using Java or Jsp or Struts Application.Please If possible could you provide coding with Example

Thanks and Regards
Sakthi  Reply With Quote

#### Posting Permissions

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