Hello,
I was having some problems with my Java code. The program writen below is for a class. The question asks:
In this exercise, you are asked to rewrite the Programming Example Classify Numbers...rewrite the program to incorporate the following requirements:
a. Data to the program is input from a file of unspecified length
b. Save the output of the program to a file
c. Write the method getNumber so that it reads a number from the input file (opened in the method main), outputs the number to the output file (opened in the method main), and sends the number read to the method main. Print only 10 numbers per line.
d. Have the program find the sum and average of the numbers
e. Write the method printResults so that it outputs the final results to the output file (opened in the method main). Other than outputting the appropriate counts, the definition of the method printResults should also output the sum and the average of the numbers
The program that you are asked to rewrite pretty much take in numbers from the keyboard (20 at a time) and then says how many of the numbers are even, odd, or zero.
For this program to work, I wrote my own class IntClass so the variables would pass from method to method, but I'm still getting errors, please help! Thanks!
Code for IntClass:
public class IntClass{
private int x;
public IntClass(){ x= 0;}
public IntClass(int num) { x = num;}
public void setNum(int num){ x = num;}
public int getNum() { return x;}
public void addToNum(int num) {x = x +num;}
public void multiplyToNum(int num) { x = x *num;}
public int compareTo(int num){ return (x-num);}
public boolean equals (int num) { if (x == num)
return true;
else return false;}
public String toString(){ return (String.valueOf(x));}
}
My current code:
import java.util.*;
import java.io.*;
public class Evensoddszeros{
public static void main (String [] args)throws IOException{
int evenscount =0;
int oddscount=0;
int zeroscount =0;
int count =0;
IntClass number = new IntClass (0);
IntClass zeros = new IntClass (0);
IntClass odds = new IntClass (0);
IntClass evens = new IntClass (0);
int sum=0;
int average =0;
Scanner inFile = new Scanner( new FileReader ("F:\\Ch7_Ex11Data.txt"));
PrintWriter outFile = new PrintWriter("F:\\classifynumbersoutput.txt");
while(inFile.hasNext()){
getNumber(number, inFile, outFile);
classifyNumber(number.getNum(), zeros, odds, evens);
count ++;
if (count % 10 == 0)
outFile.println();
sum = sum + number.getNum();
average = sum/count;
outFile.close();
inFile.close();
}
}
public static void getNumber(IntClass number, Scanner inFile, PrintWriter outFile ){
number.setNum(inFile.nextInt());
outFile.print(number.getNum() + " ");
}
public static void printResult (IntClass Number, IntClass evens, IntClass odds, IntClass zeros, int sum, int average){
outFile.println("There are " + evens + " evens, " + " there are " + odds + " odds, " + "and there are " + zeros + " zeros");
outFile.println("The sum of the numbers is: " + sum);
outFile.println("The average of the numbers is: " + average);
}
public static void classifyNumber(int number, IntClass zeroscount, IntClass oddscount, IntClass evenscount){
switch (number %2){
case 0: IntClass evens.addToNum(1);
if ( number == 0);
IntClass zeros.addToNum(1);
break;
case 1:
case -1: IntClass odds.addToNum(1);
}
}
}