Results 1 to 16 of 16
Thread: Simple Program
 12102008, 08:41 PM #1Member
 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?
 12102008, 08:43 PM #2Senior Member
 Join Date
 Nov 2008
 Posts
 286
 Rep Power
 10
Well... which is the bit you're having trouble with...?
Neil Coffey
Javamex  Java tutorials and performance info
 12102008, 09:05 PM #3Member
 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; } }
 12102008, 09:56 PM #4Member
 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[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); } }
 12102008, 10:03 PM #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.
 12102008, 10:11 PM #6Member
 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.
 12102008, 10:15 PM #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(); }
 12102008, 10:22 PM #8Member
 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.
 12102008, 10:34 PM #9
Post the error messages and show us the code that you changed.
 12102008, 10:44 PM #10Member
 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]; } }
 12102008, 10:54 PM #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.
 12102008, 11:20 PM #12Member
 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[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); } }
 12112008, 08:43 PM #13Member
 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[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:")); } } }
 12112008, 09:47 PM #14
I'm just curious as to the extra semicolons?
Java Code:int largest = findLargest(); ; float average = findAverage(); ;
 12112008, 10:40 PM #15Member
 Join Date
 Dec 2008
 Posts
 30
 Rep Power
 0
No, just a typo that Eclipse didn't catch.
 12302008, 03:35 PM #16Member
 Join Date
 Dec 2008
 Posts
 1
 Rep Power
 0
Similar Threads

Need Help With VERY Simple Program  Newbie!!!
By somethingfast in forum New To JavaReplies: 17Last Post: 11252008, 08:38 AM 
Stuck  simple program
By dirtycash in forum New To JavaReplies: 4Last Post: 11242008, 08:44 PM 
Peculiarty in code of simple program...
By Kreuz14 in forum New To JavaReplies: 4Last Post: 01232008, 04:27 AM 
help with simple program in java
By katie in forum New To JavaReplies: 2Last Post: 08062007, 09:03 PM 
help with simple java program
By leonard in forum New To JavaReplies: 3Last Post: 07302007, 10:40 AM
Bookmarks