File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Creating an object instance that is to save user input to another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating an object instance that is to save user input to another class" Watch "Creating an object instance that is to save user input to another class" New topic
Author

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

Chris Morris
Greenhorn

Joined: Aug 09, 2011
Posts: 2

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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Oct 13, 2005
Posts: 38865
    
  23
... and welcome to the Ranch
Chris Morris
Greenhorn

Joined: Aug 09, 2011
Posts: 2

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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Nov 24, 2010
Posts: 32
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.).







Regards
Prince
---------------------------------------------------------
SCJP 6 , still a life long learner
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating an object instance that is to save user input to another class