473,387 Members | 1,517 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,387 software developers and data experts.

instantiating an abstract class

50
I need to access some data which is in an abstract class. Though you cannot inistialise an abstract class.

The task to do is gathering data from a textfile where data is in this format..
P Charles Galea 134

Where my class needs to check if the first letter is a P it do the stuff in the professors class and if it is an S it do the stuff in the Students class. The professor and student class inherits the SchoolMember abstract class which is follows.


Expand|Select|Wrap|Line Numbers
  1. public abstract class SchoolMember {
  2.  
  3.     //These are the attributes for a SchoolMember class
  4.  
  5.     String typeOfMember;
  6.     String name;
  7.     String surname;
  8.     Integer score;
  9.  
  10.     public abstract boolean outstanding(String s);
  11.  
  12. }
and this class
Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5.  
  6. /* This class is going to be used to parse the file
  7.  * and gets data from this file to check if the student or the 
  8.  * professor is outstanding or not
  9. */
  10. public class FileParser {
  11.  
  12.     public SchoolMember[] convert(String s) {
  13.  
  14.     FileReader f = null;
  15.     BufferedReader buf = null;
  16.  
  17.     try {
  18.         f = new FileReader(s);
  19.         buf = new BufferedReader(f);
  20.  
  21.         String line = buf.readLine();
  22.  
  23.         while (line != null) {
  24.  
  25.             String[] datum = line.split(" ");
  26.  
  27.         //    Student stud = new Student();
  28.         //    Professor prof = new Professor();
  29.  
  30.           //  if (SchoolMember.typeOfMember.equals("S") ) {
  31.  
  32.             //    stud.outstanding(s);
  33.  
  34.             }
  35.  
  36.          //   else if (SchoolMember.typeOfMember.equals("P")) {
  37.  
  38.            //     prof.outstanding(s);
  39.     //        }
  40.  
  41.         }    
  42.     }
  43.             catch (FileNotFoundException e) {
  44.                 // TODO Auto-generated catch block
  45.                 e.printStackTrace();
  46.             } catch (IOException e) {
  47.                 // TODO Auto-generated catch block
  48.                 e.printStackTrace();
  49.             } finally {
  50.  
  51.                 try {
  52.                     if (buf != null) {
  53.                         buf.close();
  54.                     }
  55.                     buf.close();
  56.                     f.close();
  57.                 } catch (IOException e) {
  58.                     // just ignore
  59.                 }
  60.             }
  61.  
  62.             return null;
  63.  
  64.     }
  65. }
Can please some1 help me correct the commemted part..? where i need to check if the first item in the textfile is a 'p' or an 's' and according to what it is do the specified method?

thank you
Sep 25 '07 #1
19 2344
r035198x
13,262 8TB
Your thread title does not make any sense. You cannot instantiate an abstract class. The problem discussed has nothing to do with abstract classes. If you want to know how to read files you can read this article
Sep 25 '07 #2
Nepomuk
3,112 Expert 2GB
I need to access some data which is in an abstract class. Though you cannot inistialise an abstract class.

The task to do is gathering data from a textfile where data is in this format..
P Charles Galea 134

Where my class needs to check if the first letter is a P it do the stuff in the professors class and if it is an S it do the stuff in the Students class. The professor and student class inherits the SchoolMember abstract class which is follows.
Expand|Select|Wrap|Line Numbers
  1. public abstract class SchoolMember {
  2.  
  3.     //These are the attributes for a SchoolMember class
  4.  
  5.     String typeOfMember;
  6.     String name;
  7.     String surname;
  8.     Integer score;
  9.  
  10.     public abstract boolean outstanding(String s);
  11.  
  12. }
and this class
Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5.  
  6. /* This class is going to be used to parse the file
  7.  * and gets data from this file to check if the student or the 
  8.  * professor is outstanding or not
  9. */
  10. public class FileParser {
  11.  
  12.     public SchoolMember[] convert(String s) {
  13.  
  14.     FileReader f = null;
  15.     BufferedReader buf = null;
  16.  
  17.     try {
  18.         f = new FileReader(s);
  19.         buf = new BufferedReader(f);
  20.  
  21.         String line = buf.readLine();
  22.  
  23.         while (line != null) {
  24.  
  25.             String[] datum = line.split(" ");
  26.  
  27.         //    Student stud = new Student();
  28.         //    Professor prof = new Professor();
  29.  
  30.           //  if (SchoolMember.typeOfMember.equals("S") ) {
  31.  
  32.             //    stud.outstanding(s);
  33.  
  34.             }
  35.  
  36.          //   else if (SchoolMember.typeOfMember.equals("P")) {
  37.  
  38.            //     prof.outstanding(s);
  39.     //        }
  40.  
  41.         }    
  42.     }
  43.             catch (FileNotFoundException e) {
  44.                 // TODO Auto-generated catch block
  45.                 e.printStackTrace();
  46.             } catch (IOException e) {
  47.                 // TODO Auto-generated catch block
  48.                 e.printStackTrace();
  49.             } finally {
  50.  
  51.                 try {
  52.                     if (buf != null) {
  53.                         buf.close();
  54.                     }
  55.                     buf.close();
  56.                     f.close();
  57.                 } catch (IOException e) {
  58.                     // just ignore
  59.                 }
  60.             }
  61.  
  62.             return null;
  63.  
  64.     }
  65. }
