Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I Just Want One Method

 
Gary Lineker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings all. Beginner here struggling with a school project. The problem areas I have highlighted in red within the Java program. For the modules that I am struggling with, you will notice I have created four four calling statements each referring to a separate module:



The modules look like this:



Essentially I need to calculate the sums for each of these but I need to do it within one module called calculateSubjectSum. My brain has really shut down and although I'm convinced it's something really glaring, for the life of me I'm stumped. Thank you!

 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, please use code tags. The make code much easier to read. Second, when posting code, less is more. Try to narrow the code sample down as much as you can and still have a complete example that demonstrates the problem.

I'm not clear on what you mean by module in this example. I do notice that your four methods all do exactly the same thing. They just have different names and different parameters names. So maybe what you mean by only one module is that you want only one method. Something like



You can call that method four times, passing in the same parameters you pass into the four differently-named methods now, and you'll get the same results. If that's not it, then what problem are you having exactly?
 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:Something like



You can call that method four times, passing in the same parameters you pass into the four differently-named methods now, and you'll get the same results. If that's not it, then what problem are you having exactly?

Ponder Mr. Linekar. I think this is the solution considering your module means a method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Lineker wrote: . . . . The problem areas I have highlighted in red within the Java program. . . .
No, you haven’t. Colour tags don’t work inside code tags, so I shall delete them.
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:Something like

Actually, my suggestion would be: What that does is to take an array of (or several individual) values and calculate their sum; and it's properly decoupled.

@Gary: What your method is doing is not summing, it's accumulating; and that's very difficult to do in a loosely coupled way.

And, whatever you do decide on, don't forget that an int that is the sum of several other int's might overflow.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic