• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding file messes up Inheritance classes  RSS feed

Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm finishing up another program, but again I have run into a problem. I'm trying to load in a file of students with the format firstname,lastname,major. A few of my classes are inheritance classes, so when I add the file it forces me to add a string to those class constructors which I don't want to do. How do I get around this?

The class where I'm loading the students.

PersonClass I'm completing after I load students

Student Class. Same thing makes me add another String to constructor because of the inheritance

Catalog Class

and major class
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Student should have a constructor that takes 3 Strings, and it should invoke its superclass 2-string constructor and save the major. It might also have a 2-string ctor:

public class Student extends Person {
Posts: 5348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Cody

Try not to post such an amount of code especially when you're trying to solve an issue which appears in a known for you area of code.
Fred already pointed out what causes a problem. However, since you posted all that code, I thought I'll give some comments on it, so you could consider improving it.

1. As an example refering to this your code snippet:
Do you know about the enhanced-for loop? It is very handy for iterating over collection of items. Example:

This code snippet lets to set major to null, think how to not let that. How that happens? In case to this method majorName is passed a non existing name, method getMajorByName() would return null. Consider before you setting major if returned value is null - in case it is, let user know (throw an exception), that such major doesn't exist in majors list and can't be set as such.

3. Line 98 in Catalog class has the line:
this line can cause you problems too. Before you parse, check if the next token can be parsed to an integer, in case not, let user know about it, that the courseFile has been passed contains invalid data, so cannot be read and is needed other. Read about the Scanner class method hasNextInt()

4. Some instance variables aren't private - make them as such. Refering to Major class.

5. While being an explicit is a sign of good code readability, in the code part like this I think it is a bit too much (others might think differently):
Think if you want to change it to:
Did you notice what I did? It is related with the second issue of that method. This method name suggests that it should add a Student, but it appears in your case it creates/adds and returns. That is not good most likely. If the method says addSomething it should add and nothing else - ok, it might could return true or false in success or failure, but definitely not return a Student. So I'd keep such method as void (with no return type). In case you need to get student, create another method. One method needs to do one thing (similar issues in similar cases in other classes).

6. Something not quite correct with the line 85 [College class]:

7. Some of the methods starts with an Upper case while they shouldn't: methodName; variableName; ClassName; CONSTANT_NAME.

That is it for now.
Don't listen to Steve. Just read this tiny ad:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!