A program is to be devised for a university to work out whether or not, a student or a professor, is
outstanding. The criterion for a professor to be outstanding is to have over 120 publications. The
criterion for a student to be outstanding is to have a Grade Point Average of 3.3.
For a professor, the three class data members would be name, number of publications and the
method is Outstanding().
For a student, the three class data members would be name, grade point average and the method is
Outstanding().
In order to test the program, use a text file which holds 100 records such as:
P Brian Keen 79
S Tom Kelly 3.7
P Jim Zub 95
P Tim Norman 130
S Barbara Maddil 2.2
In the above list, ‘P’ stands for a professor and ‘S’ stands for a student. As examples, according to
the given criteria, Tim Norman is the outstanding professor and Tom Kelly is the outstanding
student.
Design, implement and test the necessary classes to implement the given scenario.
I' ve done this code:
Expand|Select|Wrap|Line Numbers
- public abstract class SchoolMember {
- //These are the attributes for a SchoolMember class
- /**
- * This is the type of member
- * either a professor or a student.
- */
- String typeOfMember;
- /**
- * This is the name of the school member
- * i.e. the name of the professor or the
- * student. It is of type string.
- */
- String name;
- /**
- * This is the surname of the school member
- * i.e. the surname of the professor or the
- * student. It is of type string.
- */
- String surname;
- /**
- * This is the score of the SchoolMember.
- * In the case of a student it is the average
- * while in the case of a professor it is the
- * number of publications.
- */
- String score;
- public abstract boolean outstanding(String s);
- }
Expand|Select|Wrap|Line Numbers
- public class Professor extends SchoolMember {
- Double noOfPublications;
- boolean isOutstanding;
- public Professor(Double score){
- noOfPublications = score;
- }
- public boolean outstanding (String s) {
- if (noOfPublications >120 ) {
- isOutstanding = true;
- }
- return isOutstanding;
- }
- }
Expand|Select|Wrap|Line Numbers
- Student class
- public class Student extends SchoolMember {
- /**
- * This is the grade point average of the student.
- * It is of type double.
- */
- Double gradePointAverage;
- /**
- * This is an attribute of type boolean
- * where you check if the student is outstanding or not.
- */
- Boolean isOutstanding;
- /**
- * @param score of type double which takes the integer
- * value from the text file.
- *
- * The score value taken from the text file is assigned to
- * the variable gradePointAverage
- */
- public Student(Double score) {
- gradePointAverage = score;
- }
- /**
- * if the gradePointAverage is greater then 3.3 therefore the student
- * is outstanding. If it is smaller is not outstanding.
- *
- * @param s the input string from the text file
- * @return boolean if either the student is outstanding or not
- *
- */
- public boolean outstanding(String s) {
- if (gradePointAverage > 3.3) {
- isOutstanding = true;
- }
- return true;
- }
- }
Expand|Select|Wrap|Line Numbers
- public class FileParser {
- public SchoolMember[] convert(String s) {
- FileReader f = null;
- BufferedReader buf = null;
- try {
- f = new FileReader(s);
- buf = new BufferedReader(f);
- String line = buf.readLine();
- while (line != null) {
- String[] datum = line.split(" ");
- SchoolMember member;
- String memberType = datum[0];
- Double score = Double.parseDouble(datum[3]);
- /**
- * if the memberType is equal to P
- * i.e. it is a professor therefore the data found in
- * the text file is inserted into the professors class
- * to calculate if that professor is outstanding or not.
- */
- if (memberType.equals("P") ) {
- member = new Professor(score);
- member.typeOfMember = memberType;
- member.name = datum[1];
- member.surname = datum[2];
- }
- /**
- * if the memberType is equal to S
- * i.e. it is a sudent therefore the data found in the
- * text file is inserted into the students class
- * to calculate if the student is outstanding or not.
- */
- else if (memberType.equals("S")) {
- member = new Student(score);
- member.typeOfMember = memberType;
- member.name = datum[1];
- member.surname = datum[2];
- }
- }
- }
- catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- if (buf != null) {
- buf.close();
- }
- buf.close();
- f.close();
- }
- catch (IOException e) {
- // just ignore
- }
- }
- return null;
- }
- }
Expand|Select|Wrap|Line Numbers
- public class Launcher {
- /**
- * @param args
- */
- public static void main(String[] args) {
- FileParser convertor = new FileParser();
- convertor.convert("c:/text.txt");
- }
- }
Thanks very much
Christine