Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating an object instance that is to save user input to another class

 
Chris Morris
Greenhorn
Posts: 2
Mac OS X Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being I am a beginner, I am sure the subject's explanation makes little to no sense. I hope that the code can explain the story better. I am certainly not looking for someone to do the code for me, just to maybe guide me in the right direction since I am absolutely lost at this point.

The exercise - not a homework assignment, do not worry - asks to create two classes; a CollegeCourse class and a Student Class. The CollegeCourse class is supposed to have data fields for a course ID, grade, and credit hours and then set and get methods for each. The Student class, which is where I believe I am messing up, contains a data field for a student ID and 5 CollegeCourse objects by using an array. This class requires set and get methods as well, although I am having trouble with the set and get methods for the object array. The third class, InputGrades, is the one that contains the main class and utilizes the first two classes. The third class is incomplete as of now because I have not figured out how to properly use the objects, especially the object array within the Student class.

Thank you in advance and feel free to offer any critiques, even if unrelated to topic (e.g. poor coding, etc.).





 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need constructors for those classes. And a toString() method.
ExampleThis class uses similar fields to yours, initialising the array in the constructor, not the declaration. I would have added name as a field.
Get a StudentDemo class, and use this sort of code to run your Student objectYou will have to look for spelling errors; I think I had a small c in CollegeCourse somewhere. You will also notice I have missed out the get and set methods. Write equals() and hashCode() methods too.

Using the sort of code in the second block, you can observe the workings of the class, and play with it. That's the best way to learn.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch
 
Chris Morris
Greenhorn
Posts: 2
Mac OS X Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the response!

Your suggestions were great and I will definitely put them to use as soon as I get a chance. The constructor suggestion seems to be the real link between the two classes. I think I got confused because of the language used by the exercise. It didn't mention using a constructor but gave direct orders for everything else so I assumed there shouldn't be a constructor. Although it didn't seem to make sense until you mentioned the constructor and override toString() method. I am unfamiliar with the hashCode() method, although will definitely do some research. As soon as I get it figured out or make progress, I will post an update. Thanks again!

Chris
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

I read somewhere recently (maybe here) that the Sun/Oracle convention is always to give a class a constructor. Even for classes you don't want to instantiate. I would agree; it is a good convention for every class to have a constructor explicitly written.
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The one by Campbell is much optimized indeed.. but your design also didn't had any problem.. the only problem area that
I can observe from your code is here :



you were using the same CollegeCourse object for all students...and then setting the values for the students..and every next value overrides the
previous one..as indeed those are instance values but also are the instances to the same reference (something you must know when using objects)

Regards
Prince

Chris Morris wrote:Being I am a beginner, I am sure the subject's explanation makes little to no sense. I hope that the code can explain the story better. I am certainly not looking for someone to do the code for me, just to maybe guide me in the right direction since I am absolutely lost at this point.

The exercise - not a homework assignment, do not worry - asks to create two classes; a CollegeCourse class and a Student Class. The CollegeCourse class is supposed to have data fields for a course ID, grade, and credit hours and then set and get methods for each. The Student class, which is where I believe I am messing up, contains a data field for a student ID and 5 CollegeCourse objects by using an array. This class requires set and get methods as well, although I am having trouble with the set and get methods for the object array. The third class, InputGrades, is the one that contains the main class and utilizes the first two classes. The third class is incomplete as of now because I have not figured out how to properly use the objects, especially the object array within the Student class.

Thank you in advance and feel free to offer any critiques, even if unrelated to topic (e.g. poor coding, etc.).





 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic