rearranging the code to make it modular
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
///////////////////
// SixthJava.java
//
// javac SixthJava.java
//
// produces SixthJava.class
//
// To execute
// ----------
// java SixthJava < list of arguments>
//
//
//
//
//
class SumComputer {
private double[] _c;
//
// SumComputer a = new SumComputer (arr);
//
public SumComputer (double [] c) {
_c = c;
}
//
// double x = a.SUM();
public double SUM()
{
double sum = 0;
int n = _c.length;
int k = 0;
for(k = 0;k<n;++k) {
sum = sum + _c[k];
}
return sum;
}
}
class StringArraytoDoubleArray {
private double[] _temp;
public StringArraytoDoubleArray (String [] tmp) {
int n = tmp.length;
_temp = new double[n];
for(int i = 0;i < n;i++) {
_temp[i] = Double.parseDouble(tmp[i]);
}
}
public double[] get() {
return _temp;
}
}
public class SixthJava
{
public static void main(String[] args)
{
//
// retreive the number of arguments
//int n = args.length;
//System.out.println(n);
//
// declare a double array (vector)
//double [] arr = new double[n];
//
// converting strings into double array
//for(int i = 0;i < n;i++) {
// arr[i] = Double.parseDouble(args[i]);
//}
StringArraytoDoubleArray s= new StringArraytoDoubleArray(args);
double[] arr = s.get ();
//
// computing the sum of the contents of double array
//double sum = 0;
//int k = 0;
//while (k < n) {
// sum = sum + arr[k];
// k = k+1;
//}
SumComputer a = new SumComputer (arr);
double x = a.SUM ();
//
// printing the sum
System.out.println(x);
}
}
No comments:
Post a Comment