Can please some1 help me correct the commemted part..? where i need to check if the first item in the textfile is a 'p' or an 's' and according to what it is do the specified method?

thank you
OK, you have an array of Strings called datum and the first Element of this array should be either a "P" or an "S", correct? So just check, which of both it is.

Next, the commented part seems to create both a Student and a Professor for each line - don't do that, it's a waste of space. Create one of both, depending on the earlier test.

Also, you don't seem to read that line more than once. Either add a
Expand|Select|Wrap|Line Numbers
  1. line = buf.readLine();
  2.  
at the end of the while-loop or have the while loop doing it itself with
Expand|Select|Wrap|Line Numbers
  1. while ((String line = buf.readLine()) != null)
  2. // ...
  3.  
Does that help?

Greetings,
Nepomuk
Sep 25 '07 #3
JosAH
11,448 Expert 8TB
I'd say: a static factory method in the abstract base class.

kind regards,

Jos
Sep 25 '07 #4
sugard
50
yes it helped thanks! but my problem is still how am I going to combine them both the student and the professor so that i will use only 1?
As i cant use the abstract class. How am i going to check if the first element is a 'P' or an 'S'? which class am i going to instantiate?

I dont know if u can understand what i am saying.
Sep 25 '07 #5
r035198x
13,262 8TB
I'd say: a static factory method in the abstract base class.

kind regards,

Jos
Thanks for the correction.
Today is national no defenestration day so I get away with that.
Sep 25 '07 #6
JosAH
11,448 Expert 8TB
yes it helped thanks! but my problem is still how am I going to combine them both the student and the professor so that i will use only 1?
As i cant use the abstract class. How am i going to check if the first element is a 'P' or an 'S'? which class am i going to instantiate?

I dont know if u can understand what i am saying.
Read my reply #4: the static method can determine which of the two to instantiate.
Pass that method the line you just have read and let it figure out the details.

kind regards,

Jos
Sep 25 '07 #7
r035198x
13,262 8TB
yes it helped thanks! but my problem is still how am I going to combine them both the student and the professor so that i will use only 1?
As i cant use the abstract class. How am i going to check if the first element is a 'P' or an 'S'? which class am i going to instantiate?

I dont know if u can understand what i am saying.
You can do
Expand|Select|Wrap|Line Numbers
  1. SchoolMember member;
  2. if(element.equals("P")) {
  3.    member = new Professor();
  4. }
  5. else if (element.equals("S")) {
  6.     member = new Student();
  7. }
Edit: But Jos' approach is much better of course.
Sep 25 '07 #8
JosAH
11,448 Expert 8TB
You can do
Expand|Select|Wrap|Line Numbers
  1. SchoolMember member;
  2. if(element.equals("P")) {
  3.    member = new Professor();
  4. }
  5. else if (element.equals("S")) {
  6.     member = new Student();
  7. }
Edit: But Jos' approach is much better of course.
Mais naturellement mon ami ;-) btw, the method would be similar but it's the
place where that method belongs; the abstract base class is the place where
the method should be and figure out what exactly to instantiate; it's the factory
method pattern thingy. If more and more different SchoolMembers see the light,
that little pattern should be abandoned asap.

kind regards,

Jos
Sep 25 '07 #9
sugard
50
Thanks very much for your help.
I deceided to do the last one since it is easier for me.

Expand|Select|Wrap|Line Numbers
  1. String[] datum = line.split(" ");
  2.  
  3.             SchoolMember member;
  4.  
  5.             if (typeOMember.equals("S") ) {
  6.                 member = new Student();      
  7.             }
  8.  
  9.             else if (typeOfMember.equals("S")) {
  10.  
  11.                 member = new Professor();
  12.             }
  13.  
I' ve altered the code to that. But I am still encountering a difficulty in this part the typeOfMember.equals since there is an error saying that the type typeOfMember cannot be solved.
Sep 25 '07 #10
r035198x
13,262 8TB
Thanks very much for your help.
I deceided to do the last one since it is easier for me.

