Click here to Skip to main content
15,895,557 members

Objct + class .. Question

Revision 3
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
Example:
colleges.txt:
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


college_lab/College_lab.java:
Java
package college_lab;
import java.io.FileNotFoundException;
import java.io.FileReader;
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.name = j.next();
            y.numTracks = j.nextInt();
            y.numStudents = j.nextInt();
            y.numFaculty = j.nextInt();
            y.numStaff = j.nextInt();

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

college_lab/College.java:
Java
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 !
thnx..:*
Posted 1-Oct-12 9:00am by JoOJ.K.
Tags: