I find myself in desperate straits with this program I am trying to write. I'm going to first describe what I'm trying to accomplish, then post my two classes, then the file used in conjunction. I know it sounds like a lot, but I think I have almost all the code done, I just can't put it all together.
Okay, My program should read a file containing a name and three test grades for several people, then calculate the average score and letter grade for each person, print all this to another file at one time(everything is supposed to be stored in an array), open this file for reading, and print the file to the screen as it reads.
I guess I might need a method to write al the array elements to the text file one line at a time, and some sort of loop in main, but I am just so so befuddled and hoping for some expertise. Thank you so much in advance.
//BELOW IS SERVICE CLASS, FOLLOWED BY DRIVER CLASS, FOLLOWED BY GIVEN TEXT FILE:
import java.io.*;
import java.util.*;
public class Grades
{
String name;
int test1;
int test2;
int test3;
double average;
String grade;
public Grades()
{
name="";
test1=0;
test2=0;
test3=0;
average=0.0;
grade="";
}
public Grades(String n,int t1,int t2,int t3,double avg,String letter)
{
name=n;
test1=t1;
test2=t2;
test3=t3;
average=avg;
grade=letter;
}
public void setName(String newName)
{
name=newName;
}
public String getName()
{
return name;
}
public void setTest1(int newTest1)
{
test1=newTest1;
}
public int getTest1()
{
return test1;
}
public void setTest2(int newTest2)
{
test2=newTest2;
}
public int getTest2()
{
return test2;
}
public void setTest3(int newTest3)
{
test3=newTest3;
}
public int getTest3()
{
return test3;
}
public void setAverage(double newAverage)
{
average=newAverage;
}
public double getAverage()
{
return average;
}
public void setGrade(String newGrade)
{
grade=newGrade;
}
public String getGrade()
{
return grade;
}
public double averageGrades()
{
double average=(test1+test2+test3)/3;
return average;
}
public String grades()
{
if(average >=90)
grade="A";
else if(average >=80)
grade="B";
else if(average >=70)
grade="C";
else
grade="F";
return grade;
}
public boolean readFromFile(BufferedReader inputFile)throws IOException
{
String str;
name=inputFile.readLine();
if (name!=null){
str=inputFile.readLine();
test1=Integer.parseInt(str);
str=inputFile.readLine();
test2=Integer.parseInt(str);
str=inputFile.readLine();
test3=Integer.parseInt(str);
return true;
}
return false;
}
public void print()
{
System.out.println(name);
System.out.println(test1);
System.out.println(test2);
System.out.println(test3);
}
}
*****************************************
import java.io.*;
import java.util.*;
public class GradesDriver
{
public static String getFileName()
{
Scanner scan=new Scanner(System.in);
String myFile;
System.out.print("Enter the file name please: ");
myFile=scan.nextLine();
return myFile;
}
public static void main(String[] args)throws IOException
{
Grades [] g = new Grades[10];
Grades temporary = new Grades();
String myFile=getFileName();
FileReader freader=new FileReader(myFile);
BufferedReader inputFile=new BufferedReader(freader);
FileWriter fwriter=new FileWriter(myFile);
PrintWriter outputFile=new PrintWriter(fwriter);
}
}
***********************************************
Ted Sexton
67
78
50
Dori Andrew
78
83
80
Maggie Smith
90
94
89
Robert Reed
70
76
80

New Topic/Question
Reply




MultiQuote




|