Expand|Select|Wrap|Line Numbers
  1. String[] datum = line.split(" ");
  2.  
  3.             SchoolMember member;
  4.  
  5.             if (typeOMember.equals("S") ) {
  6.                 member = new Student();      
  7.             }
  8.  
  9.             else if (typeOfMember.equals("S")) {
  10.  
  11.                 member = new Professor();
  12.             }
  13.  
I' ve altered the code to that. But I am still encountering a difficulty in this part the typeOfMember.equals since there is an error saying that the type typeOfMember cannot be solved.
A basic rule of Java is that you have to declare variables before you can use them.
P.S The other approach is not difficult, you simply move that code to the abstract class in a method called, say, getInstance.
Sep 25 '07 #11
JosAH
11,448 Expert 8TB
Thanks very much for your help.
I deceided to do the last one since it is easier for me.

Expand|Select|Wrap|Line Numbers
  1. String[] datum = line.split(" ");
  2.  
  3.             SchoolMember member;
  4.  
  5.             if (typeOMember.equals("S") ) {
  6.                 member = new Student();      
  7.             }
  8.  
  9.             else if (typeOfMember.equals("S")) {
  10.  
  11.                 member = new Professor();
  12.             }
  13.  
I' ve altered the code to that. But I am still encountering a difficulty in this part the typeOfMember.equals since there is an error saying that the type typeOfMember cannot be solved.
Where does that typeOfMember come from? Did you define a String like that?
You're splitting a line; the line parts are stored in the 'datum' String array; that's
where you should look for an "S" or "P" not in a made up 'typeOfMember' thing.

Please try to understand what those code snippets are all about before you just
blindly copy and paste them. btw that piece of code could easily be placed in
a static method in that abstract base class in which case you have defined your
factory method; that's how it's called.

kind regards,

typofKindaSortofJos
Sep 25 '07 #12
sugard
50
typeOfMember is a variable in the SchoolMember class.
Sep 25 '07 #13
r035198x
13,262 8TB
typeOfMember is a variable in the SchoolMember class.
So you now see why that code belongs in that abstract class?
Sep 25 '07 #14
JosAH
11,448 Expert 8TB
typeOfMember is a variable in the SchoolMember class.
Sure and where did you store that piece of code? In your FileParser?

kind regards,

Jos
Sep 25 '07 #15
sugard
50
yes i can understand better now what you are saying. But do i need to remove the whole fileparser class and place it in the abstract class? or just place that part only in the abstract class?
Sep 25 '07 #16
r035198x
13,262 8TB
yes i can understand better now what you are saying. But do i need to remove the whole fileparser class and place it in the abstract class? or just place that part only in the abstract class?
Now, let's not play a jigsaw puzzle game with the code here. Why does a SchoolMember need to have a FileParser?
Sep 25 '07 #17
JosAH
11,448 Expert 8TB
yes i can understand better now what you are saying. But do i need to remove the whole fileparser class and place it in the abstract class? or just place that part only in the abstract class?
You did read and tried to understand my reply #12 didn't you?

kind regards,

Jos
Sep 25 '07 #18
sugard
50
yes i read all the posts and I understood the code but i am still not understanding what i need to do. If i need to take all the code or not.
Sep 25 '07 #19
JosAH
11,448 Expert 8TB
yes i read all the posts and I understood the code but i am still not understanding what i need to do. If i need to take all the code or not.
Ok, this is a giveaway then:

Expand|Select|Wrap|Line Numbers
  1. public class Student extends SchoolMember { ... }
  2. public class Professor extends SchoolMember { ... }
  3. ...
  4. public abstract class SchoolMember {
  5.  
  6.    public static SchoolMember getInstance(String description) {
  7.       if (<blahblahbah>) return new Professor( ... );
  8.       return new Student( ... );
  9.    }
  10. }
  11.  
Now that's what a simple factory method is all about. Compare this with, say,
the Calendar class where they do something similar.

kind regards,

Jos
Sep 25 '07 #20

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

Similar topics

6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
2
by: active | last post by:
Because of an example I followed I've been instantiating Image objects. Now I notice that the documentation says the Image class is an abstract base class. Things seem to be working! Is the...
3
by: Achim Dahlhoff | last post by:
Hi. I'm trying to find out the diffrence between normal classes and classes derived from built-in types. (Which is causing me trouble trying to instantiate a class using C API calls) >>>...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
6
by: curious2007 | last post by:
So here is how I can summarize my situation. In function.hpp I have the following function: virtual void accept(FunctionVisitor<D,R,I>& v) = 0; And in detfunction.hpp I have the following: ...
4
by: dascandy | last post by:
Hi, For a project I'm working on I'm kind-of-hacking my way around deriving a class from an interface or such to create a mock, but instead creating the mock directly. It is usable as the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.