# Simple Program

• 12-10-2008, 08:41 PM
TheRocket
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?
• 12-10-2008, 08:43 PM
neilcoffey
Well... which is the bit you're having trouble with...?
• 12-10-2008, 09:05 PM
TheRocket
The first 3 steps.

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;     } }```
• 12-10-2008, 09:56 PM
TheRocket
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!

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[0];       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);   } }```
• 12-10-2008, 10:03 PM
xcallmejudasx
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.
• 12-10-2008, 10:11 PM
TheRocket
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.
• 12-10-2008, 10:15 PM
xcallmejudasx
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

Code:

```public static void main(String[] args){     getInput();     findLargest();     findSmallest();     calculateAVG(); }```
• 12-10-2008, 10:22 PM
TheRocket
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.
• 12-10-2008, 10:34 PM
xcallmejudasx
Post the error messages and show us the code that you changed.
• 12-10-2008, 10:44 PM
TheRocket
Ok I tried moving it all but I couldn't separate the Inputting of numbers and get smallest. Could you help?

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];           }       }```
• 12-10-2008, 10:54 PM
xcallmejudasx
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.
• 12-10-2008, 11:20 PM
TheRocket
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:

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[0];       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);   } }```
• 12-11-2008, 08:43 PM
uncommon
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?

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[1];         for (int x : values) {             if (largest < x)                 largest = x;         }         return largest;     }     // Find the smallest     private static int findSmallest() {         int smallest = values[1];         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:"));         }     } }```
• 12-11-2008, 09:47 PM
xcallmejudasx
I'm just curious as to the extra semi-colons?
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?
• 12-11-2008, 10:40 PM
uncommon
No, just a typo that Eclipse didn't catch.
• 12-30-2008, 03:35 PM
sakthivpm
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