• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use Of Functions in java  RSS feed

 
Charles Sparking
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help with this exercise i am baffled?




Write a function (or functions) that given a collection of files will produce a sum of integers from each line of each file. Each file can have any number of lines from 1 to N. Each line can contain only one integer and no other alphanumeric characters. All of the numbers from all of the files should be added to the final result. The result is just one number.

For either, what we are looking for is:

1. Clear separation of concerns
2. Well defined objects / interfaces
3. Application of good OO design principles to solve the problem
4. No code duplication
5. Test Driven Development
6. Well refactored code
7. Well tested code
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, there are no functions, there are methods instead. If the text of the exercise uses the word "functions", then it's using the wrong terminology.

What exactly are you baffled about? The exercise explains what you have to do: you get a file with numbers, on each line is one number, you have to read the numbers from the file and add them up and display the result.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't believe I'm actually going to say this (you people have corrupted me):

1) find a pencil and a pad of paper (yeah, so 18th century, I know)
2) turn off the computer (after reading the next couple of lines)
3) using said pencil, start laying out the tasks and methods you will need, on said paper
4) return to step 3, refining your process, until you can solve your task
5) turn the computer back on
6) now start writing the code

In all seriousness, your assignment is fairly straightforward. Once you start laying it out on paper, your bafflement should clear up nicely. You really can't (you could, but you really shouldn't) start coding until you understand what your objective(s) is.
I do not mean to be rude, but i feel compelled to point to you to <this> on asking questions and getting help here.

Regards,
Robert.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it seems pretty simple to me(the basic requirement)
this isn't a pencil but it is like this

 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as for functions and methods, isn't a function just a method that returns something???

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote: as for functions and methods, isn't a function just a method that returns something???

In Java, these things are always called methods.

In other programming languages, other words are used. For example, in C and C++ people call them "functions" (and if they're in a class, in C++, "member functions"). In functional programming, functions should be pure (and some languages only allow you to write pure functions) - that means they must do some computation and return the result and must have no side effects (they cannot update state, or do I/O, etc.).

There are also other words, such as "procedures" or "subroutines".
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote: as for functions and methods, isn't a function just a method that returns something???

As Jesper said: No.

Personally, I dislike methods that return void, so my general rule of thumb is that all my methods (even setters sometimes) return something; but that doesn't necessarily make them functions. Fluent interfaces, for example, pretty much require that every method returns an object of some kind.

I suggest you read the link that Jesper supplied, because learning how to write proper functions can help you a lot with developing nice, loosely coupled code.

HIH

Winston
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
they cannot update state, or do I/O, etc.

i agree, that makes methods very different from functions.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll agree with Winston, but also disagree.

On one hand, it'd be great if things like setters returned this so that they can be chained. I'm a big fan of chaining (no surprise given my JavaScript experience).

On the other hand, the bean standard won't recognize setters that don't "return" void. So if you're going to use beans in someplace like the web environment, you pretty darn well better declare your setters void, or ain't nothing going to work!
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will give this guy a "hint"
using java1(using java 7 might be very different).
you need a class that has a method that takes a file as a parameter and returns an array of ints
save all the arrys of ints in an array
find out which array is the longest
do nested for loops
for(int i = 0; i < longest.length(); i++)
for(each array in the array of arrays)
try
{
add to subtotal
}
catch(out of bounds)
{
}


it would be much easier to just total each array then total that, but that was not the assignment
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!