Seperating Code into differnet methods
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?
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);
}
}