Click here to Skip to main content
15,940,173 members
Please Sign up or sign in to vote.
2.20/5 (5 votes)
See more:
1. Write a class College with the following fields:
a. College Name
b. Number of tracks
c. Number of Students
d. Number of Faculty
e. Number of Staff.

2. Write a Java program:
a. Create an array of College objects.

b. Read the values of the fields of each College object from a text file
IT 7 200 50 20
ENG 4 400 60 20
MIST 3 300 40 20

c. Add the created objects to the array and display their values to the screen

d. Change the value of ‘Number of tracks’ in the ENG College to 6

e. Find the average number of students for all the colleges

package college_lab;
import java.util.Scanner;

public class College_lab {

    public static void main(String[] args) throws FileNotFoundException {

        College[] array = new College[10]; // declare array of colleges

        Scanner j = new Scanner (new FileReader("colleges.txt"));

        int counter =0;

        while (j.hasNext())
            College y = new College(); 
            y.numTracks = j.nextInt();
            y.numStudents = j.nextInt();
            y.numFaculty = j.nextInt();
            y.numStaff = j.nextInt();

            array [counter] = y ;  
            System.out.println( y.numTracks );
            System.out.println( y.numStudents );
            System.out.println( y.numFaculty );
            System.out.println( y.numStaff );

package college_lab;

public class College {
    public String name;
    public int numTracks;
    public int numStudents;
    public int numFaculty;
    public int numStaff;

.. I don't know how to change the value from txt file ! "d" and how to get the Average ! is there any mistake in my code !
Updated 1-Oct-12 9:17am

1 solution

You need to complete your Object "College".

You need:
- a constructor
- getter and setter for the values
- some additional functions for average etc.

As this is a homework task, I'll give you a jump start but not the complete class.
You will have to complete it to make it fully functional:
package college_lab;
public class College {
  // make the class members private to protect them otherwise any 
  // other object can access them directly and harm the functionality of your code.
    private String name;
    private int numTracks;
    private int numStudents;
    private int numFaculty;
    private int numStaff;

// 0-arg constructor
/* this one is a basic. One would not have to decale it to access it - it's there by
 *default. But we want to make sure the member variables of our class are working 
 * when we init an empty College 
    public College(){
      name = "unknown"; // could also use empty String ""
      numTracks = 0;
      numStudents = 0;
      numFaculty = 0;
      numStaff = 0;
  /* so there is not much going on. But you are reading the data from a txt file, 
   * so we could also init the College direclty with data. Therefor we give the 
   * object the data as arguments and fill these into the member variables.
   * pretty simple. 
    public College(String name, int numTracks, int numStudents, int numFaculty, int numStaff){ = name; // "" is the member variable, "name" the argument. 2 different things!
      this.numTracks = numTracks;
      this.numStudents = numStudents;
      this.numFaculty = numFaculty;
      this.numStaff = numStaff;
  // additional question: what would you do if only a few arguments are given?

  /* now we also need setters and getters. 
   * I will give you one set and you should add the rest.
   * You can also use the setters to init the value in the constructor 
   * (by using there e.g. "setTracks(numTracks);" 
  public void setTracks(int numTracks){
    this.numTracks = numTracks; 
    /* one could validate at this point if the given number is valid. 
     * A value of -1 for example is not valid but e.g. often used as a return 
     * value if a calculation fails */  
  public int getTracks(){
    return numTracks;

in your class College_lab you will need the method to calculate the averageStudents:

private static int averageStudents(College[] colleges){ // static only because handled inside main function
  // sum all students from all colleges and divide by number of colleges
  return -1; // ...or valid value when calculated

Have fun. And come back and ask if you need more help.
Share this answer
stib_markc 3-Oct-12 7:32am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900