473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Reading a text file

50
Abstract Class, Inheritance, Polymorphism, File Handling, Exception Handing
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
  1. public abstract class SchoolMember {
  2.  
  3.     //These are the attributes for a SchoolMember class
  4.  
  5.     /**
  6.      * This is the type of member 
  7.      * either a professor or a student.
  8.      */
  9.     String typeOfMember;
  10.  
  11.     /**
  12.      * This is the name of the school member
  13.      * i.e. the name of the professor or the 
  14.      * student.  It is of type string.
  15.      */
  16.     String name;
  17.  
  18.     /**
  19.      * This is the surname of the school member
  20.      * i.e. the surname of the professor or the
  21.      * student.  It is of type string.
  22.      */
  23.     String surname;
  24.  
  25.     /**
  26.      * This is the score of the SchoolMember. 
  27.      * In the case of a student it is the average
  28.      * while in the case of a professor it is the
  29.      * number of publications.
  30.      */
  31.     String score;
  32.  
  33.         public abstract boolean outstanding(String s);
  34.  
  35. }
Professor class

Expand|Select|Wrap|Line Numbers
  1. public class Professor extends SchoolMember {
  2.  
  3.     Double  noOfPublications;
  4.     boolean isOutstanding;
  5.  
  6.     public Professor(Double score){
  7.  
  8.  
  9.         noOfPublications = score;
  10.  
  11.     }
  12.     public boolean outstanding (String s) {
  13.  
  14.         if (noOfPublications >120 ) {
  15.  
  16.             isOutstanding = true;
  17.         }
  18.         return isOutstanding;
  19.  
  20.     }
  21.  
  22.  
  23. }
Expand|Select|Wrap|Line Numbers
  1. Student class
  2.  
  3. public class Student extends SchoolMember {
  4.     /**
  5.      * This is the grade point average of the student.
  6.      * It is of type double.
  7.      */
  8.     Double gradePointAverage;
  9.  
  10.     /**
  11.      * This is an attribute of type boolean
  12.      * where you check if the student is outstanding or not.
  13.      */
  14.     Boolean isOutstanding;
  15.  
  16.     /**
  17.      * @param score of type double which takes the integer
  18.      * value from the text file.
  19.      * 
  20.      * The score value taken from the text file is assigned to
  21.      * the variable gradePointAverage
  22.      */
  23.     public Student(Double score) {
  24.  
  25.         gradePointAverage = score;
  26.  
  27.     }
  28.  
  29.     /**
  30.      * if the gradePointAverage is greater then 3.3 therefore the student
  31.      * is outstanding.  If it is smaller is not outstanding.
  32.      * 
  33.      * @param s the input string from the text file
  34.      * @return boolean if either the student is outstanding or not
  35.      * 
  36.      */
  37.     public boolean outstanding(String s) {
  38.  
  39.         if (gradePointAverage > 3.3) {
  40.  
  41.             isOutstanding = true;
  42.         }
  43.  
  44.         return true;
  45.     }
  46.  
  47. }
FileParser Class
Expand|Select|Wrap|Line Numbers
  1. public class FileParser {
  2.  
  3.     public SchoolMember[] convert(String s) {
  4.  
  5.         FileReader f = null;
  6.         BufferedReader buf = null;
  7.  
  8.         try {
  9.             f = new FileReader(s);
  10.             buf = new BufferedReader(f);
  11.  
  12.             String line = buf.readLine();
  13.  
  14.             while (line != null) {
  15.  
  16.                 String[] datum = line.split(" ");
  17.  
  18.                 SchoolMember member;
  19.                 String memberType = datum[0];
  20.                 Double score = Double.parseDouble(datum[3]);
  21.  
  22.                 /**
  23.                  * if the memberType is equal to P
  24.                  * i.e. it is a professor therefore the data found in
  25.                  * the text file is inserted into the professors class
  26.                  * to calculate if that professor is outstanding or not.
  27.                  */
  28.                 if (memberType.equals("P") ) {
  29.  
  30.                     member = new Professor(score);
  31.                     member.typeOfMember = memberType;
  32.                     member.name = datum[1];
  33.                     member.surname = datum[2];
  34.                 }
  35.  
  36.                 /**
  37.                  * if the memberType is equal to S
  38.                  * i.e. it is a sudent therefore the data found in the 
  39.                  * text file is inserted into the students class
  40.                  * to calculate if the student is outstanding or not.
  41.                  */
  42.                 else if (memberType.equals("S")) {
  43.  
  44.                     member = new Student(score);
  45.                     member.typeOfMember = memberType;
  46.                     member.name = datum[1];
  47.                     member.surname = datum[2];
  48.                 }
  49.             }
  50.         }
  51.  
  52.         catch (FileNotFoundException e) {
  53.             // TODO Auto-generated catch block
  54.             e.printStackTrace();
  55.         } catch (IOException e) {
  56.             // TODO Auto-generated catch block
  57.             e.printStackTrace();
  58.         } finally {
  59.             try {
  60.                 if (buf != null) {                 
  61.                     buf.close();
  62.                 }
  63.  
  64.                 buf.close();
  65.                 f.close();
  66.             }
  67.  
  68.             catch (IOException e) {
  69.                 // just ignore
  70.             }
  71.         }
  72.  
  73.         return null;
  74.  
  75.     }
  76. }
