This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
Bob Roleson
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program that focuses on the concept of inheritance. This program should have a class Student with fields such as ID, name, and address. Two classes should be derived from it - ResearchAssistant and TeachingAssistant. The research assistant should have additional fields of area of research, advisor, research account, and payrate. The teaching assistant should have additional fields as well. In the program one should be able to click on a student in a list and show their information.

sample of a research assistant:
124-16-534
Jeffery Benton
1300 Main St.
CS1337,MATH2306,PHYS1211
RA
Mathematics
George Brice
234-65-896
8.75

I have my classes set up pretty good I think. I just need a little advice on how to go about finishing this. I dont exactly know how to start reading from where I left off in the text file. I know I am going to have to distinguish by type e.g. whether RA or TA.

here are my classes for the Student and ResearchAssistant.



 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

I would suggest you delete the no-args constructors, and insist that every Student object you create have a name and address supplied.

I don't think the readStudents method is an integral part of the Student class; it ought to be moved into a different class. Similarly the List<Student> ought not to be inside the Student class.
There are a few minor stylistic things, eg it is not obvious what the AOS parameter means. But otherwise the classes look all right.

Have you been told to use doubles for pay rates? You should never use floating-point arithmetic for money; use BigDecimal or integer arithmetic (demoninated in cents, pennies, etc) instead.
 
Bob Roleson
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it is not required to use double. So, would I move the readStudents method to the ResearchAssistant class.
 
Bob Roleson
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to construct the ResearchAssistants out of what is read from the file however it is saying it cannot find the constructor.

 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things Bob
1.
is not a recommended way to declare identifiers, it will lead to confusions and a heap of maintainance problems.

2. The matching constructor not found error is due to the statement
There is no constructor that matches the above arguments.

Hope this helps
 
Bob Roleson
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, because I need to include the studentID,name,address fields. But how do I do that if there private for it gives me an error.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to worry about accessing those variables from the constructor; the super(studentID, name, address) call in the constructor will do that for you.
You have to call the ResearchAssistant constructor in the read() method, it will be something like

. . . new ResearchAssistant(name, studentID, address, areaOfReasearch, advisor, researchAccount, payRate);
 
Bob Roleson
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I have my ResearchAssistant constructor set up that way now. But, it is saying studentID,name,address have private access in Student. Here is my read method.

 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you got in the ResearchAssistant constructor?
 
Bob Roleson
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I am going to post all my classes including my TeachingAssistant and TAClass classes. I do not know if I have everything correct and I am still puzzled on why I am getting the private access error.







 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your read method is very confusing. You are creating a Student object, then not doing anything with it, so it will vanish into cyber-limbo until the garbage collector finds it. You cannot get access to the studentID etc fields from a subclass; you would have to use the getStudentID() method instead.

If you are using that read method, there are three places (at least) you could legitimately use it
  • As part of a constructor, but it will be very difficult to write the super() call as the first line.
  • In a static factory method which returns the Student object.
  • In another class, to create a Student object to manipulate. This will probably be easiest for you to implement.

  • [ December 03, 2008: Message edited by: Campbell Ritchie ]
     
    Bob Roleson
    Greenhorn
    Posts: 7
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yeah in my code I add the student to an ArrayList. However, it does not seem very confusing to me unless I am doing something totally wrong. For, all I am trying to do is read information for each student- whether or not their a TA or RA will determine their parameters. Using the super keyword in the method I am hoping that it picks up form where I left off in the student read method. Again, this may not work at all; however, I really dont know another way of reading in the info from the txt file.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47270
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have got a problem, that you are dependent on the structure of the text file. The tiniest error reading the file, or the smallest change which gets the data in the text file in a different order from what you expect,and your application will fail spectacularly. This shows what a bad idea it can be to become so dependent on text files or similar.
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic