Posted: Mon Jul 17, 2006 4:52 pm Post subject: Another Program Problem
Hi my goal is:
Create a program that will read a file called "stds.txt". (The file stds.txt is in the view/complete button below). The data is in the format name-mark-mark-mark-mark. Download the file so that you can test your program. The last record has an entry xxx 0 0 0 0. With the data on each student, output the students name, their four marks, and the average of their four marks
Posted: Mon Jul 17, 2006 4:53 pm Post subject: (No subject)
The problem is that i was wodnering if there's a way to choose the next line.
Like the data from the next line.
I have smith's line, but I want to go to the next one. Thanks.
~Caceres
wtd
Posted: Mon Jul 17, 2006 5:19 pm Post subject: (No subject)
Are you required to use classes that are not in the standard Java library?
Caceres
Posted: Mon Jul 17, 2006 5:24 pm Post subject: (No subject)
wtd wrote:
Are you required to use classes that are not in the standard Java library?
I'm not sure what that question means exaclt.y But we're only allows to use
import TerminalIO.KeyboardReader;
import becker.io.*;
import java.io.*;
The smith lines works perfecltly, with the marks and everything. But then i want to read the next line from the file, and I can't seem to get it. :S
It stops after smith and says "next line is not available" or something like that.
rizzix
Posted: Mon Jul 17, 2006 5:28 pm Post subject: (No subject)
Here's an example using java 1.5's Scanner class..
Posted: Mon Jul 17, 2006 5:43 pm Post subject: (No subject)
I would suggest reading all of the information into a Hashtable, then printing it out. It will make for a more interesting exercise.
r.3volved
Posted: Mon Jul 17, 2006 5:48 pm Post subject: (No subject)
Well your first instict should be to create a class for students.
code:
public class Student
{
private String name_;
private int mark1_;
private int mark2_;
private int mark3_;
private int mark4_;
public Student() {}
//MUTATORS
public void setMark1(int myMark) {mark1_ = myMark;}
public void setMark2(int myMark) {mark2_ = myMark;}
public void setMark3(int myMark) {mark3_ = myMark;}
public void setMark4(int myMark) {mark4_ = myMark;}
//ACCESSORS
public int getMark1() {return mark1_;}
public int getMark2() {retrun mark2_;}
public int getMark3() {return mark3_;}
public int getMark4() {return mark4_;}
It doesn't look like you've been taught arrays yet so I've made it simple for you to follow for now. Using an integer array to hold grades would make this class a little more efficient, but don't worry about it for now (unless you know of arrays...in which case, you should be using them).
So to make a new object of Student and add data it's simply:
code:
Student myStudent = new Student();
myStudent.setMark1(some_number);
To get data from a particular student object then we use:
code:
myStudent.getMark1(); //returns mark1 from myStudent object
myStudent.getAverage();
wtd
Posted: Mon Jul 17, 2006 6:44 pm Post subject: (No subject)
r.3volved wrote:
It doesn't look like you've been taught arrays yet so I've made it simple for you to follow for now.
No time like the present to learn.
Also, you should probably be using an ArrayList or such. Then you have something that can be of variable length.
Oh, and the casting in your getAverage method is incorrectly positioned.
code:
class Student
{
private String name;
private ArrayList grades;
public Student(String name) {
this.name = name;
this.grades = new ArrayList();
}
public void addGrade(int newGrade) {
grades.add(newGrade);
}
public ArrayList getGrades() {
return grades;
}
public int numberOfGrades() {
return grades.size();
}
public double averageGrade() {
int sum = 0;
Iterator iter = grades.iterator();
while (iter.hasNext()) sum += iter.next();
return (double)sum / numberOfGrades();
}
}
Sponsor Sponsor
Caceres
Posted: Tue Jul 18, 2006 9:51 am Post subject: (No subject)
Thanks to everyone who helped me.
I made up my own way. The easy way lol.
I forgot how to do loops and stuff. So it's kind of long.
public class ass43 { //file name
public static void main(String[] args) {
File f1 = new File("stds.txt"); //variable for the file being used
String smith,jones,ricci,lombardi,santos; //variables for the names of the students
double s_1,s_2,s_3,s_4; //variables for the marks of Smith
double j_1,j_2,j_3,j_4; //variables for the marks of Jones
double r_1,r_2,r_3,r_4; //variables for the marks of Ricco
double l_1,l_2,l_3,l_4; //variables for the marks of Lombardi
double sa_1,sa_2,sa_3,sa_4; //variables for the marks of Santos
double average1,average2,average3,average4,average5; //variables for the averages of each student
if(f1.exists()) { //statements that will occur if the file exists
TextInput in = new TextInput("stds.txt"); //variable for the text that will be inputted
while(!in.eofIsAvailable()) {
smith = in.readWord();
System.out.print(smith+"\t\t");
s_1 = in.readDouble();
System.out.print(s_1+"\t");
s_2 = in.readDouble();
System.out.print(s_2+"\t");
s_3 = in.readDouble();
System.out.print(s_3+"\t");
s_4 = in.readDouble();
System.out.println(s_4+"\t");
/*This bunching of statements obtains the data
from the file for smith. It reads the first line
and places them with their appropriate variables.*/
in.readLine();
jones = in.readWord();
System.out.print(jones+"\t\t");
j_1 = in.readDouble();
System.out.print(j_1+"\t");
j_2 = in.readDouble();
System.out.print(j_2+"\t");
j_3 = in.readDouble();
System.out.print(j_3+"\t");
j_4 = in.readDouble();
System.out.println(j_4+"\t");
/*This bunching of statements obtains the data
from the file for Jones. It reads the second line
and places them with their appropriate variables.*/
in.readLine();
ricci = in.readWord();
System.out.print(ricci+"\t\t");
r_1 = in.readDouble();
System.out.print(r_1+"\t");
r_2 = in.readDouble();
System.out.print(r_2+"\t");
r_3 = in.readDouble();
System.out.print(r_3+"\t");
r_4 = in.readDouble();
System.out.println(r_4+"\t");
/*This bunching of statements obtains the data
from the file for Ricci. It reads the third line
and places them with their appropriate variables.*/
in.readLine();
lombardi = in.readWord();
System.out.print(lombardi+"\t");
l_1 = in.readDouble();
System.out.print(l_1+"\t");
l_2 = in.readDouble();
System.out.print(l_2+"\t");
l_3 = in.readDouble();
System.out.print(l_3+"\t");
l_4 = in.readDouble();
System.out.println(l_4+"\t");
/*This bunching of statements obtains the data
from the file for Lombardi. It reads the fourth line
and places them with their appropriate variables.*/
in.readLine();
santos = in.readWord();
System.out.print(santos+"\t\t");
sa_1 = in.readDouble();
System.out.print(sa_1+"\t");
sa_2 = in.readDouble();
System.out.print(sa_2+"\t");
sa_3 = in.readDouble();
System.out.print(sa_3+"\t");
sa_4 = in.readDouble();
System.out.println(sa_4+"\t");
/*This bunching of statements obtains the data
from the file for Santos. It reads the fifth line
and places them with their appropriate variables.*/
average1=(s_1+s_2+s_3+s_4)/4;
System.out.println("\nThe average of Smith's marks is "+average1+".");
average2=(j_1+j_2+j_3+j_4)/4;
System.out.println("\nThe average of Jones' marks is "+average2+".");
average3=(r_1+r_2+r_3+r_4)/4;
System.out.println("\nThe average of Ricci's marks is "+average3+".");
average4=(l_1+l_2+l_3+l_4)/4;
System.out.println("\nThe average of Lombardi's marks is "+average4+".");
average5=(sa_1+sa_2+sa_3+sa_4)/4;
System.out.println("\nThe average of Santos' marks is "+average5+".\n\n");
/*The above statements produce the average for each of the students. it takes
all four of their marks, adds them all up and then divides by the number of
marks(which is four). It also outputs the averages, so that the user will see
the info.*/
return;
}
in.close(); //closes the file that has been opened
}
else
System.out.print("I could not open the file stds.\n");
/*If the file cannot be opened, then this is the statement that is return.
And the program won't run.*/
}
}
r.3volved
Posted: Tue Jul 18, 2006 11:10 am Post subject: (No subject)
mmmmm...
forgot how to do loops??
You're gonna make every programmer on this board cry with that attitude
You obviously have access to the internet, or you wouldn't be posting here...ummm here's a helpful site if you "forget" concepts or have any question about anything -> http://www.google.ca
Java has great documentation...chances are you will end up with a horrible mark on that project if you've forgotten loops
If you have no drive to figure out what you don't already know, or to recap the things you've forgotten, then maybe you've chosen the wrong field to pursue
OneOffDriveByPoster
Posted: Tue Jul 18, 2006 8:38 pm Post subject: (No subject)
@Caceres: you really should use a loop--who says that you will have the same number of students in each file?
Focus on the loop; you do not need arrays here. Simplicity is key.
r.3volved
Posted: Tue Jul 18, 2006 8:47 pm Post subject: (No subject)
for sure you should be using arrays here
...who says there's the same amount of grades for each student??
OneOffDriveByPoster
Posted: Tue Jul 18, 2006 9:04 pm Post subject: (No subject)
r.3volved wrote:
for sure you should be using arrays here
...who says there's the same amount of grades for each student??
Why would I need an array even if there is a variable number of marks per student?
r.3volved
Posted: Tue Jul 18, 2006 10:33 pm Post subject: (No subject)
so you can iterate through the array in a for loop from 0 to the amount of values it contains and then divide by the array elements +1 for average
The point is, you don't want ANY hard coded values in your code
OneOffDriveByPoster
Posted: Tue Jul 18, 2006 10:46 pm Post subject: (No subject)
r.3volved wrote:
so you can iterate through the array in a for loop from 0 to the amount of values it contains and then divide by the array elements +1 for average
The point is, you don't want ANY hard coded values in your code
...and the initial size of your array (which I still do not see) is not a hard coded value?