Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Homework
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:
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:
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
JoOJ.K258
Edited 1-Oct-12 9:17am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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){
      this.name = name; // "this.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.
  Permalink  
v2
Comments
stib_markc at 3-Oct-12 7:32am
   
nice!

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

  Print Answers RSS
0 OriginalGriff 315
1 Sergey Alexandrovich Kryukov 201
2 ChauhanAjay 156
3 PhilLenoir 154
4 ClimerChinna 140
0 Sergey Alexandrovich Kryukov 6,140
1 OriginalGriff 5,195
2 CPallini 2,473
3 Richard MacCutchan 1,607
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100