Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Norm

Thread: Breaking up code into methods. Can't figure out how to code

  1. #1
    Log1c is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Breaking up code into methods. Can't figure out how to code

    Hello All,

    I am trying to complete a homework assignment that has me stumped. The only thing stopping me from getting 100% is that I have to separate the input and sum of the numbers into their own respective methods. Other then that the code works and meets all other requirements. Does anyone have any idea how to separate "//Main Method (Input / Sum)" into their own respective methods? I have to be able to input a series of numbers, calc the sum, avg and display the results using all different methods. I cant figure out how to input the numbers, count the number of inputs and tally the sum if I can only return one object? or variable within a method. Its a beginners class and just covered methods this week.

    Java Code:
    // File: XXXXXXXXX.java
    // Author: XXXXXXXXX
    // Date: 4/11/2012
    // Purpose: Project 2
    
    package XXXXXXXXX;
    import javax.swing.JOptionPane;
    public class XXXXXXXX {  
       
    //Avg Method    
    public static double avg(double n1, double n2) {
        double total = n1 / n2;
        return total;
    }    
    
    //Display Method
    public static void display(double n1, double n2, int n3){
        
        double sum = 0;
        double avg = 0;
        int count = 0;
       
        sum = n1;
        avg = n2;
        count = n3;
        
        if (sum >= 100)
        JOptionPane.showMessageDialog (null,"Your sum is over 100!!!"
                + "\nSum: " + sum + " Avg: " + avg + " Inputs: " + count);
        else 
            JOptionPane.showMessageDialog (null,"Sum: " + sum + " Avg: " + avg + " Inputs: " + count);
    }
    
    //Main Method (Input / Sum)
    public static void main(String[] args) {
    String input = JOptionPane.showInputDialog("Enter a value, the program will exit when the word stop is entered: ");
    
    double sum = 0;
    int count = 0;
    
    while (!input.equals("stop")) {
        double data = 0;
        data = Double.parseDouble(input);
        sum = sum + data;  
        input = JOptionPane.showInputDialog("Enter a value, the program will exit when the word stop is entered: ");
        count++;
    }
    
    //Call Statement
    display(sum,avg(sum,count),count);     
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Breaking up code into methods. Can't figure out how to code

    An array or a class object(say an arrayist) would be the "one" thing a method could return.
    In one method read the data into one of those and return it. Then pass it to the second method to process its contents.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Breaking up code into methods. Can't figure out how to code

    Some guidelines for methods are that methods should do one specific thing with a specific set of data which you provide to it as arguments. Method names should reflect what they do, and should generally be verbs or verb-object phrases: computeSum, displayResults, destroyUniverse...
    Get in the habit of using standard Java naming conventions!

  4. #4
    Log1c is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Breaking up code into methods. Can't figure out how to code

    Thx for the tips Norm and kjkrum. I started to code with an array but got stuck again, I couldn't figure out one i passed the array to //computeSum how to return the sum of the array. Ill try again tomorrow and post what I have done. This is beyond aggravating.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Breaking up code into methods. Can't figure out how to code

    Nothing advanced about the topic. Moving to New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Breaking up code into methods. Can't figure out how to code

    how to return the sum of the array.
    return sum;
    DarrylBurke likes this.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Is it possible to see code of java methods.
    By efozdel in forum New To Java
    Replies: 10
    Last Post: 08-25-2011, 07:46 AM
  2. Replies: 5
    Last Post: 06-05-2011, 02:19 AM
  3. Replies: 7
    Last Post: 11-04-2010, 04:10 PM
  4. Replies: 3
    Last Post: 01-11-2010, 06:48 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
  •