Monday, January 10, 2011

get Sum and Average program (Open Source)

Hi,
I've just finished writing this code, it's not a new idea but good and exciting one =P

here it is:


//@ Simple program to get the sum and average for a number of values grouped in an array <---
//@ Author: 'Liberty' <---
//@ Date: Monday, January 10 2011 12:40 AM <---
//@ ########################## <---


import javax.swing.JOptionPane;
public class getSumAndAverage {
public static void main (String args []) {

// declaring variables and arrays
int num, t, x=0, i=0, count=0;
Double [] nums={};
Double ave=0.0, sum=0.0;
String s, message;
String [] ss={};

JOptionPane pane=new JOptionPane();

while(x==0) {

s=pane.showInputDialog("Enter how many numbers you want to operate\n\n");
num=Integer.parseInt(s);
ss=new String[num];
nums=new Double[num]; // getting the amount of elements wanted to be operated on and setting num as the array elements number

do {

for (i=0; i<num; i++) {
count++;
ss[i]=pane.showInputDialog("Enter a number: \n\nsum= "+sum+"\naverage= "+ave);
nums[i]=Double.parseDouble(ss[i]);
sum+=nums[i];
ave=sum/count; // numbers are added one by one, sum and average are being evaluated
}

// final result after entering all the elements
message="sum and average of your numbers:";
pane.showMessageDialog(null,message+"\nSum : "+sum+"\nAverage : "+ave,"result",JOptionPane.INFORMATION_MESSAGE);

// giving the user options to perform other operations
if (i==num) {
t=pane.showConfirmDialog(null,"Do another opertion? (The same number of entries)\n\n'No' for different number of entries\n'Cancel' for exit","Another operation?",JOptionPane.INFORMATION_MESSAGE);
if (t==JOptionPane.YES_OPTION) {
i=0;
x=1;
sum=0.0;
ave=0.0;
count=0; // resetting variables to initial values
}

else if(t==JOptionPane.NO_OPTION) {
x=0;
i=0;
sum=0.0;
ave=0.0;
count=0;
}

else {
pane.showMessageDialog(null,"Thank You!");
System.exit(0);
}
}
} // closing bracket of do while
while (x==1);

} // end of while
} // end of main class
} // end of getSumAndAverage class




Amasi, I've written comments this time XD


people, wish me luck =P


peace & all good stuff =)


liberty~

Wednesday, January 5, 2011

the freaking exams

I hate when students get out of mind then. The 'Paper Hall' is a sad story :<