aspose file tools*
The moose likes Beginning Java and the fly likes calling and adding different elements from multiple objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling and adding different elements from multiple objects" Watch "calling and adding different elements from multiple objects" New topic
Author

calling and adding different elements from multiple objects

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
I am working on a program that needs to call in an element stored in four different objects. Obviously the element is not exactly the same or copies of an original, they each hold their own value. I am just stuck at a place where I am not sure how to call in all four elements from their appropriate object and then add them all together into one variable. I need this all to happen in the driven class if thats possible.

here is what I have so far:



I am fairly confident this is horribly wrong, but I couldnt find any info on the net about it, so I took a stab in the dark.
If you know of any articles explaining this very subject please share them with me.

I appreciate the help
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Ben Hultin wrote:I am working on a program that needs to call in an element stored in four different objects. Obviously the element is not exactly the same or copies of an original, they each hold their own value. I am just stuck at a place where I am not sure how to call in all four elements from their appropriate object and then add them all together into one variable. I need this all to happen in the driven class if thats possible.

I'm not sure I get it properly. Are Hw1, Hw2.. of same type? Then you may encapsulate them in another class and have some getters/setters as needed to get those instances, if that make sense in your model?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks for the reply.

I have initialized the objects Hw1, Hw2, etc. with two arguments and had them assigned to variables ePoints and pPoints with my constructor in the driven class obviously:



and now I am trying to call up each ePoints from Hw1, Hw2, etc. into one method in my driven class and sum them together into a single variable for printing.

Would the setters and getters still for this scenario?

Thanks a lot for the help
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

So your class contains the "printOverallGrade()" method has references to instances of "HwScore" class?. Or the method is inside the class "HwScore" itself? Please give more details..
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
It sounds to me like seeing the entire driven class would be the most helpful. The method with code at topic is at the bottom of the script.



The objects have been initialized in the driver class as such:




Thanks a lot for the help
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

I think your "HwScore" class doesn't even compile as no references for Hw1. etc.. inside it. As seen in your code I think "printOverallGrade()" method doesn't belong in there as it deals with various instances of "HwScore" class. Do you know how many instances of it will be created? Better to move it to some helper class...
And to do this..

The method should be a static method not an instance method.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
thanks for the suggestions. This is getting more complex than I would prefer, considering static methods can only access static variables which are essentially constants, so I feel I am going to need to make this work some other way. Would it be easier to just put the method in the driver class?

Also how would you suggest I make references to the Hw1, Hw2, etc. objects in the driven class?

Thanks a lot
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Ben Hultin wrote:thanks for the suggestions. This is getting more complex than I would prefer, considering static methods can only access static variables which are essentially constants, so I feel I am going to need to make this work some other way. Would it be easier to just put the method in the driver class?

That's the class you run the application from. Then you will not be able to re-use your model in any other application (such as swing based etc..) since that is part of the model.
Also how would you suggest I make references to the Hw1, Hw2, etc. objects in the driven class?

That depends on how you are going to implement the method (either method parameters/instance field etc..)
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Ben Hultin wrote:thanks for the suggestions. This is getting more complex than I would prefer, considering static methods can only access static variables which are essentially constants...


Static variables aren't essentially constants. Static just means there is 1 value for all instances of the class. They can be changed by each instance of the class. Now static final variables, those are essentially constants.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
note: sorry but didnt know you had replied while I was retooling my code. Thanks a lot for the pointers

I have made some changes to my code, created some setters and getters as you suggested before. I am now getting errors stating that Hw1, Hw2, etc. are not being found in the driver class they were created in and also being considered variables by the compiler. Maybe I am using them incorrectly for calling the getter?





my driven class:



Thanks a lot for the help
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

A few tips...

1) Remove all that code in your main method and put it in a constructor. Then just call new BenHultinProg2() in your main method. This will resolve weird static issues.

2) Use java conventions when naming variables. Java variables should begin with a lower case. So Hw1 should be hw1.

3) Use meaningful variable and class names. Instead of HwScore, how about HomeworkScore. totalEPoints. What is 'E' ? lGrade?


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

4) getters don't take an argument.



5) if pPoints is a double, shouldn't your getter be returning a double? And why are you passing int's into your setters if your member variables are doubles?
 
Don't get me started about those stupid light bulbs.
 
subject: calling and adding different elements from multiple objects