• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calling and adding different elements from multiple objects

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..)
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
We noticed he had no friends. So we gave him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic