Results 1 to 16 of 16

Thread: Simple Program

  1. #1
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default 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?

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Well... which is the bit you're having trouble with...?

  3. #3
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

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

  4. #4
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

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

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  6. #6
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    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.

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

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

  8. #8
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    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.

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Post the error messages and show us the code that you changed.

  10. #10
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    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];
               }
           }

  11. #11
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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. #12
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

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

  13. #13
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default 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:"));
            }
        }
    
    }

  14. #14
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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?

  15. #15
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    No, just a typo that Eclipse didn't catch.

  16. #16
    sakthivpm is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default 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

Similar Threads

  1. Need Help With VERY Simple Program -- Newbie!!!
    By somethingfast in forum New To Java
    Replies: 17
    Last Post: 11-25-2008, 07:38 AM
  2. Stuck - simple program
    By dirtycash in forum New To Java
    Replies: 4
    Last Post: 11-24-2008, 07:44 PM
  3. Peculiarty in code of simple program...
    By Kreuz14 in forum New To Java
    Replies: 4
    Last Post: 01-23-2008, 03:27 AM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 AM

Posting Permissions

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