Launcher

Expand|Select|Wrap|Line Numbers
  1. public class Launcher {
  2.  
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.     FileParser convertor = new FileParser();
  8.     convertor.convert("c:/text.txt");
  9.  
  10.     }
  11.  
  12. }
When I run this code nothing happens. Please can some1 correct the code and tell me whats wrong with it.

Thanks very much

Christine
Oct 9 '07 #1
13 1842
RedSon
5,000 Expert 4TB
Did you step through the code and trace its execution? What did you come up with? Is there no exception that get thrown? Does it compile properly?
Oct 9 '07 #2
sugard
50
yes it compiles normally without giving any errors. since i am a beginner i ve never used debugger before.
Oct 9 '07 #3
RedSon
5,000 Expert 4TB
yes it compiles normally without giving any errors. since i am a beginner i ve never used debugger before.
What IDE are you using?
Oct 9 '07 #4
sugard
50
i am using java eclipse.
Oct 9 '07 #5
RedSon
5,000 Expert 4TB
i am using java eclipse.
In eclipse set a breakpoint inside your main() method and then click on debug. It should execute then pause execution when your breakpoint is reached.
Oct 9 '07 #6
sugard
50
it did not work .. i dont know..
Oct 9 '07 #7
RedSon
5,000 Expert 4TB
it did not work .. i dont know..
you have this:

Expand|Select|Wrap|Line Numbers
  1. public class Launcher {
  2.  
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.     FileParser convertor = new FileParser();
  8.     convertor.convert("c:/text.txt");
  9.  
  10.     }
  11.  
  12. }
And you put a breakpoint on this line:

Expand|Select|Wrap|Line Numbers
  1. FileParser convertor = new FileParser();
And nothing happend?

Did you make sure to choose debug instead of just run?
Oct 9 '07 #8
sugard
50
Ok when i did that under the names coloumn there was writter 'args' and under value 'String[0] (id =16) ' and underneath there was displayed this '[]'
Oct 9 '07 #9
RedSon
5,000 Expert 4TB
Ok when i did that under the names coloumn there was writter 'args' and under value 'String[0] (id =16) ' and underneath there was displayed this '[]'
Ok good looks like your debugger is working, now you just need to step through your code and try it out. How long have you been programming java?
Oct 9 '07 #10
RedSon
5,000 Expert 4TB
Ok good looks like your debugger is working, now you just need to step through your code and try it out. How long have you been programming java?
sugard,

It might be time for you to read some other stuff. Like http://pages.cs.wisc.edu/~cs302/reso...l/0-start.html or http://linuxdevices.com/articles/AT6046208714.html. And maybe talk to your professor, computer lab tech, or fellow student to ask them to help you learn the debugger.
Oct 9 '07 #11
sugard
50
i've been programming for about these last 2 months. I am learning java through tutorials and this is a task from an assignment. When I ve gone through the code nothing happened...
Oct 9 '07 #12
r035198x
13,262 8TB
i've been programming for about these last 2 months. I am learning java through tutorials and this is a task from an assignment. When I ve gone through the code nothing happened...
Your while loop doesn't look good.
1.) You are not handling all the possible cases.
2.) You don't seem to have made it possible for your while loop to be exited once it has been entered. Is there anywhere you are modifying the value of line.

P.S I'll have to rename this thread because your title does not describe your problem at all.
Oct 9 '07 #13
sugard
50
ok i will try to fix it. thanks
Oct 9 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.