Hello,

I'm having little trouble solving this question correctly. first of all, I keep failing at testing most of these methods, and I don't know where did i go wrong for them not to work. thus, i'm not able to know if it's solved correctly or not.

Question is:

1.CountNumbers method: computes/returns how many numbers in a file
2.CountNumber method: counts the occurrence a number N is in the file.
3.CountOddNumbers method: to counts how many odd number in the file.
4.sum method: computes/returns the sum/total of numbers in a file
5.average method: computes/returns the average of numbers in a file.
6.sumAboveAverage: to sum the numbers that are greater than the average of all numbers.
7.Standard deviation method: computes/returns the standard deviation of numbers in a file. The standard deviation of N numbers in which their average is μ is calculated as follows:
http://img258.imageshack.us/img258/3231/17445470.png
8.indexOfSmallest: computes/returns the index of the smallest number in the file.
9.indexOfLargest: computes/returns the index of the largest number in the file.
10.copyFile method: to make a duplicate copy of a file (copy (srcFileName, dstFileName).

note: all the methods in the class will operate on a bunch of number (integers/grades) in a file.

and this is what i've done so far
(actually, i've done it all, just need some checking if my methods are logically correct or not).

``````import java.io.File;
import java.util.Scanner;
import java.io.PrintStream;

public class FileCalculator {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(new File("grades.in"));
int res = CountNumbers(sc);
int res1 = SumMethod(sc);
// int res2 = CountOddNumbers (sc);
double resultAverage = AverageMethod(sc);
System.out.println(res);
System.out.println(resultAverage);

}
/////////////////////////////////////////////////////////////

public static int CountNumbers(Scanner s) //1
{
int count = 0;
while (s.hasNextInt()) {
s.nextInt();
count++;
}
return count;
}
////////////////////////////////////////////////////////////

public static double countNumber(int[] grades, int numToFind) { //2
int occurence = 0;
while (s.hasNextInt()) {
for (int i = 0; i < grades.length; i++) {
occurence++;
}
}
}
return occurence;

}
////////////////////////////////////////////////////////////

public int CountOddNumbers(int[] grades, Scanner s, Scanner srcFileName) { //3
int oddCnt = 0;
while (s.hasNextInt()) {
for (int i = 0; i < grades.length; i++) {
if (grades[i] % 2 == 1) {
oddCnt++;
}

}
}
return oddCnt;
}
////////////////////////////////////////////////////////////

public static int SumMethod(Scanner s) //4
{
int sum = 0;
while (s.hasNextInt()) {
sum = sum + s.nextInt();
}
return sum;
}
////////////////////////////////////////////////////////////

public static double AverageMethod(Scanner s) //5
{
double Average = 0;
int count = 0;
int sum = 0;
while (s.hasNextInt()) {
sum = sum + s.nextInt();
count++;
}
Average = sum / count;
return Average;
}
////////////////////////////////////////////////////////////

public static double SumAverageMethod(Scanner s) { //6

double numGreater = 0;
while (s.hasNextInt()) {
if (x >= y) {
numGreater++;
}
}
return numGreater;

////////////////////////////////////////////////////////////    7
}

public static double f(double x, double n) {
return x * x - n;
}

public static double df(double x) {
return 2 * x;
}

public static double sqrt(double n) {
double x = 1;
for (int i = 0; i < 100; i++) {
x = x - f(x, n) / df(x);
}
return x;

}

public static double power(double base, int r) {
int R = r;
r = Math.abs(R);
double res = 1.0;
for (int i = 0; i < r; i++) {
res = res * base;
}
if (R < 0) {
return 1.0 / res;
} else {
return res;
}
}

public static double f2(double x, int r, double n) {
return power(x, r) - n;
}

public static double df2(double x, int r) {
return r * power(x, r - 1);
}

public static double root(double n, int r) {
double x = 1;
for (int i = 0; i < 500; i++) {
x = x - f2(x, r, n) / df2(x, r);
}
return x;
}

public static double standardDeviation(Scanner s) {
double a1 = 1 / a; //       1/n
double getMean = b / a;
double step1 = 0;
double step5 = 0;
while (s.hasNextInt()) {
a--;
if (a > 0) {
step1 = step1 + s.nextInt();// xi is the number s.nextInt() the first number
double step2 = step1 - c; // xi- averger
double step3 = sqrt(step2); //(step2)*2
double step4 = step3 * a1; // 1/n * (step2)
step5 = root(step4, 1);
step5 += step5;
return step5;
}
}
return step5;
}
//////////////////////////////////////////////////////////// /8

public static void smallestIndex(double[] grades, Scanner s) {

double smallestIndex = 0;
while (s.hasNextInt()) {
for (int j = 1; j < grades.length; j++) {
smallestIndex += j;
}
}
}
}
//////////////////////////////////////////////////////////// /9

public static void indexOfLargest(double[] grades, Scanner s) {
double largesttIndex = 0;
while (s.hasNextInt()) {
for (int j = 1; j < grades.length; j++) {
largesttIndex += j;
}

}
}
}
///////////////////////////////////////////////////////////// /10

public static void copyFile(Scanner srcFileName, String dstFileName) throws Exception {
PrintStream dst = new PrintStream(new File(dstFileName));
while (srcFileName.hasNextLine()) {
dst.println(srcFileName.nextLine());

}
}
}``````

You may have a problem because all these methods read the input file, so after the first method has read it all the subsequent methods will just see end-of-file.
May be better to have a method that reads the file just once into an array then pass that array to each of the methods?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.